Pages

Sunday, 20 July 2014

Getting Roll, Pitch and Yaw from MPU-6050

Part 3 in our quest for Accelerometer and gyroscope values. In the previous entry we got the acceleration values in the form of G's and also the linear velocity in terms of degrees/s however these values aren't much use to us. Wouldn't it be better if we had roll,pitch and yaw and also angular position instead? Well that's what I am going to try to achieve in this post.

Roll, Pitch and Yaw

enter image description here
For those that don't know what these are look at the diagram above, it shows that the roll is the rotation about the Y axis, the pitch is the rotation about the X axis and the yaw is the rotation about the Z axis.

How do we convert gravity into these values?

Well I am not going to go into the theory of how these values convert to roll and pitch because some nice mathematicians over on stackoverflow have done that for us.
The formulas to convert the acceleration values to pitch and roll are as follows.
Roll = atan2(Y, Z) * 180/PI;
Pitch = atan2(X, sqrt(Y*Y + Z*Z)) * 180/PI;
 We use atan2 as it works over 4 quadrants, if we used atan then we would have problems with angles over 90 degrees. Don't worry about Yaw for now as we can't determine that using just the accelerometer, this is intuitive if you think about how Yaw is rotation about the Z axis. We will just read a constant 1G in the Z axis no matter how the accelerometer is rotated, We will need to augment our acceleration values with the Gyroscope ones to get the full picture which we will do in a later entry.

I have rewritten the PSoC code to respond to an interrupt on the UART. Whenever the PSoC receives the character 'a' it will respond with the acceleration values in comma seperated form of X,Y,Z.

So if you send 'a' it will give you back something like "0.1, 0.2, 1"  < these are all abitrary but something around 0 for X, 0 for Y and 1 for Z is expected if the MPU6050 is on a flat surface.

The new PSoC code can be found on my Github under "GettingG'sWithInterrupt" I have left the conversion to Roll and Pitch in the code but kept it commented out as I will be doing the conversion from LabVIEW but just uncomment it if you want the conversion on the PSoC.

Visualising our values

Now this part is up to you are the PSoC code will work on any platform/language aslong as you poll it with the character 'a' at 9600 Baud. I personally used LabVIEW to do the polling as it has some nice inbuilt dials and bar graphs as can be seen in the screenshot below.


 I have uploaded a Windows exe on my Github under /Examples/LabVIEW/Dials/EXE//MPU6050dials.exe of the Labview progam, please note you will have to install the LabVIEW runtime engine to run the EXE

I have also added the vi if you want to have a go at changing it.

Instructions for use
Change COM port to whatever your device is connected to, click write then read, you should see the text box fill up with values and you should see the LED blinking on the PSoC pioneer. If not you probably have a problem with your serial port connections.

In the next entry I will look at getting the Gyroscope values off the PSoC onto LabVIEW dials and then move onto combining the values and filtering them to get accurate non noisy values. You will notice at the minute the values jump around a lot, we need to get rid of this.


2 comments:

  1. como puedo visualizar estos datos en labview

    ReplyDelete
  2. como puedo visualizar estos datos en labview

    ReplyDelete