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

How to generate different random cmd_vel messages?

asked 2016-08-01 13:34:16 -0600

Hugo Sardinha gravatar image

updated 2016-08-01 13:43:14 -0600

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

edit retag flag offensive close merge delete

Comments

Run rostopic list to see what topics are being published/subscribed to. Then rostopic info <topic> to check whether your robots are accidentally subscribing to the same /cmd_vel topic.

spmaniato gravatar image spmaniato  ( 2016-08-01 15:39:07 -0600 )edit

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...

Hugo Sardinha gravatar image Hugo Sardinha  ( 2016-08-01 17:56:35 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-08-02 01:18:37 -0600

mgruhler gravatar image

The problem is that you do not properly initalize the random number generator with a seed. See e.g. http://www.cplusplus.com/reference/cs... for an explanation. You do have the srand call in there already, but commented out. Thus, this program will always produce the same "random" values.

Depending on how you start the pubvel node, you should probably initialize them differently. See e.g. this stackoverflow discussion on how to initialize it differently.

Or you could use the Boost Random Library which afaik doesn't have this problems.

edit flag offensive delete link more

Comments

Thank you very much mig. the stackoverflow discussion was where I found the answer to my problem. The reason why I had srand commented was because when it wasn't it still didn't work. I had to both have it outside the while loop and initialize it like suggested in the discussion.

Thank you!

Hugo Sardinha gravatar image Hugo Sardinha  ( 2016-08-02 07:53:33 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-01 13:34:16 -0600

Seen: 1,367 times

Last updated: Aug 02 '16