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

MotorControler.cpp

Committer:
sgrsn
Date:
2021-02-02
Revision:
3:7acc824ca344
Parent:
2:543ff0150de1
Child:
4:a60052db674c

File content as of revision 3:7acc824ca344:

#include "MotorControler.h"

MotorControler::MotorControler(PinName DIR, PinName PWM, PinName SLP, DriverType) : 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);
}