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.
Quadcopter.h@7:5ab77c583ae3, 2015-02-20 (annotated)
- Committer:
- moklumbys
- Date:
- Fri Feb 20 00:51:21 2015 +0000
- Revision:
- 7:5ab77c583ae3
- Parent:
- 6:a6b8e508643d
- Child:
- 9:22c52af13ac2
corrected errors in documentation.
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:5ab77c583ae3 | 14 | * #include "Quadcopter.h" |
moklumbys | 6:a6b8e508643d | 15 | * Quadcopter quad (p21, p22, p23, p24); //intance of the Quadcopter class |
moklumbys | 6:a6b8e508643d | 16 | * |
moklumbys | 7:5ab77c583ae3 | 17 | * #define SAMPLE_NUM 50 |
moklumbys | 7:5ab77c583ae3 | 18 | * #define MIN_CALIBRATE 0.3 |
moklumbys | 7:5ab77c583ae3 | 19 | * #define MAX_CALIBRATE 1.0 |
moklumbys | 7:5ab77c583ae3 | 20 | * |
moklumbys | 6:a6b8e508643d | 21 | * int main(){ |
moklumbys | 7:5ab77c583ae3 | 22 | * quad.calibrate (MIN_CALIBRATE, MAX_CALIBRATE, SAMPLE_NUM); |
moklumbys | 7:5ab77c583ae3 | 23 | * |
moklumbys | 6:a6b8e508643d | 24 | * float speed[4]; |
moklumbys | 6:a6b8e508643d | 25 | * for (int i = 0; i < 4; i++){ //initialise speed to be 0.2 |
moklumbys | 6:a6b8e508643d | 26 | * speed[i] = 0.2; |
moklumbys | 6:a6b8e508643d | 27 | * } |
moklumbys | 6:a6b8e508643d | 28 | * |
moklumbys | 6:a6b8e508643d | 29 | * while(1){ |
moklumbys | 6:a6b8e508643d | 30 | * quad.run(speed); //run |
moklumbys | 6:a6b8e508643d | 31 | * } |
moklumbys | 6:a6b8e508643d | 32 | * } |
moklumbys | 6:a6b8e508643d | 33 | * @endcode |
moklumbys | 6:a6b8e508643d | 34 | * |
moklumbys | 6:a6b8e508643d | 35 | */ |
moklumbys | 6:a6b8e508643d | 36 | |
moklumbys | 3:84d246dccb8d | 37 | |
moklumbys | 0:341a08dbf9ba | 38 | class Quadcopter { |
moklumbys | 0:341a08dbf9ba | 39 | public: |
moklumbys | 5:2aa78a442132 | 40 | /** |
moklumbys | 5:2aa78a442132 | 41 | * Constructor. |
moklumbys | 5:2aa78a442132 | 42 | * |
moklumbys | 5:2aa78a442132 | 43 | * @param FL - Front Left motor. |
moklumbys | 5:2aa78a442132 | 44 | * @param FR - Front Right motor. |
moklumbys | 5:2aa78a442132 | 45 | * @param BL - Back Left motor. |
moklumbys | 5:2aa78a442132 | 46 | * @param BR - Back Right motor. |
moklumbys | 5:2aa78a442132 | 47 | * |
moklumbys | 5:2aa78a442132 | 48 | */ |
moklumbys | 0:341a08dbf9ba | 49 | Quadcopter(PinName FL, PinName FR, PinName BL, PinName BR); |
moklumbys | 3:84d246dccb8d | 50 | |
moklumbys | 5:2aa78a442132 | 51 | /** |
moklumbys | 5:2aa78a442132 | 52 | * Function used to calibrate all 4 ESC before actually flying the quadcopter. |
moklumbys | 5:2aa78a442132 | 53 | * It does not matter which of inputs is min and which is max as function checks that itself. |
moklumbys | 5:2aa78a442132 | 54 | * If inputs are out of boundaries, min value becomes 0.0, and max value becomes 1.0 automatically |
moklumbys | 5:2aa78a442132 | 55 | * |
moklumbys | 5:2aa78a442132 | 56 | * @param min - minimum value for the ESC in range from 0.0 to 1.0. |
moklumbys | 5:2aa78a442132 | 57 | * @param max - maximum value for the ESC in range from 0.0 to 1.0. |
moklumbys | 5:2aa78a442132 | 58 | */ |
moklumbys | 0:341a08dbf9ba | 59 | void calibrate (float min, float max); |
moklumbys | 3:84d246dccb8d | 60 | |
moklumbys | 5:2aa78a442132 | 61 | /** |
moklumbys | 5:2aa78a442132 | 62 | * function for runing motors. |
moklumbys | 5:2aa78a442132 | 63 | * |
moklumbys | 5:2aa78a442132 | 64 | * @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 | 65 | */ |
moklumbys | 0:341a08dbf9ba | 66 | void run (float* speed); |
moklumbys | 3:84d246dccb8d | 67 | |
moklumbys | 5:2aa78a442132 | 68 | /** |
moklumbys | 5:2aa78a442132 | 69 | * Function used to calculate the speed at which each of the motors should run to be able to stabilise the quadcopter. |
moklumbys | 5:2aa78a442132 | 70 | * |
moklumbys | 5:2aa78a442132 | 71 | * @param speed - current speed for each motor. |
moklumbys | 5:2aa78a442132 | 72 | * @param actSpeed - actual speed at which motors should run |
moklumbys | 5:2aa78a442132 | 73 | * @param rollDiff - calculated using PID library function PID::compute(); |
moklumbys | 5:2aa78a442132 | 74 | * @param pitchDiff - calculated using PID library function PID::compute(); |
moklumbys | 5:2aa78a442132 | 75 | * |
moklumbys | 5:2aa78a442132 | 76 | */ |
moklumbys | 0:341a08dbf9ba | 77 | void stabilise (float* speed, float* actSpeed, float rollDiff, float pitchDiff); |
moklumbys | 3:84d246dccb8d | 78 | |
moklumbys | 0:341a08dbf9ba | 79 | private: |
moklumbys | 3:84d246dccb8d | 80 | |
moklumbys | 3:84d246dccb8d | 81 | float min_calibrate; //min value at which each motor is calibrated |
moklumbys | 4:90ce5817667b | 82 | float max_calibrate; //max value ... |
moklumbys | 3:84d246dccb8d | 83 | Servo* motor[4]; //motors are used with Servo library as ESC take the same input as usual Servo motors... |
moklumbys | 3:84d246dccb8d | 84 | 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 | 85 | }; |
moklumbys | 0:341a08dbf9ba | 86 | |
moklumbys | 0:341a08dbf9ba | 87 | #endif |