Robotics StackExchange | Archived questions

Header and timestamp for Float32MultiArray in ROS2

Hi, I am working with ROS 2 foxy distribution. I have a publisher which sends an array (for every second) to the ROS queue. I am able to successfully subscribe it and save it. I want to add a timestamp every time the array is getting sent over ROS queue. I tried the following code.
Without timeStamp: // Working

datatosend = Float32MultiArray()

datatosend.data = my_array

samplepublisher.publish(datatosend)

With added timestamp code: // Not Working

pp = PublishingNode() // creating an instance of the class

datatosend = std_msgs.msg.Header()

datatosend.stamp = pp.getclock().now().tomsg()

datatosend.data = my_array

AttributeError: 'Header' object has no attribute 'data'

The aim is send an array along with the timestamp every second using python in ROS2.

Any guidance would be helpful?

Regards Niranjan

Asked by NiranjanRavi on 2021-09-03 13:44:50 UTC

Comments

Answers

If you examine std_msgs/msg/Header, the only two attributes are stamp and frame_id.

For your purpose, you could create your own custom message StampedArray.msg:

builtin_interfaces/Time stamp
std_msgs/Float32MultiArray array

Then you should be able to do something like:

data_to_send = StampedArray()
data_to_send.stamp = pp.get_clock().now().to_msg()
data_to_send.array.data = my_array

Asked by abhishek47 on 2021-09-04 05:48:05 UTC

Comments

Hi, Yes it worked perfectly. I ended up creating my own custom messages and was able to publish and receive the values.

Asked by NiranjanRavi on 2021-09-20 16:58:52 UTC