Publish a numpy.array in a UInt8MultiArray in python
Hi all,
Pretty new to python and ROS. I have a node that subscribes to some data, in the callback it then does some calculations on that data and creates a new np.array. I then want to publish this new array in a UInt8MultiArray with a fixed size so that I can receive it with another node and send it over UDP.
The example below doesn't include the calculations but the output of the code at the moment is
[[ 31.41386309 292.95704286 2.44705586]]
<type 'numpy.ndarray'>
I need to send these values with a fixed length over a Uint8MultiArray so that I can pack it and send it over UDP. I've tried multiple ways but can't seem to get it to work. I was wondering if someone can help with some example code or ideas/best ways.
#!/usr/bin/env python
import rospy
from std_msgs.msg import UInt8MultiArray
import struct
import math
import numpy as np
def callback(data):
#Calculations before to create enu with data from callback.
enu = **
print(enu)
print(type(enu))
pub_packet.publish(gps_enu)
def lla2enu():
global gps_enu, pub_packet
rospy.init_node('lla2enu', anonymous=True)
rate = rospy.Rate(250) # 10hz
rospy.Subscriber("packet", UInt8MultiArray, callback)
pub_packet = rospy.Publisher('gps_enu', UInt8MultiArray, queue_size=10)
rospy.spin()
if __name__ == '__main__':
try:
lla2enu()
except rospy.ROSInterruptException:
pass
Any help is much appreciated. Thanks guys.
pedantic, but:
UInt8MultiArray
by definition cannot have a fixed size.@gvdhoorn, thanks for the reply. Maybe I can manipulate the data inside enu to be of a specific size to guarantee the size of the UInt8MultiArray?
My comment was slightly unfair: I just meant to say that
UInt8MultiArray
uses unbounded lists for its fields, so by definition those do not have a fixed size. You can of course specify a certain size to be used, but that would be completely at the application level (and not enforced in/by ROS).I would also think that the serialisation to your UDP packet/datagram would be orthogonal to the ROS msg type you're intending on using. If an incoming msg doesn't "fit", you could ignore it / raise an error, etc.
Also note btw:
*MultiArray
is an extremely bad choice for a topic. It has .... absolutely no semantics associated, other than that it is a nD array with fields of a certain type.
That makes interpretation of the data completely dependent on information external to the msg, which goes against best practice in ROS.
Thanks a lot for the replies. What msg type would you recommend for a topic that just wants to publish 3 float values?
That completely depends on the semantics.
What do those values represent?
These values represent a local x,y,z value in metres.