Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: HMC5883L MPU6050 ledControl2 mbed
main.cpp
00001 /* Calculating Roll, Pitch and Yaw angles from IMU 00002 * 00003 * @author: Baser Kandehir 00004 * @date: August 5, 2015 00005 * @license: MIT license 00006 * 00007 * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr 00008 * 00009 * Permission is hereby granted, free of charge, to any person obtaining a copy 00010 * of this software and associated documentation files (the "Software"), to deal 00011 * in the Software without restriction, including without limitation the rights 00012 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00013 * copies of the Software, and to permit persons to whom the Software is 00014 * furnished to do so, subject to the following conditions: 00015 * 00016 * The above copyright notice and this permission notice shall be included in 00017 * all copies or substantial portions of the Software. 00018 * 00019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00022 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00024 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00025 * THE SOFTWARE. 00026 * 00027 * @description of the program: 00028 * 00029 * Program can calculate roll, pitch and yaw angles from the raw data that comes 00030 * from IMU. Yaw angle is compensated for tilt. All the angles are sent to the matlab 00031 * for further processing. 00032 * 00033 */ 00034 00035 #include "mbed.h" 00036 #include "HMC5883L.h" 00037 #include "MPU6050.h" 00038 #include "ledControl.h" 00039 00040 Serial pc(USBTX,USBRX); 00041 MPU6050 mpu6050; 00042 HMC5883L hmc5883l; 00043 Ticker toggler1; 00044 Ticker filter; 00045 Ticker compass; 00046 00047 void toggle_led1(); 00048 void toggle_led2(); 00049 void compFilter(); 00050 void tiltCompensatedAngle(); 00051 00052 float pitchAngle = 0; 00053 float rollAngle = 0; 00054 float yawAngle = 0; 00055 00056 int main() 00057 { 00058 pc.baud(9600); // baud rate: 9600 00059 mpu6050.whoAmI(); // Communication test: WHO_AM_I register reading 00060 mpu6050.calibrate(accelBias,gyroBias); // Calibrate MPU6050 and load biases into bias registers 00061 mpu6050.init(); // Initialize the sensor 00062 hmc5883l.init(); 00063 filter.attach(&compFilter, 0.005); // Call the complementaryFilter func. every 5 ms (200 Hz sampling period) 00064 compass.attach(&tiltCompensatedAngle, 0.015); // Call the tiltCompensatedAngle func. every 15 ms (75 Hz sampling period) 00065 while(1) 00066 { 00067 pc.printf("%.1f,%.1f,%.1f\r\n",rollAngle,pitchAngle,yawAngle); // send data to matlab 00068 wait_ms(40); 00069 } 00070 } 00071 00072 void toggle_led1() {ledToggle(1);} 00073 void toggle_led2() {ledToggle(2);} 00074 00075 /* This function is created to avoid address error that caused from Ticker.attach func */ 00076 void compFilter() {mpu6050.complementaryFilter(&pitchAngle, &rollAngle);} 00077 00078 /* Tilt compensated compass data */ 00079 // Works well for tilt in +/- 50 deg range 00080 void tiltCompensatedAngle() 00081 { 00082 float mag_Data[3], Xh, Yh; 00083 hmc5883l.readMagData(mag_Data); 00084 00085 Xh = mag_Data[0] * cos(rollAngle*PI/180) - mag_Data[2] * sin(rollAngle*PI/180) ; 00086 00087 Yh = mag_Data[0] * sin(pitchAngle*PI/180) * sin(rollAngle*PI/180) + 00088 mag_Data[1] * cos(pitchAngle*PI/180) - 00089 mag_Data[2] * sin(pitchAngle*PI/180) * cos(rollAngle*PI/180) ; 00090 00091 /* Calculate the compensated heading angle */ 00092 double heading = atan2(Yh, Xh); 00093 00094 // After calculating heading declination angle should be added to heading which is the error of the magnetic field in specific location. 00095 // declinationAngle can be found here http://www.magnetic-declination.com/ 00096 // For Ankara (my location) declinationAngle is ~5.5 degrees (0.096 radians) 00097 float declinationAngle = 0.096; 00098 heading += declinationAngle; 00099 00100 // Correct for when signs are reversed. 00101 if(heading < 0) 00102 heading += 2*PI; 00103 00104 // Check for wrap due to addition of declination. 00105 if(heading > 2*PI) 00106 heading -= 2*PI; 00107 00108 /* Convert radian to degrees */ 00109 heading = heading * 180 / PI; 00110 00111 yawAngle = heading; 00112 }
Generated on Tue Jul 12 2022 17:15:46 by
1.7.2