Matteo Terruzzi / ESC

Dependents:   MTQuadControl Base_Hybrid_V2 base_rekam_darurat_v1 Base_Hybrid_Latihan_Ok_Hajar_servo_pwm ... more

Embed: (wiki syntax)

« Back to documentation index

ESC Class Reference

ESC Class Reference

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

#include <esc.h>

Public Member Functions

 ESC (const PinName pwmPinOut, const int period=20)
 Initializes the PwmOut for minimum throttle (1000us).
bool setThrottle (const float t)
 Sets the throttle value without output (see pulse()).
bool operator= (const float t)
 Alias of setThrottle(float)
float getThrottle () const
 Get the last setted throttle value.
 operator float () const
 Alias of getThrottle()
void pulse ()
 Output the throttle value to the ESC.
void operator() ()
 Alias of pulse()

Detailed Description

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

Simple 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.
    }
}

Another example:

#include "mbed.h"
#include "esc.h"
 
ESC esc1(PTD4);
ESC esc2(PTA12);
ESC esc3(PTA4);
ESC esc4(PTA5);
 
Serial pc(USBTX, USBRX);    // tx, rx
 
int main() {
    
    char c;
    int var = 0;
 
    while(1) {
        c = pc.getc();
        
        if (c == 'u') {
            if (var < 100) {
                var++;
            }
            if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) {
                printf("%i\r\n", var);
            }
        }
        else if (c == 'd') {
            if (var > 0) {
                var--;
            }
            if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) {
                printf("%i\r\n", var);
            }
        }
        else if (c == 'r') {
            var = 0;
            if (esc1.setThrottle(var) && esc2.setThrottle(var) && esc3.setThrottle(var) && esc4.setThrottle(var)) {
                printf("%i\r\n", var);                
            }
        }
        
        esc1.pulse();
        esc2.pulse();
        esc3.pulse();
        esc4.pulse();
        wait_ms(20);  // 20ms is the default period of the ESC pwm
    }
}

Definition at line 87 of file esc.h.


Constructor & Destructor Documentation

ESC ( const PinName  pwmPinOut,
const int  period = 20 
)

Initializes the PwmOut for minimum throttle (1000us).

Parameters:
pwmPinOutis the pin connected to the ESC.
periodis the PWM period in ms (default is 20ms).

Definition at line 4 of file esc.cpp.


Member Function Documentation

float getThrottle (  ) const

Get the last setted throttle value.

Returns:
throttle in range [0.0-1.0].

Definition at line 23 of file esc.cpp.

operator float (  ) const

Alias of getThrottle()

Definition at line 26 of file esc.cpp.

void operator() (  )

Alias of pulse()

Definition at line 34 of file esc.cpp.

bool operator= ( const float  t )

Alias of setThrottle(float)

Definition at line 19 of file esc.cpp.

void pulse (  )

Output the throttle value to the ESC.

Definition at line 30 of file esc.cpp.

bool setThrottle ( const float  t )

Sets the throttle value without output (see pulse()).

Parameters:
tin in the range [0.0;1.0]
Returns:
true if throttle value is in range; false otherwise.

Definition at line 11 of file esc.cpp.