# How to turn the robot exactly 90 degrees using ROS?

In some courses, we are asked to turn the robot 90 degrees, but it is not given any hint on how to make this.

Are there any tips on how to turn exactly 90 degrees so that we don’t waste time trying to figure this out?

You can use /odom topic to know the pose of robot.

1 Like

There several ways to do it:

1. Just use a simple time.sleep(X_seconds). Its the most basic way and it just needs you to try it several times until you get it. This has some drawbacks, which are basically that is not very precise, and that depending on the system load, the simulation will turn more or less in the same amount of seconds. But for starters, its enough

2. As @gat.vab said, using odometry. This is the best way to do it. Because it doesn’t depend on how powerful or not is your PC. You have to create a subscriber to odom and then extract the orientation and convert it from Quaternions (x,y,z,w) to euler ( Roll, Pitch and Yaw). Here you have an example

``````def euler_from_quaternion(self, quaternion):
"""
"""
x = quaternion[0]
y = quaternion[1]
z = quaternion[2]
w = quaternion[3]

sinr_cosp = 2 * (w * x + y * z)
cosr_cosp = 1 - 2 * (x * x + y * y)
roll = np.arctan2(sinr_cosp, cosr_cosp)

sinp = 2 * (w * y - z * x)
pitch = np.arcsin(sinp)

siny_cosp = 2 * (w * z + x * y)
cosy_cosp = 1 - 2 * (y * y + z * z)
yaw = np.arctan2(siny_cosp, cosy_cosp)

return roll, pitch, yaw
``````

You can also do it like this:

``````from tf.transformations import euler_from_quaternion, quaternion_from_euler

def get_rotation (msg):
global roll, pitch, yaw
orientation_q = msg.pose.pose.orientation
orientation_list = [orientation_q.x, orientation_q.y, orientation_q.z, orientation_q.w]
(roll, pitch, yaw) = euler_from_quaternion (orientation_list)
print yaw
``````

As you wish

2 Likes

Thank you very much, @gat.vab and @duckfrost2 .

1 Like