CSE 466 Lab 4:
I2C Gyroscope control

Objectives

In this lab, you will use the GY-521 Accelerometer-Gyroscope module to measure 3-axis acceleration and 3-axis rotational movement. You will also experiment with controlling your spinning cube with movements of the module.

Suggested Reading and Resources

Part 1: To begin

A. Locate the blue GY-521 board in your kit, and the right-angle terminal strip supplied with it:
GY-521

Solder the short pins of the right-angle terminal strip to the GY-521 as shown:
pins

Connect the four pre-prepared wires in your kit to the module and to your Teensy as shown here:

Hardware setup:
MPU6050 Breakout --------- Arduino
3.3V ------------------------- 3.3V
SDA ------------------------- A4
SCL ------------------------- A5
GND ------------------------ GND

SDA and SCL should have external 10k pull-up resistors (to 3.3V).

B. To test, load and run the MPU6050BasicExample.ino sketch. Be sure to open the debug window immediately.
You should see data like this:

MPU6050 is online...
 x-axis self test: acceleration trim within : 0.6% of factory value
 y-axis self test: acceleration trim within : 0.6% of factory value
 z-axis self test: acceleration trim within : 0.7% of factory value
 x-axis self test: gyration trim within : 0.2% of factory value
 y-axis self test: gyration trim within : -0.2% of factory value
 z-axis self test: gyration trim within : 0.3% of factory value
MPU6050 initialized for active data mode....
X-acceleration: -3.30 mg Y-acceleration: 1.71 mg Z-acceleration: 1005.68 mg
 X-gyro rate: -1.9 degrees/sec Y-gyro rate: 0.3 degrees/sec Z-gyro rate: -0.3 degrees/sec
 Temperature is 13.31 degrees C
X-acceleration: 14.40 mg Y-acceleration: 0.98 mg Z-acceleration: 1003.36 mg
 X-gyro rate: 0.4 degrees/sec Y-gyro rate: 0.1 degrees/sec Z-gyro rate: 0.0 degrees/sec
 Temperature is 23.27 degrees C


Note that a version of the data is printed on the LCD display.

Also note that we are not using a library for the MPU6050, even though we have one installed. Instead, we are defining the register addresses for the MPU6050 in our sketch, and addressing those registers in the body of the sketch.

C. Make a version of the sketch called MPU6050Graph1.ino to display bar-graphs of the 6 variables, use the long axis of the display and select interesting colors for background, acceleration, and gyro rate.

Comment out the debug printing to allow your sketch to run at maximum rate.

Demonstrate your running sketch to the TA.

Question 1: How many addressable registers are in the MPU-6050? (refer to the datasheet, register reference and example code)
Question 2: What is the data format of I2C messages written to the MPU-6050?

Part 2: Smoothing the data and controlling your spinning cube

Begin with a copy of your sketch and name it MPU6050Graph2.ino. The data changes as you move your module around. To smooth the data you should insert a moving average on each of the 6 variables. Experiment with the length of moving average that works best to give both smoothness and control. Acceleration and gyro rate may need different length moving averages to optimis the smoothness and responsiveness.

Now you should add you spinning cube code from last week to a new sketch called MPU6050Cube.ino. Consider how you can use the data to control the cube's motion and/or position/size. Find a reasonable solution, and indicate how you used the data in your sketch's comments. Keep in mind that the gyro data is rotation rate, not an absolute heading.

Demonstrate your running sketch to the TA.

Question 3: What factors affect the smoothness of your spinning cube sketch when using the raw data from the MPU-6050? Is averaging the data any help?

Part 3: Quaternions

To begin this part of the lab, load and run the MPU6050IMU.ino sketch. Be sure to open the debug window immediately.
This code uses an IMU algorithm by Sebastian Madgwick (paper)
http://www.x-io.co.uk/open-source-imu-and-ahrs-algorithms/ (site may not be working, bandwidth exceeded)
which fuses acceleration and rotation rate to produce a quaternion-based estimate of relative device orientation -- which can be converted to yaw, pitch, and roll:
yaw

Prepare a sketch named MPU6050IMUgraph.ino, adding your bar-graphs. Experiment to see if you need any moving average smoothing to get stable headings.

Now make a sketch called MPU6050IMUcube.ino and control the pitch, yaw, and roll of your cube. Print the pitch, yaw and roll variables at the bottom of your screen.

Question 4: What is the performance cost of the Quaternion-based approach in part 3 over using raw data in part 2? Explain your measurement procedure.

Optional Part 4, Extra Credit: use your phone instead

To begin this part of the lab, load controller.ino from lab 3, and connect to it with your phone and the Blufeuit LE app. See the documentation here:
https://learn.adafruit.com/introducing-the-adafruit-bluefruit-spi-breakout/controller

  1. Connect your phone, and note that you can display quaternion data from the phone in the debug window.
  2. Now make a new sketch based on your spinning cube sketch to receive the bluetooth data from the phone to control the cube's spin.
  3. Demonstrate your result to a TA.

Lab 4 Deliverables