Chapter 25 Attitude Sensor MPU6050
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
printf(accelgyro.testConnection() ? "MPU6050 connection successful\n" : "MPU6050
connection failed\n");
}
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, & az, &gx, & gy, & gz);
// display accel/gyro x/y/z values
printf("a/g: %6hd %6hd %6hd %6hd %6hd %6hd\n",ax,ay,az,gx,gy,gz);
printf("a/g: %.2f g %.2f g %.2f g %.2f d/s %.2f d/s %.2f d/s
\n",(float)ax/16384,(float)ay/16384,(float)az/16384,
(float)gx/131,(float)gy/131,(float)gz/131);
}
int main()
{
setup();
w hile(1){
loop();
}
r eturn 0;
}
Two library files "MPU6050.h" and "I2Cdev.h" are used in the code. They will be compiled as others. Class
MPU6050 is used to operate the MPU6050. When used, first instantiate an object.
In the setup function, the MPU6050 is initialized and the result of the initialization will be judged.
void setup() {
// initialize device
printf("Initializing I2C devices...\n");
accelgyro.initialize(); //initialize MPU6050
// verify connection
printf("Testing device connections...\n");
printf(accelgyro.testConnection() ? "MPU6050 connection successful\n" : "MPU6050
connection failed\n");
}
In the loop function, read the original data of MPU6050 and print them out, and then convert the original
data into the corresponding acceleration and angular velocity, then print the converted data out.
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, & az, &gx, & gy, & gz);
// display accel/gyro x/y/z values
printf("a/g: %6hd %6hd %6hd %6hd %6hd %6hd\n",ax,ay,az,gx,gy,gz);