Freescale Quadcopter with Freedom K64F Board

Dependencies:   FXAS21000 FXLS8471Q FXOS8700Q MAG3110 MMA8652 MPL3115A2 mbed kalman mbed-dsp

Fork of Freescale_Multi-Sensor_Shield by Shields

Quadcopter based on Freescale FRDM-K64F, Freescale FRDM-FXS-9AXIS, Hobbypower X525 V3 Quadcopter Foldable Kit with 1000KV Motors and SimonK 30A ESC /media/uploads/julioefajardo/1.jpg

Committer:
julioefajardo
Date:
Sat Sep 19 02:23:09 2015 +0000
Revision:
6:d868495c1936
Parent:
5:74ca8be12359
Version 0.5; CMSIS-DSP PID

Who changed what in which revision?

UserRevisionLine numberNew 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"
julioefajardo 6:d868495c1936 23 #include "arm_math.h"
screamer 0:bfb567985c64 24
julioefajardo 6:d868495c1936 25 #define PI 3.1415926535897932384626433832795f
julioefajardo 6:d868495c1936 26 #define Rad2Dree 57.295779513082320876798154814105f
julioefajardo 6:d868495c1936 27
julioefajardo 6:d868495c1936 28 #define PID_ROLL_KP 0.0245f /* Proporcional */ //0.015f
julioefajardo 6:d868495c1936 29 #define PID_ROLL_KI 0.000175f /* Integral */
julioefajardo 6:d868495c1936 30 #define PID_ROLL_KD 0.0f /* Derivative */
julioefajardo 6:d868495c1936 31
julioefajardo 6:d868495c1936 32 #define PID_PITCH_KP 0.0245f /* Proporcional */ //0.015f
julioefajardo 6:d868495c1936 33 #define PID_PITCH_KI 0.000175f /* Integral */
julioefajardo 6:d868495c1936 34 #define PID_PITCH_KD 0.0f /* Derivative */
julioefajardo 6:d868495c1936 35
julioefajardo 6:d868495c1936 36 #define ROLL_SP PI/2
julioefajardo 6:d868495c1936 37 #define PITCH_SP PI/2
julioefajardo 3:58ebc00f1a68 38
julioefajardo 5:74ca8be12359 39 DigitalOut red(LED_RED);
julioefajardo 5:74ca8be12359 40 DigitalOut green(LED_GREEN);
julioefajardo 5:74ca8be12359 41
julioefajardo 2:4bc4e25328cc 42 FXOS8700Q_acc combo_acc(A5, A4, FXOS8700CQ_SLAVE_ADDR0);
julioefajardo 2:4bc4e25328cc 43 FXOS8700Q_mag combo_mag(A5, A4, FXOS8700CQ_SLAVE_ADDR0);
julioefajardo 2:4bc4e25328cc 44 FXAS21000 gyro(A5, A4);
screamer 0:bfb567985c64 45
julioefajardo 3:58ebc00f1a68 46 Timer GlobalTime;
julioefajardo 3:58ebc00f1a68 47 Timer ProgramTimer;
julioefajardo 3:58ebc00f1a68 48
julioefajardo 2:4bc4e25328cc 49 //PwmOut M1(PTB18);
julioefajardo 2:4bc4e25328cc 50 PwmOut M1(D13);
julioefajardo 2:4bc4e25328cc 51 PwmOut M2(D12);
julioefajardo 2:4bc4e25328cc 52 PwmOut M3(D11);
julioefajardo 2:4bc4e25328cc 53 PwmOut M4(D10);
screamer 0:bfb567985c64 54
screamer 0:bfb567985c64 55 Serial pc(USBTX, USBRX);
julioefajardo 5:74ca8be12359 56 Serial bt(D1,D0);
julioefajardo 5:74ca8be12359 57
julioefajardo 5:74ca8be12359 58 AnalogIn ultra(A0);
screamer 0:bfb567985c64 59
julioefajardo 3:58ebc00f1a68 60 kalman filter_pitch;
julioefajardo 3:58ebc00f1a68 61 kalman filter_roll;
julioefajardo 3:58ebc00f1a68 62
julioefajardo 3:58ebc00f1a68 63 float R;
julioefajardo 3:58ebc00f1a68 64 double angle[3];
julioefajardo 3:58ebc00f1a68 65 unsigned long timer;
julioefajardo 3:58ebc00f1a68 66 long loopStartTime;
julioefajardo 5:74ca8be12359 67 char i;
julioefajardo 5:74ca8be12359 68 char command = ' ';
julioefajardo 6:d868495c1936 69 char sflag = 0;
julioefajardo 5:74ca8be12359 70 float high;
julioefajardo 5:74ca8be12359 71
julioefajardo 6:d868495c1936 72 float ESC1 = 0.0006f; //pitch up
julioefajardo 6:d868495c1936 73 float ESC2 = 0.0006f; //roll up
julioefajardo 6:d868495c1936 74 float ESC3 = 0.0006f; //roll down
julioefajardo 6:d868495c1936 75 float ESC4 = 0.0006f; //pitch down
julioefajardo 6:d868495c1936 76
julioefajardo 6:d868495c1936 77 float roll_error;
julioefajardo 6:d868495c1936 78 float pitch_error;
julioefajardo 6:d868495c1936 79
julioefajardo 6:d868495c1936 80 float roll;
julioefajardo 6:d868495c1936 81 float pitch;
julioefajardo 5:74ca8be12359 82
julioefajardo 5:74ca8be12359 83 void bt_callback(void);
julioefajardo 5:74ca8be12359 84 void esc_start(void);
julioefajardo 5:74ca8be12359 85 void esc_stop(void);
julioefajardo 6:d868495c1936 86 float esc_control(float current, float pid, float rate);
julioefajardo 3:58ebc00f1a68 87
julioefajardo 6:d868495c1936 88 int main(void)
screamer 0:bfb567985c64 89 {
julioefajardo 2:4bc4e25328cc 90 float gyro_data[3];
screamer 0:bfb567985c64 91 MotionSensorDataUnits adata;
screamer 0:bfb567985c64 92 MotionSensorDataUnits mdata;
julioefajardo 2:4bc4e25328cc 93
julioefajardo 5:74ca8be12359 94 bt.attach(&bt_callback);
julioefajardo 5:74ca8be12359 95
screamer 0:bfb567985c64 96 printf("\r\nStarting\r\n\r\n");
julioefajardo 2:4bc4e25328cc 97
julioefajardo 6:d868495c1936 98 arm_pid_instance_f32 RPID;
julioefajardo 6:d868495c1936 99 arm_pid_instance_f32 PPID;
julioefajardo 6:d868495c1936 100
julioefajardo 6:d868495c1936 101 //Pitch
julioefajardo 6:d868495c1936 102 PPID.Kp = PID_PITCH_KP/1000.0f; /* Proporcional */
julioefajardo 6:d868495c1936 103 PPID.Ki = PID_PITCH_KI/1000.0f; /* Integral */
julioefajardo 6:d868495c1936 104 PPID.Kd = PID_PITCH_KD/1000.0f; /* Derivative */
julioefajardo 6:d868495c1936 105
julioefajardo 6:d868495c1936 106 //Roll
julioefajardo 6:d868495c1936 107 RPID.Kp = PID_ROLL_KP/1000.0f; /* Proporcional */
julioefajardo 6:d868495c1936 108 RPID.Ki = PID_ROLL_KI/1000.0f; /* Integral */
julioefajardo 6:d868495c1936 109 RPID.Kd = PID_ROLL_KD/1000.0f; /* Derivative */
julioefajardo 6:d868495c1936 110
julioefajardo 6:d868495c1936 111 arm_pid_init_f32(&RPID, 1);
julioefajardo 6:d868495c1936 112 arm_pid_init_f32(&PPID, 1);
julioefajardo 6:d868495c1936 113
julioefajardo 5:74ca8be12359 114 red = 0; green= 1;
julioefajardo 3:58ebc00f1a68 115 GlobalTime.start();
julioefajardo 3:58ebc00f1a68 116
julioefajardo 2:4bc4e25328cc 117 M1.period(0.02f); //Comparten el mismo timer
julioefajardo 5:74ca8be12359 118 M1.pulsewidth(ESC1);
julioefajardo 5:74ca8be12359 119 M2.pulsewidth(ESC2);
julioefajardo 5:74ca8be12359 120 M3.pulsewidth(ESC3);
julioefajardo 5:74ca8be12359 121 M4.pulsewidth(ESC4);
julioefajardo 2:4bc4e25328cc 122
screamer 0:bfb567985c64 123 combo_acc.enable();
screamer 0:bfb567985c64 124 combo_mag.enable();
screamer 0:bfb567985c64 125 printf("FXOS8700 Combo = %X\r\n", combo_acc.whoAmI());
screamer 0:bfb567985c64 126 printf("FXAS21000 Gyro = %X\r\n", gyro.getWhoAmI());
screamer 0:bfb567985c64 127
julioefajardo 3:58ebc00f1a68 128 kalman_init(&filter_pitch, R_matrix, Q_Gyro_matrix, Q_Accel_matrix);
julioefajardo 3:58ebc00f1a68 129 kalman_init(&filter_roll, R_matrix, Q_Gyro_matrix, Q_Accel_matrix);
julioefajardo 3:58ebc00f1a68 130
julioefajardo 5:74ca8be12359 131 esc_start();
julioefajardo 5:74ca8be12359 132 wait(2.0f);
julioefajardo 5:74ca8be12359 133 red = 1; green= 0;
julioefajardo 4:1bc3ca07a412 134
julioefajardo 3:58ebc00f1a68 135 ProgramTimer.start();
julioefajardo 3:58ebc00f1a68 136 loopStartTime = ProgramTimer.read_us();
julioefajardo 3:58ebc00f1a68 137 timer = loopStartTime;
julioefajardo 3:58ebc00f1a68 138
screamer 0:bfb567985c64 139 while(1) {
julioefajardo 5:74ca8be12359 140
julioefajardo 5:74ca8be12359 141 high = (float)(ultra.read_u16()*2.75f/512.0f)*2.54f;
julioefajardo 5:74ca8be12359 142
screamer 0:bfb567985c64 143 combo_acc.getAxis(adata);
julioefajardo 3:58ebc00f1a68 144 combo_mag.getAxis(mdata);
julioefajardo 3:58ebc00f1a68 145 gyro.ReadXYZ(gyro_data);
julioefajardo 3:58ebc00f1a68 146
julioefajardo 3:58ebc00f1a68 147 R = sqrt(std::pow(adata.x, 2) + std::pow(adata.y, 2) + std::pow(adata.z, 2));
screamer 0:bfb567985c64 148
julioefajardo 3:58ebc00f1a68 149 kalman_predict(&filter_pitch, gyro_data[0], (ProgramTimer.read_us() - timer));
julioefajardo 3:58ebc00f1a68 150 kalman_update(&filter_pitch, acos(adata.x/R));
julioefajardo 3:58ebc00f1a68 151 kalman_predict(&filter_roll, gyro_data[1], (ProgramTimer.read_us() - timer));
julioefajardo 3:58ebc00f1a68 152 kalman_update(&filter_roll, acos(adata.y/R));
julioefajardo 3:58ebc00f1a68 153
julioefajardo 3:58ebc00f1a68 154 angle[0] = kalman_get_angle(&filter_pitch);
julioefajardo 3:58ebc00f1a68 155 angle[1] = kalman_get_angle(&filter_roll);
julioefajardo 3:58ebc00f1a68 156
julioefajardo 6:d868495c1936 157 if (angle[0]>PI) angle[0] = PI;
julioefajardo 6:d868495c1936 158 else if (angle[0]<0) angle[0] = 0.0f;
julioefajardo 6:d868495c1936 159 else angle[0] += 0.0f;
julioefajardo 6:d868495c1936 160
julioefajardo 6:d868495c1936 161 if (angle[1]>PI) angle[1] = PI;
julioefajardo 6:d868495c1936 162 else if (angle[1]<0) angle[1] = 0.0f;
julioefajardo 6:d868495c1936 163 else angle[1] += 0.0f;
julioefajardo 6:d868495c1936 164
julioefajardo 6:d868495c1936 165 pitch_error = angle[0] - PITCH_SP;
julioefajardo 6:d868495c1936 166 roll_error = angle[1] - ROLL_SP;
julioefajardo 6:d868495c1936 167
julioefajardo 6:d868495c1936 168 pitch = arm_pid_f32(&PPID, pitch_error);
julioefajardo 6:d868495c1936 169 roll = arm_pid_f32(&RPID, roll_error);
julioefajardo 6:d868495c1936 170
julioefajardo 3:58ebc00f1a68 171 timer = ProgramTimer.read_us();
julioefajardo 6:d868495c1936 172
julioefajardo 6:d868495c1936 173 if (!sflag){
julioefajardo 6:d868495c1936 174
julioefajardo 6:d868495c1936 175 ESC1 = esc_control(ESC1,-pitch,0.0f);
julioefajardo 6:d868495c1936 176 ESC2 = esc_control(ESC2,-roll,0.0f);
julioefajardo 6:d868495c1936 177 ESC3 = esc_control(ESC3,roll,0.0f);
julioefajardo 6:d868495c1936 178 ESC4 = esc_control(ESC4,pitch,0.0f);
julioefajardo 6:d868495c1936 179
julioefajardo 6:d868495c1936 180 M1.pulsewidth(ESC1);
julioefajardo 6:d868495c1936 181 M2.pulsewidth(ESC2);
julioefajardo 6:d868495c1936 182 M3.pulsewidth(ESC3);
julioefajardo 6:d868495c1936 183 M4.pulsewidth(ESC4);
julioefajardo 6:d868495c1936 184 }
julioefajardo 3:58ebc00f1a68 185
julioefajardo 5:74ca8be12359 186 //printf("FXOS8700 Acc: X:%6.3f Y:%6.3f Z:%6.3f\r\n", adata.x, adata.y, adata.z);
julioefajardo 5:74ca8be12359 187 //printf("FXOS8700 Mag: X:%6.2f Y:%6.2f Z:%6.2f\r\n", mdata.x, mdata.y, mdata.z);
julioefajardo 5:74ca8be12359 188 //printf("FXAS21000 Gyro: X:%6.2f Y:%6.2f Z:%6.2f\r\n", gyro_data[0], gyro_data[1], gyro_data[2]);
julioefajardo 6:d868495c1936 189 bt.printf("Roll Angle X: %.6f Pitch Angle Y: %.6f \r\n", Rad2Dree * angle[1], Rad2Dree * angle[0]);
julioefajardo 6:d868495c1936 190 bt.printf("roll = %.6f pitch = %.6f \r\n",roll,pitch);
julioefajardo 6:d868495c1936 191 bt.printf("ESC1 = %.6f ESC4 = %.6f \r\n", ESC1,ESC4);
julioefajardo 6:d868495c1936 192 bt.printf("ESC2 = %.6f ESC3 = %.6f \r\n", ESC2,ESC3);
julioefajardo 5:74ca8be12359 193 //printf("dist = %.2f \r\n",dist);
screamer 0:bfb567985c64 194
julioefajardo 5:74ca8be12359 195 wait(0.02f);
screamer 0:bfb567985c64 196 }
screamer 0:bfb567985c64 197 }
julioefajardo 5:74ca8be12359 198
julioefajardo 5:74ca8be12359 199 void bt_callback(void) {
julioefajardo 5:74ca8be12359 200 // Note: you need to actually read from the serial to clear the RX interrupt
julioefajardo 5:74ca8be12359 201 command = bt.getc();
julioefajardo 5:74ca8be12359 202 //if (command == 'z') bt.printf("start\n\r");
julioefajardo 5:74ca8be12359 203 if (command == 'x') {
julioefajardo 6:d868495c1936 204 sflag = 1;
julioefajardo 5:74ca8be12359 205 bt.printf("stop\n\r");
julioefajardo 5:74ca8be12359 206 esc_stop();
julioefajardo 5:74ca8be12359 207 }
julioefajardo 5:74ca8be12359 208 else if (command == 'w') bt.printf("up\n\r");
julioefajardo 5:74ca8be12359 209 else if (command == 's') bt.printf("down\n\r");
julioefajardo 5:74ca8be12359 210 else if (command == 'h') bt.printf("dist = %.2f \r\n",high);
julioefajardo 5:74ca8be12359 211 else bt.printf("%c\n\r", command);
julioefajardo 5:74ca8be12359 212 }
julioefajardo 5:74ca8be12359 213
julioefajardo 6:d868495c1936 214 float esc_control(float current, float pid, float rate){
julioefajardo 6:d868495c1936 215 if ((current + pid + rate)>0.0014f) return 0.0014f;
julioefajardo 6:d868495c1936 216 else if ((current + pid + rate)<0.001f) return 0.001f;
julioefajardo 6:d868495c1936 217 else return current + pid + rate;
julioefajardo 6:d868495c1936 218 }
julioefajardo 6:d868495c1936 219
julioefajardo 5:74ca8be12359 220 void esc_start(void) {
julioefajardo 6:d868495c1936 221 sflag = 0;
julioefajardo 5:74ca8be12359 222 ESC1 = 0.0006f;
julioefajardo 5:74ca8be12359 223 ESC2 = 0.0006f;
julioefajardo 5:74ca8be12359 224 ESC3 = 0.0006f;
julioefajardo 5:74ca8be12359 225 ESC4 = 0.0006f;
julioefajardo 5:74ca8be12359 226 for (i = 0; i < 4; i++){
julioefajardo 5:74ca8be12359 227 ESC1 += 0.0001f;
julioefajardo 5:74ca8be12359 228 ESC2 += 0.0001f;
julioefajardo 5:74ca8be12359 229 ESC3 += 0.0001f;
julioefajardo 5:74ca8be12359 230 ESC4 += 0.0001f;
julioefajardo 5:74ca8be12359 231 M1.pulsewidth(ESC1);
julioefajardo 5:74ca8be12359 232 M2.pulsewidth(ESC2);
julioefajardo 5:74ca8be12359 233 M3.pulsewidth(ESC3);
julioefajardo 5:74ca8be12359 234 M4.pulsewidth(ESC4);
julioefajardo 5:74ca8be12359 235 wait_ms(1000);
julioefajardo 5:74ca8be12359 236 }
julioefajardo 5:74ca8be12359 237 }
julioefajardo 5:74ca8be12359 238
julioefajardo 5:74ca8be12359 239 void esc_stop(void){
julioefajardo 6:d868495c1936 240 red = 0; green= 1; sflag = 1;
julioefajardo 5:74ca8be12359 241 while ((ESC1 > 0.0006f)||(ESC2 > 0.0006f)||(ESC3 > 0.0006f)||(ESC4 > 0.0006f)){
julioefajardo 5:74ca8be12359 242 ESC1 -= 0.0001f;
julioefajardo 5:74ca8be12359 243 ESC2 -= 0.0001f;
julioefajardo 5:74ca8be12359 244 ESC3 -= 0.0001f;
julioefajardo 5:74ca8be12359 245 ESC4 -= 0.0001f;
julioefajardo 5:74ca8be12359 246 M1.pulsewidth(ESC1);
julioefajardo 5:74ca8be12359 247 M2.pulsewidth(ESC2);
julioefajardo 5:74ca8be12359 248 M3.pulsewidth(ESC3);
julioefajardo 5:74ca8be12359 249 M4.pulsewidth(ESC4);
julioefajardo 6:d868495c1936 250 wait_ms(250);
julioefajardo 5:74ca8be12359 251 }
julioefajardo 5:74ca8be12359 252 if (ESC1 < 0.0006f) ESC1 = 0.0006f;
julioefajardo 5:74ca8be12359 253 if (ESC2 < 0.0006f) ESC2 = 0.0006f;
julioefajardo 5:74ca8be12359 254 if (ESC3 < 0.0006f) ESC3 = 0.0006f;
julioefajardo 5:74ca8be12359 255 if (ESC4 < 0.0006f) ESC4 = 0.0006f;
julioefajardo 5:74ca8be12359 256 }