Andrew Olguin / Mbed 2 deprecated RTOS_Controller_v3

Dependencies:   IMU MODSERIAL Servo mbed

Fork of RTOS_Controller_v3 by Marco Rubio

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers esc.h Source File

esc.h

00001 #ifndef _ESC_H_
00002 #define _ESC_H_
00003 
00004 /** ESC class used to controll standard Electronic Speed Controllers for brushless motors of RC models.
00005  *  Simple usage example:
00006  *  @code
00007  *
00008 #include "mbed.h"
00009 #include "esc.h" //include the ESC class
00010 
00011 
00012 int main()
00013 {
00014     ESC esc1(p26); //declare the ESC as connected to pin p26.
00015     float throttle_var = 0.5 //that means 50%.
00016         
00017     while(1)
00018     {
00019         //... update throttle_var ...
00020     
00021         esc1 = throttle_var; //memorize the throttle value (it doesn't send it to the ESC).
00022         
00023         //... do whatever you want - e.g. call esc1.setThrottle(throttle_var) again ...
00024         
00025         esc1(); //actually sets the throttle to the ESC.
00026         
00027         wait_ms(20);  //20ms is the default period of the ESC pwm; the ESC may not run faster.
00028     }
00029 }
00030  * @endcode
00031  * 
00032  * Another example:
00033  * 
00034  * @code
00035  *
00036 #include "mbed.h"
00037 #include "esc.h"
00038  
00039 ESC esc1(PTD4);
00040 ESC esc2(PTA12);
00041 ESC esc3(PTA4);
00042 ESC esc4(PTA5);
00043  
00044 Serial pc(USBTX, USBRX);    // tx, rx
00045  
00046 int main() {
00047     
00048     char c;
00049     int var = 0;
00050  
00051     while(1) {
00052         c = pc.getc();
00053         
00054         if (c == 'u') {
00055             if (var < 100) {
00056                 var++;
00057             }
00058             if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) {
00059                 printf("%i\r\n", var);
00060             }
00061         }
00062         else if (c == 'd') {
00063             if (var > 0) {
00064                 var--;
00065             }
00066             if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) {
00067                 printf("%i\r\n", var);
00068             }
00069         }
00070         else if (c == 'r') {
00071             var = 0;
00072             if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) {
00073                 printf("%i\r\n", var);                
00074             }
00075         }
00076         
00077         esc1.pulse();
00078         esc2.pulse();
00079         esc3.pulse();
00080         esc4.pulse();
00081         wait_ms(20);  // 20ms is the default period of the ESC pwm
00082     }
00083 }
00084  * @endcode
00085  */
00086  
00087 class ESC
00088 {    
00089   private:
00090   
00091     PwmOut esc;
00092     int period;
00093     int throttle;
00094     
00095   public:
00096   
00097     /** Initializes the PwmOut for minimum throttle (1000us).
00098      *  @param pwmPinOut is the pin connected to the ESC.
00099      *  @param period is the PWM period in ms (default is 20ms).
00100      */
00101     ESC (const PinName pwmPinOut, const int period=20);
00102     
00103     /** Sets the throttle value without output (see pulse()).
00104      *  @param t in in the range [0.0;1.0]
00105      *  @return true if throttle value is in range; false otherwise.
00106      */
00107     bool setThrottle (const float t);
00108     ///Alias of setThrottle(float)
00109     bool operator= (const float t);
00110     
00111     /** Get the last setted throttle value
00112      *  @return throttle in range [0.0-1.0].
00113      */
00114     float getThrottle () const;
00115     ///Alias of getThrottle()
00116     operator float () const;
00117     
00118     /** Output the throttle value to the ESC.
00119      */
00120     void pulse ();
00121     ///Alias of pulse()
00122     void operator() ();
00123  };
00124 
00125 #endif