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: Motor.cpp
- Revision:
- 8:92f6baeea027
- Parent:
- 4:25e028102625
diff -r 2cb18392649d -r 92f6baeea027 Motor.cpp --- a/Motor.cpp Fri Feb 03 04:08:32 2017 +0000 +++ b/Motor.cpp Fri Feb 03 19:40:27 2017 +0000 @@ -1,72 +1,69 @@ #include "Motor.h" #include "mbed.h" -/* -DigitalOut DIR_L(PTA13); -DigitalOut DIR_R(PTC9); + +#include "PinAssignment.h" -PwmOut PWM_L(PTD0); -PwmOut PWM_R(PTD5); +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)) + +{ + +} -Motor::Motor() + +void Motor::Update(float deltaTime) { } + +void Motor::setLeftSpeed(float speed) +{ + if(speed < -1.0f || speed > 1.0f) + return; -void Motor::initializeMotor() -{ + m_pwmL.period_us(60); + + setLeftDirection(speed < 0 ? MDIR_Backward : MDIR_Forward); + speed = speed < 0 ? -speed : speed; + + m_pwmL = speed; } -void Motor::setLeftSpeed(int speed) +void Motor::setRightSpeed(float speed) { - init(); - PWM_L.period_us(60); - - bool reverse = 0; + if(speed < -1.0f || speed > 1.0f) + return; - if(speed < 0) - { - speed = -speed; - reverse = 1; - } - //Set Max Speed - if(speed > 60) - speed = 60; - - if(reverse) - DIR_L = 1; - else - DIR_L = 0; + m_pwmR.period_us(60); + + setRightDirection(speed < 0 ? MDIR_Backward : MDIR_Forward); + speed = speed < 0 ? -speed : speed; - PWM_L.pulsewidth_us(speed); -} - -void Motor::setRightSpeed(int speed) -{ - init(); - PWM_R.period_us(60); - - bool reverse = 0; - - if(speed < 0) - { - speed = -speed; - reverse = 1; - } - //Set Max Speed - if(speed > 60) - speed = 60; - - if(reverse) - DIR_R = 1; - else - DIR_R = 0; - - PWM_R.pulsewidth_us(speed); + m_pwmR = speed; } -void Motor::setSpeeds(int leftSpeed, int rightSpeed) +void Motor::setSpeeds(float leftSpeed, float rightSpeed) { setLeftSpeed(leftSpeed); setRightSpeed(rightSpeed); } -*/ \ No newline at end of file + + +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); +}