A library used for controlling a quadcopter. It provides an easy to use interface, which allows to mount, calibrate and run motors. It is also able to calibrate the actual speed according to calculated PID roll & pitch difference. I used the original Servo library as ESC modules use the same PWM signal as Servo motors.

Dependents:   QuadcopterProgram

Committer:
moklumbys
Date:
Fri Feb 20 00:48:15 2015 +0000
Revision:
6:a6b8e508643d
Parent:
5:2aa78a442132
Child:
7:5ab77c583ae3
added documentation and example code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
moklumbys 0:341a08dbf9ba 1 #ifndef QUADCOPTER_H
moklumbys 0:341a08dbf9ba 2 #define QUADCOPTER_H
moklumbys 0:341a08dbf9ba 3
moklumbys 0:341a08dbf9ba 4 #include "mbed.h"
moklumbys 0:341a08dbf9ba 5 #include "Servo.h"
moklumbys 0:341a08dbf9ba 6
moklumbys 3:84d246dccb8d 7 //class used for creating a user friendly interface for controlling quadcopter motors
moklumbys 6:a6b8e508643d 8 /**
moklumbys 6:a6b8e508643d 9 *
moklumbys 6:a6b8e508643d 10 *
moklumbys 6:a6b8e508643d 11 * Example:
moklumbys 6:a6b8e508643d 12 * @code
moklumbys 6:a6b8e508643d 13 * #include "mbed.h"
moklumbys 6:a6b8e508643d 14 *
moklumbys 6:a6b8e508643d 15 * Quadcopter quad (p21, p22, p23, p24); //intance of the Quadcopter class
moklumbys 6:a6b8e508643d 16 *
moklumbys 6:a6b8e508643d 17 * int main(){
moklumbys 6:a6b8e508643d 18 *
moklumbys 6:a6b8e508643d 19 * float speed[4];
moklumbys 6:a6b8e508643d 20 * for (int i = 0; i < 4; i++){ //initialise speed to be 0.2
moklumbys 6:a6b8e508643d 21 * speed[i] = 0.2;
moklumbys 6:a6b8e508643d 22 * }
moklumbys 6:a6b8e508643d 23 *
moklumbys 6:a6b8e508643d 24 * while(1){
moklumbys 6:a6b8e508643d 25 * quad.run(speed); //run
moklumbys 6:a6b8e508643d 26 * }
moklumbys 6:a6b8e508643d 27 * }
moklumbys 6:a6b8e508643d 28 * @endcode
moklumbys 6:a6b8e508643d 29 *
moklumbys 6:a6b8e508643d 30 */
moklumbys 6:a6b8e508643d 31
moklumbys 3:84d246dccb8d 32
moklumbys 0:341a08dbf9ba 33 class Quadcopter {
moklumbys 0:341a08dbf9ba 34 public:
moklumbys 5:2aa78a442132 35 /**
moklumbys 5:2aa78a442132 36 * Constructor.
moklumbys 5:2aa78a442132 37 *
moklumbys 5:2aa78a442132 38 * @param FL - Front Left motor.
moklumbys 5:2aa78a442132 39 * @param FR - Front Right motor.
moklumbys 5:2aa78a442132 40 * @param BL - Back Left motor.
moklumbys 5:2aa78a442132 41 * @param BR - Back Right motor.
moklumbys 5:2aa78a442132 42 *
moklumbys 5:2aa78a442132 43 */
moklumbys 0:341a08dbf9ba 44 Quadcopter(PinName FL, PinName FR, PinName BL, PinName BR);
moklumbys 3:84d246dccb8d 45
moklumbys 5:2aa78a442132 46 /**
moklumbys 5:2aa78a442132 47 * Function used to calibrate all 4 ESC before actually flying the quadcopter.
moklumbys 5:2aa78a442132 48 * It does not matter which of inputs is min and which is max as function checks that itself.
moklumbys 5:2aa78a442132 49 * If inputs are out of boundaries, min value becomes 0.0, and max value becomes 1.0 automatically
moklumbys 5:2aa78a442132 50 *
moklumbys 5:2aa78a442132 51 * @param min - minimum value for the ESC in range from 0.0 to 1.0.
moklumbys 5:2aa78a442132 52 * @param max - maximum value for the ESC in range from 0.0 to 1.0.
moklumbys 5:2aa78a442132 53 */
moklumbys 0:341a08dbf9ba 54 void calibrate (float min, float max);
moklumbys 3:84d246dccb8d 55
moklumbys 5:2aa78a442132 56 /**
moklumbys 5:2aa78a442132 57 * function for runing motors.
moklumbys 5:2aa78a442132 58 *
moklumbys 5:2aa78a442132 59 * @param speed - array of 4 variables, which corresponds to the speed for all 4 motors. speeds are in range from 0.0 to 1.0.
moklumbys 5:2aa78a442132 60 */
moklumbys 0:341a08dbf9ba 61 void run (float* speed);
moklumbys 3:84d246dccb8d 62
moklumbys 5:2aa78a442132 63 /**
moklumbys 5:2aa78a442132 64 * Function used to calculate the speed at which each of the motors should run to be able to stabilise the quadcopter.
moklumbys 5:2aa78a442132 65 *
moklumbys 5:2aa78a442132 66 * @param speed - current speed for each motor.
moklumbys 5:2aa78a442132 67 * @param actSpeed - actual speed at which motors should run
moklumbys 5:2aa78a442132 68 * @param rollDiff - calculated using PID library function PID::compute();
moklumbys 5:2aa78a442132 69 * @param pitchDiff - calculated using PID library function PID::compute();
moklumbys 5:2aa78a442132 70 *
moklumbys 5:2aa78a442132 71 */
moklumbys 0:341a08dbf9ba 72 void stabilise (float* speed, float* actSpeed, float rollDiff, float pitchDiff);
moklumbys 3:84d246dccb8d 73
moklumbys 0:341a08dbf9ba 74 private:
moklumbys 3:84d246dccb8d 75
moklumbys 3:84d246dccb8d 76 float min_calibrate; //min value at which each motor is calibrated
moklumbys 4:90ce5817667b 77 float max_calibrate; //max value ...
moklumbys 3:84d246dccb8d 78 Servo* motor[4]; //motors are used with Servo library as ESC take the same input as usual Servo motors...
moklumbys 3:84d246dccb8d 79 float map(float x, float in_min, float in_max, float out_min, float out_max); //function for mapping values in the range from min calibrate to max_calibrate
moklumbys 0:341a08dbf9ba 80 };
moklumbys 0:341a08dbf9ba 81
moklumbys 0:341a08dbf9ba 82 #endif