# measuring angle using gyro sensor

I have gyro sensor l3g4200d and output from sensor is angular velocity (deg/sec), i want convert output from sensor to angle value (deg). And to convert, i must integrate angular velocity (deg/sec) like equation below:

$$\Theta = \Theta_0 + \int \omega(t) dt$$

Where:

$\Theta$ = angle (deg)

$\Theta_0$ = start angle (deg)

$\omega(t)$ = angular velocity (deg/sec)

$dt$ = time (sec)

How to write that equation into cvavr source code?

• WHat is cvavr source code? – Gustavo Litovsky Sep 26 '13 at 5:57
• I just can't help myself... no gyro sensor is required; instead, begin by measuring the diameter of the pinhead, p; then measure the number of angels you can fit on the pinhead, n; the size of each angel is p^2*PI/4n. – markt Sep 26 '13 at 7:31
• Angels are imaginary and hence can't be measured. – John U Sep 26 '13 at 8:35

You just need to perform discrete time integration. It's quite simple actually. The absolute simplest version goes something like this for each sample.

1. Collect the sample from your gyro

sample = gyro_sample;

2. Get the current time

time = current_time;

3. Calculate the time elapsed since the last sample by differencing the time samples

delta_time = current_time - previous_time;

4. Multiply the sample and the time difference - this is your change in angle since the last sample

delta_angle = sample * delta_time;


current_angle = current_angle + delta_angle;

6. Save the current time as previous time

previous_time = time;


You'll need to do this for each axis coming from the gyro.

This all being said, and answering your question, there is a bit more.

This will provide rather horrible results. The angle will drift very rapidly. You'll need to start with a known orientation and then you'll have an accurate orientation on the order of seconds. Additionally, if you're using Euler angles you'll run into issues with poles. To get more accurate results you'll need to implement some rather complex filtering. You haven't described your application though, so it's hard to say what you actually require. You might be able to scavenge some code from full IMU projects. Many of the IMUs on Sparkfun have example code.

http://www.instructables.com/id/Accelerometer-Gyro-Tutorial/

This will provide you some equations which you may need. I've used L3g420d to make IMU using I2C interfacing. All the best