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:
Sat Dec 01 07:13:04 2012 +0000
Revision:
28:ba6ca9f4def4
Parent:
27:9e546fa47c33
Child:
29:8b7362a2ee14
bevor eigener PID

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 7:9d4313510646 8 #include "RC_Channel.h" // RemoteControl Chnnels 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 15:753c5d6a63b3 14 #define RATE 0.02 // speed of the interrupt for Sensors and PID
maetugr 28:ba6ca9f4def4 15 #define MAXPITCH 40 // maximal angle from horizontal that the PID is aming for
maetugr 28:ba6ca9f4def4 16 #define YAWSPEED 2 // maximal speed of yaw rotation in degree per Rate
maetugr 15:753c5d6a63b3 17
maetugr 25:0498d3041afa 18 #define P_VALUE 0.02 // PID values
maetugr 28:ba6ca9f4def4 19 #define I_VALUE 20
maetugr 25:0498d3041afa 20 #define D_VALUE 0.004
maetugr 25:0498d3041afa 21
maetugr 14:cf260677ecde 22 //#define COMPASSCALIBRATE // decomment if you want to calibrate the Compass on start
maetugr 21:c2a2e7cbabdd 23 #define PC_CONNECTED // decoment if you want to debug per USB and your PC
maetugr 2:93f703d2c4d7 24
maetugr 14:cf260677ecde 25 Timer GlobalTimer; // global time to calculate processing speed
maetugr 28:ba6ca9f4def4 26 Ticker Dutycycler; // timecontrolled interrupt to get data form IMU and RC
maetugr 14:cf260677ecde 27
maetugr 14:cf260677ecde 28 // initialisation of hardware (see includes for more info)
maetugr 5:818c0668fd2d 29 LED LEDs;
maetugr 21:c2a2e7cbabdd 30 #ifdef PC_CONNECTED
maetugr 21:c2a2e7cbabdd 31 PC pc(USBTX, USBRX, 115200);
maetugr 21:c2a2e7cbabdd 32 #endif
maetugr 25:0498d3041afa 33 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
maetugr 25:0498d3041afa 34 FILE *Logger;
maetugr 5:818c0668fd2d 35 L3G4200D Gyro(p28, p27);
maetugr 5:818c0668fd2d 36 ADXL345 Acc(p28, p27);
maetugr 11:9bf69bc6df45 37 HMC5883 Comp(p28, p27);
maetugr 14:cf260677ecde 38 BMP085_old Alt(p28, p27);
maetugr 27:9e546fa47c33 39 RC_Channel RC[] = {RC_Channel(p11,1), RC_Channel(p12,2), RC_Channel(p13,3), RC_Channel(p14,4)}; // no p19/p20 !
maetugr 26:96a072233d7a 40 Servo_PWM ESC[] = {p21, p22, p23, p24}; // p21 - p26 only because PWM!
maetugr 26:96a072233d7a 41 IMU_Filter IMU; // don't write () after constructor for no arguments!
maetugr 26:96a072233d7a 42 Mixer MIX;
maetugr 8:d25ecdcdbeb5 43
maetugr 21:c2a2e7cbabdd 44 // 0:X:Roll 1:Y:Pitch 2:Z:Yaw
maetugr 28:ba6ca9f4def4 45 PID Controller[] = {PID(P_VALUE, I_VALUE, D_VALUE, RATE), PID(P_VALUE, I_VALUE, D_VALUE, RATE), PID(0.02, 0, 0.004, RATE)}; // TODO: RATE != dt immer anpassen
maetugr 14:cf260677ecde 46
maetugr 22:d301b455a1ad 47 // global variables
maetugr 20:e116e596e540 48 bool armed = false; // this variable is for security
maetugr 26:96a072233d7a 49 unsigned long dt = 0;
maetugr 26:96a072233d7a 50 unsigned long time_for_dt = 0;
maetugr 12:67a06c9b69d5 51 unsigned long dt_read_sensors = 0;
maetugr 12:67a06c9b69d5 52 unsigned long time_read_sensors = 0;
maetugr 26:96a072233d7a 53 float tempangle = 0; // temporärer winkel für yaw mit kompass
maetugr 21:c2a2e7cbabdd 54 float controller_value[] = {0,0,0};
maetugr 28:ba6ca9f4def4 55 float virt_angle[] = {0,0,0};
maetugr 28:ba6ca9f4def4 56 float yawposition = 0;
maetugr 21:c2a2e7cbabdd 57
maetugr 28:ba6ca9f4def4 58 void dutycycle() // method which is called by the Ticker Dutycycler every RATE seconds
maetugr 8:d25ecdcdbeb5 59 {
maetugr 12:67a06c9b69d5 60 time_read_sensors = GlobalTimer.read_us();
maetugr 12:67a06c9b69d5 61
maetugr 15:753c5d6a63b3 62 // read data from sensors // ATTENTION! the I2C option repeated true is important because otherwise interrupts while bus communications cause crashes
maetugr 14:cf260677ecde 63 Gyro.read();
maetugr 15:753c5d6a63b3 64 Acc.read(); // TODO: nicht jeder Sensor immer? höhe nicht so wichtig
maetugr 21:c2a2e7cbabdd 65 //Comp.read();
maetugr 13:4737ee9ebfee 66 //Alt.Update(); TODO braucht zu lange zum auslesen!
maetugr 12:67a06c9b69d5 67
maetugr 12:67a06c9b69d5 68 dt_read_sensors = GlobalTimer.read_us() - time_read_sensors;
maetugr 8:d25ecdcdbeb5 69
maetugr 8:d25ecdcdbeb5 70 // meassure dt
maetugr 26:96a072233d7a 71 dt = GlobalTimer.read_us() - time_for_dt; // time in us since last loop
maetugr 26:96a072233d7a 72 time_for_dt = GlobalTimer.read_us(); // set new time for next measurement
maetugr 12:67a06c9b69d5 73
maetugr 26:96a072233d7a 74 IMU.compute(dt, Gyro.data, Acc.data);
maetugr 8:d25ecdcdbeb5 75
maetugr 21:c2a2e7cbabdd 76 // Arming / disarming
maetugr 25:0498d3041afa 77 if(RC[2].read() < 20 && RC[3].read() > 850) {
maetugr 21:c2a2e7cbabdd 78 armed = true;
maetugr 25:0498d3041afa 79 #ifdef LOGGER
maetugr 25:0498d3041afa 80 if(Logger == NULL)
maetugr 25:0498d3041afa 81 Logger = fopen("/local/log.csv", "a");
maetugr 25:0498d3041afa 82 #endif
maetugr 25:0498d3041afa 83 }
maetugr 25:0498d3041afa 84 if((RC[2].read() < 30 && RC[3].read() < 30) || RC[3].read() < -10 || RC[2].read() < -10 || RC[1].read() < -10 || RC[0].read() < -10) {
maetugr 20:e116e596e540 85 armed = false;
maetugr 25:0498d3041afa 86 #ifdef LOGGER
maetugr 25:0498d3041afa 87 if(Logger != NULL) {
maetugr 25:0498d3041afa 88 fclose(Logger);
maetugr 25:0498d3041afa 89 Logger = NULL;
maetugr 25:0498d3041afa 90 }
maetugr 25:0498d3041afa 91 #endif
maetugr 25:0498d3041afa 92 }
maetugr 20:e116e596e540 93
maetugr 21:c2a2e7cbabdd 94 if (armed) // for SECURITY!
maetugr 22:d301b455a1ad 95 {
maetugr 28:ba6ca9f4def4 96 // RC controlling
maetugr 28:ba6ca9f4def4 97 /*virt_angle[0] = IMU.angle[0] + (RC[0].read()-500)*MAXPITCH/500.0; // TODO: zuerst RC calibration
maetugr 28:ba6ca9f4def4 98 virt_angle[1] = IMU.angle[1] + (RC[1].read()-500)*MAXPITCH/500.0;
maetugr 28:ba6ca9f4def4 99 yawposition += (RC[3].read()-500)*YAWSPEED/500;
maetugr 28:ba6ca9f4def4 100 virt_angle[2] = IMU.angle[2] + yawposition;*/
maetugr 28:ba6ca9f4def4 101
maetugr 24:c5a3cba48498 102 // PID controlling
maetugr 28:ba6ca9f4def4 103 /*if (!(RC[0].read() == -100)) { // the RC must be there to controll // alte version mit setpoint, nicht nötig? granzen bei yaw los? :)
maetugr 28:ba6ca9f4def4 104 Controller[0].setSetPoint(-((RC[0].read()-500)*MAXPITCH/500.0)); // set angles based on RC input
maetugr 28:ba6ca9f4def4 105 Controller[1].setSetPoint(-((RC[1].read()-500)*MAXPITCH/500.0));
maetugr 28:ba6ca9f4def4 106 Controller[2].setSetPoint(-((RC[3].read()-500)*180.0/500.0));
maetugr 28:ba6ca9f4def4 107 }*/
maetugr 28:ba6ca9f4def4 108 for(int i=0;i<3;i++) {
maetugr 28:ba6ca9f4def4 109 Controller[i].setProcessValue(virt_angle[i]); // give the controller the new measured angles that are allready controlled by RC
maetugr 28:ba6ca9f4def4 110 controller_value[i] = Controller[i].compute() - 1000; // -1000 because controller has output from 0 to 2000
maetugr 24:c5a3cba48498 111 }
maetugr 28:ba6ca9f4def4 112
maetugr 28:ba6ca9f4def4 113 MIX.compute(dt, IMU.angle, RC[2].read(), controller_value); // let the Mixer compute motorspeeds based on throttle and controller output
maetugr 28:ba6ca9f4def4 114
maetugr 28:ba6ca9f4def4 115 for(int i=0;i<4;i++) // Set new motorspeeds
maetugr 26:96a072233d7a 116 ESC[i] = (int)MIX.Motor_speed[i];
maetugr 25:0498d3041afa 117
maetugr 25:0498d3041afa 118 #ifdef LOGGER
maetugr 25:0498d3041afa 119 // Writing Log
maetugr 25:0498d3041afa 120 for(int i = 0; i < 3; i++) {
maetugr 25:0498d3041afa 121 fprintf(Logger, "%f;", angle[i]);
maetugr 25:0498d3041afa 122 fprintf(Logger, "%f;", controller_value[i]);
maetugr 25:0498d3041afa 123 }
maetugr 25:0498d3041afa 124 fprintf(Logger, "\r\n");
maetugr 25:0498d3041afa 125 #endif
maetugr 15:753c5d6a63b3 126 } else {
maetugr 26:96a072233d7a 127 for(int i=0;i<4;i++) // for security reason, set every motor to zero speed
maetugr 28:ba6ca9f4def4 128 ESC[i] = 0;
maetugr 24:c5a3cba48498 129 for(int i=0;i<3;i++)
maetugr 26:96a072233d7a 130 Controller[i].reset(); // TODO: schon ok so? anfangspeek?!
maetugr 21:c2a2e7cbabdd 131 }
maetugr 8:d25ecdcdbeb5 132 }
maetugr 5:818c0668fd2d 133
maetugr 26:96a072233d7a 134 int main() { // main programm for initialisation and debug output
maetugr 26:96a072233d7a 135 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 136
maetugr 25:0498d3041afa 137 #ifdef LOGGER
maetugr 25:0498d3041afa 138 Logger = fopen("/local/log.csv", "w"); // Prepare Logfile
maetugr 25:0498d3041afa 139 for(int i = 0; i < 3; i++) {
maetugr 25:0498d3041afa 140 fprintf(Logger, "angle[%d];", i);
maetugr 25:0498d3041afa 141 fprintf(Logger, "controller_value[%d];", i);
maetugr 25:0498d3041afa 142 }
maetugr 25:0498d3041afa 143 fprintf(Logger, "\r\n");
maetugr 25:0498d3041afa 144 fclose(Logger);
maetugr 25:0498d3041afa 145 Logger = NULL;
maetugr 25:0498d3041afa 146 #endif
maetugr 25:0498d3041afa 147
maetugr 25:0498d3041afa 148 // Prepare PID Controllers
maetugr 21:c2a2e7cbabdd 149 for(int i=0;i<3;i++) {
maetugr 21:c2a2e7cbabdd 150 Controller[i].setInputLimits(-90.0, 90.0);
maetugr 21:c2a2e7cbabdd 151 Controller[i].setOutputLimits(0.0, 2000.0);
maetugr 21:c2a2e7cbabdd 152 Controller[i].setBias(1000);
maetugr 21:c2a2e7cbabdd 153 Controller[i].setMode(MANUAL_MODE);//AUTO_MODE);
maetugr 21:c2a2e7cbabdd 154 Controller[i].setSetPoint(0);
maetugr 21:c2a2e7cbabdd 155 }
maetugr 26:96a072233d7a 156 //Controller[2].setInputLimits(-180.0, 180.0); // yaw 360 grad TODO: Yawsteuerung mit -180 bis 180 grad
maetugr 12:67a06c9b69d5 157
maetugr 21:c2a2e7cbabdd 158 #ifdef PC_CONNECTED
maetugr 21:c2a2e7cbabdd 159 #ifdef COMPASSCALIBRATE
maetugr 21:c2a2e7cbabdd 160 pc.locate(10,5);
maetugr 21:c2a2e7cbabdd 161 pc.printf("CALIBRATING");
maetugr 21:c2a2e7cbabdd 162 Comp.calibrate(60);
maetugr 21:c2a2e7cbabdd 163 #endif
maetugr 21:c2a2e7cbabdd 164
maetugr 21:c2a2e7cbabdd 165 // init screen
maetugr 12:67a06c9b69d5 166 pc.locate(10,5);
maetugr 21:c2a2e7cbabdd 167 pc.printf("Flybed v0.2");
maetugr 12:67a06c9b69d5 168 #endif
maetugr 1:5a64632b1eb9 169 LEDs.roll(2);
maetugr 5:818c0668fd2d 170
maetugr 21:c2a2e7cbabdd 171 // Start!
maetugr 2:93f703d2c4d7 172 GlobalTimer.start();
maetugr 28:ba6ca9f4def4 173 Dutycycler.attach(&dutycycle, RATE); // start to process all RATEms
maetugr 12:67a06c9b69d5 174
maetugr 12:67a06c9b69d5 175 while(1) {
maetugr 21:c2a2e7cbabdd 176 #ifdef PC_CONNECTED
maetugr 21:c2a2e7cbabdd 177 pc.locate(30,0); // PC output
maetugr 26:96a072233d7a 178 pc.printf("dt:%dms dt_sensors:%dus Altitude:%6.1fm ", dt/1000, dt_read_sensors, Alt.CalcAltitude(Alt.Pressure));
maetugr 21:c2a2e7cbabdd 179 pc.locate(5,1);
maetugr 21:c2a2e7cbabdd 180 if(armed)
maetugr 21:c2a2e7cbabdd 181 pc.printf("ARMED!!!!!!!!!!!!!");
maetugr 21:c2a2e7cbabdd 182 else
maetugr 21:c2a2e7cbabdd 183 pc.printf("DIS_ARMED ");
maetugr 21:c2a2e7cbabdd 184 pc.locate(5,3);
maetugr 26:96a072233d7a 185 pc.printf("Roll:%6.1f Pitch:%6.1f Yaw:%6.1f ", IMU.angle[0], IMU.angle[1], IMU.angle[2]);
maetugr 21:c2a2e7cbabdd 186 pc.locate(5,5);
maetugr 21:c2a2e7cbabdd 187 pc.printf("Gyro.data: X:%6.1f Y:%6.1f Z:%6.1f", Gyro.data[0], Gyro.data[1], Gyro.data[2]);
maetugr 21:c2a2e7cbabdd 188 pc.locate(5,8);
maetugr 21:c2a2e7cbabdd 189 pc.printf("Acc.data: X:%6d Y:%6d Z:%6d", Acc.data[0], Acc.data[1], Acc.data[2]);
maetugr 21:c2a2e7cbabdd 190 pc.locate(5,11);
maetugr 21:c2a2e7cbabdd 191 pc.printf("PID Result:");
maetugr 21:c2a2e7cbabdd 192 for(int i=0;i<3;i++)
maetugr 21:c2a2e7cbabdd 193 pc.printf(" %d: %6.1f", i, controller_value[i]);
maetugr 23:955a7c7ddf8b 194 pc.locate(5,14);
maetugr 28:ba6ca9f4def4 195 pc.printf("RC controll: roll: %f pitch: %f yaw: %f ", (RC[0].read()-500)*MAXPITCH/500.0, (RC[0].read()-500)*MAXPITCH/500.0, yawposition);
maetugr 23:955a7c7ddf8b 196
maetugr 21:c2a2e7cbabdd 197 pc.locate(10,15);
maetugr 21:c2a2e7cbabdd 198 pc.printf("Debug_Yaw: Comp:%6.1f tempangle:%6.1f ", Comp.get_angle(), tempangle);
maetugr 21:c2a2e7cbabdd 199 pc.locate(10,16);
maetugr 21:c2a2e7cbabdd 200 pc.printf("Comp_data: %6.1f %6.1f %6.1f |||| %6.1f ", Comp.data[0], Comp.data[1], Comp.data[2], Comp.get_angle());
maetugr 21:c2a2e7cbabdd 201 pc.locate(10,17);
maetugr 21:c2a2e7cbabdd 202 //pc.printf("Comp_scale: %6.4f %6.4f %6.4f ", Comp.scale[0], Comp.scale[1], Comp.scale[2]); no more accessible its private
maetugr 21:c2a2e7cbabdd 203 pc.locate(10,18);
maetugr 21:c2a2e7cbabdd 204 pc.printf("Comp_data: %6.1f %6.1f %6.1f |||| %6.1f ", Comp.data[0], Comp.data[1], Comp.data[2], Comp.get_angle());
maetugr 21:c2a2e7cbabdd 205
maetugr 28:ba6ca9f4def4 206 // graphical representation for RC signal // TODO: nicht nötig, nach funktionieren der RC kalibrierung weg damit
maetugr 21:c2a2e7cbabdd 207 pc.locate(10,19);
maetugr 21:c2a2e7cbabdd 208 pc.printf("RC0: %4d :[", RC[0].read());
maetugr 21:c2a2e7cbabdd 209 for (int i = 0; i < RC[0].read()/17; i++)
maetugr 21:c2a2e7cbabdd 210 pc.printf("=");
maetugr 21:c2a2e7cbabdd 211 pc.printf(" ");
maetugr 21:c2a2e7cbabdd 212 pc.locate(10,20);
maetugr 21:c2a2e7cbabdd 213 pc.printf("RC1: %4d :[", RC[1].read());
maetugr 21:c2a2e7cbabdd 214 for (int i = 0; i < RC[1].read()/17; i++)
maetugr 21:c2a2e7cbabdd 215 pc.printf("=");
maetugr 21:c2a2e7cbabdd 216 pc.printf(" ");
maetugr 21:c2a2e7cbabdd 217 pc.locate(10,21);
maetugr 21:c2a2e7cbabdd 218 pc.printf("RC2: %4d :[", RC[2].read());
maetugr 21:c2a2e7cbabdd 219 for (int i = 0; i < RC[2].read()/17; i++)
maetugr 21:c2a2e7cbabdd 220 pc.printf("=");
maetugr 21:c2a2e7cbabdd 221 pc.printf(" ");
maetugr 21:c2a2e7cbabdd 222 pc.locate(10,22);
maetugr 21:c2a2e7cbabdd 223 pc.printf("RC3: %4d :[", RC[3].read());
maetugr 21:c2a2e7cbabdd 224 for (int i = 0; i < RC[3].read()/17; i++)
maetugr 21:c2a2e7cbabdd 225 pc.printf("=");
maetugr 21:c2a2e7cbabdd 226 pc.printf(" ");
maetugr 21:c2a2e7cbabdd 227 #endif
maetugr 21:c2a2e7cbabdd 228 if(armed){
maetugr 21:c2a2e7cbabdd 229 LEDs.rollnext();
maetugr 21:c2a2e7cbabdd 230 } else {
maetugr 26:96a072233d7a 231 for(int i=1;i<=4;i++)
maetugr 26:96a072233d7a 232 LEDs.set(i);
maetugr 21:c2a2e7cbabdd 233 }
maetugr 0:0c4fafa398b4 234 }
maetugr 28:ba6ca9f4def4 235 }