Pages

Thursday 14 August 2014

Getting Gyroscope values from MPU-6050

Part 4 in my series on the MPU-6050, In the last entry we got the roll and pitch values out of the Accelerometer and displayed them on a little LabVIEW vi with some dials. In this entry I want to get to the gyroscope values and see if I can get them displaying on some dials too. After we have the gyroscope values we can look at combining the 2 sets of data and having a useful Roll,Pitch and Yaw sensor.

How does a gyroscope work?

A gyroscope is a MEMS which contain small strips of metal that bend when the gyro twists and moves. By measuring the amount of bending the gryo can accurately report what angular velocity it is experiencing (1)

I found Pieter Jan's blog post on this very useful to understand this subject, all the maths is from there.

Well what is "Angular velocity"? 

Angular velocity is "The rate of change of angular position" and it's units are "degrees per second". The gyroscope has 3 axis so the value we get when we poll each axis is going to be the angular velocity at the current time in deg/sec.

$$Angular\: Velocity = \: \dot{\theta}= \: \frac{\mathrm{d}\theta }{\mathrm{d} t}$$
where theta is just the angle.

We really want the angle not the rate of change of the angle so if you remember from calculus, to get rid of a derivative you integrate it giving us.

$$ \theta(t)\:=\:\int_{0}^{t}\dot{\theta}\:dt $$
 So all we need to do is integrate the 3 axis values and it will give us the current angle, now you ask yourself..

How do we integrate in software? 

As we are using a microcontroller (digital system) we are going to need to make a software approximation of an integral. You may remember that an integral is just the area under a curve so what if we just took the current sample and multiplied it by the time since the last sample to give us the area of a rectangle and we just sum this and all other rectangles together to give us the area under the curve as shown in the photo below.

http://www.facstaff.bucknell.edu/mastascu/econtrolhtml/sampled/Sampled3A02.gif
Where u(t) is our current value and T is the sample time. This is known as the rectangular approximation  it is good enough for what we need but as you can see from the diagram if the sample time is too long then bits of the graph may be overestimated (the red bits going over the curve). A more accurate approximation is the trapezoidal but we will just use rectangular for ease.

The pseudocode of what we are trying to achieve is going to be:
  1. Start timer
  2. Wait for timer to elapse
  3. Read in gyroscope values
  4. Multiply each by the sample time to get area of rectangle
  5. Add area to sum of all previous areas to give us current position

 Building on the project we made in the last part I am going to get the PSoC to constantly sample the gyroscope values and when it is polled with the character "g" at 9600 baud it will respond with the current integrated angles.

What sample time should I choose?


I will finish this soon


$$E=mc^2$$

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Mate did you ever get DMP working with this unit?

    ReplyDelete
  3. dude did you ever get it working???????

    ReplyDelete