Marco Rubio
/
RTOS_Controller
Controller for Seagoat in the RoboSub competition
Fork of ESC by
esc.h@5:07bbe020eb65, 2016-07-26 (annotated)
- Committer:
- gelmes
- Date:
- Tue Jul 26 17:22:33 2016 +0000
- Revision:
- 5:07bbe020eb65
- Parent:
- 1:735702ea5519
This is a working implementation of the Controller;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MatteoT | 0:116260c66d88 | 1 | #ifndef _ESC_H_ |
MatteoT | 0:116260c66d88 | 2 | #define _ESC_H_ |
MatteoT | 0:116260c66d88 | 3 | |
MatteoT | 0:116260c66d88 | 4 | /** ESC class used to controll standard Electronic Speed Controllers for brushless motors of RC models. |
MatteoT | 0:116260c66d88 | 5 | * Simple usage example: |
MatteoT | 0:116260c66d88 | 6 | * @code |
MatteoT | 0:116260c66d88 | 7 | * |
MatteoT | 0:116260c66d88 | 8 | #include "mbed.h" |
MatteoT | 0:116260c66d88 | 9 | #include "esc.h" //include the ESC class |
MatteoT | 0:116260c66d88 | 10 | |
MatteoT | 0:116260c66d88 | 11 | |
MatteoT | 0:116260c66d88 | 12 | int main() |
MatteoT | 0:116260c66d88 | 13 | { |
MatteoT | 0:116260c66d88 | 14 | ESC esc1(p26); //declare the ESC as connected to pin p26. |
MatteoT | 0:116260c66d88 | 15 | float throttle_var = 0.5 //that means 50%. |
MatteoT | 0:116260c66d88 | 16 | |
MatteoT | 0:116260c66d88 | 17 | while(1) |
MatteoT | 0:116260c66d88 | 18 | { |
MatteoT | 0:116260c66d88 | 19 | //... update throttle_var ... |
MatteoT | 0:116260c66d88 | 20 | |
MatteoT | 0:116260c66d88 | 21 | esc1 = throttle_var; //memorize the throttle value (it doesn't send it to the ESC). |
MatteoT | 0:116260c66d88 | 22 | |
MatteoT | 0:116260c66d88 | 23 | //... do whatever you want - e.g. call esc1.setThrottle(throttle_var) again ... |
MatteoT | 0:116260c66d88 | 24 | |
MatteoT | 0:116260c66d88 | 25 | esc1(); //actually sets the throttle to the ESC. |
MatteoT | 0:116260c66d88 | 26 | |
MatteoT | 0:116260c66d88 | 27 | wait_ms(20); //20ms is the default period of the ESC pwm; the ESC may not run faster. |
MatteoT | 0:116260c66d88 | 28 | } |
MatteoT | 0:116260c66d88 | 29 | } |
MatteoT | 0:116260c66d88 | 30 | * @endcode |
MatteoT | 0:116260c66d88 | 31 | * |
MatteoT | 0:116260c66d88 | 32 | * Another example: |
MatteoT | 0:116260c66d88 | 33 | * |
MatteoT | 0:116260c66d88 | 34 | * @code |
MatteoT | 0:116260c66d88 | 35 | * |
MatteoT | 0:116260c66d88 | 36 | #include "mbed.h" |
MatteoT | 0:116260c66d88 | 37 | #include "esc.h" |
MatteoT | 0:116260c66d88 | 38 | |
MatteoT | 0:116260c66d88 | 39 | ESC esc1(PTD4); |
MatteoT | 0:116260c66d88 | 40 | ESC esc2(PTA12); |
MatteoT | 0:116260c66d88 | 41 | ESC esc3(PTA4); |
MatteoT | 0:116260c66d88 | 42 | ESC esc4(PTA5); |
MatteoT | 0:116260c66d88 | 43 | |
MatteoT | 0:116260c66d88 | 44 | Serial pc(USBTX, USBRX); // tx, rx |
MatteoT | 0:116260c66d88 | 45 | |
MatteoT | 0:116260c66d88 | 46 | int main() { |
MatteoT | 0:116260c66d88 | 47 | |
MatteoT | 0:116260c66d88 | 48 | char c; |
MatteoT | 0:116260c66d88 | 49 | int var = 0; |
MatteoT | 0:116260c66d88 | 50 | |
MatteoT | 0:116260c66d88 | 51 | while(1) { |
MatteoT | 0:116260c66d88 | 52 | c = pc.getc(); |
MatteoT | 0:116260c66d88 | 53 | |
MatteoT | 0:116260c66d88 | 54 | if (c == 'u') { |
MatteoT | 0:116260c66d88 | 55 | if (var < 100) { |
MatteoT | 0:116260c66d88 | 56 | var++; |
MatteoT | 0:116260c66d88 | 57 | } |
MatteoT | 0:116260c66d88 | 58 | if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) { |
MatteoT | 0:116260c66d88 | 59 | printf("%i\r\n", var); |
MatteoT | 0:116260c66d88 | 60 | } |
MatteoT | 0:116260c66d88 | 61 | } |
MatteoT | 0:116260c66d88 | 62 | else if (c == 'd') { |
MatteoT | 0:116260c66d88 | 63 | if (var > 0) { |
MatteoT | 0:116260c66d88 | 64 | var--; |
MatteoT | 0:116260c66d88 | 65 | } |
MatteoT | 0:116260c66d88 | 66 | if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) { |
MatteoT | 0:116260c66d88 | 67 | printf("%i\r\n", var); |
MatteoT | 0:116260c66d88 | 68 | } |
MatteoT | 0:116260c66d88 | 69 | } |
MatteoT | 0:116260c66d88 | 70 | else if (c == 'r') { |
MatteoT | 0:116260c66d88 | 71 | var = 0; |
MatteoT | 0:116260c66d88 | 72 | if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) { |
MatteoT | 0:116260c66d88 | 73 | printf("%i\r\n", var); |
MatteoT | 0:116260c66d88 | 74 | } |
MatteoT | 0:116260c66d88 | 75 | } |
MatteoT | 0:116260c66d88 | 76 | |
MatteoT | 0:116260c66d88 | 77 | esc1.pulse(); |
MatteoT | 0:116260c66d88 | 78 | esc2.pulse(); |
MatteoT | 0:116260c66d88 | 79 | esc3.pulse(); |
MatteoT | 0:116260c66d88 | 80 | esc4.pulse(); |
MatteoT | 0:116260c66d88 | 81 | wait_ms(20); // 20ms is the default period of the ESC pwm |
MatteoT | 0:116260c66d88 | 82 | } |
MatteoT | 0:116260c66d88 | 83 | } |
MatteoT | 0:116260c66d88 | 84 | * @endcode |
MatteoT | 0:116260c66d88 | 85 | */ |
MatteoT | 0:116260c66d88 | 86 | |
MatteoT | 0:116260c66d88 | 87 | class ESC |
MatteoT | 0:116260c66d88 | 88 | { |
MatteoT | 0:116260c66d88 | 89 | private: |
MatteoT | 0:116260c66d88 | 90 | |
MatteoT | 0:116260c66d88 | 91 | PwmOut esc; |
MatteoT | 0:116260c66d88 | 92 | int period; |
MatteoT | 0:116260c66d88 | 93 | int throttle; |
MatteoT | 0:116260c66d88 | 94 | |
MatteoT | 0:116260c66d88 | 95 | public: |
MatteoT | 0:116260c66d88 | 96 | |
MatteoT | 0:116260c66d88 | 97 | /** Initializes the PwmOut for minimum throttle (1000us). |
MatteoT | 0:116260c66d88 | 98 | * @param pwmPinOut is the pin connected to the ESC. |
MatteoT | 0:116260c66d88 | 99 | * @param period is the PWM period in ms (default is 20ms). |
MatteoT | 0:116260c66d88 | 100 | */ |
MatteoT | 0:116260c66d88 | 101 | ESC (const PinName pwmPinOut, const int period=20); |
MatteoT | 0:116260c66d88 | 102 | |
MatteoT | 0:116260c66d88 | 103 | /** Sets the throttle value without output (see pulse()). |
MatteoT | 0:116260c66d88 | 104 | * @param t in in the range [0.0;1.0] |
MatteoT | 0:116260c66d88 | 105 | * @return true if throttle value is in range; false otherwise. |
MatteoT | 0:116260c66d88 | 106 | */ |
MatteoT | 1:735702ea5519 | 107 | bool setThrottle (const float t); |
MatteoT | 0:116260c66d88 | 108 | ///Alias of setThrottle(float) |
MatteoT | 1:735702ea5519 | 109 | bool operator= (const float t); |
MatteoT | 0:116260c66d88 | 110 | |
MatteoT | 0:116260c66d88 | 111 | /** Get the last setted throttle value |
MatteoT | 0:116260c66d88 | 112 | * @return throttle in range [0.0-1.0]. |
MatteoT | 0:116260c66d88 | 113 | */ |
MatteoT | 1:735702ea5519 | 114 | float getThrottle () const; |
MatteoT | 0:116260c66d88 | 115 | ///Alias of getThrottle() |
MatteoT | 1:735702ea5519 | 116 | operator float () const; |
MatteoT | 0:116260c66d88 | 117 | |
MatteoT | 0:116260c66d88 | 118 | /** Output the throttle value to the ESC. |
MatteoT | 0:116260c66d88 | 119 | */ |
MatteoT | 1:735702ea5519 | 120 | void pulse (); |
MatteoT | 0:116260c66d88 | 121 | ///Alias of pulse() |
MatteoT | 1:735702ea5519 | 122 | void operator() (); |
MatteoT | 0:116260c66d88 | 123 | }; |
MatteoT | 0:116260c66d88 | 124 | |
MatteoT | 0:116260c66d88 | 125 | #endif |