[ROS2][rclpy] how to set parameter hosted by another Node via service [closed]
I am looking for a solution to set the Parameter which is host by another node using rclpy
.
I found there is a relavent post, but in rclcpp
.
While, I tried below. But the error occurred. Any idea how I can fix it? Thank you very much!
import time
import rclpy
from rclpy.node import Node
import rclpy.qos as qos
from rclpy.parameter import Parameter
# from rcl_interfaces.msg import Parameter
from rcl_interfaces.msg import ParameterValue
from rcl_interfaces.srv import SetParameters, GetParameters, ListParameters
from rcl_interfaces.msg import ParameterDescriptor, ParameterValue
class MinimalClientAsync(Node):
def __init__(self):
super().__init__('my_node')
self.cli = self.create_client(SetParameters, '/another_node/set_parameters')
while not self.cli.wait_for_service(timeout_sec=1.0):
self.get_logger().info('service not available, waiting again...')
self.req = SetParameters.Request()
def send_request(self):
self.req.parameters = [Parameter(name='parameter_1', value=13)]
self.future = self.cli.call_async(self.req)
def main(args=None):
rclpy.init(args=args)
minimal_client = MinimalClientAsync()
minimal_client.send_request()
while rclpy.ok():
rclpy.spin_once(minimal_client)
if minimal_client.future.done():
try:
response = minimal_client.future.result()
except Exception as e:
minimal_client.get_logger().info(
'Service call failed %r' % (e,))
break
minimal_client.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
The error is:
File "/opt/ros/dashing/lib/python3.6/site-packages/rcl_interfaces/srv/_set_parameters.py", line 138, in parameters
"The 'parameters' field must be a set or sequence and each value of type 'Parameter'"
AssertionError: The 'parameters' field must be a set or sequence and each value of type 'Parameter'
I also tried to use rcl_interfaces.msg.Parameter
instead of rclpy.parameter.Parameter
so it won't failed at rcl_interfaces/srv/_set_parameters.py
. However it failed at rcl_interfaces/msg/_parameter.py
this time:
File "/opt/ros/dashing/lib/python3.6/site-packages/rcl_interfaces/msg/_parameter.py", line 80, in __init__
self.value = kwargs.get('value', ParameterValue())
File "/opt/ros/dashing/lib/python3.6/site-packages/rcl_interfaces/msg/_parameter.py", line 149, in value
"The 'value' field must be a sub message of type 'ParameterValue'"
AssertionError: The 'value' field must be a sub message of type 'ParameterValue'
Then I kept trying, used ParameterValue
, like this:
Parameter(name='parameter_1', value=ParameterValue(integer_value=3))
It did not cause any error, but when I check the param, it shows Parameter not set.
. I am confused. The parameter parameter_1
seems to be undeclared.