Electronic – measuring angle using gyro sensor

avrmicrocontrollersensor

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?

Best Answer

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;
    
  5. Add this difference to your current angle

    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.