How to make RosBOT turning to 90 degree?

Hello,
First post in the forum, I hope that it meets the rules. I am trying to make RosBOT turning to 90 degree. I was able to make it work by the following code:

ros::Publisher action_pub;
geometry_msgs::Twist set_vel;

case RIGHT:
    set_vel.linear.x = 0;
    set_vel.angular.z = -2;
    action_pub.publish(set_vel);
    sleep(2);
    set_vel.linear.x = 0.2;
    set_vel.angular.z = 0;
    break;
I know its not the correct way. And the angle depends on a lot of external factors. I found in the forums that some node should be publishing nav_msgs/Odometry, probably as an /odom or /odom_combined topic. The orientation field in that message contains a quaternion indicating your robot's 3-dimensional orientation. Converting that to a yaw angle should give your 2-dimensional heading, if that is what you want. But I dont know how to implement this in my code. Can anybody help with a practical example?

Hi gdgeorgiev,
In Twist message angular velocity is using SI units so in this case it is radians per second (rad/s), so theoretically your rosbot should turn 4 radians which is 230 degrees. the reason why it’s turning only 90 degrees is because if you don’t send data often enough, the robot stops for security reason. So if you want to use twist message you should publish message many times in short period of time, for example:

case RIGHT:
    set_vel.linear.x = 0;
    set_vel.angular.z = -1.57;  // 1.57 radians = 90 degrees
    for(int i = 0; i < 4; i++){
        action_pub.publish(set_vel); // we publish the same message many times because otherwise robot will stop
        sleep(0.25);
    }
    set_vel.angular.z = 0;
    action_pub.publish(set_vel);
    set_vel.linear.x = 0.2;
    break;

but in the real world this solution is not perfect because of delay. I’ll post more practical example using the /tf message soon.
Best regards,
Karol Konkol
Husarion team

Hi gdgeorgiev,
As I promised, I found for you a practical example to turn your rosbot to a specific angle.
https://wiki.ros.org/pr2_controllers/Tutorials/Using%20the%20base%20controller%20with%20odometry%20and%20transform%20information
If you have any more questions please let me know :slight_smile:
Best regards,
Karol Konkol
Husarion team

2 Likes

Hi Karol_Konkol,
Thank you for your help! I tried the provided code in the first reply. However, setting the angular.z to -1,57 wasn’t enough for 90 degree turn. By trial and error, i found that -1,90 makes the robot turn to 90 degree. I tried with and without the for loop and in my scenario, it doesnt have a difference. It works even without “for” loop.
This is the code that i run for right turn now:
case RIGHT:
set_vel.linear.x = 0;
set_vel.angular.z = -1.90; // 1.57 radians = 90 degrees
//for(int i = 0; i < 4; i++){
action_pub.publish(set_vel); // we publish the same message many times because otherwise robot will stop
//sleep(0.5);
// }
sleep(2);
set_vel.angular.z = 0;
action_pub.publish(set_vel);
set_vel.linear.x = 0.2;
break;
I am trying to port the turning function from the tutorial, but having a hard time with for example: declaring NodeHandle in main, then call it from another function and so on.
Thank you for the guidance! For now, I will use the first method. If i have success with the second method, i will write here.

Hi gdgeorgiev
I am glad that I could help you. You are right it is not necessary to send message every 0,25 sec but it is necessary to send the message every 1 sec. You can try to change time in sleep() function to 8 sec and then to 1 sec and you will see the same result. About the link I provided, at first you can try to subscribe to /pose topic for example in other thread and try to look at the change of the angle and if it matches your goal stop the rosbot. Here is how data /from pose looks:

header: 
  seq: 7564
  stamp: 
    secs: 1618238073
    nsecs: 280737043
  frame_id: "odom"
pose: 
  position: 
    x: 0.0124747110531
    y: -0.016926350072
    z: 0.0
  orientation: 
    x: 0.0
    y: 0.0
    z: -0.8232588583
    w: 0.567666145046

If you want to measure how much you turn look at orientation parameters.