Bluepill version of mpu9250-dmp library of sparkfun

Dependencies:   MotionDriver_6_1 SparkFunMPU9250-DMP mbed-STM32F103C8T6 mbed

Fork of MPU9250-dmp by Oğuz Özdemir

Committer:
mbedoguz
Date:
Mon Aug 14 09:03:52 2017 +0000
Revision:
8:b9206956f396
Parent:
7:a862072e0981
Child:
9:ef46e12b4560
Data rate is increased, simultenious delays are fixed.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbedoguz 0:c6daf7016b8c 1 #include "SparkFunMPU9250-DMP.h"
mbedoguz 2:7be1ac6eb8cb 2 #include "mdcompat.h"
mbedoguz 8:b9206956f396 3
mbedoguz 8:b9206956f396 4 Serial pc(USBTX,USBRX);
mbedoguz 0:c6daf7016b8c 5 DigitalOut led(LED1);
mbedoguz 0:c6daf7016b8c 6 MPU9250_DMP imu;
mbedoguz 4:9d706f783b5a 7 unsigned char whoami[1]={0};
mbedoguz 4:9d706f783b5a 8 unsigned char fifo_count[2]={32,32};
mbedoguz 4:9d706f783b5a 9 unsigned char temp[1]={32};
mbedoguz 4:9d706f783b5a 10 unsigned char outbuff[4];
mbedoguz 5:3a9280cea2ff 11 unsigned char inbuff[4];
mbedoguz 4:9d706f783b5a 12 unsigned char regadd;
mbedoguz 5:3a9280cea2ff 13 char registeradress[5];
mbedoguz 4:9d706f783b5a 14 unsigned char registerdata[]={33};
mbedoguz 4:9d706f783b5a 15 void printIMUData(void);
mbedoguz 0:c6daf7016b8c 16 int main()
mbedoguz 0:c6daf7016b8c 17 {
mbedoguz 0:c6daf7016b8c 18 pc.baud(115200);
mbedoguz 0:c6daf7016b8c 19 pc.printf("Hello World\n");
mbedoguz 4:9d706f783b5a 20 imu_init();
mbedoguz 4:9d706f783b5a 21 stamper_init();
mbedoguz 5:3a9280cea2ff 22 #if 1//Regular program
mbedoguz 0:c6daf7016b8c 23 // Call imu.begin() to verify communication and initialize
mbedoguz 0:c6daf7016b8c 24 if (imu.begin() != INV_SUCCESS){
mbedoguz 0:c6daf7016b8c 25 while (1){
mbedoguz 0:c6daf7016b8c 26 pc.printf("Unable to communicate with MPU-9250");
mbedoguz 0:c6daf7016b8c 27 pc.printf("Check connections, and try again.\n");
mbedoguz 0:c6daf7016b8c 28 wait_ms(5000);
mbedoguz 0:c6daf7016b8c 29 }
mbedoguz 0:c6daf7016b8c 30 }
mbedoguz 5:3a9280cea2ff 31 pc.printf("imu.begin() suceeded\n");
mbedoguz 4:9d706f783b5a 32
mbedoguz 4:9d706f783b5a 33 if(imu.dmpBegin(DMP_FEATURE_6X_LP_QUAT | // Enable 6-axis quat
mbedoguz 0:c6daf7016b8c 34 DMP_FEATURE_GYRO_CAL, // Use gyro calibration
mbedoguz 8:b9206956f396 35 150)==INV_ERROR){ // Set DMP FIFO rate to 150 Hz
mbedoguz 4:9d706f783b5a 36 pc.printf("imu.dmpBegin have failed\n");//dmpLoad function under it fails which is caused by memcmp(firmware+ii, cur, this_write) (it is located at row 2871 of inv_mpu.c)
mbedoguz 4:9d706f783b5a 37 }
mbedoguz 4:9d706f783b5a 38 else{
mbedoguz 4:9d706f783b5a 39 pc.printf("imu.dmpBegin() suceeded\n");
mbedoguz 4:9d706f783b5a 40 }
mbedoguz 8:b9206956f396 41 pc.printf("$GETEU,*\n");
mbedoguz 4:9d706f783b5a 42 #endif
mbedoguz 5:3a9280cea2ff 43 #if 0//this scope is only for debugging purposes of mbed_i2c_read and write functions
mbedoguz 4:9d706f783b5a 44 mbed_i2c_read(0x68,0x75,1,whoami);
mbedoguz 4:9d706f783b5a 45 pc.printf("whoami=%d\n",whoami[0]);
mbedoguz 5:3a9280cea2ff 46 mbed_i2c_read(0x68,0x23,2,inbuff);
mbedoguz 5:3a9280cea2ff 47 pc.printf("Buffer=%d-%d\n",inbuff[0],inbuff[1]);
mbedoguz 5:3a9280cea2ff 48 outbuff[0]=inbuff[0]&0x01;
mbedoguz 5:3a9280cea2ff 49 outbuff[1]=inbuff[1]&0x02;
mbedoguz 5:3a9280cea2ff 50 pc.printf("updated buffer=%d-%d\n",outbuff[0],outbuff[1]);
mbedoguz 4:9d706f783b5a 51 mbed_i2c_write(0x68,0x23,2,outbuff);
mbedoguz 5:3a9280cea2ff 52 mbed_i2c_read(0x68,0x23,2,inbuff);
mbedoguz 5:3a9280cea2ff 53 pc.printf("new out buffer=%d-%d(if zero, write function still needs to be fixed)\n",inbuff[0],inbuff[1]);
mbedoguz 4:9d706f783b5a 54 #endif
mbedoguz 0:c6daf7016b8c 55 while(1){
mbedoguz 4:9d706f783b5a 56 if(pc.readable()){
mbedoguz 4:9d706f783b5a 57 pc.scanf("%s",&registeradress);
mbedoguz 4:9d706f783b5a 58 regadd=(registeradress[0]-48)*100+(registeradress[1]-48)*10+(registeradress[2]-48);
mbedoguz 4:9d706f783b5a 59 mbed_i2c_read(0x68,(unsigned)regadd,1,registerdata);
mbedoguz 4:9d706f783b5a 60 pc.printf("%d is gotten from serial port, data at that register is %d\n",regadd,registerdata[0]);
mbedoguz 4:9d706f783b5a 61 }
mbedoguz 5:3a9280cea2ff 62 /*if(imu.dmpBegin(DMP_FEATURE_6X_LP_QUAT | // Enable 6-axis quat
mbedoguz 5:3a9280cea2ff 63 DMP_FEATURE_GYRO_CAL, // Use gyro calibration
mbedoguz 5:3a9280cea2ff 64 20)==INV_ERROR){ // Set DMP FIFO rate to 20 Hz
mbedoguz 5:3a9280cea2ff 65 pc.printf("imu.dmpBegin have failed\n");//dmpLoad function under it fails which is caused by memcmp(firmware+ii, cur, this_write) (it is located at row 2871 of inv_mpu.c)
mbedoguz 5:3a9280cea2ff 66 }
mbedoguz 5:3a9280cea2ff 67 else{
mbedoguz 5:3a9280cea2ff 68 pc.printf("imu.dmpBegin() suceeded\n");
mbedoguz 5:3a9280cea2ff 69 }*/
mbedoguz 0:c6daf7016b8c 70 // Check for new data in the FIFO
mbedoguz 3:740f15b45cb8 71 if (imu.fifoAvailable()){//fifo is not being available
mbedoguz 8:b9206956f396 72 led=0;
mbedoguz 8:b9206956f396 73 wait_ms(1);
mbedoguz 8:b9206956f396 74 led=1;
mbedoguz 8:b9206956f396 75 wait_ms(1);
mbedoguz 0:c6daf7016b8c 76 // Use dmpUpdateFifo to update the ax, gx, mx, etc. values
mbedoguz 5:3a9280cea2ff 77 if ( imu.dmpUpdateFifo() == INV_SUCCESS){
mbedoguz 0:c6daf7016b8c 78 // computeEulerAngles can be used -- after updating the
mbedoguz 0:c6daf7016b8c 79 // quaternion values -- to estimate roll, pitch, and yaw
mbedoguz 0:c6daf7016b8c 80 imu.computeEulerAngles();
mbedoguz 5:3a9280cea2ff 81 printIMUData();
mbedoguz 0:c6daf7016b8c 82 }
mbedoguz 0:c6daf7016b8c 83 }
mbedoguz 4:9d706f783b5a 84 else{
mbedoguz 8:b9206956f396 85 led=0;
mbedoguz 4:9d706f783b5a 86 }
mbedoguz 0:c6daf7016b8c 87 }
mbedoguz 0:c6daf7016b8c 88 return 0;
mbedoguz 0:c6daf7016b8c 89 }
mbedoguz 0:c6daf7016b8c 90
mbedoguz 0:c6daf7016b8c 91 void printIMUData(void)
mbedoguz 0:c6daf7016b8c 92 {
mbedoguz 0:c6daf7016b8c 93 // After calling dmpUpdateFifo() the ax, gx, mx, etc. values
mbedoguz 0:c6daf7016b8c 94 // are all updated.
mbedoguz 0:c6daf7016b8c 95 // Quaternion values are, by default, stored in Q30 long
mbedoguz 0:c6daf7016b8c 96 // format. calcQuat turns them into a float between -1 and 1
mbedoguz 0:c6daf7016b8c 97 float q0 = imu.calcQuat(imu.qw);
mbedoguz 0:c6daf7016b8c 98 float q1 = imu.calcQuat(imu.qx);
mbedoguz 0:c6daf7016b8c 99 float q2 = imu.calcQuat(imu.qy);
mbedoguz 0:c6daf7016b8c 100 float q3 = imu.calcQuat(imu.qz);
mbedoguz 0:c6daf7016b8c 101
mbedoguz 8:b9206956f396 102 pc.printf("$,%.4lf,%.4lf,%.4lf,*\n", imu.yaw,imu.pitch,imu.roll);
mbedoguz 0:c6daf7016b8c 103 }