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
Diff: main.cpp
- Revision:
- 8:56bdb0d7002b
- Parent:
- 7:0c31ccba7b3c
- Child:
- 9:588b1618c710
--- a/main.cpp Wed Feb 25 10:38:30 2015 +0000 +++ b/main.cpp Sun Mar 01 22:45:31 2015 +0000 @@ -25,14 +25,14 @@ //input and output values for pitch #define PITCH_IN_MIN -90.0 #define PITCH_IN_MAX 90.0 -#define PITCH_OUT_MIN -0.2 -#define PITCH_OUT_MAX 0.2 +#define PITCH_OUT_MIN -0.1 +#define PITCH_OUT_MAX 0.1 //input and output values for roll #define ROLL_IN_MIN -90.0 #define ROLL_IN_MAX 90.0 -#define ROLL_OUT_MIN -0.2 -#define ROLL_OUT_MAX 0.2 +#define ROLL_OUT_MIN -0.1 +#define ROLL_OUT_MAX 0.1 //PID intervals/constants #define Kc 1.0 @@ -82,7 +82,23 @@ mpu.setAlpha(0.97); //set Alpha coefficient for low/high pass filters quad.setLimits(MIN_CALIBRATE, MAX_CALIBRATE); //set calibration limits for the system - quad.calibrate(); //calibrating motors + + while(1){ + if (!bt.readable()){ + if (!bt.scanf("%f", &val)){ + bt.scanf("%c", &buff); + } else { + if (val == 'c'){ + pc.printf ("Calibrating motors.\n"); + quad.calibrate(); //calibrating motors + } + if (val == 's'){ + pc.printf ("Skipping calibration.\n"); + break; + } + } + } + } pitchPID.setInputLimits (PITCH_IN_MIN, PITCH_IN_MAX); //seting input and output limits for both pitch and roll pitchPID.setOutputLimits (PITCH_OUT_MIN, PITCH_OUT_MAX); @@ -105,6 +121,11 @@ timer.start(); //will need timer to detect when was the last time the values were updated prevTime = timer.read(); //set previous timer value + + for (int i = 0; i < 4; i++){ //initialise speed to be 0.3 + speed[i] = 0.2; + } + quad.run (speed); //-------------------------------------------START INFINITE LOOP------------------------------------------------- while(1) { if(bt.readable()){ @@ -115,22 +136,7 @@ rollPID.setTunings(val, 0.0, 0.0); pc.printf ("______________________SET______________________\n"); } -// if (buff == 'k'){ -// bt.scanf("%f", &val); -// pitchPID.setTunings(val, 0.0, 0.0); -// rollPID.setTunings(val, 0.0, 0.0); -// } -// if (buff == 's'){ -// bt.scanf("%f", &val); -// spd = val; -// } - // } } - - for (int i = 0; i < 4; i++){ //initialise speed to be 0.0 - speed[i] = 0.3; - } - quad.run (speed); mpu.computeAngle (angle, accOffset, gyroOffset, timer.read()-prevTime); // get angle using all these values