ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Does ROS have anything similar to a "global variable"?

asked 2021-12-09 20:27:54 -0500

kidpaul gravatar image

updated 2021-12-09 20:30:12 -0500

I created a node that subscribes data from 3 other nodes (2 nodes publish data in 30Hz while 1 node publishes in about 10Hz). The node I created uses a transformation matrix(4x4) from the slower node (10Hz) to transform data from other 2 faster nodes (30Hz) and publishes them. The problem is that I don't need every single transformation matrix (but occasionally) from the data stream of the slower node and I also don't want this one becomes a bottleneck of this entire process by using message filter time_sync. So, I wonder whether ROS has something similar to a global variable that sits in one place and can be read or modified so that the node I created doesn't have to wait until all data from 3 other nodes get subscribed.

edit retag flag offensive close merge delete


I don't think there's a global variable concept in ROS1 for this purpose. Could you perhaps limit the publishing rate of the 10Hz node itself since you want to use it at a lower rate anyway? Or you could have a wrapper node around the 10Hz node that implements that rate reduction and/or potentially caches the transform matrix.

bergercookie gravatar image bergercookie  ( 2021-12-10 04:10:18 -0500 )edit

Thank you for the verification! The reason why I want to find something like global variable is to avoid the lower rate publishing.

kidpaul gravatar image kidpaul  ( 2021-12-10 11:20:21 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2021-12-10 07:02:56 -0500

Mike Scheutzow gravatar image

You tagged this rospy so I'll assume you're asking about python. Yes, python supports global variables within a single OS process. However, you should implement thread-safety if you read/write such a variable from multiple places in your code, typically by using a mutex.

edit flag offensive delete link more


Thank you for your answer! Yeah, it seems like python global variable is the way to do what I want.

kidpaul gravatar image kidpaul  ( 2021-12-10 11:16:13 -0500 )edit

answered 2021-12-10 12:37:41 -0500

Airuno2L gravatar image

If you're only using the data in a single node, the most common way to do this would be to make your node a class, then have the subscriber to the slow data update a class variable. Then your fast subscribers would just access the class variable.

ROS does have network wide variables like the parameter server and dynamic reconfigure, but since it sounds like what you're doing is within a single node, I think the class variable is the way to go.

edit flag offensive delete link more


I would go with this approach if I needed constant updates by the data stream of a slower node. Thank you for the idea though!

kidpaul gravatar image kidpaul  ( 2021-12-10 20:00:47 -0500 )edit

Question Tools



Asked: 2021-12-09 20:27:54 -0500

Seen: 460 times

Last updated: Dec 10 '21