0
$\begingroup$

I tried to use the service /turtlesim/set_parameters [rcl_interfaces/srv/SetParameters] to set the background of the turtlesim GUI. But I cannot get right the form of the request.

import rclpy
from rclpy.node import Node
from rcl_interfaces.srv import SetParameters

class SetParametersClient(Node):
    def __init__(self):
        super().__init__('set_parameters_client')
        self.client = self.create_client(SetParameters, '/turtlesim/set_parameters')
        while not self.client.wait_for_service(timeout_sec=1.0):
            self.get_logger().info('Service not available, waiting again...')

    def set_parameters(self, parameters):
        request = SetParameters.Request()
        request.parameters = parameters


        future = self.client.call_async(request)
        rclpy.spin_until_future_complete(self, future)

        if future.result() is not None:
            self.get_logger().info('Parameters set successfully')
        else:
            self.get_logger().error('Failed to set parameters')

def main(args=None):
    rclpy.init(args=args)
    set_params_client = SetParametersClient()

    # Define the parameters to be set
    parameters = [
            {
                "name": "background_r",
                "value": 255.0  # Set red component to maximum value
            },
            {
                "name": "background_g",
                "value": 0.0    # Set green component to zero
            },
            {
                "name": "background_b",
                "value": 0.0    # Set blue component to zero
            }
        ]
    # Call the set_parameters service
    set_params_client.set_parameters(parameters)

    rclpy.shutdown()

if __name__ == '__main__':
    main()

when I run the script, I got the following error message:

AssertionError: The 'parameters' field must be a set or sequence and each value of type 'Parameter'

How should I define the parameters to be set?

$\endgroup$

1 Answer 1

1
$\begingroup$

I found the solution by accident, but I cannot know why it is so. Below is the code

import rclpy
from rclpy.node import Node
from rcl_interfaces.srv import SetParameters
from rcl_interfaces.msg import ParameterValue
from rcl_interfaces.msg import Parameter
class SetParametersClient(Node):
    def __init__(self):
        super().__init__('set_parameters_client')
        self.client = self.create_client(SetParameters, '/turtlesim/set_parameters')
        while not self.client.wait_for_service(timeout_sec=1.0):
            self.get_logger().info('Service not available, waiting again...')

    def set_parameters(self, parameters):
        request = SetParameters.Request()
        request.parameters = parameters


        future = self.client.call_async(request)
        rclpy.spin_until_future_complete(self, future)

        if future.result() is not None:
            self.get_logger().info('Parameters set successfully')
        else:
            self.get_logger().error('Failed to set parameters')

def main(args=None):
    rclpy.init(args=args)
    set_params_client = SetParametersClient()

    # Define the parameters to be set
    parameters = [Parameter(name='background_r', value=ParameterValue(type=2, bool_value=False, integer_value=255, double_value=0.0, string_value='', byte_array_value=[], bool_array_value=[], integer_array_value=[], double_array_value=[], string_array_value=[]))]
    # Call the set_parameters service
    set_params_client.set_parameters(parameters)

    rclpy.shutdown()

if __name__ == '__main__':
    main()
$\endgroup$
2
  • $\begingroup$ If you compare your original code with this code, then you should be able to see what has changed and (hopefully) why it now works $\endgroup$ Apr 13 at 14:38
  • $\begingroup$ @Greenonline You are right. Maybe it is because I do not know the defination of parameters, or my poor grasp of the programming language python is to be blame. $\endgroup$ Apr 13 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.