NOT FINISHED YET!!! My first try to get a self built fully working Quadrocopter based on an mbed, a self built frame and some other more or less cheap parts.
Servo/Servo.h@40:2ca410923691, 2014-02-14 (annotated)
- Committer:
- maetugr
- Date:
- Fri Feb 14 14:17:32 2014 +0000
- Revision:
- 40:2ca410923691
- Parent:
- 9:4e0c3936c756
now with MPU6050 before taking it too FlyBed2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maetugr | 0:0c4fafa398b4 | 1 | // based on http://mbed.org/users/jdenkers/code/Servo/ |
maetugr | 0:0c4fafa398b4 | 2 | |
maetugr | 9:4e0c3936c756 | 3 | #ifndef SERVO_H |
maetugr | 9:4e0c3936c756 | 4 | #define SERVO_H |
maetugr | 0:0c4fafa398b4 | 5 | |
maetugr | 0:0c4fafa398b4 | 6 | #include "mbed.h" |
maetugr | 0:0c4fafa398b4 | 7 | |
maetugr | 0:0c4fafa398b4 | 8 | /** Class to control a servo on any pin, without using pwm |
maetugr | 0:0c4fafa398b4 | 9 | * |
maetugr | 0:0c4fafa398b4 | 10 | * Example: |
maetugr | 0:0c4fafa398b4 | 11 | * @code |
maetugr | 0:0c4fafa398b4 | 12 | * // Keep sweeping servo from left to right |
maetugr | 0:0c4fafa398b4 | 13 | * #include "mbed.h" |
maetugr | 0:0c4fafa398b4 | 14 | * #include "Servo.h" |
maetugr | 0:0c4fafa398b4 | 15 | * |
maetugr | 0:0c4fafa398b4 | 16 | * Servo Servo1(p20); |
maetugr | 0:0c4fafa398b4 | 17 | * |
maetugr | 0:0c4fafa398b4 | 18 | * Servo1.Enable(1500,20000); |
maetugr | 0:0c4fafa398b4 | 19 | * |
maetugr | 0:0c4fafa398b4 | 20 | * while(1) { |
maetugr | 0:0c4fafa398b4 | 21 | * for (int pos = 1000; pos < 2000; pos += 25) { |
maetugr | 0:0c4fafa398b4 | 22 | * Servo1.SetPosition(pos); |
maetugr | 0:0c4fafa398b4 | 23 | * wait_ms(20); |
maetugr | 0:0c4fafa398b4 | 24 | * } |
maetugr | 0:0c4fafa398b4 | 25 | * for (int pos = 2000; pos > 1000; pos -= 25) { |
maetugr | 0:0c4fafa398b4 | 26 | * Servo1.SetPosition(pos); |
maetugr | 0:0c4fafa398b4 | 27 | * wait_ms(20); |
maetugr | 0:0c4fafa398b4 | 28 | * } |
maetugr | 0:0c4fafa398b4 | 29 | * } |
maetugr | 0:0c4fafa398b4 | 30 | * @endcode |
maetugr | 0:0c4fafa398b4 | 31 | */ |
maetugr | 0:0c4fafa398b4 | 32 | |
maetugr | 0:0c4fafa398b4 | 33 | class Servo { |
maetugr | 0:0c4fafa398b4 | 34 | |
maetugr | 0:0c4fafa398b4 | 35 | public: |
maetugr | 0:0c4fafa398b4 | 36 | /** Create a new Servo object on any mbed pin |
maetugr | 0:0c4fafa398b4 | 37 | * |
maetugr | 0:0c4fafa398b4 | 38 | * @param Pin Pin on mbed to connect servo to |
maetugr | 0:0c4fafa398b4 | 39 | */ |
maetugr | 0:0c4fafa398b4 | 40 | Servo(PinName Pin); |
maetugr | 0:0c4fafa398b4 | 41 | |
maetugr | 0:0c4fafa398b4 | 42 | /** Change the position of the servo. Position in us |
maetugr | 0:0c4fafa398b4 | 43 | * |
maetugr | 0:0c4fafa398b4 | 44 | * @param NewPos The new value of the servos position (us) |
maetugr | 0:0c4fafa398b4 | 45 | */ |
maetugr | 0:0c4fafa398b4 | 46 | void SetPosition(int NewPos); |
maetugr | 0:0c4fafa398b4 | 47 | |
maetugr | 0:0c4fafa398b4 | 48 | /** Enable the servo. Without enabling the servo won't be running. Startposition and period both in us. |
maetugr | 0:0c4fafa398b4 | 49 | * |
maetugr | 0:0c4fafa398b4 | 50 | * @param StartPos The position of the servo to start (us) |
maetugr | 0:0c4fafa398b4 | 51 | * @param Period The time between every pulse. 20000 us = 50 Hz(standard) (us) |
maetugr | 0:0c4fafa398b4 | 52 | */ |
maetugr | 0:0c4fafa398b4 | 53 | void Enable(int StartPos, int Period); |
maetugr | 0:0c4fafa398b4 | 54 | |
maetugr | 0:0c4fafa398b4 | 55 | //Disable the servo. After disabling the servo won't get any signal anymore |
maetugr | 0:0c4fafa398b4 | 56 | void Disable(); |
maetugr | 0:0c4fafa398b4 | 57 | |
maetugr | 0:0c4fafa398b4 | 58 | //operator for confortable positioning |
maetugr | 0:0c4fafa398b4 | 59 | void operator=(int position); |
maetugr | 1:5a64632b1eb9 | 60 | |
maetugr | 1:5a64632b1eb9 | 61 | // initialize ESC |
maetugr | 1:5a64632b1eb9 | 62 | void initialize(); |
maetugr | 0:0c4fafa398b4 | 63 | |
maetugr | 0:0c4fafa398b4 | 64 | private: |
maetugr | 0:0c4fafa398b4 | 65 | void StartPulse(); |
maetugr | 0:0c4fafa398b4 | 66 | void EndPulse(); |
maetugr | 0:0c4fafa398b4 | 67 | |
maetugr | 0:0c4fafa398b4 | 68 | int Position; |
maetugr | 0:0c4fafa398b4 | 69 | DigitalOut ServoPin; |
maetugr | 0:0c4fafa398b4 | 70 | Ticker Pulse; |
maetugr | 0:0c4fafa398b4 | 71 | Timeout PulseStop; |
maetugr | 0:0c4fafa398b4 | 72 | }; |
maetugr | 0:0c4fafa398b4 | 73 | |
maetugr | 0:0c4fafa398b4 | 74 | #endif |