Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hy, following a working code tried on an Arduino Mega and ROS Kinetic:

include <ros.h>

include <std_msgs uint16multiarray.h="">

ros::NodeHandle nh;

std_msgs::UInt16MultiArray arr; ros::Publisher pub_arr( "/arr", &arr);

void setup() { nh.getHardware()->setBaud(115200); nh.initNode(); arr.layout.dim = (std_msgs::MultiArrayDimension ) malloc(sizeof(std_msgs::MultiArrayDimension)2); arr.layout.dim[0].label = "height"; arr.layout.dim[0].size = 4; arr.layout.dim[0].stride = 1; arr.layout.data_offset = 0; arr.data = (int )malloc(sizeof(int)8); arr.data_length = 4; nh.advertise(pub_arr);

}

void loop() {
arr.data[0] = 1; arr.data[1] = 2; arr.data[2] = 3; arr.data[3] = 4; pub_arr.publish(&arr); nh.spinOnce(); }

Hy, following a working code tried on an Arduino Mega and ROS Kinetic:Kinetic: Commands in ROS:

include <ros.h>

include <std_msgs uint16multiarray.h="">

$ roscore

ros::NodeHandle nh;//New terminal

$ rosrun rosserial_python serial_node.py /dev/ttyACM0 _baud:=115200

//New terminal

$ rostopic echo /arr

Arduino Code for Publishing an array with 4 words:

#include <ros.h>
#include <std_msgs/UInt16MultiArray.h>

ros::NodeHandle nh;

std_msgs::UInt16MultiArray arr;
ros::Publisher pub_arr( "/arr", &arr);

&arr);

void setup() { nh.getHardware()->setBaud(115200); nh.initNode(); arr.layout.dim = (std_msgs::MultiArrayDimension ) *) malloc(sizeof(std_msgs::MultiArrayDimension)2); malloc(sizeof(std_msgs::MultiArrayDimension)*2); arr.layout.dim[0].label = "height"; arr.layout.dim[0].size = 4; arr.layout.dim[0].stride = 1; arr.layout.data_offset = 0; arr.data = (int )malloc(sizeof(int)8); *)malloc(sizeof(int)*8); arr.data_length = 4; nh.advertise(pub_arr);

nh.advertise(pub_arr); }

}

void loop() {
arr.data[0] = 1; arr.data[1] = 2; arr.data[2] = 3; arr.data[3] = 4; pub_arr.publish(&arr); nh.spinOnce(); }

}