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

Send an array or a list between nodes?

asked 2023-01-26 04:47:48 -0500

LucB gravatar image

updated 2023-01-26 14:43:36 -0500

Hi, i'm using ROS2 Humble.

I'm trying to collect my sensordata in one node and want to use these sensordata in another node.

therefore i ceated a numpy array to save the data in

    self.sens_data=np.zeros((1, self.timesteps, self.features))

then i tried to convert it to a list and publish it.

    msg = Float32MultiArray()
    msg.data = self.sens_data.tolist()
    self.publisher_.publish(msg)

but then i get this error.

AssertionError: The 'data' field must be a set or sequence and each value of type 'float' and each float in [-340282346600000016151267322115014000640.000000, 340282346600000016151267322115014000640.000000]
[ros2run]: Process exited with failure 1

The output of print(sens_data.dtype) is float 64. The output of print(type(data)) after converting sens_data to a list like data = self.sens_data.tolist() is <class 'list'>.

For example my array has the size (1,80,6). I also found out that it is possible to save the message to a ros bag which might be faster. But there i have the same problem with converting the Data.

How can i send a list or an array to another Node?

EDIT:

The data i collect is Sensordata. The purpose of sending this data is, that i have a AI which is predicting something based on the Sensordata. The AI is a LSTM-Network, thats why the data has to be in that shape. Maybe there is a better way of getting the data to my AI node?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
0

answered 2023-01-26 12:29:08 -0500

Just running these in an ipython terminal to look at the data, this is definitely not just a 1D list of data. Its a list of lists... of lists. You'd need to either flatten this out or create a new ROS message that is a list of list of lists of floats (though the outer most list only as the 1 enter, so maybe just list of lists).

sens_data=np.zeros((1, 100, 10))
sens_data.tolist()

Out[4]: 
[[[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  ... 
  [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]]
edit flag offensive delete link more

Comments

Thanks for your answer!!

If i flatten it, how can i get the previous shape on the subscriber site?

So far i got no experience with creating a custom message. Could you maybe give me an example on how i could do this? I couldn‘t figure out how it works on the tutorial for numpy arrays.

I also tried to convert it to a string which is deprecated and converts it direct to bytes. But also sending this message with bytes didn’t work.

The data i collect is Sensordata. The purpose of sending this data is, that i have a AI which is predicting something based on the Sensordata. The AI is a LSTM-Network, thats why the data has to be in that shape. Maybe there is a better way of getting the data to my AI node?

LucB gravatar image LucB  ( 2023-01-26 14:06:27 -0500 )edit

Recovering the shape would be up to you to figure out as the implementer. But if you can't, then define a new message which can contain the information you're interested in. That tutorial you linked to is pretty straight forward. You need an array of arrays (of arrays? if that 3rd level ever changes). Then do what you did here to populate it.

stevemacenski gravatar image stevemacenski  ( 2023-01-26 18:29:34 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2023-01-26 04:47:48 -0500

Seen: 507 times

Last updated: Jan 26 '23