my
Fork of ESC by
esc.h@2:6ef05596f0e7, 2014-04-06 (annotated)
- Committer:
- redplam
- Date:
- Sun Apr 06 04:10:34 2014 +0000
- Revision:
- 2:6ef05596f0e7
- Parent:
- 1:4c02fede684b
ver1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MitchJCarlson | 0:ec466ef657a2 | 1 | #ifndef UWBQuad__ESC__H |
MitchJCarlson | 0:ec466ef657a2 | 2 | #define UWBQuad__ESC__H |
MitchJCarlson | 0:ec466ef657a2 | 3 | |
MitchJCarlson | 0:ec466ef657a2 | 4 | /** ESC Class Example: |
MitchJCarlson | 0:ec466ef657a2 | 5 | * |
MitchJCarlson | 0:ec466ef657a2 | 6 | * @code |
MitchJCarlson | 0:ec466ef657a2 | 7 | * |
MitchJCarlson | 0:ec466ef657a2 | 8 | #include "mbed.h" |
MitchJCarlson | 0:ec466ef657a2 | 9 | #include "esc.h" |
MitchJCarlson | 0:ec466ef657a2 | 10 | |
MitchJCarlson | 0:ec466ef657a2 | 11 | ESC esc1(PTD4); |
MitchJCarlson | 0:ec466ef657a2 | 12 | ESC esc2(PTA12); |
MitchJCarlson | 0:ec466ef657a2 | 13 | ESC esc3(PTA4); |
MitchJCarlson | 0:ec466ef657a2 | 14 | ESC esc4(PTA5); |
MitchJCarlson | 0:ec466ef657a2 | 15 | |
MitchJCarlson | 0:ec466ef657a2 | 16 | Serial pc(USBTX, USBRX); // tx, rx |
MitchJCarlson | 0:ec466ef657a2 | 17 | |
MitchJCarlson | 0:ec466ef657a2 | 18 | int main() { |
MitchJCarlson | 0:ec466ef657a2 | 19 | |
MitchJCarlson | 0:ec466ef657a2 | 20 | char c; |
MitchJCarlson | 0:ec466ef657a2 | 21 | int var = 0; |
MitchJCarlson | 0:ec466ef657a2 | 22 | |
MitchJCarlson | 0:ec466ef657a2 | 23 | while(1) { |
MitchJCarlson | 0:ec466ef657a2 | 24 | c = pc.getc(); |
MitchJCarlson | 0:ec466ef657a2 | 25 | |
MitchJCarlson | 0:ec466ef657a2 | 26 | if (c == 'u') { |
MitchJCarlson | 0:ec466ef657a2 | 27 | if (var < 100) { |
MitchJCarlson | 0:ec466ef657a2 | 28 | var++; |
MitchJCarlson | 0:ec466ef657a2 | 29 | } |
MitchJCarlson | 0:ec466ef657a2 | 30 | if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) { |
MitchJCarlson | 0:ec466ef657a2 | 31 | printf("%i\r\n", var); |
MitchJCarlson | 0:ec466ef657a2 | 32 | } |
MitchJCarlson | 0:ec466ef657a2 | 33 | } |
MitchJCarlson | 0:ec466ef657a2 | 34 | else if (c == 'd') { |
MitchJCarlson | 0:ec466ef657a2 | 35 | if (var > 0) { |
MitchJCarlson | 0:ec466ef657a2 | 36 | var--; |
MitchJCarlson | 0:ec466ef657a2 | 37 | } |
MitchJCarlson | 0:ec466ef657a2 | 38 | if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) { |
MitchJCarlson | 0:ec466ef657a2 | 39 | printf("%i\r\n", var); |
MitchJCarlson | 0:ec466ef657a2 | 40 | } |
MitchJCarlson | 0:ec466ef657a2 | 41 | } |
MitchJCarlson | 0:ec466ef657a2 | 42 | else if (c == 'r') { |
MitchJCarlson | 0:ec466ef657a2 | 43 | var = 0; |
MitchJCarlson | 0:ec466ef657a2 | 44 | if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) { |
MitchJCarlson | 0:ec466ef657a2 | 45 | printf("%i\r\n", var); |
MitchJCarlson | 0:ec466ef657a2 | 46 | } |
MitchJCarlson | 0:ec466ef657a2 | 47 | } |
MitchJCarlson | 0:ec466ef657a2 | 48 | |
MitchJCarlson | 0:ec466ef657a2 | 49 | esc1.pulse(); |
MitchJCarlson | 0:ec466ef657a2 | 50 | esc2.pulse(); |
MitchJCarlson | 0:ec466ef657a2 | 51 | esc3.pulse(); |
MitchJCarlson | 0:ec466ef657a2 | 52 | esc4.pulse(); |
MitchJCarlson | 0:ec466ef657a2 | 53 | wait_ms(20); // 20ms is the default period of the ESC pwm |
MitchJCarlson | 0:ec466ef657a2 | 54 | } |
MitchJCarlson | 0:ec466ef657a2 | 55 | } |
MitchJCarlson | 0:ec466ef657a2 | 56 | * @endcode |
MitchJCarlson | 0:ec466ef657a2 | 57 | */ |
MitchJCarlson | 0:ec466ef657a2 | 58 | |
MitchJCarlson | 0:ec466ef657a2 | 59 | class ESC { |
MitchJCarlson | 0:ec466ef657a2 | 60 | public: |
redplam | 2:6ef05596f0e7 | 61 | ESC(PwmOut,int=10); // Constructor(pwmPinOut,period_ms) |
MitchJCarlson | 0:ec466ef657a2 | 62 | bool setThrottle(int); // range 0-100 |
MitchJCarlson | 0:ec466ef657a2 | 63 | void pulse(void); |
MitchJCarlson | 1:4c02fede684b | 64 | |
MitchJCarlson | 0:ec466ef657a2 | 65 | private: |
MitchJCarlson | 0:ec466ef657a2 | 66 | PwmOut esc; // pinout from MCU |
MitchJCarlson | 0:ec466ef657a2 | 67 | int period; |
MitchJCarlson | 0:ec466ef657a2 | 68 | int throttle; |
MitchJCarlson | 0:ec466ef657a2 | 69 | }; |
MitchJCarlson | 0:ec466ef657a2 | 70 | |
MitchJCarlson | 0:ec466ef657a2 | 71 | #endif // end of ESC |