Andrew Bates
/
pdiot-MPU9250-test
Test program for MPU9250 sensor
main.cpp@1:e542337d8586, 2017-09-27 (annotated)
- Committer:
- andrewbates11
- Date:
- Wed Sep 27 17:52:29 2017 +0000
- Revision:
- 1:e542337d8586
- Parent:
- 0:6ffb1296d690
update readme
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewbates11 | 0:6ffb1296d690 | 1 | #include "mbed.h" |
andrewbates11 | 0:6ffb1296d690 | 2 | #include "MPU9250.h" |
andrewbates11 | 0:6ffb1296d690 | 3 | |
andrewbates11 | 0:6ffb1296d690 | 4 | // Serial comms |
andrewbates11 | 0:6ffb1296d690 | 5 | Serial pc(USBTX, USBRX); |
andrewbates11 | 0:6ffb1296d690 | 6 | |
andrewbates11 | 0:6ffb1296d690 | 7 | // Sensor board library |
andrewbates11 | 0:6ffb1296d690 | 8 | MPU9250 mpu = MPU9250(p26, p27); |
andrewbates11 | 0:6ffb1296d690 | 9 | |
andrewbates11 | 0:6ffb1296d690 | 10 | |
andrewbates11 | 0:6ffb1296d690 | 11 | // Configuration |
andrewbates11 | 0:6ffb1296d690 | 12 | bool test_comms = true; |
andrewbates11 | 0:6ffb1296d690 | 13 | bool do_sensor_init = false; |
andrewbates11 | 0:6ffb1296d690 | 14 | bool do_sensor_self_test = true; |
andrewbates11 | 0:6ffb1296d690 | 15 | bool print_accel = true; |
andrewbates11 | 0:6ffb1296d690 | 16 | bool print_gyro = true; |
andrewbates11 | 0:6ffb1296d690 | 17 | |
andrewbates11 | 0:6ffb1296d690 | 18 | int main() { |
andrewbates11 | 0:6ffb1296d690 | 19 | float test_result[6] = {0.0,0.0,0.0,0.0,0.0,0.0}; |
andrewbates11 | 0:6ffb1296d690 | 20 | int16_t accel[3] = {0,0,0}; |
andrewbates11 | 0:6ffb1296d690 | 21 | int16_t gyro[3] = {0,0,0}; |
andrewbates11 | 0:6ffb1296d690 | 22 | int16_t temp = 0; |
andrewbates11 | 0:6ffb1296d690 | 23 | |
andrewbates11 | 0:6ffb1296d690 | 24 | // Test MPU connection |
andrewbates11 | 0:6ffb1296d690 | 25 | if (test_comms) { |
andrewbates11 | 0:6ffb1296d690 | 26 | uint8_t whoami = mpu.readByte(MPU9250_ADDRESS, WHO_AM_I_MPU9250); // Read WHO_AM_I register for MPU-9250 |
andrewbates11 | 0:6ffb1296d690 | 27 | pc.printf("I AM 0x%x\n\r", whoami); pc.printf("I SHOULD BE 0x71\n\r"); |
andrewbates11 | 0:6ffb1296d690 | 28 | wait(1); |
andrewbates11 | 0:6ffb1296d690 | 29 | } |
andrewbates11 | 0:6ffb1296d690 | 30 | |
andrewbates11 | 0:6ffb1296d690 | 31 | // Initialise sensor board |
andrewbates11 | 0:6ffb1296d690 | 32 | if (do_sensor_init) { |
andrewbates11 | 0:6ffb1296d690 | 33 | pc.printf("starting Init\n\r"); |
andrewbates11 | 0:6ffb1296d690 | 34 | mpu.initMPU9250(); |
andrewbates11 | 0:6ffb1296d690 | 35 | pc.printf("returned from init\n\r"); |
andrewbates11 | 0:6ffb1296d690 | 36 | wait(1); |
andrewbates11 | 0:6ffb1296d690 | 37 | } |
andrewbates11 | 0:6ffb1296d690 | 38 | |
andrewbates11 | 0:6ffb1296d690 | 39 | // Run sensor board self-test |
andrewbates11 | 0:6ffb1296d690 | 40 | if (do_sensor_self_test) { |
andrewbates11 | 0:6ffb1296d690 | 41 | mpu.MPU9250SelfTest(test_result); |
andrewbates11 | 0:6ffb1296d690 | 42 | for (int i = 0; i<6; i++) |
andrewbates11 | 0:6ffb1296d690 | 43 | pc.printf("Self test result %i: %f\n", i, test_result[i]); |
andrewbates11 | 0:6ffb1296d690 | 44 | |
andrewbates11 | 0:6ffb1296d690 | 45 | wait(1); |
andrewbates11 | 0:6ffb1296d690 | 46 | } |
andrewbates11 | 0:6ffb1296d690 | 47 | |
andrewbates11 | 0:6ffb1296d690 | 48 | // Stream sensor values (accel or gyro) |
andrewbates11 | 0:6ffb1296d690 | 49 | while(1) { |
andrewbates11 | 0:6ffb1296d690 | 50 | if (print_accel) { |
andrewbates11 | 0:6ffb1296d690 | 51 | mpu.readAccelData(accel); |
andrewbates11 | 0:6ffb1296d690 | 52 | float ax = accel[0] * 2.0 / 32768.0; |
andrewbates11 | 0:6ffb1296d690 | 53 | float ay = accel[1] * 2.0 / 32768.0; |
andrewbates11 | 0:6ffb1296d690 | 54 | float az = accel[2] * 2.0 / 32768.0; |
andrewbates11 | 0:6ffb1296d690 | 55 | pc.printf("accel: (%f, %f, %f)\n", ax,ay,az); |
andrewbates11 | 0:6ffb1296d690 | 56 | } |
andrewbates11 | 0:6ffb1296d690 | 57 | |
andrewbates11 | 0:6ffb1296d690 | 58 | if (print_gyro) { |
andrewbates11 | 0:6ffb1296d690 | 59 | mpu.readGyroData(gyro); |
andrewbates11 | 0:6ffb1296d690 | 60 | float gx = gyro[0] * 250.0 / 32768.0; |
andrewbates11 | 0:6ffb1296d690 | 61 | float gy = gyro[1] * 250.0 / 32768.0; |
andrewbates11 | 0:6ffb1296d690 | 62 | float gz = gyro[2] * 250.0 / 32768.0; |
andrewbates11 | 0:6ffb1296d690 | 63 | pc.printf("gyro: (%f, %f, %f)\n", gx,gy,gz); |
andrewbates11 | 0:6ffb1296d690 | 64 | } |
andrewbates11 | 0:6ffb1296d690 | 65 | wait(0.1); |
andrewbates11 | 0:6ffb1296d690 | 66 | } |
andrewbates11 | 0:6ffb1296d690 | 67 | } |