my

Fork of ESC by Mitch Carlson

Committer:
MitchJCarlson
Date:
Thu Jun 06 18:57:02 2013 +0000
Revision:
0:ec466ef657a2
Child:
1:4c02fede684b
Made library folders

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MitchJCarlson 0:ec466ef657a2 1 /**
MitchJCarlson 0:ec466ef657a2 2 * esc.h
MitchJCarlson 0:ec466ef657a2 3 * UWB Quadcopter Project
MitchJCarlson 0:ec466ef657a2 4 *
MitchJCarlson 0:ec466ef657a2 5 *
MitchJCarlson 0:ec466ef657a2 6 * @Author
MitchJCarlson 0:ec466ef657a2 7 * Mitch Carlson
MitchJCarlson 0:ec466ef657a2 8 */
MitchJCarlson 0:ec466ef657a2 9 #ifndef UWBQuad__ESC__H
MitchJCarlson 0:ec466ef657a2 10 #define UWBQuad__ESC__H
MitchJCarlson 0:ec466ef657a2 11
MitchJCarlson 0:ec466ef657a2 12 /** ESC Class Example:
MitchJCarlson 0:ec466ef657a2 13 *
MitchJCarlson 0:ec466ef657a2 14 * @code
MitchJCarlson 0:ec466ef657a2 15 *
MitchJCarlson 0:ec466ef657a2 16 #include "mbed.h"
MitchJCarlson 0:ec466ef657a2 17 #include "esc.h"
MitchJCarlson 0:ec466ef657a2 18
MitchJCarlson 0:ec466ef657a2 19 ESC esc1(PTD4);
MitchJCarlson 0:ec466ef657a2 20 ESC esc2(PTA12);
MitchJCarlson 0:ec466ef657a2 21 ESC esc3(PTA4);
MitchJCarlson 0:ec466ef657a2 22 ESC esc4(PTA5);
MitchJCarlson 0:ec466ef657a2 23
MitchJCarlson 0:ec466ef657a2 24 Serial pc(USBTX, USBRX); // tx, rx
MitchJCarlson 0:ec466ef657a2 25
MitchJCarlson 0:ec466ef657a2 26 int main() {
MitchJCarlson 0:ec466ef657a2 27
MitchJCarlson 0:ec466ef657a2 28 char c;
MitchJCarlson 0:ec466ef657a2 29 int var = 0;
MitchJCarlson 0:ec466ef657a2 30
MitchJCarlson 0:ec466ef657a2 31 while(1) {
MitchJCarlson 0:ec466ef657a2 32 c = pc.getc();
MitchJCarlson 0:ec466ef657a2 33
MitchJCarlson 0:ec466ef657a2 34 if (c == 'u') {
MitchJCarlson 0:ec466ef657a2 35 if (var < 100) {
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 == 'd') {
MitchJCarlson 0:ec466ef657a2 43 if (var > 0) {
MitchJCarlson 0:ec466ef657a2 44 var--;
MitchJCarlson 0:ec466ef657a2 45 }
MitchJCarlson 0:ec466ef657a2 46 if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) {
MitchJCarlson 0:ec466ef657a2 47 printf("%i\r\n", var);
MitchJCarlson 0:ec466ef657a2 48 }
MitchJCarlson 0:ec466ef657a2 49 }
MitchJCarlson 0:ec466ef657a2 50 else if (c == 'r') {
MitchJCarlson 0:ec466ef657a2 51 var = 0;
MitchJCarlson 0:ec466ef657a2 52 if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) {
MitchJCarlson 0:ec466ef657a2 53 printf("%i\r\n", var);
MitchJCarlson 0:ec466ef657a2 54 }
MitchJCarlson 0:ec466ef657a2 55 }
MitchJCarlson 0:ec466ef657a2 56
MitchJCarlson 0:ec466ef657a2 57 esc1.pulse();
MitchJCarlson 0:ec466ef657a2 58 esc2.pulse();
MitchJCarlson 0:ec466ef657a2 59 esc3.pulse();
MitchJCarlson 0:ec466ef657a2 60 esc4.pulse();
MitchJCarlson 0:ec466ef657a2 61 wait_ms(20); // 20ms is the default period of the ESC pwm
MitchJCarlson 0:ec466ef657a2 62 }
MitchJCarlson 0:ec466ef657a2 63 }
MitchJCarlson 0:ec466ef657a2 64 * @endcode
MitchJCarlson 0:ec466ef657a2 65 */
MitchJCarlson 0:ec466ef657a2 66
MitchJCarlson 0:ec466ef657a2 67 class ESC {
MitchJCarlson 0:ec466ef657a2 68 public:
MitchJCarlson 0:ec466ef657a2 69 ESC(PwmOut,int=20); // Constructor(pwmPinOut,period_ms)
MitchJCarlson 0:ec466ef657a2 70 bool setThrottle(int); // range 0-100
MitchJCarlson 0:ec466ef657a2 71 void pulse(void);
MitchJCarlson 0:ec466ef657a2 72
MitchJCarlson 0:ec466ef657a2 73 private:
MitchJCarlson 0:ec466ef657a2 74 PwmOut esc; // pinout from MCU
MitchJCarlson 0:ec466ef657a2 75 int period;
MitchJCarlson 0:ec466ef657a2 76 int throttle;
MitchJCarlson 0:ec466ef657a2 77 };
MitchJCarlson 0:ec466ef657a2 78
MitchJCarlson 0:ec466ef657a2 79 #endif // end of ESC