Freescale Quadcopter with Freedom K64F Board
Dependencies: FXAS21000 FXLS8471Q FXOS8700Q MAG3110 MMA8652 MPL3115A2 mbed kalman mbed-dsp
Fork of Freescale_Multi-Sensor_Shield by
Quadcopter based on Freescale FRDM-K64F, Freescale FRDM-FXS-9AXIS, Hobbypower X525 V3 Quadcopter Foldable Kit with 1000KV Motors and SimonK 30A ESC
main.cpp@5:74ca8be12359, 2015-09-11 (annotated)
- Committer:
- julioefajardo
- Date:
- Fri Sep 11 22:24:22 2015 +0000
- Revision:
- 5:74ca8be12359
- Parent:
- 4:1bc3ca07a412
- Child:
- 6:d868495c1936
Version 0.3; Maxbotix LV-MaxSonar-EZ0 Sonar Range Finder; Red Led; Green Led; Bluetooth (RX Interrupt) - Sparkfun BlueSMiRF Silver; Start and Stop functions
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
screamer | 0:bfb567985c64 | 1 | /* Copyright (c) 2010-2011 mbed.org, MIT License |
screamer | 0:bfb567985c64 | 2 | * |
screamer | 0:bfb567985c64 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
screamer | 0:bfb567985c64 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
screamer | 0:bfb567985c64 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
screamer | 0:bfb567985c64 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
screamer | 0:bfb567985c64 | 7 | * Software is furnished to do so, subject to the following conditions: |
screamer | 0:bfb567985c64 | 8 | * |
screamer | 0:bfb567985c64 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
screamer | 0:bfb567985c64 | 10 | * substantial portions of the Software. |
screamer | 0:bfb567985c64 | 11 | * |
screamer | 0:bfb567985c64 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
screamer | 0:bfb567985c64 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
screamer | 0:bfb567985c64 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
screamer | 0:bfb567985c64 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
screamer | 0:bfb567985c64 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
screamer | 0:bfb567985c64 | 17 | */ |
screamer | 0:bfb567985c64 | 18 | |
screamer | 0:bfb567985c64 | 19 | #include "mbed.h" |
screamer | 0:bfb567985c64 | 20 | #include "FXOS8700Q.h" |
screamer | 0:bfb567985c64 | 21 | #include "FXAS21000.h" |
julioefajardo | 3:58ebc00f1a68 | 22 | #include "kalman.c" |
screamer | 0:bfb567985c64 | 23 | |
julioefajardo | 3:58ebc00f1a68 | 24 | #define PI 3.1415926535897932384626433832795 |
julioefajardo | 3:58ebc00f1a68 | 25 | #define Rad2Dree 57.295779513082320876798154814105 |
julioefajardo | 3:58ebc00f1a68 | 26 | |
julioefajardo | 5:74ca8be12359 | 27 | DigitalOut red(LED_RED); |
julioefajardo | 5:74ca8be12359 | 28 | DigitalOut green(LED_GREEN); |
julioefajardo | 5:74ca8be12359 | 29 | |
julioefajardo | 2:4bc4e25328cc | 30 | FXOS8700Q_acc combo_acc(A5, A4, FXOS8700CQ_SLAVE_ADDR0); |
julioefajardo | 2:4bc4e25328cc | 31 | FXOS8700Q_mag combo_mag(A5, A4, FXOS8700CQ_SLAVE_ADDR0); |
julioefajardo | 2:4bc4e25328cc | 32 | FXAS21000 gyro(A5, A4); |
screamer | 0:bfb567985c64 | 33 | |
julioefajardo | 3:58ebc00f1a68 | 34 | Timer GlobalTime; |
julioefajardo | 3:58ebc00f1a68 | 35 | Timer ProgramTimer; |
julioefajardo | 3:58ebc00f1a68 | 36 | |
julioefajardo | 2:4bc4e25328cc | 37 | //PwmOut M1(PTB18); |
julioefajardo | 2:4bc4e25328cc | 38 | PwmOut M1(D13); |
julioefajardo | 2:4bc4e25328cc | 39 | PwmOut M2(D12); |
julioefajardo | 2:4bc4e25328cc | 40 | PwmOut M3(D11); |
julioefajardo | 2:4bc4e25328cc | 41 | PwmOut M4(D10); |
screamer | 0:bfb567985c64 | 42 | |
screamer | 0:bfb567985c64 | 43 | Serial pc(USBTX, USBRX); |
julioefajardo | 5:74ca8be12359 | 44 | Serial bt(D1,D0); |
julioefajardo | 5:74ca8be12359 | 45 | |
julioefajardo | 5:74ca8be12359 | 46 | AnalogIn ultra(A0); |
screamer | 0:bfb567985c64 | 47 | |
julioefajardo | 3:58ebc00f1a68 | 48 | kalman filter_pitch; |
julioefajardo | 3:58ebc00f1a68 | 49 | kalman filter_roll; |
julioefajardo | 3:58ebc00f1a68 | 50 | |
julioefajardo | 3:58ebc00f1a68 | 51 | float R; |
julioefajardo | 3:58ebc00f1a68 | 52 | double angle[3]; |
julioefajardo | 3:58ebc00f1a68 | 53 | unsigned long timer; |
julioefajardo | 3:58ebc00f1a68 | 54 | long loopStartTime; |
julioefajardo | 5:74ca8be12359 | 55 | char i; |
julioefajardo | 5:74ca8be12359 | 56 | char command = ' '; |
julioefajardo | 5:74ca8be12359 | 57 | float high; |
julioefajardo | 5:74ca8be12359 | 58 | |
julioefajardo | 5:74ca8be12359 | 59 | float ESC1 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 60 | float ESC2 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 61 | float ESC3 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 62 | float ESC4 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 63 | |
julioefajardo | 5:74ca8be12359 | 64 | void bt_callback(void); |
julioefajardo | 5:74ca8be12359 | 65 | void esc_start(void); |
julioefajardo | 5:74ca8be12359 | 66 | void esc_stop(void); |
julioefajardo | 3:58ebc00f1a68 | 67 | |
screamer | 0:bfb567985c64 | 68 | int main() |
screamer | 0:bfb567985c64 | 69 | { |
julioefajardo | 2:4bc4e25328cc | 70 | float gyro_data[3]; |
screamer | 0:bfb567985c64 | 71 | MotionSensorDataUnits adata; |
screamer | 0:bfb567985c64 | 72 | MotionSensorDataUnits mdata; |
julioefajardo | 2:4bc4e25328cc | 73 | |
julioefajardo | 5:74ca8be12359 | 74 | bt.attach(&bt_callback); |
julioefajardo | 5:74ca8be12359 | 75 | |
screamer | 0:bfb567985c64 | 76 | printf("\r\nStarting\r\n\r\n"); |
julioefajardo | 2:4bc4e25328cc | 77 | |
julioefajardo | 5:74ca8be12359 | 78 | red = 0; green= 1; |
julioefajardo | 3:58ebc00f1a68 | 79 | GlobalTime.start(); |
julioefajardo | 3:58ebc00f1a68 | 80 | |
julioefajardo | 2:4bc4e25328cc | 81 | M1.period(0.02f); //Comparten el mismo timer |
julioefajardo | 5:74ca8be12359 | 82 | M1.pulsewidth(ESC1); |
julioefajardo | 5:74ca8be12359 | 83 | M2.pulsewidth(ESC2); |
julioefajardo | 5:74ca8be12359 | 84 | M3.pulsewidth(ESC3); |
julioefajardo | 5:74ca8be12359 | 85 | M4.pulsewidth(ESC4); |
julioefajardo | 2:4bc4e25328cc | 86 | |
screamer | 0:bfb567985c64 | 87 | combo_acc.enable(); |
screamer | 0:bfb567985c64 | 88 | combo_mag.enable(); |
screamer | 0:bfb567985c64 | 89 | printf("FXOS8700 Combo = %X\r\n", combo_acc.whoAmI()); |
screamer | 0:bfb567985c64 | 90 | printf("FXAS21000 Gyro = %X\r\n", gyro.getWhoAmI()); |
screamer | 0:bfb567985c64 | 91 | |
julioefajardo | 3:58ebc00f1a68 | 92 | kalman_init(&filter_pitch, R_matrix, Q_Gyro_matrix, Q_Accel_matrix); |
julioefajardo | 3:58ebc00f1a68 | 93 | kalman_init(&filter_roll, R_matrix, Q_Gyro_matrix, Q_Accel_matrix); |
julioefajardo | 3:58ebc00f1a68 | 94 | |
julioefajardo | 5:74ca8be12359 | 95 | esc_start(); |
julioefajardo | 5:74ca8be12359 | 96 | wait(2.0f); |
julioefajardo | 5:74ca8be12359 | 97 | red = 1; green= 0; |
julioefajardo | 4:1bc3ca07a412 | 98 | |
julioefajardo | 3:58ebc00f1a68 | 99 | ProgramTimer.start(); |
julioefajardo | 3:58ebc00f1a68 | 100 | loopStartTime = ProgramTimer.read_us(); |
julioefajardo | 3:58ebc00f1a68 | 101 | timer = loopStartTime; |
julioefajardo | 3:58ebc00f1a68 | 102 | |
screamer | 0:bfb567985c64 | 103 | while(1) { |
julioefajardo | 5:74ca8be12359 | 104 | |
julioefajardo | 5:74ca8be12359 | 105 | high = (float)(ultra.read_u16()*2.75f/512.0f)*2.54f; |
julioefajardo | 5:74ca8be12359 | 106 | |
screamer | 0:bfb567985c64 | 107 | combo_acc.getAxis(adata); |
julioefajardo | 3:58ebc00f1a68 | 108 | combo_mag.getAxis(mdata); |
julioefajardo | 3:58ebc00f1a68 | 109 | gyro.ReadXYZ(gyro_data); |
julioefajardo | 3:58ebc00f1a68 | 110 | |
julioefajardo | 3:58ebc00f1a68 | 111 | R = sqrt(std::pow(adata.x, 2) + std::pow(adata.y, 2) + std::pow(adata.z, 2)); |
screamer | 0:bfb567985c64 | 112 | |
julioefajardo | 3:58ebc00f1a68 | 113 | kalman_predict(&filter_pitch, gyro_data[0], (ProgramTimer.read_us() - timer)); |
julioefajardo | 3:58ebc00f1a68 | 114 | kalman_update(&filter_pitch, acos(adata.x/R)); |
julioefajardo | 3:58ebc00f1a68 | 115 | kalman_predict(&filter_roll, gyro_data[1], (ProgramTimer.read_us() - timer)); |
julioefajardo | 3:58ebc00f1a68 | 116 | kalman_update(&filter_roll, acos(adata.y/R)); |
julioefajardo | 3:58ebc00f1a68 | 117 | |
julioefajardo | 3:58ebc00f1a68 | 118 | angle[0] = kalman_get_angle(&filter_pitch); |
julioefajardo | 3:58ebc00f1a68 | 119 | angle[1] = kalman_get_angle(&filter_roll); |
julioefajardo | 3:58ebc00f1a68 | 120 | |
julioefajardo | 3:58ebc00f1a68 | 121 | timer = ProgramTimer.read_us(); |
julioefajardo | 3:58ebc00f1a68 | 122 | |
julioefajardo | 5:74ca8be12359 | 123 | //printf("FXOS8700 Acc: X:%6.3f Y:%6.3f Z:%6.3f\r\n", adata.x, adata.y, adata.z); |
julioefajardo | 5:74ca8be12359 | 124 | //printf("FXOS8700 Mag: X:%6.2f Y:%6.2f Z:%6.2f\r\n", mdata.x, mdata.y, mdata.z); |
julioefajardo | 5:74ca8be12359 | 125 | //printf("FXAS21000 Gyro: X:%6.2f Y:%6.2f Z:%6.2f\r\n", gyro_data[0], gyro_data[1], gyro_data[2]); |
julioefajardo | 5:74ca8be12359 | 126 | printf("Roll Angle X: %.6f Pitch Angle Y: %.6f \r\n", Rad2Dree * angle[1], Rad2Dree * angle[0]); |
julioefajardo | 5:74ca8be12359 | 127 | //printf("dist = %.2f \r\n",dist); |
screamer | 0:bfb567985c64 | 128 | |
julioefajardo | 5:74ca8be12359 | 129 | wait(0.02f); |
screamer | 0:bfb567985c64 | 130 | } |
screamer | 0:bfb567985c64 | 131 | } |
julioefajardo | 5:74ca8be12359 | 132 | |
julioefajardo | 5:74ca8be12359 | 133 | void bt_callback(void) { |
julioefajardo | 5:74ca8be12359 | 134 | // Note: you need to actually read from the serial to clear the RX interrupt |
julioefajardo | 5:74ca8be12359 | 135 | command = bt.getc(); |
julioefajardo | 5:74ca8be12359 | 136 | //if (command == 'z') bt.printf("start\n\r"); |
julioefajardo | 5:74ca8be12359 | 137 | if (command == 'x') { |
julioefajardo | 5:74ca8be12359 | 138 | bt.printf("stop\n\r"); |
julioefajardo | 5:74ca8be12359 | 139 | esc_stop(); |
julioefajardo | 5:74ca8be12359 | 140 | } |
julioefajardo | 5:74ca8be12359 | 141 | else if (command == 'w') bt.printf("up\n\r"); |
julioefajardo | 5:74ca8be12359 | 142 | else if (command == 's') bt.printf("down\n\r"); |
julioefajardo | 5:74ca8be12359 | 143 | else if (command == 'h') bt.printf("dist = %.2f \r\n",high); |
julioefajardo | 5:74ca8be12359 | 144 | else bt.printf("%c\n\r", command); |
julioefajardo | 5:74ca8be12359 | 145 | } |
julioefajardo | 5:74ca8be12359 | 146 | |
julioefajardo | 5:74ca8be12359 | 147 | void esc_start(void) { |
julioefajardo | 5:74ca8be12359 | 148 | ESC1 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 149 | ESC2 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 150 | ESC3 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 151 | ESC4 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 152 | for (i = 0; i < 4; i++){ |
julioefajardo | 5:74ca8be12359 | 153 | ESC1 += 0.0001f; |
julioefajardo | 5:74ca8be12359 | 154 | ESC2 += 0.0001f; |
julioefajardo | 5:74ca8be12359 | 155 | ESC3 += 0.0001f; |
julioefajardo | 5:74ca8be12359 | 156 | ESC4 += 0.0001f; |
julioefajardo | 5:74ca8be12359 | 157 | M1.pulsewidth(ESC1); |
julioefajardo | 5:74ca8be12359 | 158 | M2.pulsewidth(ESC2); |
julioefajardo | 5:74ca8be12359 | 159 | M3.pulsewidth(ESC3); |
julioefajardo | 5:74ca8be12359 | 160 | M4.pulsewidth(ESC4); |
julioefajardo | 5:74ca8be12359 | 161 | wait_ms(1000); |
julioefajardo | 5:74ca8be12359 | 162 | } |
julioefajardo | 5:74ca8be12359 | 163 | } |
julioefajardo | 5:74ca8be12359 | 164 | |
julioefajardo | 5:74ca8be12359 | 165 | void esc_stop(void){ |
julioefajardo | 5:74ca8be12359 | 166 | red = 0; green= 1; |
julioefajardo | 5:74ca8be12359 | 167 | while ((ESC1 > 0.0006f)||(ESC2 > 0.0006f)||(ESC3 > 0.0006f)||(ESC4 > 0.0006f)){ |
julioefajardo | 5:74ca8be12359 | 168 | ESC1 -= 0.0001f; |
julioefajardo | 5:74ca8be12359 | 169 | ESC2 -= 0.0001f; |
julioefajardo | 5:74ca8be12359 | 170 | ESC3 -= 0.0001f; |
julioefajardo | 5:74ca8be12359 | 171 | ESC4 -= 0.0001f; |
julioefajardo | 5:74ca8be12359 | 172 | M1.pulsewidth(ESC1); |
julioefajardo | 5:74ca8be12359 | 173 | M2.pulsewidth(ESC2); |
julioefajardo | 5:74ca8be12359 | 174 | M3.pulsewidth(ESC3); |
julioefajardo | 5:74ca8be12359 | 175 | M4.pulsewidth(ESC4); |
julioefajardo | 5:74ca8be12359 | 176 | wait_ms(1000); |
julioefajardo | 5:74ca8be12359 | 177 | } |
julioefajardo | 5:74ca8be12359 | 178 | if (ESC1 < 0.0006f) ESC1 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 179 | if (ESC2 < 0.0006f) ESC2 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 180 | if (ESC3 < 0.0006f) ESC3 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 181 | if (ESC4 < 0.0006f) ESC4 = 0.0006f; |
julioefajardo | 5:74ca8be12359 | 182 | } |