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@9:22c52af13ac2, 2015-02-25 (annotated)
- Committer:
- moklumbys
- Date:
- Wed Feb 25 00:55:08 2015 +0000
- Revision:
- 9:22c52af13ac2
- Parent:
- 7:5ab77c583ae3
- Child:
- 10:8147131fcebd
corrected min & max calibration values - now they are initialised as previously if someone didn't use calibrate at the beginning, then mapping was done from 0.0 to 0.0... which is obviously stupid.
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 MIN_CALIBRATE 0.3 |
moklumbys | 7:5ab77c583ae3 | 18 | * #define MAX_CALIBRATE 1.0 |
moklumbys | 7:5ab77c583ae3 | 19 | * |
moklumbys | 6:a6b8e508643d | 20 | * int main(){ |
moklumbys | 9:22c52af13ac2 | 21 | * quad.setLimits (MIN_CALIBRATE, MAX_CALIBRATE); |
moklumbys | 9:22c52af13ac2 | 22 | * quad.calibrate (); |
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 | 9:22c52af13ac2 | 53 | */ |
moklumbys | 9:22c52af13ac2 | 54 | void calibrate (); |
moklumbys | 9:22c52af13ac2 | 55 | |
moklumbys | 9:22c52af13ac2 | 56 | /** |
moklumbys | 9:22c52af13ac2 | 57 | * Function used to calibrate all 4 ESC before actually flying the quadcopter. |
moklumbys | 5:2aa78a442132 | 58 | * It does not matter which of inputs is min and which is max as function checks that itself. |
moklumbys | 5:2aa78a442132 | 59 | * If inputs are out of boundaries, min value becomes 0.0, and max value becomes 1.0 automatically |
moklumbys | 5:2aa78a442132 | 60 | * |
moklumbys | 5:2aa78a442132 | 61 | * @param min - minimum value for the ESC in range from 0.0 to 1.0. |
moklumbys | 5:2aa78a442132 | 62 | * @param max - maximum value for the ESC in range from 0.0 to 1.0. |
moklumbys | 5:2aa78a442132 | 63 | */ |
moklumbys | 9:22c52af13ac2 | 64 | void setLimits(float min, float max); |
moklumbys | 3:84d246dccb8d | 65 | |
moklumbys | 5:2aa78a442132 | 66 | /** |
moklumbys | 5:2aa78a442132 | 67 | * function for runing motors. |
moklumbys | 5:2aa78a442132 | 68 | * |
moklumbys | 5:2aa78a442132 | 69 | * @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 | 70 | */ |
moklumbys | 0:341a08dbf9ba | 71 | void run (float* speed); |
moklumbys | 3:84d246dccb8d | 72 | |
moklumbys | 5:2aa78a442132 | 73 | /** |
moklumbys | 5:2aa78a442132 | 74 | * Function used to calculate the speed at which each of the motors should run to be able to stabilise the quadcopter. |
moklumbys | 5:2aa78a442132 | 75 | * |
moklumbys | 5:2aa78a442132 | 76 | * @param speed - current speed for each motor. |
moklumbys | 5:2aa78a442132 | 77 | * @param actSpeed - actual speed at which motors should run |
moklumbys | 5:2aa78a442132 | 78 | * @param rollDiff - calculated using PID library function PID::compute(); |
moklumbys | 5:2aa78a442132 | 79 | * @param pitchDiff - calculated using PID library function PID::compute(); |
moklumbys | 5:2aa78a442132 | 80 | * |
moklumbys | 5:2aa78a442132 | 81 | */ |
moklumbys | 0:341a08dbf9ba | 82 | void stabilise (float* speed, float* actSpeed, float rollDiff, float pitchDiff); |
moklumbys | 3:84d246dccb8d | 83 | |
moklumbys | 9:22c52af13ac2 | 84 | float getLowerLimit(); |
moklumbys | 9:22c52af13ac2 | 85 | float getUpperLimit(); |
moklumbys | 0:341a08dbf9ba | 86 | private: |
moklumbys | 3:84d246dccb8d | 87 | |
moklumbys | 3:84d246dccb8d | 88 | float min_calibrate; //min value at which each motor is calibrated |
moklumbys | 9:22c52af13ac2 | 89 | float max_calibrate; //max value ... |
moklumbys | 3:84d246dccb8d | 90 | Servo* motor[4]; //motors are used with Servo library as ESC take the same input as usual Servo motors... |
moklumbys | 3:84d246dccb8d | 91 | 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 | 92 | }; |
moklumbys | 0:341a08dbf9ba | 93 | |
moklumbys | 0:341a08dbf9ba | 94 | #endif |