ROS2: How to query all parameters from RViz using Python
The following script will query all parameters from RViz (/rviz/get_parameters
) using Python.
This is achieved by first listing all parameters, then querying and printing them.
#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from rcl_interfaces.srv import GetParameters, ListParameters
class RvizParamQuery(Node):
def __init__(self):
super().__init__('rviz_param_query')
self.get_client = self.create_client(GetParameters, '/rviz2/get_parameters')
self.list_client = self.create_client(ListParameters, '/rviz2/list_parameters')
# Wait for services
while not all([
self.get_client.wait_for_service(timeout_sec=1.0),
self.list_client.wait_for_service(timeout_sec=1.0)
]):
self.get_logger().info('Services not available, waiting...')
def list_all_parameters(self):
request = ListParameters.Request()
future = self.list_client.call_async(request)
rclpy.spin_until_future_complete(self, future)
return future.result().result.names
def query_parameters(self, param_names):
request = GetParameters.Request()
request.names = param_names
future = self.get_client.call_async(request)
rclpy.spin_until_future_complete(self, future)
return future.result()
def main():
rclpy.init()
param_query = RvizParamQuery()
# Get list of all parameters
param_names = param_query.list_all_parameters()
# Query values for all parameters
result = param_query.query_parameters(param_names)
# Display results
for name, value in zip(param_names, result.values):
if value.type == 1: # PARAMETER_BOOL
val = value.bool_value
elif value.type == 2: # PARAMETER_INTEGER
val = value.integer_value
elif value.type == 3: # PARAMETER_DOUBLE
val = value.double_value
elif value.type == 4: # PARAMETER_STRING
val = value.string_value
elif value.type == 5: # PARAMETER_BYTE_ARRAY
val = value.byte_array_value
elif value.type == 6: # PARAMETER_BOOL_ARRAY
val = value.bool_array_value
elif value.type == 7: # PARAMETER_INTEGER_ARRAY
val = value.integer_array_value
elif value.type == 8: # PARAMETER_DOUBLE_ARRAY
val = value.double_array_value
elif value.type == 9: # PARAMETER_STRING_ARRAY
val = value.string_array_value
else:
val = "Unknown type"
print(f"{name}: {val}")
param_query.destroy_node()
rclpy.shutdown()
if __name__ == '__main__':
main()
Example output
qos_overrides./parameter_events.publisher.depth: 1000
qos_overrides./parameter_events.publisher.durability: volatile
qos_overrides./parameter_events.publisher.history: keep_last
qos_overrides./parameter_events.publisher.reliability: reliable
start_type_description_service: True
tf_buffer_cache_time_ms: 10000
use_sim_time: False
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow