ESC class used to controll standard Electronic Speed Controllers for brushless motors of RC models

Dependents:   MTQuadControl Base_Hybrid_V2 base_rekam_darurat_v1 Base_Hybrid_Latihan_Ok_Hajar_servo_pwm ... more

Simple ESC usage example

#include "mbed.h"
#include "esc.h" //include the ESC class


int main()
{
    ESC esc1(p26); //declare the ESC as connected to pin p26.
    float throttle_var = 0.5 //that means 50%.
        
    while(1)
    {
        //... update throttle_var ...
    
        esc1 = throttle_var; //memorize the throttle value (it doesn't send it to the ESC).
        
        //... do whatever you want - e.g. call esc1.setThrottle(throttle_var) again ...
        
        esc1(); //actually sets the throttle to the ESC.
        
        wait_ms(20);  //20ms is the default period of the ESC pwm; the ESC may not run faster.
    }
}

Revision:
0:116260c66d88
Child:
1:735702ea5519
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/esc.cpp	Fri Jul 19 03:11:05 2013 +0000
@@ -0,0 +1,37 @@
+#include "mbed.h"
+#include "esc.h"
+
+ESC::ESC(const PinName pwmPinOut, const int period)
+        : esc(pwmPinOut), period(period), throttle(1000)
+{
+    esc.period_ms(period);
+    esc.pulsewidth_us(throttle);
+}
+
+inline bool ESC::setThrottle (const float t)
+{
+    if (t >= 0.0 && t <= 1.0) {       // qualify range, 0-1
+        throttle = 1000.0*t + 1000;     // map to range, 1-2 ms (1000-2000us)
+        return true;
+    }
+    return false;
+}
+inline bool ESC::operator= (const float t){
+    return this->setThrottle(t);
+}
+
+inline float ESC::getThrottle () const{
+    return throttle;
+}
+inline ESC::operator float () const{
+    return this->getThrottle();
+}
+
+inline void ESC::pulse ()
+{
+    esc.pulsewidth_us(throttle);
+}
+inline void ESC::operator() ()
+{
+    this->pulse();
+}
\ No newline at end of file