ROS2 Python single parameter callback

asked 2021-10-08 11:56:37 -0500

TonySpark gravatar image

I am reading the documentation for parameter changes in ROS2 with rclpy and I am not sure how to implement parameters callback properly.

It seems examples given implement a single callback function, i.e. when a parameter is changed, we need to update all parameters.

   from rcl_interfaces.msg import SetParametersResult
   from rclpy.parameter import Parameter
   ...
   def cb_params(self, data):
        for parameter in data:
        if parameter.name == "camera_device":
                self.change_cam_device()
        if parameter.name == "imu_device":
                self.change_imu_device()
    return SetParametersResult(successful=True)

In this exemple, the device were already initialized using their device file (i.e. /dev/video0). If a parameter is changed dynamically, we need to re-initialize the device. However, it seems that if the camera_device parameter is modified, the imu device will also get reinitialized. Is there a better way to implement this correctly that does not require caching the old value and manually checking if each parameter is equal to its previous value ?

edit retag flag offensive close merge delete

Comments

After some testing, it seems only one parameter is passed to the callback. I assume the "data" argument is a list in case multiple parameters are modified in the same timestep

TonySpark gravatar image TonySpark  ( 2021-10-08 20:52:37 -0500 )edit