a dc_motor drive lib use two PWM IO to drive the dc_motor

Dependents:   Nucleo_F411RE_OS_Robot_Tank

motodriver.cpp

Committer:
adaphoto
Date:
2018-06-19
Revision:
0:cd71771d494f
Child:
1:d31307e787fd

File content as of revision 0:cd71771d494f:

#include "mbed.h"
#include "motodriver.h"

Motor::Motor(PinName pwm1pin, PinName pwm2pin) : _pwm1(pwm1pin) , _pwm2(pwm2pin)
{
    init();    
}

void Motor::init()
{
    // Set initial condition of PWM
    _pwm1.period(0.001);
    _pwm1 = 0;
    
    // Set initial condition of PWM
    _pwm2.period(0.001);
    _pwm2 = 0;
}

float Motor::Speed(float speed)
{
    float temp;
    temp = abs(speed);
    
    if (speed > 0.0)
    {
        _pwm1 = temp;
        _pwm2 = 0;
    }
    else
    {
        _pwm1 = 0;
        _pwm2 = temp;
    }
    
    return temp;
}

void Motor::Stop()
{
    _pwm1 = 0;
    _pwm2 = 0;
}