SmartWheels self-driving race car. Designed for NXP Cup. Uses FRDM-KL25Z, area-scan camera, and simple image processing to detect and navigate any NXP spec track.
Dependencies: TSI USBDevice mbed-dev
Fork of SmartWheels by
Diff: Hardwares/Motor.cpp
- Revision:
- 9:b72e18f80f49
- Parent:
- 8:92f6baeea027
- Child:
- 11:676ea42afd56
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Hardwares/Motor.cpp Mon Feb 06 21:07:52 2017 +0000 @@ -0,0 +1,69 @@ +#include "Motor.h" +#include "mbed.h" + +#include "PinAssignment.h" + +Motor::Motor() : + m_dirL(DigitalOut(MC_DIR_L, 1)), + m_dirR(DigitalOut(MC_DIR_R, 1)), + m_pwmL(PwmOut(MC_SPEED_L)), + m_pwmR(PwmOut(MC_SPEED_R)) + +{ + +} + + +void Motor::Update(float deltaTime) +{ + +} + +void Motor::setLeftSpeed(float speed) +{ + if(speed < -1.0f || speed > 1.0f) + return; + + m_pwmL.period_us(60); + + setLeftDirection(speed < 0 ? MDIR_Backward : MDIR_Forward); + speed = speed < 0 ? -speed : speed; + + m_pwmL = speed; +} + +void Motor::setRightSpeed(float speed) +{ + if(speed < -1.0f || speed > 1.0f) + return; + + m_pwmR.period_us(60); + + setRightDirection(speed < 0 ? MDIR_Backward : MDIR_Forward); + speed = speed < 0 ? -speed : speed; + + m_pwmR = speed; +} + +void Motor::setSpeeds(float leftSpeed, float rightSpeed) +{ + setLeftSpeed(leftSpeed); + setRightSpeed(rightSpeed); +} + + +void Motor::setLeftDirection(MotorDir dir) +{ + m_dirL = static_cast<int>(dir); +} + +void Motor::setRightDirection(MotorDir dir) +{ + m_dirR = static_cast<int>(dir); +} + +void Motor::setDirections(MotorDir dirL, MotorDir dirR) +{ + setLeftDirection(dirL); + setRightDirection(dirR); +}