Arduino library for MPU9250 Nine-Axis (Gyro + Accelerometer + Compass) MEMS MotionTracking™ Device
This library is based on the great work by kriswiner, and re-writen for the simple usage.
#include "MPU9250.h"
MPU9250 mpu;
void setup()
{
Serial.begin(115200);
Wire.begin();
delay(2000);
mpu.setup();
}
void loop()
{
static uint32_t prev_ms = millis();
if ((millis() - prev_ms) > 16)
{
mpu.update();
mpu.print();
Serial.print("roll (x-forward (north)) : ");
Serial.println(mpu.getRoll());
Serial.print("pitch (y-right (east)) : ");
Serial.println(mpu.getPitch());
Serial.print("yaw (z-down (down)) : ");
Serial.println(mpu.getYaw());
prev_ms = millis();
}
}
- device should be stay still during accel/gyro calibration
- round device around during mag calibration
#include "MPU9250.h"
MPU9250 mpu;
void setup()
{
Serial.begin(115200);
Wire.begin();
delay(2000);
mpu.setup();
delay(5000);
// calibrate anytime you want to
mpu.calibrateAccelGyro();
mpu.calibrateMag();
mpu.printCalibration();
}
void loop()
{
}
Magnetic declination should be set depending on where you are to get accurate data. To set it, use this method.
mpu.setMagneticDeclination(value);
You can find magnetic declination in your city here.
For more details, see wiki.
enum class AFS { A2G, A4G, A8G, A16G };
enum class GFS { G250DPS, G500DPS, G1000DPS, G2000DPS };
enum class MFS { M14BITS, M16BITS }; // 0.6mG, 0.15mG per LSB
MPU9250 class is defined as follows.
template <
typename WireType,
AFS AFSSEL = AFS::A16G,
GFS GFSSEL = GFS::G2000DPS,
MFS MFSSEL = MFS::M16BITS
>
class MPU9250_;
So, please use like
class MPU9250_<TwoWire, AFS::A4G, GFS::G500DPS, MFS::M14BITS> mpu; // most of Arduino
class MPU9250_<i2c_t3, AFS::A4G, GFS::G500DPS, MFS::M14BITS> mpu; // Teensy
MIT