Pub to cmd_vel the robot didn't move

#define Vcomf 0.2;
float ang = 0.75634; // ang and dis are calculated from another function, I can sure ang has value not empty.
float dis = 13.2353;
void Avoid::global_control() {
  get_obstacle_info();
  if (ang > 0) {
    ROS_INFO("Start turn left");
    vel.angular.z = 0.1;
    float time = ang / vel.angular.z;
    ROS_INFO("time to totate: %f", time);
    pub.publish(vel);
    ros::Duration(time).sleep();
    ROS_INFO("Rotate finish");
  } else {
    ROS_INFO("Start turn right");
    vel.angular.z = -0.1;
    float time = ang / vel.angular.z;
    ROS_INFO("time to totate: %f", time);
    pub.publish(vel);
    ros::Duration(time).sleep();
    ROS_INFO("Rotate finish");
  }
  ROS_INFO("Start move forward");
  vel.linear.x = Vcomf;
  vel.angular.z = 0;
  pub.publish(vel);
  //  ros::Duration(dis / vel.linear.x).sleep();
  //  vel.linear.x = 0;
  //  pub.publish(vel);
}

I create the code like this, and try to make the robot rotate for a specified time then move straight.However, the robot didn’t rotate but stop for a specified time them move forward. Could any one help me with this problem?

Where are those ang & Vcomf variables defined and how?

PS: Please make use of markdown to ease the help of others. It will make code much easier to read and hence debug.

1 Like

Sorry about this, I just add them in the head of the code.

Does it print:

Start turn left
time to totate: 7.5634
Rotate finish
Start move forward

?

Yes, totally the same.

As far as I know, having done the ROS basics in 5 days (Python) course, I know that publishing once and for the first time to a topic can fail. In the course I’ve done they give a class with a function to publish to a topic once. But I could not find the same in the C++ version.

Could you try adding this at the beginning of the if statement?

    vel.angular.z = 0.1;
    pub.publish(vel);
    ros::Duration(1).sleep();

Yes! it’s works! amazing! thanks!

1 Like

Perfect!

Of course my solution is a cheap trick which gets the job done. To give a better example in the python course the following is done:

    while not self.ctrl_c:
        connections = self.bb8_vel_publisher.get_num_connections()
        if connections > 0:
            self.bb8_vel_publisher.publish(self.cmd)
            rospy.loginfo("Cmd Published")
            break
        else:
            self.rate.sleep()
3 Likes