Program used to control a quadcopter. It uses a PID library which can be found in: http://developer.mbed.org/cookbook/PID I also uses my own written library for easily controlling quadcopter motors, which can be found in: https://developer.mbed.org/users/moklumbys/code/Quadcopter/ One more library that I used is MPU6050 that was previously written by Erik Olieman but I was able to update it with new functions: https://developer.mbed.org/users/moklumbys/code/MPU6050/

Dependencies:   MPU6050 PID Quadcopter Servo mbed

Committer:
moklumbys
Date:
Fri Feb 20 00:52:54 2015 +0000
Revision:
5:8b3f82abe3a4
Parent:
4:eb418af66d81
Child:
6:5e815d4b4d8f
All functions should work now.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
moklumbys 0:894ba50f267c 1 #include "mbed.h"
moklumbys 0:894ba50f267c 2 #include "Quadcopter.h"
moklumbys 1:40ade596b1e3 3 #include "PID.h"
moklumbys 1:40ade596b1e3 4 #include "MPU6050.h"
moklumbys 1:40ade596b1e3 5 #include "Timer.h"
moklumbys 1:40ade596b1e3 6
moklumbys 4:eb418af66d81 7 //defines the number of samples to be taken when calculating the offset for gyro and accelerometer
moklumbys 4:eb418af66d81 8 #define OFFSET_SAMPLES 50
moklumbys 1:40ade596b1e3 9
moklumbys 1:40ade596b1e3 10 //define how the accelerometer is placed on surface
moklumbys 1:40ade596b1e3 11 #define X_AXIS 1
moklumbys 1:40ade596b1e3 12 #define Y_AXIS 2
moklumbys 1:40ade596b1e3 13 #define Z_AXIS 0
moklumbys 0:894ba50f267c 14
moklumbys 4:eb418af66d81 15 //ESC calibration values
moklumbys 0:894ba50f267c 16 #define MAX_CALIBRATE 1.0
moklumbys 0:894ba50f267c 17 #define MIN_CALIBRATE 0.35
moklumbys 0:894ba50f267c 18
moklumbys 4:eb418af66d81 19 //Just to remember which motor corresponds to which number...
moklumbys 0:894ba50f267c 20 #define FL 0 // Front left
moklumbys 0:894ba50f267c 21 #define FR 1 // Front right
moklumbys 0:894ba50f267c 22 #define BL 2 // back left
moklumbys 0:894ba50f267c 23 #define BR 3 // back right
moklumbys 1:40ade596b1e3 24
moklumbys 4:eb418af66d81 25 //input and output values for pitch
moklumbys 1:40ade596b1e3 26 #define PITCH_IN_MIN -90.0
moklumbys 1:40ade596b1e3 27 #define PITCH_IN_MAX 90.0
moklumbys 3:5f43c8374ff2 28 #define PITCH_OUT_MIN -0.3
moklumbys 3:5f43c8374ff2 29 #define PITCH_OUT_MAX 0.3
moklumbys 1:40ade596b1e3 30
moklumbys 4:eb418af66d81 31 //input and output values for roll
moklumbys 1:40ade596b1e3 32 #define ROLL_IN_MIN -90.0
moklumbys 1:40ade596b1e3 33 #define ROLL_IN_MAX 90.0
moklumbys 3:5f43c8374ff2 34 #define ROLL_OUT_MIN -0.3
moklumbys 3:5f43c8374ff2 35 #define ROLL_OUT_MAX 0.3
moklumbys 1:40ade596b1e3 36
moklumbys 4:eb418af66d81 37 //PID intervals/constants
moklumbys 3:5f43c8374ff2 38 #define Kc 0.5
moklumbys 3:5f43c8374ff2 39 #define Ti 0.01
moklumbys 3:5f43c8374ff2 40 #define Td 0.00
moklumbys 2:3161f535d71a 41 #define RATE 0.01
moklumbys 4:eb418af66d81 42
moklumbys 0:894ba50f267c 43 //--------------------------------ALL THE FUNCTION HEADERS-----------------------
moklumbys 4:eb418af66d81 44 float map(float x, float in_min, float in_max, float out_min, float out_max); //might be a useful function. One is used inside Quadcopter library though
moklumbys 0:894ba50f267c 45 //---------------------------------------END-------------------------------------
moklumbys 0:894ba50f267c 46
moklumbys 4:eb418af66d81 47 Quadcopter quad (p21, p22, p23, p24); //intance of the Quadcopter class
moklumbys 4:eb418af66d81 48 Serial pc(USBTX, USBRX); // tx, rx
moklumbys 4:eb418af66d81 49 MPU6050 mpu(p9, p10); //Also disables sleep mode
moklumbys 4:eb418af66d81 50 Timer timer; //need a timer to tell how much time passed from the last calculation
moklumbys 1:40ade596b1e3 51
moklumbys 4:eb418af66d81 52 //put Kc, Ti, Td, interval for both pitch and roll PID models
moklumbys 1:40ade596b1e3 53 PID pitchPID (Kc, Ti, Td, RATE);
moklumbys 1:40ade596b1e3 54 PID rollPID (Kc, Ti, Td, RATE);
moklumbys 0:894ba50f267c 55
moklumbys 2:3161f535d71a 56 //***************************************STARTING MAIN FUNCTION*********************
moklumbys 0:894ba50f267c 57 int main() {
moklumbys 4:eb418af66d81 58 pc.baud (115200); //fast transmition speed...
moklumbys 1:40ade596b1e3 59
moklumbys 4:eb418af66d81 60 float pitchDiff; //difference in pitch. Explained in PID library...
moklumbys 4:eb418af66d81 61 float rollDiff; //diference in roll
moklumbys 1:40ade596b1e3 62
moklumbys 4:eb418af66d81 63 float speed[4]; //speed for motors
moklumbys 4:eb418af66d81 64 float actSpeed[4]; //actual speed of for all motors
moklumbys 0:894ba50f267c 65
moklumbys 1:40ade596b1e3 66 float accOffset[3]; //offset values
moklumbys 1:40ade596b1e3 67 float gyroOffset[3];
moklumbys 1:40ade596b1e3 68 float angle[3]; //total calculated angle
moklumbys 1:40ade596b1e3 69
moklumbys 4:eb418af66d81 70 float prevTime; //previous time values will be given in the function
moklumbys 1:40ade596b1e3 71
moklumbys 1:40ade596b1e3 72 if (mpu.testConnection()) //just testing if things are working...
moklumbys 1:40ade596b1e3 73 pc.printf("MPU connection succeeded\n\r");
moklumbys 1:40ade596b1e3 74 else
moklumbys 1:40ade596b1e3 75 pc.printf("MPU connection failed\n\r");
moklumbys 1:40ade596b1e3 76
moklumbys 1:40ade596b1e3 77 mpu.setAlpha(0.97); //set Alpha coefficient for low/high pass filters
moklumbys 1:40ade596b1e3 78
moklumbys 4:eb418af66d81 79 quad.calibrate(MIN_CALIBRATE, MAX_CALIBRATE); //calibrating motors
moklumbys 1:40ade596b1e3 80
moklumbys 1:40ade596b1e3 81 pitchPID.setInputLimits (PITCH_IN_MIN, PITCH_IN_MAX); //seting input and output limits for both pitch and roll
moklumbys 1:40ade596b1e3 82 pitchPID.setOutputLimits (PITCH_OUT_MIN, PITCH_OUT_MAX);
moklumbys 1:40ade596b1e3 83
moklumbys 1:40ade596b1e3 84 rollPID.setInputLimits (ROLL_IN_MIN, ROLL_IN_MAX);
moklumbys 1:40ade596b1e3 85 rollPID.setOutputLimits (ROLL_OUT_MIN, ROLL_OUT_MAX);
moklumbys 1:40ade596b1e3 86
moklumbys 1:40ade596b1e3 87 pitchPID.setMode(AUTO_MODE); //start stabilising by puting AUTO mode
moklumbys 1:40ade596b1e3 88 rollPID.setMode(AUTO_MODE);
moklumbys 1:40ade596b1e3 89
moklumbys 2:3161f535d71a 90 //need to vary this one to move quadcopter
moklumbys 4:eb418af66d81 91 pitchPID.setSetPoint (0.0); //seting the middle point meaning that quadcopter is balancing in one place
moklumbys 4:eb418af66d81 92 rollPID.setSetPoint (0.0);
moklumbys 1:40ade596b1e3 93
moklumbys 1:40ade596b1e3 94 mpu.getOffset(accOffset, gyroOffset, OFFSET_SAMPLES); //take some samples at the beginning to get an offset
moklumbys 4:eb418af66d81 95 wait(0.1); //wait to settle down
moklumbys 1:40ade596b1e3 96
moklumbys 4:eb418af66d81 97 timer.start(); //will need timer to detect when was the last time the values were updated
moklumbys 4:eb418af66d81 98 prevTime = timer.read(); //set previous timer value
moklumbys 1:40ade596b1e3 99
moklumbys 4:eb418af66d81 100 for (int i = 0; i < 4; i++){ //initialise speed to be 0.0
moklumbys 2:3161f535d71a 101 speed[i] = 0.0;
moklumbys 2:3161f535d71a 102 }
moklumbys 4:eb418af66d81 103
moklumbys 4:eb418af66d81 104 // for (int i = 0; i < 4; i++){ //start running motors ar 20% jus to make sure everything works fine
moklumbys 4:eb418af66d81 105 // speed[i] = 0.2;
moklumbys 4:eb418af66d81 106 // }
moklumbys 4:eb418af66d81 107 // quad.run (speed);
moklumbys 2:3161f535d71a 108 //-------------------------------------------START INFINITE LOOP-------------------------------------------------
moklumbys 0:894ba50f267c 109 while(1) {
moklumbys 5:8b3f82abe3a4 110 mpu.computeAngle (angle, accOffset, gyroOffset, timer.read()-prevTime); // get angle using all these values
moklumbys 2:3161f535d71a 111
moklumbys 2:3161f535d71a 112 rollPID.setInterval(timer.read()-prevTime); //need to change the interval because don't know how much time passed
moklumbys 2:3161f535d71a 113 pitchPID.setInterval(timer.read()-prevTime);
moklumbys 2:3161f535d71a 114
moklumbys 2:3161f535d71a 115 prevTime = timer.read(); //get present time -> will be used later on as previous value
moklumbys 0:894ba50f267c 116
moklumbys 4:eb418af66d81 117 rollPID.setProcessValue (angle[X_AXIS]); //take angle values, which correspond to pitch and roll and do processing
moklumbys 1:40ade596b1e3 118 pitchPID.setProcessValue (angle[Y_AXIS]);
moklumbys 1:40ade596b1e3 119
moklumbys 2:3161f535d71a 120 pitchDiff = pitchPID.compute(); //compute the difference
moklumbys 1:40ade596b1e3 121 rollDiff = rollPID.compute();
moklumbys 4:eb418af66d81 122
moklumbys 4:eb418af66d81 123 pc.printf ("pitchDiff=%0.4f, rollDiff=%0.4f\n", pitchDiff, rollDiff);
moklumbys 4:eb418af66d81 124 quad.stabilise(speed, actSpeed, rollDiff, pitchDiff); //stabilise the speed by giving out actual Speed value
moklumbys 1:40ade596b1e3 125
moklumbys 2:3161f535d71a 126 //print some values to check how thing work out
moklumbys 3:5f43c8374ff2 127 // pc.printf("x=%0.3f y=%0.3f z=%0.3f\n", angle[X_AXIS], angle[Y_AXIS], angle[Z_AXIS]);
moklumbys 3:5f43c8374ff2 128 // pc.printf("Speed_FL=%0.4f, Speed_FR=%0.4f, Speed_BL= %0.4f, Speed_BR=%0.4f\n", speed[FL], speed[FR], speed[BL], speed[BR]);
moklumbys 3:5f43c8374ff2 129 // pc.printf("ActSpeed_FL=%0.4f, ActSpeed_FR=%0.4f, ActSpeed_BL=%0.4f, ActSpeed_BR=%0.4f\n", actSpeed[FL], actSpeed[FR], actSpeed[BL], actSpeed[BR]);
moklumbys 1:40ade596b1e3 130
moklumbys 4:eb418af66d81 131 quad.run(actSpeed); //run the motors at the spesified speed actSpeed
moklumbys 0:894ba50f267c 132 wait (0.01);
moklumbys 0:894ba50f267c 133 }
moklumbys 0:894ba50f267c 134 }
moklumbys 2:3161f535d71a 135 //************************************************END MAIN FUNCTION********************************************************
moklumbys 0:894ba50f267c 136
moklumbys 0:894ba50f267c 137 //-----------------------------Mapping function-----------------------------
moklumbys 1:40ade596b1e3 138 float map(float x, float in_min, float in_max, float out_min, float out_max){
moklumbys 0:894ba50f267c 139 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
moklumbys 0:894ba50f267c 140 }