ROS2 Python single parameter callback
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 ?
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