# Determining the turn of robot using gyroscope and accelerometer data

I have the "x", "y" and "z" readings of gyroscope and accelerometer. From these readings I want to interpret that wether my robot took a right turn or left turn. Can some one guide me how should I proceed?

• Welcome to Robotics Shifali Sharma, but I'm afraid that it is not clear what you are asking. We prefer practical, answerable questions based on actual problems that you face, so it's a good idea to include details of what you want to achieve, what you tried, what you saw & what you expected to see. Please take a look at How to Ask & tour for more information on how stack exchange works and work through the Robotics question checklist to edit your question to make it clearer. – Ben Apr 3 at 0:58
• If you have a gyro and an accelerometer, you should have 6 sensor values. Have you tried simply watching the sensor output stream as you rotate the robot? – Ben Apr 3 at 0:59

Expanding on the answer given by C.O Park and comment by Ben.

The gyroscope and accelerometer will give you 6 sensor values corresponding to the $$x$$/$$y$$/$$z$$ linear accelerations and the $$x$$/$$y$$/$$z$$ angular velocities. If you are looking to interpret whether your robot is turning left or right at any particular moment then you can simply check the sign of the angular velocity about the z-axis (assuming you are taking z to be the vertical direction). If you are looking to interpret whether your robot has turned left or right relative to its original direction then you can keep track of an accumulated angle through numerical integration. A simple algorithm for this would be the following:

Let $$\theta(t)$$ denote the rotation about the vertical axis (also called heading angle) at time $$t$$. Let $$\omega(t)$$ denote the angular velocity about the vertical axis at time $$t$$. Every $$t_s$$ seconds you will take a measurement of $$\omega(t)$$ from your gyroscope sensor. The rotation can then be estimated by $$\theta((k+1)t_s) = \theta(kt_s)+\omega(kt_s)t_s,$$ where $$k$$ is an integer that denotes how many measurements you have taken so far. $$\theta(0)$$ can be assumed zero since it is your initial direction. In words, the heading angle at timestep $$k+1$$ is the heading angle at timestep $$k$$ plus the rotation that occurred during that timestep. Then to interpret whether your robot has turned left or right relative to its original direction you can look at the sign of $$\theta$$. In pseudocode:

# intialize heading angle estimate
theta = 0;

# update information every ts seconds
loop:

# read sensor and update estimate
theta = theta + omega*ts;

# check turning direction
if omega > 0: turning = left;
else if omega < 0: turning = right;

# check net direction
if theta > 0: turned = left;
else if theta < 0: turned = right;


In practice there will be some complications like sensor noise and drift that will result in errors in your estimate of the heading angle, but this should provide you with a starting point.

• Thanks tmengi for your detailed explanation – Shifali Sharma Apr 3 at 21:13

Just monitor the rotation velocity of z-axis (z value from gyroscope or any other axis according to your setup).

If the value is over some threshold and the sign is positive, it is turning left. If the sign is negative it is the right turn.

The right way to do this is to accumulate the value stream and see if the accumulated angle is near to 90 or -90 degree. But it would be difficult for you.

• Can you share some resources where I can find the way to calculate accumulated angle? – Shifali Sharma Apr 3 at 6:36

Some common accelerometer/gyro chips have built-in processor that performs integration for you. An example I am familiar with is MPU-6050.

Given that capabilty, you can get the ready-made data for current direction of your chip directly from the chip using I2C.

Please search for "mpu 6050 dmp" to see some examples.