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:
Wed Oct 31 16:53:01 2012 +0000
Revision:
17:e096e85f6c36
Parent:
16:74a6531350b5
Child:
18:c8c09a3913ba
alle Sensoren mit i2c auf I2C_Sensor umgestellt (nicht getestet), noch keine calibration save per I2C_Sensor

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 13:4737ee9ebfee 11 #include "IntCtrl.h" // Interrupt Control by Roland Elmiger
maetugr 0:0c4fafa398b4 12
maetugr 14:cf260677ecde 13 #define PI 3.1415926535897932384626433832795 // ratio of a circle's circumference to its diameter
maetugr 17:e096e85f6c36 14 #define RAD2DEG 57.295779513082320876798154814105 // ratio between radians and degree (360/2Pi)
maetugr 15:753c5d6a63b3 15 #define RATE 0.02 // speed of the interrupt for Sensors and PID
maetugr 15:753c5d6a63b3 16
maetugr 15:753c5d6a63b3 17 #define P_VALUE 0.1 // viel zu tief!!!!!
maetugr 15:753c5d6a63b3 18 #define I_VALUE 0.0 //
maetugr 15:753c5d6a63b3 19 #define D_VALUE 0.0 //
maetugr 12:67a06c9b69d5 20
maetugr 14:cf260677ecde 21 //#define COMPASSCALIBRATE // decomment if you want to calibrate the Compass on start
maetugr 2:93f703d2c4d7 22
maetugr 14:cf260677ecde 23 Timer GlobalTimer; // global time to calculate processing speed
maetugr 14:cf260677ecde 24 Ticker Datagetter; // timecontrolled interrupt to get data form IMU and RC
maetugr 14:cf260677ecde 25
maetugr 14:cf260677ecde 26 // initialisation of hardware (see includes for more info)
maetugr 5:818c0668fd2d 27 LED LEDs;
maetugr 10:953afcbcebfc 28 PC pc(USBTX, USBRX, 57600);
maetugr 5:818c0668fd2d 29 L3G4200D Gyro(p28, p27);
maetugr 5:818c0668fd2d 30 ADXL345 Acc(p28, p27);
maetugr 11:9bf69bc6df45 31 HMC5883 Comp(p28, p27);
maetugr 14:cf260677ecde 32 BMP085_old Alt(p28, p27);
maetugr 15:753c5d6a63b3 33 RC_Channel RC[] = {(p11), (p12), (p13), (p14)}; // noooo p19/p20 !
maetugr 15:753c5d6a63b3 34 Servo_PWM Motor[] = {(p21), (p22), (p23), (p24)}; // p21 - p26 only !
maetugr 8:d25ecdcdbeb5 35
maetugr 15:753c5d6a63b3 36 //PID Controller[] = {(P_VALUE, I_VALUE, D_VALUE, RATE), (P_VALUE, I_VALUE, D_VALUE, RATE), (P_VALUE, I_VALUE, D_VALUE, RATE)}; // TODO: RATE != dt immer anpassen
maetugr 15:753c5d6a63b3 37 //PID P:3,0 bis 3,5 I:0,010 und 0,050 D:5 und 25
maetugr 15:753c5d6a63b3 38 PID Controller(P_VALUE, I_VALUE, D_VALUE, RATE);
maetugr 14:cf260677ecde 39
maetugr 14:cf260677ecde 40
maetugr 14:cf260677ecde 41 // global varibles
maetugr 14:cf260677ecde 42 unsigned long dt_get_data = 0; // TODO: dt namen
maetugr 12:67a06c9b69d5 43 unsigned long time_get_data = 0;
maetugr 12:67a06c9b69d5 44 unsigned long dt_read_sensors = 0;
maetugr 12:67a06c9b69d5 45 unsigned long time_read_sensors = 0;
maetugr 14:cf260677ecde 46 float angle[3] = {0,0,0}; // calculated values of the position [0: x,roll | 1: y,pitch | 2: z,yaw]
maetugr 14:cf260677ecde 47 float tempangle = 0; // temporärer winkel für yaw ohne kompass
maetugr 15:753c5d6a63b3 48 float co; // PID test
maetugr 8:d25ecdcdbeb5 49
maetugr 14:cf260677ecde 50 void get_Data() // method which is called by the Ticker Datagetter every RATE seconds
maetugr 8:d25ecdcdbeb5 51 {
maetugr 12:67a06c9b69d5 52 time_read_sensors = GlobalTimer.read_us();
maetugr 12:67a06c9b69d5 53
maetugr 15:753c5d6a63b3 54 // read data from sensors // ATTENTION! the I2C option repeated true is important because otherwise interrupts while bus communications cause crashes
maetugr 14:cf260677ecde 55 Gyro.read();
maetugr 15:753c5d6a63b3 56 Acc.read(); // TODO: nicht jeder Sensor immer? höhe nicht so wichtig
maetugr 15:753c5d6a63b3 57 Comp.read();
maetugr 13:4737ee9ebfee 58 //Alt.Update(); TODO braucht zu lange zum auslesen!
maetugr 12:67a06c9b69d5 59
maetugr 12:67a06c9b69d5 60 dt_read_sensors = GlobalTimer.read_us() - time_read_sensors;
maetugr 8:d25ecdcdbeb5 61
maetugr 8:d25ecdcdbeb5 62 // meassure dt
maetugr 12:67a06c9b69d5 63 dt_get_data = GlobalTimer.read_us() - time_get_data; // time in us since last loop
maetugr 12:67a06c9b69d5 64 time_get_data = GlobalTimer.read_us(); // set new time for next measurement
maetugr 8:d25ecdcdbeb5 65
maetugr 8:d25ecdcdbeb5 66 // calculate angles for roll, pitch an yaw
maetugr 12:67a06c9b69d5 67 angle[0] += (Acc.angle[0] - angle[0])/50 + Gyro.data[0] *dt_get_data/15000000.0;
maetugr 12:67a06c9b69d5 68 angle[1] += (Acc.angle[1]+3 - angle[1])/50 + Gyro.data[1] *dt_get_data/15000000.0;// TODO Offset accelerometer einstellen
maetugr 12:67a06c9b69d5 69 tempangle += (Comp.get_angle() - tempangle)/50 + Gyro.data[2] *dt_get_data/15000000.0;
maetugr 12:67a06c9b69d5 70 angle[2] += Gyro.data[2] *dt_get_data/15000000.0; // gyro only here
maetugr 12:67a06c9b69d5 71
maetugr 15:753c5d6a63b3 72 // PID controlling
maetugr 15:753c5d6a63b3 73 Controller.setProcessValue(angle[1]);
maetugr 8:d25ecdcdbeb5 74
maetugr 12:67a06c9b69d5 75 // calculate new motorspeeds
maetugr 15:753c5d6a63b3 76 co = Controller.compute() - 1000;
maetugr 16:74a6531350b5 77 if (RC[2].read() > 1100) // zu SICHERHEIT! zum testen (später ersetzen armed unarmed)
maetugr 15:753c5d6a63b3 78 {
maetugr 15:753c5d6a63b3 79 /*Motor[0] = RC[2].read()+((RC[0].read() - 1500)/10.0)+40;
maetugr 15:753c5d6a63b3 80 Motor[2] = RC[2].read()-((RC[0].read() - 1500)/10.0)-40;*/
maetugr 15:753c5d6a63b3 81 Motor[0] = RC[2].read()+co+40;
maetugr 15:753c5d6a63b3 82 Motor[2] = RC[2].read()-co-40;
maetugr 15:753c5d6a63b3 83 } else {
maetugr 15:753c5d6a63b3 84 Motor[0] = 1000;
maetugr 15:753c5d6a63b3 85 Motor[1] = 1000;
maetugr 15:753c5d6a63b3 86 Motor[2] = 1000;
maetugr 15:753c5d6a63b3 87 Motor[3] = 1000;
maetugr 15:753c5d6a63b3 88 }
maetugr 16:74a6531350b5 89 /*Motor[0] = 1000 + (100 - (angle[1] * 500/90)) * (RC[2].read() - 1000) / 1000; // test für erste reaktion der motoren entgegen der Auslenkung
maetugr 15:753c5d6a63b3 90 Motor[1] = 1000 + (100 - (angle[0] * 500/90)) * (RC[2].read() - 1000) / 1000;
maetugr 15:753c5d6a63b3 91 Motor[2] = 1000 + (100 + (angle[1] * 500/90)) * (RC[2].read() - 1000) / 1000;
maetugr 15:753c5d6a63b3 92 Motor[3] = 1000 + (100 + (angle[0] * 500/90)) * (RC[2].read() - 1000) / 1000;*/
maetugr 8:d25ecdcdbeb5 93 }
maetugr 5:818c0668fd2d 94
maetugr 14:cf260677ecde 95 int main() { // main programm only used for initialisation and debug output
maetugr 16:74a6531350b5 96 NVIC_SetPriority(TIMER3_IRQn, 1); // set priorty of tickers below hardware interrupts (standard priority is 0)
maetugr 15:753c5d6a63b3 97
maetugr 15:753c5d6a63b3 98 //for(int i=0;i<3;i++)
maetugr 15:753c5d6a63b3 99 Controller.setInputLimits(-90.0, 90.0);
maetugr 15:753c5d6a63b3 100 Controller.setOutputLimits(0.0, 2000.0);
maetugr 15:753c5d6a63b3 101 Controller.setBias(1000);
maetugr 15:753c5d6a63b3 102 Controller.setMode(MANUAL_MODE);//AUTO_MODE);
maetugr 15:753c5d6a63b3 103 Controller.setSetPoint(0);
maetugr 12:67a06c9b69d5 104
maetugr 12:67a06c9b69d5 105 #ifdef COMPASSCALIBRATE
maetugr 12:67a06c9b69d5 106 pc.locate(10,5);
maetugr 12:67a06c9b69d5 107 pc.printf("CALIBRATING");
maetugr 12:67a06c9b69d5 108 Comp.calibrate(60);
maetugr 12:67a06c9b69d5 109 #endif
maetugr 12:67a06c9b69d5 110
maetugr 6:179752756e9f 111 // init screen
maetugr 6:179752756e9f 112 pc.locate(10,5);
maetugr 6:179752756e9f 113 pc.printf("Flybed v0.2");
maetugr 1:5a64632b1eb9 114 LEDs.roll(2);
maetugr 5:818c0668fd2d 115
maetugr 14:cf260677ecde 116 // Start! TODO: Motor und RC start (armed....?)
maetugr 2:93f703d2c4d7 117 GlobalTimer.start();
maetugr 12:67a06c9b69d5 118 Datagetter.attach(&get_Data, RATE); // start to get data all RATEms
maetugr 12:67a06c9b69d5 119
maetugr 12:67a06c9b69d5 120 while(1) {
maetugr 9:4e0c3936c756 121 pc.locate(10,5); // PC output
maetugr 12:67a06c9b69d5 122 pc.printf("dt:%dms dt_sensors:%dus Altitude:%6.1fm ", dt_get_data/1000, dt_read_sensors, Alt.CalcAltitude(Alt.Pressure));
maetugr 7:9d4313510646 123 pc.locate(10,8);
maetugr 6:179752756e9f 124 pc.printf("Roll:%6.1f Pitch:%6.1f Yaw:%6.1f ", angle[0], angle[1], angle[2]);
maetugr 10:953afcbcebfc 125 pc.locate(10,9);
maetugr 10:953afcbcebfc 126 pc.printf("ACC: Roll:%6.1f Pitch:%6.1f Yaw:%6.1f ", Acc.angle[0], Acc.angle[1], Acc.angle[2]);
maetugr 7:9d4313510646 127 pc.locate(10,10);
maetugr 12:67a06c9b69d5 128 pc.printf("Debug_Yaw: Comp:%6.1f tempangle:%6.1f ", Comp.get_angle(), tempangle);
maetugr 7:9d4313510646 129 pc.locate(10,12);
maetugr 12:67a06c9b69d5 130 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 7:9d4313510646 131 pc.locate(10,13);
maetugr 14:cf260677ecde 132 //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 10:953afcbcebfc 133 pc.locate(10,15);
maetugr 15:753c5d6a63b3 134 pc.printf("PID Test: %6.1f", co);
maetugr 12:67a06c9b69d5 135
maetugr 11:9bf69bc6df45 136 pc.locate(10,19);
maetugr 14:cf260677ecde 137 pc.printf("RC0: %4d :[", RC[0].read());
maetugr 12:67a06c9b69d5 138 for (int i = 0; i < (RC[0].read() - 1000)/17; i++)
maetugr 12:67a06c9b69d5 139 pc.printf("=");
maetugr 12:67a06c9b69d5 140 pc.printf(" ");
maetugr 12:67a06c9b69d5 141 pc.locate(10,20);
maetugr 14:cf260677ecde 142 pc.printf("RC1: %4d :[", RC[1].read());
maetugr 12:67a06c9b69d5 143 for (int i = 0; i < (RC[1].read() - 1000)/17; i++)
maetugr 12:67a06c9b69d5 144 pc.printf("=");
maetugr 12:67a06c9b69d5 145 pc.printf(" ");
maetugr 12:67a06c9b69d5 146 pc.locate(10,21);
maetugr 14:cf260677ecde 147 pc.printf("RC2: %4d :[", RC[2].read());
maetugr 12:67a06c9b69d5 148 for (int i = 0; i < (RC[2].read() - 1000)/17; i++)
maetugr 12:67a06c9b69d5 149 pc.printf("=");
maetugr 12:67a06c9b69d5 150 pc.printf(" ");
maetugr 12:67a06c9b69d5 151 pc.locate(10,22);
maetugr 14:cf260677ecde 152 pc.printf("RC3: %4d :[", RC[3].read());
maetugr 12:67a06c9b69d5 153 for (int i = 0; i < (RC[3].read() - 1000)/17; i++)
maetugr 12:67a06c9b69d5 154 pc.printf("=");
maetugr 12:67a06c9b69d5 155 pc.printf(" ");
maetugr 10:953afcbcebfc 156 LEDs.rollnext();
maetugr 0:0c4fafa398b4 157 }
maetugr 0:0c4fafa398b4 158 }