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:
Thu Feb 19 12:20:06 2015 +0000
Revision:
3:5f43c8374ff2
Parent:
2:3161f535d71a
Child:
4:eb418af66d81
finally looks like it gives sensible ritchDiff and rollDiff values. I will have to check what does it give for the actual speed values too.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
moklumbys 3:5f43c8374ff2 1
moklumbys 3:5f43c8374ff2 2 //firstly enable calibrating
moklumbys 0:894ba50f267c 3 #include "mbed.h"
moklumbys 0:894ba50f267c 4 #include "Quadcopter.h"
moklumbys 1:40ade596b1e3 5 #include "PID.h"
moklumbys 1:40ade596b1e3 6 #include "MPU6050.h"
moklumbys 1:40ade596b1e3 7 #include "Timer.h"
moklumbys 1:40ade596b1e3 8
moklumbys 1:40ade596b1e3 9 #define MAX_CALIBRATE 1.0
moklumbys 1:40ade596b1e3 10 #define MIN_CALIBRATE 0.35
moklumbys 1:40ade596b1e3 11
moklumbys 1:40ade596b1e3 12 #define OFFSET_SAMPLES 50
moklumbys 1:40ade596b1e3 13 //define how the accelerometer is placed on surface
moklumbys 1:40ade596b1e3 14 #define X_AXIS 1
moklumbys 1:40ade596b1e3 15 #define Y_AXIS 2
moklumbys 1:40ade596b1e3 16 #define Z_AXIS 0
moklumbys 0:894ba50f267c 17
moklumbys 0:894ba50f267c 18 #define MAX_CALIBRATE 1.0
moklumbys 0:894ba50f267c 19 #define MIN_CALIBRATE 0.35
moklumbys 0:894ba50f267c 20
moklumbys 0:894ba50f267c 21 #define FL 0 // Front left
moklumbys 0:894ba50f267c 22 #define FR 1 // Front right
moklumbys 0:894ba50f267c 23 #define BL 2 // back left
moklumbys 0:894ba50f267c 24 #define BR 3 // back right
moklumbys 1:40ade596b1e3 25
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 1:40ade596b1e3 31 #define ROLL_IN_MIN -90.0
moklumbys 1:40ade596b1e3 32 #define ROLL_IN_MAX 90.0
moklumbys 3:5f43c8374ff2 33 #define ROLL_OUT_MIN -0.3
moklumbys 3:5f43c8374ff2 34 #define ROLL_OUT_MAX 0.3
moklumbys 1:40ade596b1e3 35
moklumbys 3:5f43c8374ff2 36 #define Kc 0.5
moklumbys 3:5f43c8374ff2 37 #define Ti 0.01
moklumbys 3:5f43c8374ff2 38 #define Td 0.00
moklumbys 2:3161f535d71a 39 #define RATE 0.01
moklumbys 0:894ba50f267c 40 //--------------------------------ALL THE FUNCTION HEADERS-----------------------
moklumbys 0:894ba50f267c 41 float map(float x, float in_min, float in_max, float out_min, float out_max);
moklumbys 0:894ba50f267c 42 //---------------------------------------END-------------------------------------
moklumbys 0:894ba50f267c 43
moklumbys 0:894ba50f267c 44 Quadcopter quad (p21, p22, p23, p24);
moklumbys 0:894ba50f267c 45 Serial pc(USBTX, USBRX); // tx, rx
moklumbys 1:40ade596b1e3 46 MPU6050 mpu(p9, p10); //Also disables sleep mode
moklumbys 1:40ade596b1e3 47 Timer timer;
moklumbys 1:40ade596b1e3 48
moklumbys 1:40ade596b1e3 49 //Kc, Ti, Td, interval
moklumbys 1:40ade596b1e3 50 PID pitchPID (Kc, Ti, Td, RATE);
moklumbys 1:40ade596b1e3 51 PID rollPID (Kc, Ti, Td, RATE);
moklumbys 0:894ba50f267c 52
moklumbys 2:3161f535d71a 53 //***************************************STARTING MAIN FUNCTION*********************
moklumbys 0:894ba50f267c 54 int main() {
moklumbys 1:40ade596b1e3 55 pc.baud (115200);
moklumbys 1:40ade596b1e3 56
moklumbys 0:894ba50f267c 57 float pitchDiff;
moklumbys 0:894ba50f267c 58 float rollDiff;
moklumbys 1:40ade596b1e3 59
moklumbys 0:894ba50f267c 60 float speed[4];
moklumbys 0:894ba50f267c 61 float actSpeed[4];
moklumbys 0:894ba50f267c 62
moklumbys 1:40ade596b1e3 63 float accOffset[3]; //offset values
moklumbys 1:40ade596b1e3 64 float gyroOffset[3];
moklumbys 1:40ade596b1e3 65 float angle[3]; //total calculated angle
moklumbys 1:40ade596b1e3 66
moklumbys 1:40ade596b1e3 67 float currTime;
moklumbys 1:40ade596b1e3 68 float prevTime;
moklumbys 1:40ade596b1e3 69
moklumbys 1:40ade596b1e3 70 if (mpu.testConnection()) //just testing if things are working...
moklumbys 1:40ade596b1e3 71 pc.printf("MPU connection succeeded\n\r");
moklumbys 1:40ade596b1e3 72 else
moklumbys 1:40ade596b1e3 73 pc.printf("MPU connection failed\n\r");
moklumbys 1:40ade596b1e3 74
moklumbys 1:40ade596b1e3 75 mpu.setAlpha(0.97); //set Alpha coefficient for low/high pass filters
moklumbys 1:40ade596b1e3 76
moklumbys 3:5f43c8374ff2 77 // quad.calibrate(MIN_CALIBRATE, MAX_CALIBRATE); //calibrating motors
moklumbys 1:40ade596b1e3 78
moklumbys 1:40ade596b1e3 79 pitchPID.setInputLimits (PITCH_IN_MIN, PITCH_IN_MAX); //seting input and output limits for both pitch and roll
moklumbys 1:40ade596b1e3 80 pitchPID.setOutputLimits (PITCH_OUT_MIN, PITCH_OUT_MAX);
moklumbys 1:40ade596b1e3 81
moklumbys 1:40ade596b1e3 82 rollPID.setInputLimits (ROLL_IN_MIN, ROLL_IN_MAX);
moklumbys 1:40ade596b1e3 83 rollPID.setOutputLimits (ROLL_OUT_MIN, ROLL_OUT_MAX);
moklumbys 1:40ade596b1e3 84
moklumbys 1:40ade596b1e3 85 pitchPID.setMode(AUTO_MODE); //start stabilising by puting AUTO mode
moklumbys 1:40ade596b1e3 86 rollPID.setMode(AUTO_MODE);
moklumbys 1:40ade596b1e3 87
moklumbys 2:3161f535d71a 88 //need to vary this one to move quadcopter
moklumbys 1:40ade596b1e3 89 pitchPID.setSetPoint (0.0); //seting the middle point.. Or smth like that, need to look into it more
moklumbys 2:3161f535d71a 90 rollPID.setSetPoint (0.0); //meaning that quadcopter is flying at a constant place.. no turning, blah blah blah
moklumbys 1:40ade596b1e3 91
moklumbys 1:40ade596b1e3 92 mpu.getOffset(accOffset, gyroOffset, OFFSET_SAMPLES); //take some samples at the beginning to get an offset
moklumbys 1:40ade596b1e3 93 wait(0.1); //wait to settle down
moklumbys 1:40ade596b1e3 94
moklumbys 1:40ade596b1e3 95 timer.start(); //will need timer to detect when was the last time the values were updated
moklumbys 1:40ade596b1e3 96 prevTime = timer.read();
moklumbys 1:40ade596b1e3 97
moklumbys 2:3161f535d71a 98 for (int i = 0; i < 4; i++){
moklumbys 2:3161f535d71a 99 speed[i] = 0.0;
moklumbys 2:3161f535d71a 100 }
moklumbys 2:3161f535d71a 101 //-------------------------------------------START INFINITE LOOP-------------------------------------------------
moklumbys 3:5f43c8374ff2 102 pitchPID.setSetPoint (45.0); //head forward! by 45 degrees :D
moklumbys 0:894ba50f267c 103 while(1) {
moklumbys 1:40ade596b1e3 104 // for (float height = 0.0; height < 0.5; height+=0.05){
moklumbys 1:40ade596b1e3 105 // for (int i = 0; i < 4; i++){
moklumbys 1:40ade596b1e3 106 // speed[i] = height;
moklumbys 1:40ade596b1e3 107 // }
moklumbys 1:40ade596b1e3 108 // quad.run (speed);
moklumbys 1:40ade596b1e3 109 // wait(0.1);
moklumbys 1:40ade596b1e3 110 // }
moklumbys 1:40ade596b1e3 111 // for (uint16_t i = 0; i < 600; i++)
moklumbys 1:40ade596b1e3 112 // {
moklumbys 2:3161f535d71a 113 currTime = timer.read(); //get present time
moklumbys 2:3161f535d71a 114 mpu.computeAngle (angle, accOffset, gyroOffset, &currTime, &prevTime); // get angle using all these values
moklumbys 2:3161f535d71a 115
moklumbys 2:3161f535d71a 116 rollPID.setInterval(timer.read()-prevTime); //need to change the interval because don't know how much time passed
moklumbys 2:3161f535d71a 117 pitchPID.setInterval(timer.read()-prevTime);
moklumbys 2:3161f535d71a 118
moklumbys 2:3161f535d71a 119 prevTime = timer.read(); //get present time -> will be used later on as previous value
moklumbys 0:894ba50f267c 120
moklumbys 2:3161f535d71a 121 rollPID.setProcessValue (angle[X_AXIS]); //take some values to do processing
moklumbys 1:40ade596b1e3 122 pitchPID.setProcessValue (angle[Y_AXIS]);
moklumbys 1:40ade596b1e3 123
moklumbys 2:3161f535d71a 124 pitchDiff = pitchPID.compute(); //compute the difference
moklumbys 1:40ade596b1e3 125 rollDiff = rollPID.compute();
moklumbys 3:5f43c8374ff2 126 pc.printf ("pitchDiff=%0.4f, rollDiff=%0.4f\n", pitchDiff, rollDiff);
moklumbys 2:3161f535d71a 127 quad.stabilise(speed, actSpeed, rollDiff, pitchDiff); //stabilise the speed by giving new actSpeed value
moklumbys 1:40ade596b1e3 128
moklumbys 2:3161f535d71a 129 //print some values to check how thing work out
moklumbys 3:5f43c8374ff2 130 // pc.printf("x=%0.3f y=%0.3f z=%0.3f\n", angle[X_AXIS], angle[Y_AXIS], angle[Z_AXIS]);
moklumbys 3:5f43c8374ff2 131 // 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 132 // 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 133
moklumbys 2:3161f535d71a 134 // quad.run(actSpeed); //run the motors at the spesified speed actSpeed
moklumbys 0:894ba50f267c 135
moklumbys 0:894ba50f267c 136 wait (0.01);
moklumbys 1:40ade596b1e3 137 // }
moklumbys 0:894ba50f267c 138
moklumbys 1:40ade596b1e3 139 // for (float height = 0.5; height > 0.0; height-=0.05){
moklumbys 1:40ade596b1e3 140 // for (int i = 0; i < 4; i++){
moklumbys 1:40ade596b1e3 141 // speed[i] = height;
moklumbys 1:40ade596b1e3 142 // }
moklumbys 1:40ade596b1e3 143 // quad.run (speed);
moklumbys 1:40ade596b1e3 144 // wait(0.1);
moklumbys 1:40ade596b1e3 145 // }
moklumbys 1:40ade596b1e3 146 // wait(1);
moklumbys 0:894ba50f267c 147 }
moklumbys 0:894ba50f267c 148 }
moklumbys 2:3161f535d71a 149 //************************************************END MAIN FUNCTION********************************************************
moklumbys 0:894ba50f267c 150
moklumbys 0:894ba50f267c 151 //-----------------------------Mapping function-----------------------------
moklumbys 1:40ade596b1e3 152 float map(float x, float in_min, float in_max, float out_min, float out_max){
moklumbys 0:894ba50f267c 153 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
moklumbys 0:894ba50f267c 154 }