Trouble accessing Int32MultiArray message data in Arduino

asked 2021-06-28 19:33:36 -0500

HM_1991 gravatar image

Hi everyone!

I am sending an Int32MultiArray message "act_msg" from Python, which I am trying to read in Arduino.

The topic over which the message is being broadcasted is "move_B1L".

Here is the Python code which is publishing two numbers (60 and 90) in an array. I have left out much of the code as this part is working fine:

act_msg = Int32MultiArray()
act_msg.data = [60, 90]
pub.publish(act_msg)

To confirm it is working, here is what rostopic echo move_B1L shows me:

layout:
   dim: []
   data_offset: 0
data: [60, 90]
---

I am now trying to read the contents of move_B1L in an Arduino callback function:

std_msgs::Int32MultiArray try1_msg;          //Setup test message of Int32MultiArray
ros::Publisher test("test", &try1_msg);       //Setup test message publisher

void B1LCallback(const std_msgs::Int32MultiArray& move_B1L){
  test.publish( &move_B1L );

I am simply getting Arduino to publish the same array it has received from Python, over the topic "test".

Now when I do try rostopic echo test, here is the output I get:

layout: 
  dim: []
  data_offset: 0
data: [29, 90]
---

The two arrays are not the same.

I am trying to assign each of the elements of move_B1L to a separate variable in Arduino, but for some reason I am not able to access the data inside the message.

For example, I followed the guidance on this link: https://github.com/ros-drivers/rosserial/issues/45 and tried to implement the following:

std_msgs::Int32MultiArray try1_msg;

long real_data_storage[2];

ros::Publisher test("test", &try1_msg);

void B1LCallback(const std_msgs::Int32MultiArray& move_B1L){
  try1_msg.data_length = 2;
  try1_msg.data = real_data_storage;
  try1_msg.data[0] = move_B1L.data[0];
  try1_msg.data[1] = move_B1L.data[1];
  test.publish( &try1_msg );

And this gives me something unexpected when I do rostopic echo test again:

layout: 
  dim: []
  data_offset: 0
data: [0, 90]
---

This is different from the previous run of rostopic echo test, even though nothing else has changed.

Can you please help me access the values inside the message correctly?

Thank you so much!

HM

edit retag flag offensive close merge delete

Comments

SInce you are using c++, could it be that move_B1L.data is a std::vector? If you were using c, it would definitely be a ptr to array. You need to look in the .h file that was generated to define std_msgs::Int32MultiArray.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-06-29 19:37:59 -0500 )edit

I generated the client-side messages myself, and data is a plain pointer, an int32*. The next thing I would suggest trying is to properly define the one-dimensional data array in layout.dim.

Mike Scheutzow gravatar image Mike Scheutzow  ( 2021-07-01 07:06:48 -0500 )edit

Hello! I had a quite same problem. I try read UInt16MultiArray by Arduino using rosserial (ver. 0.9.1) and first element of array was always particular value (128). Using more older version solved this problem (for example, 0.7.9).

404_user gravatar image 404_user  ( 2022-02-09 03:31:58 -0500 )edit