![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ST2CMV
Dependencies: mbed MPU6050 ledControl2
main.cpp@8:7b491d7b6d9d, 2020-10-21 (annotated)
- Committer:
- remig
- Date:
- Wed Oct 21 08:22:49 2020 +0000
- Revision:
- 8:7b491d7b6d9d
- Parent:
- 7:3e1e78b34e1a
NUCLEO STM32L476RG
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
BaserK | 4:33fef1998fc8 | 1 | /* Getting Pitch and Roll Angles from MPU6050 |
BaserK | 0:9203a021a0be | 2 | * |
BaserK | 0:9203a021a0be | 3 | * @author: Baser Kandehir |
BaserK | 4:33fef1998fc8 | 4 | * @date: July 16, 2015 |
BaserK | 5:a6a235d5442d | 5 | * @license: MIT license |
BaserK | 5:a6a235d5442d | 6 | * |
BaserK | 5:a6a235d5442d | 7 | * Copyright (c) 2015, Baser Kandehir, baser.kandehir@ieee.metu.edu.tr |
BaserK | 5:a6a235d5442d | 8 | * |
BaserK | 5:a6a235d5442d | 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
BaserK | 5:a6a235d5442d | 10 | * of this software and associated documentation files (the "Software"), to deal |
BaserK | 5:a6a235d5442d | 11 | * in the Software without restriction, including without limitation the rights |
BaserK | 5:a6a235d5442d | 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
BaserK | 5:a6a235d5442d | 13 | * copies of the Software, and to permit persons to whom the Software is |
BaserK | 5:a6a235d5442d | 14 | * furnished to do so, subject to the following conditions: |
BaserK | 5:a6a235d5442d | 15 | * |
BaserK | 5:a6a235d5442d | 16 | * The above copyright notice and this permission notice shall be included in |
BaserK | 5:a6a235d5442d | 17 | * all copies or substantial portions of the Software. |
BaserK | 5:a6a235d5442d | 18 | * |
BaserK | 5:a6a235d5442d | 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
BaserK | 5:a6a235d5442d | 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
BaserK | 5:a6a235d5442d | 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
BaserK | 5:a6a235d5442d | 22 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
BaserK | 5:a6a235d5442d | 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
BaserK | 5:a6a235d5442d | 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
BaserK | 5:a6a235d5442d | 25 | * THE SOFTWARE. |
BaserK | 0:9203a021a0be | 26 | * |
BaserK | 0:9203a021a0be | 27 | * @description of the program: |
BaserK | 0:9203a021a0be | 28 | * |
BaserK | 2:497faa1563ea | 29 | * First of all most of the credit goes to Kris Winer for his useful MPU6050 code. |
BaserK | 0:9203a021a0be | 30 | * I rewrite the code in my way using class prototypes and my comments. This program |
BaserK | 0:9203a021a0be | 31 | * can be a starter point for more advanced projects including quadcopters, balancing |
BaserK | 0:9203a021a0be | 32 | * robots etc. Program takes accelerometer and gyroscope data from the MPU6050 registers |
BaserK | 4:33fef1998fc8 | 33 | * and calibrates them for better results. Then uses this data is to obtain pitch and roll |
BaserK | 4:33fef1998fc8 | 34 | * angles and writes these angles to the terminal which mbed is connected to. |
BaserK | 0:9203a021a0be | 35 | * |
BaserK | 0:9203a021a0be | 36 | * @connections: |
BaserK | 0:9203a021a0be | 37 | *-------------------------------------------------------------- |
BaserK | 0:9203a021a0be | 38 | * |LPC1768| |Peripherals| |
BaserK | 0:9203a021a0be | 39 | * Pin 9 ---------> SDA of MPU6050 |
BaserK | 0:9203a021a0be | 40 | * Pin 10 --------> SCL of MPU6050 |
BaserK | 4:33fef1998fc8 | 41 | * GND -----------> GND of MPU6050 |
BaserK | 0:9203a021a0be | 42 | * VOUT (3.3 V) --> VCC of MPU6050 |
BaserK | 0:9203a021a0be | 43 | *--------------------------------------------------------------- |
BaserK | 6:12ff524abb6a | 44 | *-------------------------------------------------------------- |
BaserK | 6:12ff524abb6a | 45 | * |NUCLEO F411RE| |Peripherals| |
BaserK | 6:12ff524abb6a | 46 | * D14 -----------> SDA of MPU6050 |
BaserK | 6:12ff524abb6a | 47 | * D15 -----------> SCL of MPU6050 |
BaserK | 6:12ff524abb6a | 48 | * GND -----------> GND of MPU6050 |
BaserK | 6:12ff524abb6a | 49 | * VOUT (3.3 V) --> VCC of MPU6050 |
BaserK | 6:12ff524abb6a | 50 | *--------------------------------------------------------------- |
BaserK | 6:12ff524abb6a | 51 | |
BaserK | 6:12ff524abb6a | 52 | |
BaserK | 0:9203a021a0be | 53 | * Note: For any mistakes or comments, please contact me. |
BaserK | 0:9203a021a0be | 54 | */ |
BaserK | 0:9203a021a0be | 55 | |
BaserK | 0:9203a021a0be | 56 | #include "mbed.h" |
BaserK | 0:9203a021a0be | 57 | #include "MPU6050.h" |
BaserK | 0:9203a021a0be | 58 | #include "ledControl.h" |
BaserK | 0:9203a021a0be | 59 | |
remig | 8:7b491d7b6d9d | 60 | //! Variable debug |
remig | 8:7b491d7b6d9d | 61 | #define DEBUG |
BaserK | 0:9203a021a0be | 62 | /* */ |
BaserK | 0:9203a021a0be | 63 | |
BaserK | 0:9203a021a0be | 64 | /* Defined in the MPU6050.cpp file */ |
BaserK | 0:9203a021a0be | 65 | // I2C i2c(p9,p10); // setup i2c (SDA,SCL) |
BaserK | 0:9203a021a0be | 66 | |
remig | 8:7b491d7b6d9d | 67 | //! Instance pc de classe Serial |
BaserK | 4:33fef1998fc8 | 68 | Serial pc(USBTX,USBRX); // default baud rate: 9600 |
remig | 8:7b491d7b6d9d | 69 | //! Instance mpu6050 de classe MPU6050 |
BaserK | 4:33fef1998fc8 | 70 | MPU6050 mpu6050; // class: MPU6050, object: mpu6050 |
remig | 8:7b491d7b6d9d | 71 | //! instance toggler1 de classe Ticker |
remig | 8:7b491d7b6d9d | 72 | //! instance filer de classe Ticker |
remig | 8:7b491d7b6d9d | 73 | // Ticker |
remig | 8:7b491d7b6d9d | 74 | // A Ticker is used to call a function at a recurring interval. |
remig | 8:7b491d7b6d9d | 75 | // You can use as many separate Ticker objects as you require. |
BaserK | 0:9203a021a0be | 76 | Ticker toggler1; |
BaserK | 3:88737ad5c803 | 77 | Ticker filter; |
BaserK | 0:9203a021a0be | 78 | |
BaserK | 0:9203a021a0be | 79 | void toggle_led1(); |
BaserK | 0:9203a021a0be | 80 | void toggle_led2(); |
BaserK | 3:88737ad5c803 | 81 | void compFilter(); |
BaserK | 3:88737ad5c803 | 82 | |
BaserK | 3:88737ad5c803 | 83 | float pitchAngle = 0; |
BaserK | 3:88737ad5c803 | 84 | float rollAngle = 0; |
BaserK | 0:9203a021a0be | 85 | |
BaserK | 0:9203a021a0be | 86 | int main() |
BaserK | 0:9203a021a0be | 87 | { |
BaserK | 4:33fef1998fc8 | 88 | pc.baud(9600); // baud rate: 9600 |
BaserK | 0:9203a021a0be | 89 | mpu6050.whoAmI(); // Communication test: WHO_AM_I register reading |
BaserK | 0:9203a021a0be | 90 | wait(1); |
BaserK | 0:9203a021a0be | 91 | mpu6050.calibrate(accelBias,gyroBias); // Calibrate MPU6050 and load biases into bias registers |
BaserK | 4:33fef1998fc8 | 92 | pc.printf("Calibration is completed. \r\n"); |
BaserK | 0:9203a021a0be | 93 | wait(0.5); |
BaserK | 0:9203a021a0be | 94 | mpu6050.init(); // Initialize the sensor |
BaserK | 0:9203a021a0be | 95 | wait(1); |
BaserK | 4:33fef1998fc8 | 96 | pc.printf("MPU6050 is initialized for operation.. \r\n\r\n"); |
BaserK | 0:9203a021a0be | 97 | wait_ms(500); |
BaserK | 0:9203a021a0be | 98 | |
BaserK | 0:9203a021a0be | 99 | while(1) |
BaserK | 0:9203a021a0be | 100 | { |
BaserK | 4:33fef1998fc8 | 101 | |
BaserK | 4:33fef1998fc8 | 102 | /* Uncomment below if you want to see accel and gyro data */ |
BaserK | 4:33fef1998fc8 | 103 | |
remig | 8:7b491d7b6d9d | 104 | pc.printf(" _____________________________________________________________ \r\n"); |
remig | 8:7b491d7b6d9d | 105 | pc.printf("| Accelerometer(g) | ax=%.3f | ay=%.3f | az=%.3f \r\n",ax,ay,az); |
remig | 8:7b491d7b6d9d | 106 | pc.printf("| Gyroscope(deg/s) | gx=%.3f | gy=%.3f | gz=%.3f \r\n",gx,gy,gz); |
remig | 8:7b491d7b6d9d | 107 | pc.printf("|_____________________________________________________________ \r\n\r\n"); |
BaserK | 3:88737ad5c803 | 108 | // |
remig | 8:7b491d7b6d9d | 109 | wait(2.5); |
BaserK | 3:88737ad5c803 | 110 | |
remig | 8:7b491d7b6d9d | 111 | //filter.attach(&compFilter, 0.005); // Call the complementaryFilter func. every 5 ms (200 Hz sampling period) |
BaserK | 0:9203a021a0be | 112 | |
remig | 8:7b491d7b6d9d | 113 | #ifdef DEBUG |
remig | 8:7b491d7b6d9d | 114 | pc.printf(" [Debug On] \r\n"); |
remig | 8:7b491d7b6d9d | 115 | mpu6050.complementaryFilter(&pitchAngle, &rollAngle); |
remig | 8:7b491d7b6d9d | 116 | #endif |
BaserK | 4:33fef1998fc8 | 117 | pc.printf(" _______________\r\n"); |
BaserK | 4:33fef1998fc8 | 118 | pc.printf("| Pitch: %.3f \r\n",pitchAngle); |
BaserK | 4:33fef1998fc8 | 119 | pc.printf("| Roll: %.3f \r\n",rollAngle); |
BaserK | 4:33fef1998fc8 | 120 | pc.printf("|_______________\r\n\r\n"); |
BaserK | 0:9203a021a0be | 121 | |
BaserK | 3:88737ad5c803 | 122 | wait(1); |
BaserK | 4:33fef1998fc8 | 123 | |
BaserK | 0:9203a021a0be | 124 | } |
BaserK | 0:9203a021a0be | 125 | } |
BaserK | 0:9203a021a0be | 126 | |
BaserK | 0:9203a021a0be | 127 | void toggle_led1() {ledToggle(1);} |
BaserK | 0:9203a021a0be | 128 | void toggle_led2() {ledToggle(2);} |
BaserK | 3:88737ad5c803 | 129 | |
BaserK | 3:88737ad5c803 | 130 | /* This function is created to avoid address error that caused from Ticker.attach func */ |
BaserK | 4:33fef1998fc8 | 131 | void compFilter() {mpu6050.complementaryFilter(&pitchAngle, &rollAngle);} |