Simple motor controller library, using DIR, PWM, nSLP pin like pololu.

MotorControler.cpp

Committer:
sgrsn
Date:
2020-03-04
Revision:
2:543ff0150de1
Parent:
1:ed3dfe4ecc8a
Child:
3:7acc824ca344

File content as of revision 2:543ff0150de1:

#include "MotorControler.h"

MotorControler::MotorControler(PinName DIR, PinName PWM, PinName SLP) : _DIR(DIR), _PWM(PWM), _nSLP(SLP)
{
    _nSLP = 0;
    reverse_direction = 0;
}

void MotorControler::enableDriver()
{
    _nSLP = 1;
}

void MotorControler::disableDriver()
{
    _nSLP = 0;
}

void MotorControler::setSpeed(float speed)
{
    uint8_t reverse = 0;
    if (speed < 0)
    {
        speed = -speed;  // Make speed a positive quantity
        reverse = 1;  // Preserve the direction
    }
    // 最大デューティ比で制限
    if (speed > 1)
        speed = 1;
        
    _PWM = speed;
    if (reverse ^ FLIP_MOTOR_DIR ^ reverse_direction)
    {
        _DIR = 1;
    }
    else
    {
        _DIR = 0;
    }
}

void MotorControler::setMotorDirection(MotorDirection dir)
{
    reverse_direction = dir;
}

void MotorControler::setPwmFrequency(float frequency)
{
    float period = 1.0/frequency;
    _PWM.period(period);
}