NOT FINISHED YET!!! My first try to get a self built fully working Quadrocopter based on an mbed, a self built frame and some other more or less cheap parts.
main.cpp@36:128c55793728, 2013-06-12 (annotated)
- Committer:
- maetugr
- Date:
- Wed Jun 12 10:26:18 2013 +0000
- Revision:
- 36:128c55793728
- Parent:
- 35:2a9465fedb99
- Child:
- 37:34917f7c10ae
after inserting MODI2C to get an undisturbed I2C-communication
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maetugr | 7:9d4313510646 | 1 | #include "mbed.h" // Standard Library |
maetugr | 7:9d4313510646 | 2 | #include "LED.h" // LEDs framework for blinking ;) |
maetugr | 13:4737ee9ebfee | 3 | #include "PC.h" // Serial Port via USB by Roland Elmiger for debugging with Terminal (driver needed: https://mbed.org/media/downloads/drivers/mbedWinSerial_16466.exe) |
maetugr | 7:9d4313510646 | 4 | #include "L3G4200D.h" // Gyro (Gyroscope) |
maetugr | 7:9d4313510646 | 5 | #include "ADXL345.h" // Acc (Accelerometer) |
maetugr | 7:9d4313510646 | 6 | #include "HMC5883.h" // Comp (Compass) |
maetugr | 14:cf260677ecde | 7 | #include "BMP085_old.h" // Alt (Altitude sensor) |
maetugr | 30:021e13b62575 | 8 | #include "RC_Channel.h" // RemoteControl Channels with PPM |
maetugr | 15:753c5d6a63b3 | 9 | #include "Servo_PWM.h" // Motor PPM using PwmOut |
maetugr | 13:4737ee9ebfee | 10 | #include "PID.h" // PID Library by Aaron Berk |
maetugr | 26:96a072233d7a | 11 | #include "IMU_Filter.h" // Class to calculate position angles |
maetugr | 26:96a072233d7a | 12 | #include "Mixer.h" // Class to calculate motorspeeds from Angles, Regulation and RC-Signals |
maetugr | 0:0c4fafa398b4 | 13 | |
maetugr | 30:021e13b62575 | 14 | #define RATE 0.002 // speed of the interrupt for Sensors and PID |
maetugr | 30:021e13b62575 | 15 | #define PPM_FREQU 495 // Hz Frequency of PPM Signal for ESCs (maximum <500Hz) |
maetugr | 33:fd98776b6cc7 | 16 | #define RC_SENSITIVITY 30 // maximal angle from horizontal that the PID is aming for |
maetugr | 36:128c55793728 | 17 | #define YAWSPEED 0.2 // maximal speed of yaw rotation in degree per Rate |
maetugr | 15:753c5d6a63b3 | 18 | |
maetugr | 34:3aa1cbcde59d | 19 | // RC |
maetugr | 34:3aa1cbcde59d | 20 | #define AILERON 0 |
maetugr | 34:3aa1cbcde59d | 21 | #define ELEVATOR 1 |
maetugr | 34:3aa1cbcde59d | 22 | #define RUDDER 2 |
maetugr | 34:3aa1cbcde59d | 23 | #define THROTTLE 3 |
maetugr | 34:3aa1cbcde59d | 24 | // Axes |
maetugr | 34:3aa1cbcde59d | 25 | #define ROLL 0 |
maetugr | 34:3aa1cbcde59d | 26 | #define PITCH 1 |
maetugr | 34:3aa1cbcde59d | 27 | #define YAW 2 |
maetugr | 25:0498d3041afa | 28 | |
maetugr | 29:8b7362a2ee14 | 29 | #define PC_CONNECTED // decoment if you want to debug per USB/Bluetooth and your PC |
maetugr | 2:93f703d2c4d7 | 30 | |
maetugr | 34:3aa1cbcde59d | 31 | // Global variables |
maetugr | 34:3aa1cbcde59d | 32 | bool armed = false; // this variable is for security (when false no motor rotates any more) |
maetugr | 34:3aa1cbcde59d | 33 | float dt = 0; |
maetugr | 34:3aa1cbcde59d | 34 | float time_for_dt = 0; |
maetugr | 34:3aa1cbcde59d | 35 | float dt_read_sensors = 0; |
maetugr | 34:3aa1cbcde59d | 36 | float time_read_sensors = 0; |
maetugr | 34:3aa1cbcde59d | 37 | float controller_value[] = {0,0,0}; // The calculated answer form the Controller |
maetugr | 34:3aa1cbcde59d | 38 | float RC_angle[] = {0,0,0}; // Angle of the RC Sticks, to steer the QC |
maetugr | 34:3aa1cbcde59d | 39 | |
maetugr | 35:2a9465fedb99 | 40 | float P = 4.0; // PID values |
maetugr | 34:3aa1cbcde59d | 41 | float I = 0; |
maetugr | 35:2a9465fedb99 | 42 | float D = 2.0; |
maetugr | 34:3aa1cbcde59d | 43 | |
maetugr | 14:cf260677ecde | 44 | Timer GlobalTimer; // global time to calculate processing speed |
maetugr | 34:3aa1cbcde59d | 45 | Ticker Dutycycler; // timecontrolled interrupt for exact timed control loop |
maetugr | 14:cf260677ecde | 46 | |
maetugr | 34:3aa1cbcde59d | 47 | // Initialisation of hardware (see includes for more info) |
maetugr | 5:818c0668fd2d | 48 | LED LEDs; |
maetugr | 21:c2a2e7cbabdd | 49 | #ifdef PC_CONNECTED |
maetugr | 35:2a9465fedb99 | 50 | PC pc(USBTX, USBRX, 921600); // USB |
maetugr | 35:2a9465fedb99 | 51 | //PC pc(p9, p10, 115200); // Bluetooth |
maetugr | 21:c2a2e7cbabdd | 52 | #endif |
maetugr | 5:818c0668fd2d | 53 | L3G4200D Gyro(p28, p27); |
maetugr | 5:818c0668fd2d | 54 | ADXL345 Acc(p28, p27); |
maetugr | 11:9bf69bc6df45 | 55 | HMC5883 Comp(p28, p27); |
maetugr | 14:cf260677ecde | 56 | BMP085_old Alt(p28, p27); |
maetugr | 34:3aa1cbcde59d | 57 | RC_Channel RC[] = {RC_Channel(p5,1), RC_Channel(p6,2), RC_Channel(p8,4), RC_Channel(p7,3)}; // no p19/p20 ! |
maetugr | 34:3aa1cbcde59d | 58 | Servo_PWM ESC[] = {Servo_PWM(p21,PPM_FREQU), Servo_PWM(p22,PPM_FREQU), Servo_PWM(p23,PPM_FREQU), Servo_PWM(p24,PPM_FREQU)}; // p21 - p26 only because PWM needed! |
maetugr | 34:3aa1cbcde59d | 59 | IMU_Filter IMU; // (don't write () after constructor for no arguments!) |
maetugr | 34:3aa1cbcde59d | 60 | Mixer MIX(1); // 0 for +-Formation, 1 for X-Formation |
maetugr | 34:3aa1cbcde59d | 61 | PID Controller[] = {PID(P, I, D, 1000), PID(P, I, D, 1000), PID(0.5, 0, 0, 1000)}; // 0:X:Roll 1:Y:Pitch 2:Z:Yaw |
maetugr | 21:c2a2e7cbabdd | 62 | |
maetugr | 28:ba6ca9f4def4 | 63 | void dutycycle() // method which is called by the Ticker Dutycycler every RATE seconds |
maetugr | 8:d25ecdcdbeb5 | 64 | { |
maetugr | 33:fd98776b6cc7 | 65 | time_read_sensors = GlobalTimer.read(); // start time measure for sensors |
maetugr | 12:67a06c9b69d5 | 66 | |
maetugr | 15:753c5d6a63b3 | 67 | // read data from sensors // ATTENTION! the I2C option repeated true is important because otherwise interrupts while bus communications cause crashes |
maetugr | 14:cf260677ecde | 68 | Gyro.read(); |
maetugr | 34:3aa1cbcde59d | 69 | Acc.read(); |
maetugr | 34:3aa1cbcde59d | 70 | //Comp.read(); // TODO: not every loop every sensor? altitude not that important |
maetugr | 34:3aa1cbcde59d | 71 | //Alt.Update(); // TODO needs very long to read because of waits |
maetugr | 12:67a06c9b69d5 | 72 | |
maetugr | 35:2a9465fedb99 | 73 | pc.printf("%6.1f,%6.1f,%6.1f,%6.1f,%6.1f,%6.1f\r\n", Gyro.data[0], Gyro.data[1], Gyro.data[2], Acc.data[0], Acc.data[1], Acc.data[2]); |
maetugr | 35:2a9465fedb99 | 74 | |
maetugr | 33:fd98776b6cc7 | 75 | dt_read_sensors = GlobalTimer.read() - time_read_sensors; // stop time measure for sensors |
maetugr | 8:d25ecdcdbeb5 | 76 | |
maetugr | 33:fd98776b6cc7 | 77 | // meassure dt for the filter |
maetugr | 33:fd98776b6cc7 | 78 | dt = GlobalTimer.read() - time_for_dt; // time in us since last loop |
maetugr | 33:fd98776b6cc7 | 79 | time_for_dt = GlobalTimer.read(); // set new time for next measurement |
maetugr | 12:67a06c9b69d5 | 80 | |
maetugr | 26:96a072233d7a | 81 | IMU.compute(dt, Gyro.data, Acc.data); |
maetugr | 35:2a9465fedb99 | 82 | //pc.printf("%f,%f,%f,%3.5fs,%3.5fs\r\n", IMU.angle[0], IMU.angle[1], IMU.angle[2], dt, dt_read_sensors); |
maetugr | 35:2a9465fedb99 | 83 | |
maetugr | 21:c2a2e7cbabdd | 84 | // Arming / disarming |
maetugr | 34:3aa1cbcde59d | 85 | if(RC[THROTTLE].read() < 20 && RC[RUDDER].read() > 850) { |
maetugr | 21:c2a2e7cbabdd | 86 | armed = true; |
maetugr | 25:0498d3041afa | 87 | } |
maetugr | 34:3aa1cbcde59d | 88 | if((RC[THROTTLE].read() < 30 && RC[RUDDER].read() < 30) || RC[RUDDER].read() < -10 || RC[THROTTLE].read() < -10 || RC[ELEVATOR].read() < -10 || RC[AILERON].read() < -10) { |
maetugr | 20:e116e596e540 | 89 | armed = false; |
maetugr | 25:0498d3041afa | 90 | } |
maetugr | 20:e116e596e540 | 91 | |
maetugr | 34:3aa1cbcde59d | 92 | // RC Angle |
maetugr | 33:fd98776b6cc7 | 93 | for(int i=0;i<2;i++) { // calculate new angle we want the QC to have |
maetugr | 31:872d8b8c7812 | 94 | RC_angle[i] = (RC[i].read()-500)*RC_SENSITIVITY/500.0; |
maetugr | 33:fd98776b6cc7 | 95 | if (RC_angle[i] < -RC_SENSITIVITY-2) |
maetugr | 33:fd98776b6cc7 | 96 | RC_angle[i] = 0; |
maetugr | 33:fd98776b6cc7 | 97 | } |
maetugr | 33:fd98776b6cc7 | 98 | //RC_angle[2] += (RC[3].read()-500)*YAWSPEED/500; // for yaw angle is integrated |
maetugr | 30:021e13b62575 | 99 | |
maetugr | 34:3aa1cbcde59d | 100 | // PID controlling |
maetugr | 30:021e13b62575 | 101 | for(int i=0;i<3;i++) { |
maetugr | 29:8b7362a2ee14 | 102 | Controller[i].setIntegrate(armed); // only integrate in controller when armed, so the value is not totally odd from not flying |
maetugr | 35:2a9465fedb99 | 103 | controller_value[i] = Controller[i].compute(0, IMU.angle[i]); |
maetugr | 35:2a9465fedb99 | 104 | //controller_value[i] = Controller[i].compute(RC_angle[i], IMU.angle[i]); // give the controller the actual angle and get his advice to correct |
maetugr | 30:021e13b62575 | 105 | } |
maetugr | 30:021e13b62575 | 106 | |
maetugr | 29:8b7362a2ee14 | 107 | |
maetugr | 21:c2a2e7cbabdd | 108 | if (armed) // for SECURITY! |
maetugr | 22:d301b455a1ad | 109 | { |
maetugr | 34:3aa1cbcde59d | 110 | MIX.compute(RC[THROTTLE].read(), controller_value); // let the Mixer compute motorspeeds based on throttle and controller output |
maetugr | 28:ba6ca9f4def4 | 111 | |
maetugr | 28:ba6ca9f4def4 | 112 | for(int i=0;i<4;i++) // Set new motorspeeds |
maetugr | 26:96a072233d7a | 113 | ESC[i] = (int)MIX.Motor_speed[i]; |
maetugr | 25:0498d3041afa | 114 | |
maetugr | 15:753c5d6a63b3 | 115 | } else { |
maetugr | 26:96a072233d7a | 116 | for(int i=0;i<4;i++) // for security reason, set every motor to zero speed |
maetugr | 28:ba6ca9f4def4 | 117 | ESC[i] = 0; |
maetugr | 21:c2a2e7cbabdd | 118 | } |
maetugr | 8:d25ecdcdbeb5 | 119 | } |
maetugr | 5:818c0668fd2d | 120 | |
maetugr | 33:fd98776b6cc7 | 121 | void commandexecuter(char* command) { // take new PID values on the fly |
maetugr | 31:872d8b8c7812 | 122 | if (command[0] == 'p') |
maetugr | 33:fd98776b6cc7 | 123 | P = atof(&command[1]); |
maetugr | 31:872d8b8c7812 | 124 | if (command[0] == 'i') |
maetugr | 33:fd98776b6cc7 | 125 | I = atof(&command[1]); |
maetugr | 31:872d8b8c7812 | 126 | if (command[0] == 'd') |
maetugr | 33:fd98776b6cc7 | 127 | D = atof(&command[1]); |
maetugr | 33:fd98776b6cc7 | 128 | for(int i=0;i<2;i++) { |
maetugr | 33:fd98776b6cc7 | 129 | Controller[i].setPID(P,I,D); // give the controller the new PID values |
maetugr | 31:872d8b8c7812 | 130 | } |
maetugr | 31:872d8b8c7812 | 131 | } |
maetugr | 31:872d8b8c7812 | 132 | |
maetugr | 26:96a072233d7a | 133 | int main() { // main programm for initialisation and debug output |
maetugr | 26:96a072233d7a | 134 | NVIC_SetPriority(TIMER3_IRQn, 1); // set priorty of tickers below hardware interrupts (standard priority is 0)(this is to prevent the RC interrupt from waiting until ticker is finished) |
maetugr | 15:753c5d6a63b3 | 135 | |
maetugr | 21:c2a2e7cbabdd | 136 | #ifdef PC_CONNECTED |
maetugr | 21:c2a2e7cbabdd | 137 | // init screen |
maetugr | 12:67a06c9b69d5 | 138 | pc.locate(10,5); |
maetugr | 21:c2a2e7cbabdd | 139 | pc.printf("Flybed v0.2"); |
maetugr | 12:67a06c9b69d5 | 140 | #endif |
maetugr | 1:5a64632b1eb9 | 141 | LEDs.roll(2); |
maetugr | 5:818c0668fd2d | 142 | |
maetugr | 33:fd98776b6cc7 | 143 | Gyro.calibrate(50, 0.02); |
maetugr | 33:fd98776b6cc7 | 144 | Acc.calibrate(50, 0.02); |
maetugr | 33:fd98776b6cc7 | 145 | |
maetugr | 21:c2a2e7cbabdd | 146 | // Start! |
maetugr | 2:93f703d2c4d7 | 147 | GlobalTimer.start(); |
maetugr | 28:ba6ca9f4def4 | 148 | Dutycycler.attach(&dutycycle, RATE); // start to process all RATEms |
maetugr | 12:67a06c9b69d5 | 149 | |
maetugr | 12:67a06c9b69d5 | 150 | while(1) { |
maetugr | 35:2a9465fedb99 | 151 | #ifdef PC_CONNECTED |
maetugr | 35:2a9465fedb99 | 152 | if (pc.readable()) // Get Serial input (polled because interrupts disturb I2C) |
maetugr | 35:2a9465fedb99 | 153 | pc.readcommand(&commandexecuter); |
maetugr | 35:2a9465fedb99 | 154 | //pc.printf("%f %f %f %f %f %f\r\n", IMU.angle[0], IMU.angle[1], IMU.angle[2], controller_value[0], controller_value[1], controller_value[2]); // For live plot in MATLAB of IMU |
maetugr | 35:2a9465fedb99 | 155 | //pc.printf("%f,%f,%f,%f,%f,%f\r\n", IMU.angle[0], IMU.angle[1], IMU.angle[2], controller_value[0], controller_value[1], controller_value[2]); |
maetugr | 35:2a9465fedb99 | 156 | #if 0 //pc.cls(); |
maetugr | 35:2a9465fedb99 | 157 | pc.locate(20,0); // PC output |
maetugr | 35:2a9465fedb99 | 158 | pc.printf("dt:%3.5fs dt_sensors:%3.5fs Altitude:%6.1fm ", dt, dt_read_sensors, Alt.CalcAltitude(Alt.Pressure)); |
maetugr | 35:2a9465fedb99 | 159 | pc.locate(5,1); |
maetugr | 35:2a9465fedb99 | 160 | if(armed) |
maetugr | 35:2a9465fedb99 | 161 | pc.printf("ARMED!!!!!!!!!!!!!"); |
maetugr | 35:2a9465fedb99 | 162 | else |
maetugr | 35:2a9465fedb99 | 163 | pc.printf("DIS_ARMED "); |
maetugr | 35:2a9465fedb99 | 164 | pc.locate(5,3); |
maetugr | 35:2a9465fedb99 | 165 | pc.printf("Roll:%6.1f Pitch:%6.1f Yaw:%6.1f ", IMU.angle[0], IMU.angle[1], IMU.angle[2]); |
maetugr | 35:2a9465fedb99 | 166 | pc.locate(5,4); |
maetugr | 35:2a9465fedb99 | 167 | pc.printf("q0:%6.1f q1:%6.1f q2:%6.1f q3:%6.1f ", IMU.q0, IMU.q1, IMU.q2, IMU.q3); |
maetugr | 35:2a9465fedb99 | 168 | pc.locate(5,5); |
maetugr | 35:2a9465fedb99 | 169 | pc.printf("Gyro.data: X:%6.1f Y:%6.1f Z:%6.1f", Gyro.data[0], Gyro.data[1], Gyro.data[2]); |
maetugr | 35:2a9465fedb99 | 170 | pc.locate(5,6); |
maetugr | 35:2a9465fedb99 | 171 | pc.printf("Acc.data: X:%6.1f Y:%6.1f Z:%6.1f", Acc.data[0], Acc.data[1], Acc.data[2]); |
maetugr | 35:2a9465fedb99 | 172 | |
maetugr | 35:2a9465fedb99 | 173 | pc.locate(5,8); |
maetugr | 35:2a9465fedb99 | 174 | pc.printf("P:%6.1f I:%6.1f D:%6.1f ", P, I, D); |
maetugr | 35:2a9465fedb99 | 175 | |
maetugr | 35:2a9465fedb99 | 176 | pc.locate(5,11); |
maetugr | 35:2a9465fedb99 | 177 | pc.printf("PID Result:"); |
maetugr | 35:2a9465fedb99 | 178 | for(int i=0;i<3;i++) |
maetugr | 35:2a9465fedb99 | 179 | pc.printf(" %d: %6.1f", i, controller_value[i]); |
maetugr | 35:2a9465fedb99 | 180 | pc.locate(5,14); |
maetugr | 35:2a9465fedb99 | 181 | pc.printf("RC angle: roll: %f pitch: %f yaw: %f ", RC_angle[0], RC_angle[1], RC_angle[2]); |
maetugr | 35:2a9465fedb99 | 182 | pc.locate(5,16); |
maetugr | 35:2a9465fedb99 | 183 | pc.printf("Motor: 0:%d 1:%d 2:%d 3:%d ", (int)MIX.Motor_speed[0], (int)MIX.Motor_speed[1], (int)MIX.Motor_speed[2], (int)MIX.Motor_speed[3]); |
maetugr | 35:2a9465fedb99 | 184 | |
maetugr | 35:2a9465fedb99 | 185 | // RC |
maetugr | 35:2a9465fedb99 | 186 | pc.locate(10,19); |
maetugr | 35:2a9465fedb99 | 187 | pc.printf("RC0: %4d RC1: %4d RC2: %4d RC3: %4d ", RC[0].read(), RC[1].read(), RC[2].read(), RC[3].read()); |
maetugr | 35:2a9465fedb99 | 188 | |
maetugr | 35:2a9465fedb99 | 189 | pc.locate(10,21); |
maetugr | 35:2a9465fedb99 | 190 | pc.printf("Commandline: %s ", pc.command); |
maetugr | 35:2a9465fedb99 | 191 | #endif |
maetugr | 21:c2a2e7cbabdd | 192 | #endif |
maetugr | 21:c2a2e7cbabdd | 193 | if(armed){ |
maetugr | 21:c2a2e7cbabdd | 194 | LEDs.rollnext(); |
maetugr | 21:c2a2e7cbabdd | 195 | } else { |
maetugr | 26:96a072233d7a | 196 | for(int i=1;i<=4;i++) |
maetugr | 26:96a072233d7a | 197 | LEDs.set(i); |
maetugr | 21:c2a2e7cbabdd | 198 | } |
maetugr | 29:8b7362a2ee14 | 199 | wait(0.05); |
maetugr | 0:0c4fafa398b4 | 200 | } |
maetugr | 28:ba6ca9f4def4 | 201 | } |