Robotics StackExchange | Archived questions

Send an array or a list between nodes?

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 sensdata to a list like `data = self.sensdata.tolist()is`.

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?

Asked by LucB on 2023-01-26 05:47:48 UTC

Comments

Answers

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]]]

Asked by stevemacenski on 2023-01-26 13:29:08 UTC

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?

Asked by LucB on 2023-01-26 15:06:27 UTC

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.

Asked by stevemacenski on 2023-01-26 19:29:34 UTC