0
$\begingroup$

Rosanswers logo

Hi Buddy, I'm newbie in ROS and i trying to send data from arduino to Ros Topic. I have send string and integer type. But in Array type i have a small problem. This is my source code:

#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#include <ros.h>
#include <std_msgs/Int32MultiArray.h>
#include <string.h>

//Creating Nodehandle
ros::NodeHandle nh;
//Declaring String variable

std_msgs::Int32MultiArray msg_arr;
int value[2] = {12,96};

//Defining Publisher
ros::Publisher Pub_Arr("Arduino_Publish", &msg_arr);

void setup(){
  //Initializing node
  nh.initNode();
  //Start advertising
  nh.advertise(Pub_Arr);
}

void loop(){
  
  msg_arr.data = value[2];
  msg_arr.data_length =2;
  Pub_Arr.publish(&msg_arr);
  
  nh.spinOnce();
  delay(10);
}

Then i check topic:

rostopic echo /Arduino_Publish

And i get:

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

I mean i was send nothing. So, how can i fix this ? Thank you !


Originally posted by ToanJunifer on ROS Answers with karma: 7 on 2019-01-30

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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

$\endgroup$

Your Answer

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