Jeffrey Resnik / Mbed 2 deprecated proj_working

Dependencies:   mbed airMouseWorkingLib ledControl2 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*   Getting Pitch and Roll Angles from MPU6050
00002 *
00003 *    @author: Baser Kandehir 
00004 *    @date: July 16, 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 *    First of all most of the credit goes to Kris Winer for his useful MPU6050 code.
00030 *    I rewrite the code in my way using class prototypes and my comments. This program
00031 *    can be a starter point for more advanced projects including quadcopters, balancing
00032 *    robots etc. Program takes accelerometer and gyroscope data from the MPU6050 registers
00033 *    and calibrates them for better results. Then uses this data is to obtain pitch and roll
00034 *    angles and writes these angles to the terminal which mbed is connected to. 
00035 *   
00036 *    @connections:
00037 *-------------------------------------------------------------- 
00038 *    |LPC1768|        |Peripherals|
00039 *    Pin 9 ---------> SDA of MPU6050
00040 *    Pin 10 --------> SCL of MPU6050
00041 *    GND -----------> GND of MPU6050
00042 *    VOUT (3.3 V) --> VCC of MPU6050
00043 *---------------------------------------------------------------
00044 *-------------------------------------------------------------- 
00045 *    |NUCLEO F411RE|  |Peripherals|
00046 *    D14 -----------> SDA of MPU6050
00047 *    D15 -----------> SCL of MPU6050
00048 *    GND -----------> GND of MPU6050
00049 *    VOUT (3.3 V) --> VCC of MPU6050
00050 *---------------------------------------------------------------
00051 
00052 
00053 *   Note: For any mistakes or comments, please contact me.
00054 */
00055 
00056 #include "MPU6050.h"
00057 #include "ledControl.h"
00058 
00059 //! Variable debug
00060 #define DEBUG
00061 /* */
00062 #include "mbed.h"
00063 #include "USBMouse.h"
00064 
00065 //create mouse object
00066 USBMouse mouse;
00067 /* Defined in the MPU6050.cpp file  */
00068 // I2C i2c(p9,p10);         // setup i2c (SDA,SCL)  
00069 
00070 //! Instance pc de classe Serial
00071 Serial pc(USBTX,USBRX);    // default baud rate: 9600
00072 //! Instance mpu6050 de classe MPU6050
00073 MPU6050 mpu6050;           // class: MPU6050, object: mpu6050 
00074 //! instance toggler1 de classe Ticker
00075 //! instance filer de classe Ticker
00076 // Ticker 
00077 // A Ticker is used to call a function at a recurring interval.
00078 // You can use as many separate Ticker objects as you require.
00079 Ticker toggler1;
00080 Ticker filter;           
00081 
00082 void toggle_led1();
00083 void toggle_led2();
00084 void compFilter();
00085 
00086 float pitchAngle = 0;
00087 float rollAngle = 0;
00088 
00089 int main() 
00090 {
00091     pc.baud(9600);                              // baud rate: 9600
00092     mpu6050.whoAmI();                           // Communication test: WHO_AM_I register reading 
00093     wait(1);
00094     mpu6050.calibrate(accelBias,gyroBias);      // Calibrate MPU6050 and load biases into bias registers
00095     pc.printf("Calibration is completed. \r\n");
00096     wait(0.5);
00097     mpu6050.init();                             // Initialize the sensor
00098     wait(1);
00099     pc.printf("MPU6050 is initialized for operation.. \r\n\r\n");
00100     wait_ms(500);
00101     
00102     /*
00103     gxDelta = 0
00104     gyDelta = 0
00105     gzDelta = 0
00106     
00107     axDelta = 0
00108     ayDelta = 0
00109     azDelta = 0
00110     
00111     prevgx = gx
00112     prevgy = gy
00113     prevgz = gz
00114     
00115     prevax = ax
00116     prevay = ay
00117     prevaz = az
00118     */
00119     
00120     //deltaThreshold = 0.2
00121     
00122     while(1) 
00123     {
00124      
00125      /* Uncomment below if you want to see accel and gyro data */
00126         
00127         pc.printf(" _____________________________________________________________  \r\n");
00128         pc.printf("| Accelerometer(g) | ax=%.3f | ay=%.3f | az=%.3f                \r\n",ax,ay,az);
00129         pc.printf("| Gyroscope(deg/s) | gx=%.3f | gy=%.3f | gz=%.3f                \r\n",gx,gy,gz);
00130         pc.printf("|_____________________________________________________________  \r\n\r\n");
00131 //        
00132         wait(0.1);
00133                 
00134         filter.attach(&compFilter, 0.005);    // Call the complementaryFilter func. every 5 ms (200 Hz sampling period)
00135         
00136         #ifdef DEBUG
00137         pc.printf(" [Debug On]  \r\n");
00138         mpu6050.complementaryFilter(&pitchAngle, &rollAngle);
00139         #endif
00140         
00141         
00142         pc.printf(" _______________\r\n");
00143         pc.printf("| Pitch: %.3f   \r\n",pitchAngle);
00144         pc.printf("| Roll:  %.3f   \r\n",rollAngle);
00145         pc.printf("|_______________\r\n\r\n");
00146         
00147         wait(1.1);
00148         
00149         /*
00150         gxDelta = gx - prevgx;
00151         gyDelta = gy - prevgy;
00152         gzDelta = gz - prevgz;
00153         
00154         axDelta = ax - prevax;
00155         ayDelta = ay - prevay;
00156         azDelta = az - prevaz;
00157         */
00158         
00159         
00160     }
00161 }
00162 
00163 
00164 void toggle_led1() {ledToggle(1);}
00165 void toggle_led2() {ledToggle(2);}
00166 
00167 /* This function is created to avoid address error that caused from Ticker.attach func */ 
00168 void compFilter() {mpu6050.complementaryFilter(&pitchAngle, &rollAngle);}
00169