Simple motor controller library, using DIR, PWM, nSLP pin like pololu.
MotorControler.cpp
- Committer:
- sgrsn
- Date:
- 2020-03-04
- Revision:
- 1:ed3dfe4ecc8a
- Parent:
- 0:e3207dd7809a
- Child:
- 2:543ff0150de1
File content as of revision 1:ed3dfe4ecc8a:
#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; }