How to generate different random cmd_vel messages?
Hello everyone,
I'm trying to have two (or several) robots receiving a random cmd_vel message. I was expecting that each robot would present a different velocity (I'm using Gazebo) but instead what I get is that every robot has the same velocity (however random, it is still the same for each one)
publisher pubvel is below as well as my launch file where I launch both the robot model and the publisher within the same namespace. One namespace for each robot.
#include "ros/ros.h"
#include "std_msgs/String.h"
#include <geometry_msgs/Twist.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
// Initialize the ROS system and become a node .
ros::init(argc, argv, "publish_velocity");
ros::NodeHandle nh;
// Create a publisher object .
ros::Publisher publisher = nh.advertise<geometry_msgs::Twist>("cmd_vel", 1000);
// Loop at 2Hz until the node i s shut down.
ros::Rate rate(2);
while (ros::ok())
{
//srand(time(0));
geometry_msgs::Twist msg;
msg.linear.x=double(rand())/double(RAND_MAX);
msg.angular.z=double(rand())/double(RAND_MAX)-1;
publisher.publish(msg);
ROS_INFO_STREAM("Sending random velocity command:"<<" linear="<<msg.linear.x<<" angular=" <<msg.angular.z);
rate.sleep();
}
return 0;
}
<launch>
<group ns="robot1">
<param name="tf_prefix" value="robot1_tf" />
<include file="$(find random_vel)/launch_files/one_robot.launch" >
<arg name="init_pose" value="-x 0 -y 2 -z 0" />
<arg name="robot_name" value="Robot1" />
<arg name="namespace_arg" value="robot1"/>
</include>
<node pkg="random_vel" type="pubvel" name="pubvel" output="screen" />
</group>
<group ns="robot2">
<param name="tf_prefix" value="robot2_tf" />
<include file="$(find random_vel)/launch_files/one_robot.launch" >
<arg name="init_pose" value="-x 0 -y -2 -z 0" />
<arg name="robot_name" value="Robot2" />
<arg name="namespace_arg" value="robot2"/>
</include>
<node pkg="random_vel" type="pubvel" name="pubvel" output="screen"/>
</group>
</launch>
Thank you very much in advance,
Hugo
Run
rostopic list
to see what topics are being published/subscribed to. Thenrostopic info <topic>
to check whether your robots are accidentally subscribing to the same/cmd_vel
topic.Thank you very much for your comment but everything seems to be fine, from the rostopic list i get both /robot1/cmd_vel and /robot2/cmd_vel. and from rostopic info i get both publishers /robot1/pubvel and /robot2/pubvel. Naturally, rqt_graph also shows this. I still get the same cmd_vel for each...