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.
Diff: Quadcopter.cpp
- Revision:
- 9:22c52af13ac2
- Parent:
- 8:0e9474cce85b
- Child:
- 10:8147131fcebd
diff -r 0e9474cce85b -r 22c52af13ac2 Quadcopter.cpp --- a/Quadcopter.cpp Fri Feb 20 01:22:20 2015 +0000 +++ b/Quadcopter.cpp Wed Feb 25 00:55:08 2015 +0000 @@ -7,29 +7,20 @@ motor[1] = new Servo (FR); motor[2] = new Servo (BL); motor[3] = new Servo (BR); + + min_calibrate = 0.0; + max_calibrate = 1.0; } //------------------------------Function for ESC calibration--------------------- -void Quadcopter::calibrate (float min, float max){ - if (min > max){ //here detect if someone tried making min to be more than max. If that is the case, then flip them together - min_calibrate = max; - max_calibrate = min; - } else { - min_calibrate = min; - max_calibrate = max; - } - if ((min > 1.0) || (min < 0.0)) //here chech if values are in correct range. If they are not, make them to be in correct range - min_calibrate = 0.0; - if ((max > 1.0) || (max < 0.0)) - max_calibrate = 1.0; - +void Quadcopter::calibrate (){ for (int i = 0; i < 4; i++){ //run motors for some time in min speed *motor[i] = max_calibrate; } wait(6.0); //wait for the response from ESC modules for (int i = 0; i < 4; i++){ *motor[i] = min_calibrate; //run motors at maximum speed - } + } wait(2.0); //again wait for response } //-------------------------------------Function for Stabilising--------------- @@ -44,14 +35,37 @@ //simply map values in the correct range and run PWM signals for each motor for (int i = 0; i < 4; i++){ if (speed[i] < 0.0) - *motor[i] = min.calibrate; + *motor[i] = min_calibrate; else if (speed[i] > 1.0) *motor[i] = max_calibrate; else *motor[i] = this->map(speed[i], 0.0, 1.0, min_calibrate, max_calibrate); } } +//--------------------------Function for setting calibration limits----------- +void Quadcopter::setLimits(float min, float max){ + if (min > max){ //here detect if someone tried making min to be more than max. If that is the case, then flip them together + min_calibrate = max; + max_calibrate = min; + } else { + min_calibrate = min; + max_calibrate = max; + } + if ((min_calibrate > 1.0) || (min_calibrate < 0.0)) //here chech if values are in correct range. If they are not, make them to be in correct range + min_calibrate = 0.0; + if ((max_calibrate > 1.0) || (max_calibrate < 0.0)) + max_calibrate = 1.0; +} //-----------------------------Mapping function----------------------------- float Quadcopter::map(float x, float in_min, float in_max, float out_min, float out_max){ //simply maps values in the given range return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; +} + +float Quadcopter::getLowerLimit(){ + return min_calibrate; +} + +float Quadcopter::getUpperLimit(){ + return max_calibrate; + } \ No newline at end of file