0
$\begingroup$

Rosanswers logo

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.


Originally posted by DRos on ROS Answers with karma: 23 on 2018-08-22

Post score: 0


Original comments

Comment by gvdhoorn on 2018-08-22:\

in a UInt8MultiArray with a fixed size

pedantic, but: UInt8MultiArray by definition cannot have a fixed size.

Comment by DRos on 2018-08-22:
@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?

Comment by gvdhoorn on 2018-08-22:
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).

Comment by gvdhoorn on 2018-08-22:
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 ..

Comment by gvdhoorn on 2018-08-22:
.. 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.

Comment by DRos on 2018-08-22:
Thanks a lot for the replies. What msg type would you recommend for a topic that just wants to publish 3 float values?

Comment by gvdhoorn on 2018-08-22:
That completely depends on the semantics.

What do those values represent?

Comment by DRos on 2018-08-22:
These values represent a local x,y,z value in metres.

Comment by gvdhoorn on 2018-08-22:
Then I would suggest to use an appropriate msg from the geometry_msgs pkg, such as PointStamped or perhaps even PoseStamped.

I'm recommending the Stamped variaties, as the non-stamped ones don't provide a reference frame. Even if the value is a ..

Comment by gvdhoorn on 2018-08-22:
.. "local coordinate", that sounds like something that would still be relative to something. The stamped msgs allow you to capture that. The non-stamped versions don't.

Comment by DRos on 2018-08-22:
Thanks gvdhoorn, I'll take a look at the links you've mentioned below with the numpy support in rospy and the Point/PoseStamped msgs. I'll get back to you if successful

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Haven't used it myself, but perhaps eric-wieser/ros_numpy can help here.

And rospy itself also has support for numpy. See rospy.numpy_msg and Using numpy with rospy fi.


Originally posted by gvdhoorn with karma: 86574 on 2018-08-22

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by DRos on 2018-08-24:
Thanks for this. Using the talker example and numpy_msg(Floats) as a message type worked perfectly. Thank you!

Comment by gvdhoorn on 2018-08-24:
Glad it worked for you.

Just noticed this btw in your code:

rate = rospy.Rate(250) # 10hz

250 != 10 Hz.

Comment by DRos on 2018-08-24:
Ahh yes, it's carried over from another node. Poor documentation, my bad!

Comment by gvdhoorn on 2018-08-24:
Also: the rate is not even being used.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.