Base 6050

Dependencies:   HMC5883L MPU6050 ledControl2 mbed

Fork of IMU_fusion by Baser Kandehir

Committer:
kong4580
Date:
Mon Nov 05 10:55:41 2018 +0000
Revision:
3:4bd8dff84324
Parent:
2:3881894aaff5
Base

Who changed what in which revision?

UserRevisionLine numberNew contents of line
BaserK 0:d8d055e8f830 1 /* Calculating Roll, Pitch and Yaw angles from IMU
BaserK 0:d8d055e8f830 2 *
BaserK 0:d8d055e8f830 3 * @author: Baser Kandehir
BaserK 0:d8d055e8f830 4 * @date: August 5, 2015
BaserK 0:d8d055e8f830 5 * @license: MIT license
BaserK 0:d8d055e8f830 6 *
BaserK 0:d8d055e8f830 7 * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr
BaserK 0:d8d055e8f830 8 *
BaserK 0:d8d055e8f830 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
BaserK 0:d8d055e8f830 10 * of this software and associated documentation files (the "Software"), to deal
BaserK 0:d8d055e8f830 11 * in the Software without restriction, including without limitation the rights
BaserK 0:d8d055e8f830 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
BaserK 0:d8d055e8f830 13 * copies of the Software, and to permit persons to whom the Software is
BaserK 0:d8d055e8f830 14 * furnished to do so, subject to the following conditions:
BaserK 0:d8d055e8f830 15 *
BaserK 0:d8d055e8f830 16 * The above copyright notice and this permission notice shall be included in
BaserK 0:d8d055e8f830 17 * all copies or substantial portions of the Software.
BaserK 0:d8d055e8f830 18 *
BaserK 0:d8d055e8f830 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
BaserK 0:d8d055e8f830 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
BaserK 0:d8d055e8f830 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
BaserK 0:d8d055e8f830 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
BaserK 0:d8d055e8f830 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
BaserK 0:d8d055e8f830 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
BaserK 0:d8d055e8f830 25 * THE SOFTWARE.
BaserK 0:d8d055e8f830 26 *
BaserK 0:d8d055e8f830 27 * @description of the program:
BaserK 0:d8d055e8f830 28 *
BaserK 0:d8d055e8f830 29 * Program can calculate roll, pitch and yaw angles from the raw data that comes
BaserK 0:d8d055e8f830 30 * from IMU. Yaw angle is compensated for tilt. All the angles are sent to the matlab
BaserK 2:3881894aaff5 31 * for further processing.
BaserK 0:d8d055e8f830 32 *
BaserK 0:d8d055e8f830 33 */
BaserK 0:d8d055e8f830 34
BaserK 0:d8d055e8f830 35 #include "mbed.h"
BaserK 0:d8d055e8f830 36 #include "HMC5883L.h"
BaserK 0:d8d055e8f830 37 #include "MPU6050.h"
BaserK 0:d8d055e8f830 38 #include "ledControl.h"
BaserK 0:d8d055e8f830 39
BaserK 0:d8d055e8f830 40 Serial pc(USBTX,USBRX);
BaserK 0:d8d055e8f830 41 MPU6050 mpu6050;
BaserK 0:d8d055e8f830 42 HMC5883L hmc5883l;
BaserK 0:d8d055e8f830 43 Ticker toggler1;
BaserK 0:d8d055e8f830 44 Ticker filter;
BaserK 0:d8d055e8f830 45 Ticker compass;
kong4580 3:4bd8dff84324 46 Timer tt;
BaserK 0:d8d055e8f830 47 void toggle_led1();
BaserK 0:d8d055e8f830 48 void toggle_led2();
BaserK 0:d8d055e8f830 49 void compFilter();
BaserK 0:d8d055e8f830 50 void tiltCompensatedAngle();
BaserK 0:d8d055e8f830 51
BaserK 0:d8d055e8f830 52 float pitchAngle = 0;
BaserK 0:d8d055e8f830 53 float rollAngle = 0;
BaserK 0:d8d055e8f830 54 float yawAngle = 0;
kong4580 3:4bd8dff84324 55 float ve[3] ={0,0,0};
kong4580 3:4bd8dff84324 56 float n_a[3]={0,0,0};
kong4580 3:4bd8dff84324 57 static float s_b[3]={0,0,0};
kong4580 3:4bd8dff84324 58 void AcceltoVelo(float aax,float aay,float aaz,float delttat)
kong4580 3:4bd8dff84324 59 { int cc=0;
kong4580 3:4bd8dff84324 60 static float old_a[3]={0,0,0};
kong4580 3:4bd8dff84324 61 float deax,deay,deaz;
kong4580 3:4bd8dff84324 62
kong4580 3:4bd8dff84324 63 deax=aax-old_a[0];
kong4580 3:4bd8dff84324 64 deay=aay-old_a[1];
kong4580 3:4bd8dff84324 65 deaz=aaz-old_a[2];
kong4580 3:4bd8dff84324 66 ve[0]=deax*delttat;
kong4580 3:4bd8dff84324 67 ve[1]=deay*delttat;
kong4580 3:4bd8dff84324 68 ve[2]=deaz*delttat;
kong4580 3:4bd8dff84324 69 old_a[0]=aax;
kong4580 3:4bd8dff84324 70 old_a[1]=aay;
kong4580 3:4bd8dff84324 71 old_a[2]=aaz;
kong4580 3:4bd8dff84324 72 //pc.printf("%f %f %f\n",deax,deay,deaz);
kong4580 3:4bd8dff84324 73 }
kong4580 3:4bd8dff84324 74 void VelotoDis(float vvx,float vvy,float vvz,float delttat){
kong4580 3:4bd8dff84324 75 s_b[0]+=vvx*delttat;
kong4580 3:4bd8dff84324 76 s_b[1]+=vvy*delttat;
kong4580 3:4bd8dff84324 77 s_b[2]+=vvz*delttat;
kong4580 3:4bd8dff84324 78 }
kong4580 3:4bd8dff84324 79 void cali(){
kong4580 3:4bd8dff84324 80 int c=0;
kong4580 3:4bd8dff84324 81 float c_ax=0,c_ay=0,c_az=0;
kong4580 3:4bd8dff84324 82 while (c<1000){
kong4580 3:4bd8dff84324 83 c_ax+=ax;
kong4580 3:4bd8dff84324 84 c_ay+=ay;
kong4580 3:4bd8dff84324 85 c_az+=az;
kong4580 3:4bd8dff84324 86 c+=1;
kong4580 3:4bd8dff84324 87 wait(0.0001);
kong4580 3:4bd8dff84324 88 }
kong4580 3:4bd8dff84324 89 pc.printf("%f %f %f\n",c_ax/1000,c_ay/1000,c_az/1000);
kong4580 3:4bd8dff84324 90 }
BaserK 0:d8d055e8f830 91 int main()
kong4580 3:4bd8dff84324 92
kong4580 3:4bd8dff84324 93 {static float vvv[3]={0,0,0};
kong4580 3:4bd8dff84324 94 tt.start();
kong4580 3:4bd8dff84324 95 pc.baud(115200); // baud rate: 9600
BaserK 0:d8d055e8f830 96 mpu6050.whoAmI(); // Communication test: WHO_AM_I register reading
BaserK 0:d8d055e8f830 97 mpu6050.calibrate(accelBias,gyroBias); // Calibrate MPU6050 and load biases into bias registers
BaserK 0:d8d055e8f830 98 mpu6050.init(); // Initialize the sensor
BaserK 0:d8d055e8f830 99 hmc5883l.init();
BaserK 0:d8d055e8f830 100 filter.attach(&compFilter, 0.005); // Call the complementaryFilter func. every 5 ms (200 Hz sampling period)
BaserK 0:d8d055e8f830 101 compass.attach(&tiltCompensatedAngle, 0.015); // Call the tiltCompensatedAngle func. every 15 ms (75 Hz sampling period)
kong4580 3:4bd8dff84324 102 //cali();
kong4580 3:4bd8dff84324 103 float cc0=0;
kong4580 3:4bd8dff84324 104 int dd=0;
BaserK 0:d8d055e8f830 105 while(1)
kong4580 3:4bd8dff84324 106 {
kong4580 3:4bd8dff84324 107 AcceltoVelo(ax*10,ay*10,az*10,3);
kong4580 3:4bd8dff84324 108 vvv[0]=ve[0]*0.02+vvv[0]*0.98;
kong4580 3:4bd8dff84324 109 vvv[1]=ve[1]*0.02+vvv[1]*0.98;
kong4580 3:4bd8dff84324 110 vvv[2]=ve[2]*0.02+vvv[2]*0.98;
kong4580 3:4bd8dff84324 111 //wait_ms(4);
kong4580 3:4bd8dff84324 112 //pc.printf("%f\n",vvv[2]);
kong4580 3:4bd8dff84324 113 //pc.printf("%f %f %f\r\n",vvv[0],vvv[1],vvv[2]);
kong4580 3:4bd8dff84324 114 /*f(tt.read_us()-cc0>3000 && dd==0){
kong4580 3:4bd8dff84324 115 dd=1;
kong4580 3:4bd8dff84324 116 VelotoDis(vvv[0],vvv[1],vvv[2],6);
kong4580 3:4bd8dff84324 117 pc.printf("%f %f %f\r\n",s_b[0],s_b[1],s_b[2]);cc0=tt.read_us();
kong4580 3:4bd8dff84324 118 }else{
kong4580 3:4bd8dff84324 119 VelotoDis(vvv[0],vvv[1],vvv[2],6);
kong4580 3:4bd8dff84324 120 pc.printf("%f %f %f\r\n",s_b[0],s_b[1],s_b[2]);cc0=tt.read_us();
kong4580 3:4bd8dff84324 121 }*/
kong4580 3:4bd8dff84324 122 pc.printf("%f %f %f\r\n",ax,ay,az);
kong4580 3:4bd8dff84324 123 //pc.printf("%.1f,%.1f,%.1f\r\n",rollAngle,pitchAngle,yawAngle); // send data to matlab
kong4580 3:4bd8dff84324 124 wait_us(3);
BaserK 0:d8d055e8f830 125 }
BaserK 0:d8d055e8f830 126 }
BaserK 0:d8d055e8f830 127
BaserK 0:d8d055e8f830 128 void toggle_led1() {ledToggle(1);}
BaserK 0:d8d055e8f830 129 void toggle_led2() {ledToggle(2);}
BaserK 0:d8d055e8f830 130
BaserK 0:d8d055e8f830 131 /* This function is created to avoid address error that caused from Ticker.attach func */
BaserK 0:d8d055e8f830 132 void compFilter() {mpu6050.complementaryFilter(&pitchAngle, &rollAngle);}
BaserK 0:d8d055e8f830 133
BaserK 0:d8d055e8f830 134 /* Tilt compensated compass data */
BaserK 0:d8d055e8f830 135 // Works well for tilt in +/- 50 deg range
BaserK 0:d8d055e8f830 136 void tiltCompensatedAngle()
BaserK 0:d8d055e8f830 137 {
BaserK 0:d8d055e8f830 138 float mag_Data[3], Xh, Yh;
BaserK 0:d8d055e8f830 139 hmc5883l.readMagData(mag_Data);
BaserK 0:d8d055e8f830 140
BaserK 0:d8d055e8f830 141 Xh = mag_Data[0] * cos(rollAngle*PI/180) - mag_Data[2] * sin(rollAngle*PI/180) ;
BaserK 0:d8d055e8f830 142
BaserK 0:d8d055e8f830 143 Yh = mag_Data[0] * sin(pitchAngle*PI/180) * sin(rollAngle*PI/180) +
BaserK 0:d8d055e8f830 144 mag_Data[1] * cos(pitchAngle*PI/180) -
BaserK 0:d8d055e8f830 145 mag_Data[2] * sin(pitchAngle*PI/180) * cos(rollAngle*PI/180) ;
BaserK 0:d8d055e8f830 146
BaserK 0:d8d055e8f830 147 /* Calculate the compensated heading angle */
BaserK 0:d8d055e8f830 148 double heading = atan2(Yh, Xh);
BaserK 0:d8d055e8f830 149
BaserK 0:d8d055e8f830 150 // After calculating heading declination angle should be added to heading which is the error of the magnetic field in specific location.
BaserK 0:d8d055e8f830 151 // declinationAngle can be found here http://www.magnetic-declination.com/
BaserK 0:d8d055e8f830 152 // For Ankara (my location) declinationAngle is ~5.5 degrees (0.096 radians)
BaserK 0:d8d055e8f830 153 float declinationAngle = 0.096;
BaserK 0:d8d055e8f830 154 heading += declinationAngle;
BaserK 0:d8d055e8f830 155
BaserK 0:d8d055e8f830 156 // Correct for when signs are reversed.
BaserK 0:d8d055e8f830 157 if(heading < 0)
BaserK 0:d8d055e8f830 158 heading += 2*PI;
BaserK 0:d8d055e8f830 159
BaserK 0:d8d055e8f830 160 // Check for wrap due to addition of declination.
BaserK 0:d8d055e8f830 161 if(heading > 2*PI)
BaserK 0:d8d055e8f830 162 heading -= 2*PI;
BaserK 0:d8d055e8f830 163
BaserK 0:d8d055e8f830 164 /* Convert radian to degrees */
BaserK 0:d8d055e8f830 165 heading = heading * 180 / PI;
BaserK 0:d8d055e8f830 166
BaserK 0:d8d055e8f830 167 yawAngle = heading;
BaserK 0:d8d055e8f830 168 }