Publishing multiple variables using a single publisher
I have written a publisher to publish a float32
value to a topic depth_m
. Now, I need to publish two more variables and they are of type int
, not float
. I would like all three of these variables to be published to the same topic. I am referring to the official tutorial). Do I just replace Float32
in the following lines with Int32
and add it to the existing publisher?
ros::Publisher depth_pub = pub_.advertise<std_msgs::Float32>("depth_m", 1000);
std_msgs::Float32 msg;
I believe I will have to edit msg
to a different variable name msg1
, msg2
. According to me the whole thing will have the following lines:
ros::Publisher depth_pub = pub_.advertise<std_msgs::Float32>("depth_m", 1000);
ros::Publisher depth_pub2 = pub_.advertise<std_msgs::Int32>("depth_m", 1000);
std_msgs::Float32 msg;
std_msgs::Int32 msg1;
std_msgs::Int32 msg2;
msg.data=depth;
msg1.data=var1;
msg2.data=var2;
depth_pub.publish(msg);
depth_pub2.publish(msg1);
depth_pub2.publish(msg2);
Is this the right strategy? If do this, will the whole thing act as a single message or multiple messages? Can I avoid two advertise
lines by using just std_msgs
instead of std_msgs::Float32
?