#include <otMath.h>
#include <otSerial.h>
#include <otTimer.h>
#include <otMalloc.h>
#include <otPrintf.h>
#include <otI2cBus.h>
#include <otMPU9250.h>
U32 _ticker = 0;
otMPU9250 imu;
FAST_CODE_2 void _timer1CallBack()
{
_ticker++;
}
void imuCalibrate()
{
F32 SelfTest[6];
imu.reset();
printf("IMU Self test started ...\n");
imu.selfTest(SelfTest);
printf("Acceleration trim within = X:%6.1f, Y:%6.1f, Z:%6.1f %% of factory value\n", SelfTest[0], SelfTest[1], SelfTest[2]);
printf("Gyroscope trim within = X:%6.1f, Y:%6.1f, Z:%6.1f %% of factory value\n", SelfTest[3], SelfTest[4], SelfTest[5]);
printf("Calibrating gyroscope and accelerometer ...\n");
imu.accelGyroCalibration();
printf("Accelerometer biases (mg) = X:%6.1f, Y:%6.1f, Z:%6.1f\n", 1000. * imu.d->accelBias[0], 1000. * imu.d->accelBias[1], 1000. * imu.d->accelBias[2]);
printf("Gyroscope biases (dps)= X:%6.1f, Y:%6.1f, Z:%6.1f\n", imu.d->gyroBias[0], imu.d->gyroBias[1], imu.d->gyroBias[2]);
imu.setRange(otMPU9250::AFS_2G, otMPU9250::GFS_250DPS, otMPU9250::MFS_16BITS);
imu.imuInit();
if(imu.magnetometerCheck())
{
imu.magnetometerInit();
printf("Magnetometer initialized for active data mode ...\n");
imu.magnetometerCalibration();
printf("Magnetometer biases (mG) = X:%6.1f, Y:%6.1f, Z:%6.1f\n", imu.d->magBias[0], imu.d->magBias[1], imu.d->magBias[2]);
printf("Magnetometer scale (mG) = X:%6.1f, Y:%6.1f, Z:%6.1f\n", imu.d->magScale[0], imu.d->magScale[1], imu.d->magScale[2]);
}
}
void setup()
{
UartEnableSignals(UART0);
UartSetBaud(UART0, 115200);
TimerSetInterruptService(TIMER1, _timer1CallBack, TimerRateMs(1));
TimerEnable(TIMER1, true);
_initMalloc();
setStdout(4096, UART0);
I2cBusReset();
I2cBusFrequency(100, 40);
DelayMs(1000);
imu.init();
if(imu.check())
imuCalibrate();
else
{
printf("Can't find MPU9250\n");
IDLE;
}
}
void loop()
{
bool dump = false;
U32 st, et, loop = 0;
F32 deltat;
F32 ax, ay, az, gx, gy, gz, mx, my, mz;
F32 yaw, pitch, roll;
st = _ticker;
while(1)
{
if(imu.ready())
{
imu.accelerometerGyroscope(ax, ay, az, gx, gy, gz);
imu.magnetometer(mx, my, mz);
et = _ticker;
deltat = ((float) (et - st)) / 1000.f;
st = _ticker;
if(dump) printf("[%6u] Integration time (s) = t:%6.3f\n", _ticker, deltat);
imu.madgwickQuaternionUpdate(-ax, ay, az, gx * M_PI / 180.0f, -gy * M_PI / 180.0f, -gz * M_PI / 180.0f, my, -mx, mz, deltat);
if(dump) printf("[%6u] Accelerometer (mg) = X:%7.2f, Y:%7.2f, Z:%7.2f\n", _ticker, 1000.f * ax, 1000.f * ay, 1000.f * az);
if(dump) printf("[%6u] Gyroscope (deg/s) = X:%7.2f, Y:%7.2f, Z:%7.2f\n", _ticker, gx, gy, gz);
if(dump) printf("[%6u] Magnetometer (mG) = X:%7.2f, Y:%7.2f, Z:%7.2f\n", _ticker, mx, my, mz);
if(dump) printf("[%6u] Quaternion = 0: %5.3f, X: %5.3f, Y: %5.3f, Z: %5.3f\n", _ticker, imu.d->q[0], imu.d->q[1], imu.d->q[2], imu.d->q[3]);
if(dump) printf("[%6u] Temperature = C: %6.1f\n", _ticker, (float) imu.temperature());
imu.taitBryanAngles(yaw, pitch, roll);
if(dump)
{
printf("[%6u] Euler angle = Y: %5d, P: %5d, R: %5d\n", _ticker, (int) yaw, (int) pitch, (int) roll);
dump = false;
}
loop++;
}
if((loop % 100) == 0) dump = true;
}
}