My fully self designed first stable working Quadrocopter Software.

Dependencies:   mbed

Dependents:   fluy343

/media/uploads/maetugr/dsc09031.jpg

Committer:
maetugr
Date:
Sun Jul 06 05:51:25 2014 +0000
Revision:
5:06e978fd147a
Parent:
4:b0a60b0b24a9
Child:
7:ac2895479e34
switched from dedicated PWM Motor Driver to Interrupt Motor Driver which works on every Pin because PWM crashed a lot of I2C Sensor Values!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maetugr 0:12950aa67f2a 1 #include "mbed.h"
maetugr 0:12950aa67f2a 2 #include "LED.h" // LEDs framework for blinking ;)
maetugr 0:12950aa67f2a 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 0:12950aa67f2a 4
maetugr 0:12950aa67f2a 5 #include "IMU_10DOF.h" // Complete IMU class for 10DOF-Board (L3G4200D, ADXL345, HMC5883, BMP085)
maetugr 0:12950aa67f2a 6 #include "RC_Channel.h" // RemoteControl Channels with PPM
maetugr 0:12950aa67f2a 7 #include "PID.h" // PID Library (slim, self written)
maetugr 5:06e978fd147a 8 #include "Servo.h" // Motor PPM using any DigitalOut Pin
maetugr 0:12950aa67f2a 9
maetugr 0:12950aa67f2a 10 #define PPM_FREQU 495 // Hz Frequency of PPM Signal for ESCs (maximum <500Hz)
maetugr 0:12950aa67f2a 11 #define INTEGRAL_MAX 300 // maximal output offset that can result from integrating errors
maetugr 2:03e5f7ab473f 12 #define RC_SENSITIVITY 30 // maximal angle from horizontal that the PID is aming for
maetugr 2:03e5f7ab473f 13 #define YAWSPEED 0.2 // maximal speed of yaw rotation in degree per Rate
maetugr 0:12950aa67f2a 14 #define AILERON 0 // RC
maetugr 0:12950aa67f2a 15 #define ELEVATOR 1
maetugr 0:12950aa67f2a 16 #define RUDDER 2
maetugr 0:12950aa67f2a 17 #define THROTTLE 3
maetugr 1:5e2b81f2d0b4 18 #define CHANNEL8 4
maetugr 1:5e2b81f2d0b4 19 #define CHANNEL7 5
maetugr 1:5e2b81f2d0b4 20 #define CHANNEL6 6
maetugr 0:12950aa67f2a 21 #define ROLL 0 // Axes
maetugr 0:12950aa67f2a 22 #define PITCH 1
maetugr 0:12950aa67f2a 23 #define YAW 2
maetugr 0:12950aa67f2a 24
maetugr 5:06e978fd147a 25 bool armed = false; // is for security (when false no motor rotates any more)
maetugr 5:06e978fd147a 26 bool debug = true; // shows if we want output for the computer
maetugr 5:06e978fd147a 27 bool RC_present = false; // shows if an RC is present
maetugr 5:06e978fd147a 28 float P = 0, I = 0, D = 0;
maetugr 5:06e978fd147a 29 //float P = 13.16, I = 8, D = 2.73; // PID values
maetugr 5:06e978fd147a 30 float PY = 0, IY = 0, DY = 0;
maetugr 5:06e978fd147a 31 //float PY = 5.37, IY = 0, DY = 3; // PID values for Yaw
maetugr 2:03e5f7ab473f 32 float RC_angle[] = {0,0,0}; // Angle of the RC Sticks, to steer the QC
maetugr 0:12950aa67f2a 33 float Motor_speed[4] = {0,0,0,0}; // Mixed Motorspeeds, ready to send
maetugr 5:06e978fd147a 34 float * command_pointer = &D;
maetugr 5:06e978fd147a 35
maetugr 5:06e978fd147a 36 /*float max[3] = {-10000,-10000,-10000};
maetugr 5:06e978fd147a 37 float min[3] = {10000,10000,10000};*/
maetugr 0:12950aa67f2a 38
maetugr 0:12950aa67f2a 39 LED LEDs;
maetugr 0:12950aa67f2a 40 PC pc(USBTX, USBRX, 921600); // USB
maetugr 1:5e2b81f2d0b4 41 //PC pc(p9, p10, 115200); // Bluetooth
maetugr 0:12950aa67f2a 42 IMU_10DOF IMU(p28, p27);
maetugr 1:5e2b81f2d0b4 43 RC_Channel RC[] = {RC_Channel(p8,1), RC_Channel(p7,2), RC_Channel(p5,4), RC_Channel(p6,3), RC_Channel(p15,2), RC_Channel(p16,4), RC_Channel(p17,3)}; // no p19/p20 !
maetugr 2:03e5f7ab473f 44 PID Controller[] = {PID(P, I, D, INTEGRAL_MAX), PID(P, I, D, INTEGRAL_MAX), PID(PY, IY, DY, INTEGRAL_MAX)}; // 0:X:Roll 1:Y:Pitch 2:Z:Yaw
maetugr 5:06e978fd147a 45 Servo ESC[] = {Servo(p21,PPM_FREQU), Servo(p22,PPM_FREQU), Servo(p23,PPM_FREQU), Servo(p24,PPM_FREQU)}; // use any DigitalOit Pin
maetugr 0:12950aa67f2a 46
maetugr 1:5e2b81f2d0b4 47 extern "C" void mbed_reset();
maetugr 1:5e2b81f2d0b4 48
maetugr 0:12950aa67f2a 49 void executer() {
maetugr 1:5e2b81f2d0b4 50 char command = pc.getc();
maetugr 1:5e2b81f2d0b4 51 if (command == 'X')
maetugr 1:5e2b81f2d0b4 52 mbed_reset();
maetugr 5:06e978fd147a 53 if (command == '-')
maetugr 5:06e978fd147a 54 debug = !debug;
maetugr 2:03e5f7ab473f 55 if (command == 'A') {
maetugr 2:03e5f7ab473f 56 IMU.Acc.calibrate(100,0.05);
maetugr 2:03e5f7ab473f 57 pc.printf("\r\n***A***%.3f,%.3f,%.3f***\r\n", IMU.Acc.offset[ROLL], IMU.Acc.offset[PITCH], IMU.Acc.offset[YAW]);
maetugr 2:03e5f7ab473f 58 wait(10);
maetugr 2:03e5f7ab473f 59 }
maetugr 2:03e5f7ab473f 60 if (command == 'C') {
maetugr 2:03e5f7ab473f 61 IMU.Comp.calibrate(60);
maetugr 2:03e5f7ab473f 62 pc.printf("\r\n***C***%.3f,%.3f,%.3f***\r\n", IMU.Comp.offset[ROLL], IMU.Comp.offset[PITCH], IMU.Comp.offset[YAW]);
maetugr 2:03e5f7ab473f 63 wait(20);
maetugr 2:03e5f7ab473f 64 }
maetugr 2:03e5f7ab473f 65
maetugr 2:03e5f7ab473f 66 pc.putc(command);
maetugr 0:12950aa67f2a 67 LEDs.tilt(2);
maetugr 0:12950aa67f2a 68 }
maetugr 0:12950aa67f2a 69
maetugr 0:12950aa67f2a 70 int main() {
maetugr 0:12950aa67f2a 71 pc.attach(&executer);
maetugr 0:12950aa67f2a 72 while(1) {
maetugr 0:12950aa67f2a 73 // IMU
maetugr 0:12950aa67f2a 74 IMU.readAngles();
maetugr 2:03e5f7ab473f 75 //IMU.readAltitude(); // TODO: reading altitude takes much more time than the angles -> don't do this in your fast loop, Ticker?
maetugr 0:12950aa67f2a 76 //pc.printf("%.1f,%.1f,%.1f,%.1f'C,%.1fhPa,%.1fmaS,%.5fs,%.5fs\r\n", IMU.angle[0], IMU.angle[1], IMU.angle[2], IMU.temperature, IMU.pressure, IMU.altitude, IMU.dt, IMU.dt_sensors); // Output for Python
maetugr 0:12950aa67f2a 77
maetugr 0:12950aa67f2a 78 // Arming / disarming
maetugr 2:03e5f7ab473f 79 RC_present = !(RC[AILERON].read() == -100 || RC[ELEVATOR].read() == -100 || RC[RUDDER].read() == -100 || RC[THROTTLE].read() == -100); // TODO: Failsafe
maetugr 1:5e2b81f2d0b4 80 if(RC[THROTTLE].read() < 20 && RC[RUDDER].read() > 850) {
maetugr 1:5e2b81f2d0b4 81 armed = true;
maetugr 2:03e5f7ab473f 82 RC_angle[YAW] = IMU.angle[YAW];
maetugr 1:5e2b81f2d0b4 83 }
maetugr 2:03e5f7ab473f 84 if((RC[THROTTLE].read() < 30 && RC[RUDDER].read() < 30) || !RC_present) {
maetugr 0:12950aa67f2a 85 armed = false;
maetugr 0:12950aa67f2a 86 }
maetugr 0:12950aa67f2a 87
maetugr 2:03e5f7ab473f 88 // Setting PID Values from auxiliary RC channels
maetugr 1:5e2b81f2d0b4 89 if (RC[CHANNEL8].read() > 0 && RC[CHANNEL8].read() < 1000)
maetugr 5:06e978fd147a 90 D = 0 + (((float)RC[CHANNEL8].read()) * 15 / 1000);
maetugr 5:06e978fd147a 91 /*if (RC[CHANNEL7].read() > 0 && RC[CHANNEL7].read() < 1000)
maetugr 5:06e978fd147a 92 D = 2 + (((float)RC[CHANNEL7].read()) * 4 / 1000);*/
maetugr 2:03e5f7ab473f 93 for(int i=0;i<2;i++)
maetugr 2:03e5f7ab473f 94 Controller[i].setPID(P,I,D); // give the new PID values to roll and pitch controller
maetugr 2:03e5f7ab473f 95 Controller[YAW].setPID(PY,IY,DY);
maetugr 1:5e2b81f2d0b4 96
maetugr 2:03e5f7ab473f 97 // RC Angle ROLL-PITCH-Part
maetugr 2:03e5f7ab473f 98 for(int i=0;i<2;i++) { // calculate new angle we want the QC to have
maetugr 2:03e5f7ab473f 99 if (RC_present)
maetugr 2:03e5f7ab473f 100 RC_angle[i] = (RC[i].read()-500)*RC_SENSITIVITY/500.0;
maetugr 2:03e5f7ab473f 101 else
maetugr 2:03e5f7ab473f 102 RC_angle[i] = 0;
maetugr 2:03e5f7ab473f 103 }
maetugr 2:03e5f7ab473f 104
maetugr 2:03e5f7ab473f 105 // RC Angle YAW-Part
maetugr 2:03e5f7ab473f 106 float RC_yaw_adding; // temporary variable to take the desired yaw adjustment
maetugr 2:03e5f7ab473f 107 if (RC_present && RC[THROTTLE].read() > 20)
maetugr 2:03e5f7ab473f 108 RC_yaw_adding = -(RC[RUDDER].read()-500)*YAWSPEED/500;
maetugr 1:5e2b81f2d0b4 109 else
maetugr 2:03e5f7ab473f 110 RC_yaw_adding = 0;
maetugr 2:03e5f7ab473f 111
maetugr 2:03e5f7ab473f 112 while(RC_angle[YAW] + RC_yaw_adding < -180 || RC_angle[YAW] + RC_yaw_adding > 180) { // make shure it's in the cycle -180 to 180
maetugr 2:03e5f7ab473f 113 if(RC_angle[YAW] + RC_yaw_adding < -180)
maetugr 2:03e5f7ab473f 114 RC_yaw_adding += 360;
maetugr 2:03e5f7ab473f 115 if(RC_angle[YAW] + RC_yaw_adding > 180)
maetugr 2:03e5f7ab473f 116 RC_yaw_adding -= 360;
maetugr 2:03e5f7ab473f 117 }
maetugr 2:03e5f7ab473f 118 RC_angle[YAW] += RC_yaw_adding; // the yaw angle is integrated from stick input
maetugr 1:5e2b81f2d0b4 119
maetugr 0:12950aa67f2a 120 // Controlling
maetugr 2:03e5f7ab473f 121 for(int i=0;i<2;i++) {
maetugr 2:03e5f7ab473f 122 Controller[i].setIntegrate(armed); // only integrate in controller when armed, so the value is not totally odd from not flying
maetugr 4:b0a60b0b24a9 123 Controller[i].compute(RC_angle[i], IMU.angle[i], IMU.Sensor.data_gyro[i]); // give the controller the actual gyro values for D and angle for P,I and get his advice to correct
maetugr 2:03e5f7ab473f 124 }
maetugr 2:03e5f7ab473f 125 Controller[YAW].setIntegrate(armed); // same for YAW
maetugr 2:03e5f7ab473f 126 if (abs(RC_angle[YAW] - IMU.angle[YAW]) > 180) // for YAW a special calculation because of range -180 to 180
maetugr 2:03e5f7ab473f 127 if (RC_angle[YAW] > IMU.angle[YAW])
maetugr 4:b0a60b0b24a9 128 Controller[YAW].compute(RC_angle[YAW] - 360, IMU.angle[YAW], IMU.Sensor.data_gyro[YAW]);
maetugr 2:03e5f7ab473f 129 else
maetugr 4:b0a60b0b24a9 130 Controller[YAW].compute(RC_angle[YAW] + 360, IMU.angle[YAW], IMU.Sensor.data_gyro[YAW]);
maetugr 2:03e5f7ab473f 131 else
maetugr 4:b0a60b0b24a9 132 Controller[YAW].compute(RC_angle[YAW], IMU.angle[YAW], IMU.Sensor.data_gyro[YAW]);
maetugr 0:12950aa67f2a 133
maetugr 0:12950aa67f2a 134 // Mixing
maetugr 5:06e978fd147a 135 Motor_speed[2] = RC[THROTTLE].read() + Controller[PITCH].Value;
maetugr 5:06e978fd147a 136 Motor_speed[0] = RC[THROTTLE].read() - Controller[PITCH].Value;
maetugr 5:06e978fd147a 137 Motor_speed[1] = RC[THROTTLE].read() + Controller[ROLL].Value;
maetugr 5:06e978fd147a 138 Motor_speed[3] = RC[THROTTLE].read() - Controller[ROLL].Value;
maetugr 5:06e978fd147a 139
maetugr 5:06e978fd147a 140 Motor_speed[0] -= Controller[YAW].Value;
maetugr 5:06e978fd147a 141 Motor_speed[2] -= Controller[YAW].Value;
maetugr 5:06e978fd147a 142 Motor_speed[3] += Controller[YAW].Value;
maetugr 5:06e978fd147a 143 Motor_speed[1] += Controller[YAW].Value;
maetugr 5:06e978fd147a 144
maetugr 0:12950aa67f2a 145 if (armed) // for SECURITY!
maetugr 0:12950aa67f2a 146 {
maetugr 5:06e978fd147a 147 ESC[0] = (int)Motor_speed[0];
maetugr 5:06e978fd147a 148 ESC[2] = (int)Motor_speed[2];
maetugr 5:06e978fd147a 149 //for(int i=0;i<4;i++) // Set new motorspeeds
maetugr 5:06e978fd147a 150 //ESC[i] = (int)Motor_speed[i];
maetugr 0:12950aa67f2a 151
maetugr 0:12950aa67f2a 152 } else {
maetugr 0:12950aa67f2a 153 for(int i=0;i<4;i++) // for security reason, set every motor to zero speed
maetugr 0:12950aa67f2a 154 ESC[i] = 0;
maetugr 0:12950aa67f2a 155 }
maetugr 0:12950aa67f2a 156
maetugr 5:06e978fd147a 157 if (debug) {
maetugr 4:b0a60b0b24a9 158 //pc.printf("%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\r\n", IMU.Acc.data[0], IMU.Acc.data[1], IMU.Acc.data[2], D, IMU.angle[PITCH], Controller[PITCH].Value, RC_angle[YAW], IMU.dt);
maetugr 5:06e978fd147a 159 //MAIN OUTPUT pc.printf("%d,%.1f,%.1f,%.1f,%.3f,%.3f,%.3f,%.2f,%.2f\r\n", armed, IMU.angle[ROLL], IMU.angle[PITCH], IMU.angle[YAW], Controller[ROLL].Value, Controller[PITCH].Value, Controller[YAW].Value, P, D); // RC[0].read(), RC[1].read(), RC[2].read(), RC[3].read()
maetugr 3:e277653258ab 160 //pc.printf("%d,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\r\n", armed, P, PY, D, IMU.angle[PITCH], Controller[PITCH].Value, RC_angle[YAW], IMU.dt);
maetugr 3:e277653258ab 161 //pc.printf("%d,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\r\n", armed, P, PY, D, IMU.angle[PITCH], Controller[PITCH].Value, RC_angle[YAW], IMU.dt);
maetugr 5:06e978fd147a 162 //pc.printf("%+.3f,%+.3f,%+.3f,%+.3f,%+.3f,%+.3f,%.5f\r\n", IMU.angle[0], IMU.angle[1], IMU.angle[2], IMU.Sensor.data_gyro[0], IMU.Sensor.data_gyro[1], IMU.Sensor.data_gyro[2], IMU.dt);
maetugr 5:06e978fd147a 163 pc.printf("$STATE,%d,%.3f\r\n", armed, IMU.dt);
maetugr 5:06e978fd147a 164 pc.printf("$RC,%d,%d,%d,%d,%d,%d,%d\r\n", RC[AILERON].read(), RC[ELEVATOR].read(), RC[RUDDER].read(), RC[THROTTLE].read(), RC[CHANNEL6].read(), RC[CHANNEL7].read(), RC[CHANNEL8].read());
maetugr 5:06e978fd147a 165 pc.printf("$GYRO,%.3f,%.3f,%.3f\r\n", IMU.Sensor.data_gyro[ROLL], IMU.Sensor.data_gyro[PITCH], IMU.Sensor.data_gyro[YAW]);
maetugr 5:06e978fd147a 166 pc.printf("$ACC,%.3f,%.3f,%.3f\r\n", IMU.Sensor.data_acc[ROLL], IMU.Sensor.data_acc[PITCH], IMU.Sensor.data_acc[YAW]);
maetugr 5:06e978fd147a 167 pc.printf("$ANG,%.3f,%.3f,%.3f\r\n", IMU.angle[ROLL], IMU.angle[PITCH], IMU.angle[YAW]);
maetugr 5:06e978fd147a 168 pc.printf("$CONT,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\r\n", Controller[ROLL].Value, Controller[PITCH].Value, Controller[YAW].Value, P, I, D);
maetugr 5:06e978fd147a 169 pc.printf("$MOT,%d,%d,%d,%d\r\n", (int)Motor_speed[0], (int)Motor_speed[1], (int)Motor_speed[2], (int)Motor_speed[3]);
maetugr 5:06e978fd147a 170 /*for (int i=0;i<3;i++) {
maetugr 5:06e978fd147a 171 min[i] = IMU.Sensor.data_gyro[i]<min[i] ? IMU.Sensor.data_gyro[i] : min[i];
maetugr 5:06e978fd147a 172 max[i] = IMU.Sensor.data_gyro[i]>max[i] ? IMU.Sensor.data_gyro[i] : max[i];
maetugr 5:06e978fd147a 173 }*/
maetugr 5:06e978fd147a 174 //pc.printf("%.5f\r\n", IMU.dt);
maetugr 5:06e978fd147a 175 //pc.printf("%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n", IMU.Sensor.raw_gyro[ROLL], IMU.Sensor.raw_gyro[PITCH], IMU.Sensor.raw_gyro[YAW], min[0], min[1], min[2], max[0], max[1], max[2]);
maetugr 5:06e978fd147a 176 //pc.printf("%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f,%.3f\r\n", IMU.Sensor.data_gyro[ROLL], IMU.Sensor.data_gyro[PITCH], IMU.Sensor.data_gyro[YAW], min[0], min[1], min[2], max[0], max[1], max[2]);
maetugr 5:06e978fd147a 177 wait(0.04);
maetugr 5:06e978fd147a 178 }
maetugr 0:12950aa67f2a 179
maetugr 0:12950aa67f2a 180 LEDs.rollnext();
maetugr 0:12950aa67f2a 181 }
maetugr 0:12950aa67f2a 182 }