-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathMPU9250_DMP_Quaternion.ino
93 lines (80 loc) · 2.73 KB
/
MPU9250_DMP_Quaternion.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/************************************************************
MPU9250_DMP_Quaternion
Quaternion example for MPU-9250 DMP Arduino Library
Jim Lindblom @ SparkFun Electronics
original creation date: November 23, 2016
https://github.com/sparkfun/SparkFun_MPU9250_DMP_Arduino_Library
The MPU-9250's digital motion processor (DMP) can calculate
four unit quaternions, which can be used to represent the
rotation of an object.
This exmaple demonstrates how to configure the DMP to
calculate quaternions, and prints them out to the serial
monitor. It also calculates pitch, roll, and yaw from those
values.
Development environment specifics:
Arduino IDE 1.6.12
SparkFun 9DoF Razor IMU M0
Supported Platforms:
- ATSAMD21 (Arduino Zero, SparkFun SAMD21 Breakouts)
*************************************************************/
#include <SparkFunMPU9250-DMP.h>
#ifdef defined(SAMD)
#define SerialPort SerialUSB
#else
#define SerialPort Serial
#endif
MPU9250_DMP imu;
void setup()
{
SerialPort.begin(115200);
// Call imu.begin() to verify communication and initialize
if (imu.begin() != INV_SUCCESS)
{
while (1)
{
SerialPort.println("Unable to communicate with MPU-9250");
SerialPort.println("Check connections, and try again.");
SerialPort.println();
delay(5000);
}
}
imu.dmpBegin(DMP_FEATURE_6X_LP_QUAT | // Enable 6-axis quat
DMP_FEATURE_GYRO_CAL, // Use gyro calibration
10); // Set DMP FIFO rate to 10 Hz
// DMP_FEATURE_LP_QUAT can also be used. It uses the
// accelerometer in low-power mode to estimate quat's.
// DMP_FEATURE_LP_QUAT and 6X_LP_QUAT are mutually exclusive
}
void loop()
{
// Check for new data in the FIFO
if ( imu.fifoAvailable() )
{
// Use dmpUpdateFifo to update the ax, gx, mx, etc. values
if ( imu.dmpUpdateFifo() == INV_SUCCESS)
{
// computeEulerAngles can be used -- after updating the
// quaternion values -- to estimate roll, pitch, and yaw
imu.computeEulerAngles();
printIMUData();
}
}
}
void printIMUData(void)
{
// After calling dmpUpdateFifo() the ax, gx, mx, etc. values
// are all updated.
// Quaternion values are, by default, stored in Q30 long
// format. calcQuat turns them into a float between -1 and 1
float q0 = imu.calcQuat(imu.qw);
float q1 = imu.calcQuat(imu.qx);
float q2 = imu.calcQuat(imu.qy);
float q3 = imu.calcQuat(imu.qz);
SerialPort.println("Q: " + String(q0, 4) + ", " +
String(q1, 4) + ", " + String(q2, 4) +
", " + String(q3, 4));
SerialPort.println("R/P/Y: " + String(imu.roll) + ", "
+ String(imu.pitch) + ", " + String(imu.yaw));
SerialPort.println("Time: " + String(imu.time) + " ms");
SerialPort.println();
}