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

How to send Array Message from Arduino to ROS Topic ?

asked 2019-01-30 05:48:44 -0500

ToanJunifer gravatar image

updated 2019-01-30 08:57:27 -0500

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 !

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
4

answered 2019-01-30 06:46:52 -0500

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.

edit flag offensive delete link more

Comments

Thank you buddy. I am trying your guide. I will reply soon

ToanJunifer gravatar image ToanJunifer  ( 2019-01-30 07:39:49 -0500 )edit

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;
ToanJunifer gravatar image ToanJunifer  ( 2019-01-30 08:33:31 -0500 )edit
1

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 <name>_length which needs to be set as well. data_length in your case.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-01-30 08:35:36 -0500 )edit

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
ToanJunifer gravatar image ToanJunifer  ( 2019-01-30 08:41:22 -0500 )edit
1

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.

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-01-30 08:50:49 -0500 )edit

ok. I will update source again.

ToanJunifer gravatar image ToanJunifer  ( 2019-01-30 08:52:15 -0500 )edit

Source Code is updated, buddy

ToanJunifer gravatar image ToanJunifer  ( 2019-01-30 08:58:11 -0500 )edit

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!

ToanJunifer gravatar image ToanJunifer  ( 2019-01-30 09:02:44 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2019-01-30 05:48:44 -0500

Seen: 2,178 times

Last updated: Jan 30 '19