How to assign an array to a ROS message in C++
Hello, I am very new to C++ so this might be somewhat of a simple syntax question but I really don't know the keyword to look up or how to search for this problem. Similar topic in the forum does not seem to solve my problem. Basically what I want to do is to create an array of type Uint8 and assign a value to each element of the array, then publish it to a topic (in this case "chatter").
If I assign an array directly to msg.data (Ex. msg.data = {2,1,0};) then the node publish array of [2,1,0] as expected but if I create an array and assign value to it, why can't I say "msg.data = array_I_created". Is there a way to assign an array to msg so that ROS can publish it?
Thank you very much. Any help would be appreciated.
The code is as follows:
#include "ros/ros.h"
#include "std_msgs/UInt8MultiArray.h"
int main(int argc, char **argv)
{
ros::init(argc, argv, "talker");
ros::NodeHandle n;
ros::Publisher chatter_pub = n.advertise<std_msgs::UInt8MultiArray>("chatter", 1000);
ros::Rate loop_rate(10);
while (ros::ok())
{ std_msgs::UInt8MultiArray msg;
uint8_t image[5][2]; //create an array and assign value to each element
int i = 0;
int j = 0;
for (i = 0; i<5; i++){
for(j = 0; j<2; j++){
image[i][j] = i;
}
}
//msg.data = {2,1,0}; //if assign directly like this, it works but why the below doesn't?
msg.data = image //this gives an error
chatter_pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}