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.

Dependencies:   mbed MODI2C

Committer:
maetugr
Date:
Mon Jun 10 13:22:46 2013 +0000
Revision:
34:3aa1cbcde59d
Parent:
33:fd98776b6cc7
Child:
35:2a9465fedb99
First version with new ESCs (they are working with the KK Board); some strange kicks in the behaviour of balancing, next step a logger

Who changed what in which revision?

UserRevisionLine numberNew 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 28:ba6ca9f4def4 17 #define YAWSPEED 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 34:3aa1cbcde59d 40 float P = 1.0; // PID values
maetugr 34:3aa1cbcde59d 41 float I = 0;
maetugr 34:3aa1cbcde59d 42 float D = 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 32:e2e02338805e 50 //PC pc(USBTX, USBRX, 115200); // USB
maetugr 33:fd98776b6cc7 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 33:fd98776b6cc7 73 dt_read_sensors = GlobalTimer.read() - time_read_sensors; // stop time measure for sensors
maetugr 8:d25ecdcdbeb5 74
maetugr 33:fd98776b6cc7 75 // meassure dt for the filter
maetugr 33:fd98776b6cc7 76 dt = GlobalTimer.read() - time_for_dt; // time in us since last loop
maetugr 33:fd98776b6cc7 77 time_for_dt = GlobalTimer.read(); // set new time for next measurement
maetugr 12:67a06c9b69d5 78
maetugr 26:96a072233d7a 79 IMU.compute(dt, Gyro.data, Acc.data);
maetugr 8:d25ecdcdbeb5 80
maetugr 21:c2a2e7cbabdd 81 // Arming / disarming
maetugr 34:3aa1cbcde59d 82 if(RC[THROTTLE].read() < 20 && RC[RUDDER].read() > 850) {
maetugr 21:c2a2e7cbabdd 83 armed = true;
maetugr 25:0498d3041afa 84 }
maetugr 34:3aa1cbcde59d 85 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 86 armed = false;
maetugr 25:0498d3041afa 87 }
maetugr 20:e116e596e540 88
maetugr 34:3aa1cbcde59d 89 // RC Angle
maetugr 33:fd98776b6cc7 90 for(int i=0;i<2;i++) { // calculate new angle we want the QC to have
maetugr 31:872d8b8c7812 91 RC_angle[i] = (RC[i].read()-500)*RC_SENSITIVITY/500.0;
maetugr 33:fd98776b6cc7 92 if (RC_angle[i] < -RC_SENSITIVITY-2)
maetugr 33:fd98776b6cc7 93 RC_angle[i] = 0;
maetugr 33:fd98776b6cc7 94 }
maetugr 33:fd98776b6cc7 95 //RC_angle[2] += (RC[3].read()-500)*YAWSPEED/500; // for yaw angle is integrated
maetugr 30:021e13b62575 96
maetugr 34:3aa1cbcde59d 97 // PID controlling
maetugr 30:021e13b62575 98 for(int i=0;i<3;i++) {
maetugr 29:8b7362a2ee14 99 Controller[i].setIntegrate(armed); // only integrate in controller when armed, so the value is not totally odd from not flying
maetugr 30:021e13b62575 100 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 101 }
maetugr 30:021e13b62575 102
maetugr 29:8b7362a2ee14 103
maetugr 21:c2a2e7cbabdd 104 if (armed) // for SECURITY!
maetugr 22:d301b455a1ad 105 {
maetugr 34:3aa1cbcde59d 106 MIX.compute(RC[THROTTLE].read(), controller_value); // let the Mixer compute motorspeeds based on throttle and controller output
maetugr 28:ba6ca9f4def4 107
maetugr 28:ba6ca9f4def4 108 for(int i=0;i<4;i++) // Set new motorspeeds
maetugr 26:96a072233d7a 109 ESC[i] = (int)MIX.Motor_speed[i];
maetugr 25:0498d3041afa 110
maetugr 15:753c5d6a63b3 111 } else {
maetugr 26:96a072233d7a 112 for(int i=0;i<4;i++) // for security reason, set every motor to zero speed
maetugr 28:ba6ca9f4def4 113 ESC[i] = 0;
maetugr 21:c2a2e7cbabdd 114 }
maetugr 8:d25ecdcdbeb5 115 }
maetugr 5:818c0668fd2d 116
maetugr 33:fd98776b6cc7 117 void commandexecuter(char* command) { // take new PID values on the fly
maetugr 31:872d8b8c7812 118 if (command[0] == 'p')
maetugr 33:fd98776b6cc7 119 P = atof(&command[1]);
maetugr 31:872d8b8c7812 120 if (command[0] == 'i')
maetugr 33:fd98776b6cc7 121 I = atof(&command[1]);
maetugr 31:872d8b8c7812 122 if (command[0] == 'd')
maetugr 33:fd98776b6cc7 123 D = atof(&command[1]);
maetugr 33:fd98776b6cc7 124 for(int i=0;i<2;i++) {
maetugr 33:fd98776b6cc7 125 Controller[i].setPID(P,I,D); // give the controller the new PID values
maetugr 31:872d8b8c7812 126 }
maetugr 31:872d8b8c7812 127 }
maetugr 31:872d8b8c7812 128
maetugr 26:96a072233d7a 129 int main() { // main programm for initialisation and debug output
maetugr 26:96a072233d7a 130 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 131
maetugr 21:c2a2e7cbabdd 132 #ifdef PC_CONNECTED
maetugr 21:c2a2e7cbabdd 133 // init screen
maetugr 12:67a06c9b69d5 134 pc.locate(10,5);
maetugr 21:c2a2e7cbabdd 135 pc.printf("Flybed v0.2");
maetugr 12:67a06c9b69d5 136 #endif
maetugr 1:5a64632b1eb9 137 LEDs.roll(2);
maetugr 5:818c0668fd2d 138
maetugr 33:fd98776b6cc7 139 Gyro.calibrate(50, 0.02);
maetugr 33:fd98776b6cc7 140 Acc.calibrate(50, 0.02);
maetugr 33:fd98776b6cc7 141
maetugr 21:c2a2e7cbabdd 142 // Start!
maetugr 2:93f703d2c4d7 143 GlobalTimer.start();
maetugr 28:ba6ca9f4def4 144 Dutycycler.attach(&dutycycle, RATE); // start to process all RATEms
maetugr 12:67a06c9b69d5 145
maetugr 12:67a06c9b69d5 146 while(1) {
maetugr 33:fd98776b6cc7 147 if (pc.readable()) // Get Serial input (polled because interrupts disturb I2C)
maetugr 33:fd98776b6cc7 148 pc.readcommand(&commandexecuter);
maetugr 33:fd98776b6cc7 149 //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 34:3aa1cbcde59d 150 //pc.printf("%f,%f,%f\r\n", IMU.angle[0], IMU.angle[1], IMU.angle[2]);
maetugr 30:021e13b62575 151 #if 1 //pc.cls();
maetugr 33:fd98776b6cc7 152 pc.locate(20,0); // PC output
maetugr 33:fd98776b6cc7 153 pc.printf("dt:%3.5fs dt_sensors:%3.5fs Altitude:%6.1fm ", dt, dt_read_sensors, Alt.CalcAltitude(Alt.Pressure));
maetugr 21:c2a2e7cbabdd 154 pc.locate(5,1);
maetugr 21:c2a2e7cbabdd 155 if(armed)
maetugr 21:c2a2e7cbabdd 156 pc.printf("ARMED!!!!!!!!!!!!!");
maetugr 21:c2a2e7cbabdd 157 else
maetugr 21:c2a2e7cbabdd 158 pc.printf("DIS_ARMED ");
maetugr 21:c2a2e7cbabdd 159 pc.locate(5,3);
maetugr 26:96a072233d7a 160 pc.printf("Roll:%6.1f Pitch:%6.1f Yaw:%6.1f ", IMU.angle[0], IMU.angle[1], IMU.angle[2]);
maetugr 31:872d8b8c7812 161 pc.locate(5,4);
maetugr 33:fd98776b6cc7 162 pc.printf("q0:%6.1f q1:%6.1f q2:%6.1f q3:%6.1f ", IMU.q0, IMU.q1, IMU.q2, IMU.q3);
maetugr 21:c2a2e7cbabdd 163 pc.locate(5,5);
maetugr 21:c2a2e7cbabdd 164 pc.printf("Gyro.data: X:%6.1f Y:%6.1f Z:%6.1f", Gyro.data[0], Gyro.data[1], Gyro.data[2]);
maetugr 30:021e13b62575 165 pc.locate(5,6);
maetugr 33:fd98776b6cc7 166 pc.printf("Acc.data: X:%6.1f Y:%6.1f Z:%6.1f", Acc.data[0], Acc.data[1], Acc.data[2]);
maetugr 33:fd98776b6cc7 167
maetugr 33:fd98776b6cc7 168 pc.locate(5,8);
maetugr 33:fd98776b6cc7 169 pc.printf("P:%6.1f I:%6.1f D:%6.1f ", P, I, D);
maetugr 33:fd98776b6cc7 170
maetugr 21:c2a2e7cbabdd 171 pc.locate(5,11);
maetugr 21:c2a2e7cbabdd 172 pc.printf("PID Result:");
maetugr 21:c2a2e7cbabdd 173 for(int i=0;i<3;i++)
maetugr 21:c2a2e7cbabdd 174 pc.printf(" %d: %6.1f", i, controller_value[i]);
maetugr 23:955a7c7ddf8b 175 pc.locate(5,14);
maetugr 30:021e13b62575 176 pc.printf("RC angle: roll: %f pitch: %f yaw: %f ", RC_angle[0], RC_angle[1], RC_angle[2]);
maetugr 29:8b7362a2ee14 177 pc.locate(5,16);
maetugr 29:8b7362a2ee14 178 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 23:955a7c7ddf8b 179
maetugr 29:8b7362a2ee14 180 // RC
maetugr 21:c2a2e7cbabdd 181 pc.locate(10,19);
maetugr 33:fd98776b6cc7 182 pc.printf("RC0: %4d RC1: %4d RC2: %4d RC3: %4d ", RC[0].read(), RC[1].read(), RC[2].read(), RC[3].read());
maetugr 31:872d8b8c7812 183
maetugr 31:872d8b8c7812 184 pc.locate(10,21);
maetugr 33:fd98776b6cc7 185 pc.printf("Commandline: %s ", pc.command);
maetugr 21:c2a2e7cbabdd 186 #endif
maetugr 21:c2a2e7cbabdd 187 if(armed){
maetugr 21:c2a2e7cbabdd 188 LEDs.rollnext();
maetugr 21:c2a2e7cbabdd 189 } else {
maetugr 26:96a072233d7a 190 for(int i=1;i<=4;i++)
maetugr 26:96a072233d7a 191 LEDs.set(i);
maetugr 21:c2a2e7cbabdd 192 }
maetugr 29:8b7362a2ee14 193 wait(0.05);
maetugr 0:0c4fafa398b4 194 }
maetugr 28:ba6ca9f4def4 195 }