
You're almost there, but there is a difference between how rosserial constructs vectors to the rest of ROS. rosserial doesn't use std::vector objects, the overhead is too high for the embedded platform so they use simple C pointer and length arrays in instead.
So in your case you need to set the data member as you have but you also need to set the data_length member to 2 in this case. This is explained here.
Hope this helps.
Originally posted by PeteBlackerThe3rd with karma: 9529 on 2019-01-30
This answer was ACCEPTED on the original site
Post score: 4
Original comments
Comment by ToanJunifer on 2019-01-30:
Thank you buddy. I am trying your guide. I will reply soon
Comment by ToanJunifer on 2019-01-30:
I dont understand this code clearly. I understand that jstate class has name, position field so we can call it. but in my case i dont know how to call anything.
Do you have any example ?
jstate.name=name;
jstate.position=pos;
jstate.velocity=vel;
Comment by PeteBlackerThe3rd on 2019-01-30:
The example I linked to is for a joint state message, which contains several array elements. It's more complex than your but the point is that in rosserial each array element also has a value _length which needs to be set as well. data_length in your case.
Comment by ToanJunifer on 2019-01-30:
I'm trying to set:
Arr_msg.data_int=datax;
Arr_msg.data_int_length = 2;
but it return an error !
'Arr_msg' does not name a type
Comment by PeteBlackerThe3rd on 2019-01-30:
can you update your source code in the question again, it looks like something has been mixed up. You should be setting Arr_msg.data and Arr_msg.data_length, and it looks like you've broken the declaration of Arr_msg.
Comment by ToanJunifer on 2019-01-30:
ok. I will update source again.
Comment by ToanJunifer on 2019-01-30:
Source Code is updated, buddy
Comment by ToanJunifer on 2019-01-30:
After I run:
rostopic echo /Arduino_Publish
Return this:
layout:
dim: []
data_offset: 0
data: [-25493504, -846506371]
---
layout:
dim: []
data_offset: 0
data: [-25493504, -846506371]
It sent something wrong value or wrong type!
Comment by PeteBlackerThe3rd on 2019-01-30:
It sent the contents of a fairly random memory address! You need set Arr_msg.data = value; using the name without the square brackets makes c decompose it to a pointer. If you use value[2] it takes the value of the 3rd element and uses it as an address, this is definitely not what you want!
Comment by ToanJunifer on 2019-01-30:
Thank you for your support. I sent successfully