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
Motor.cpp@8:92f6baeea027, 2017-02-03 (annotated)
- Committer:
- Bobymicjohn
- Date:
- Fri Feb 03 19:40:27 2017 +0000
- Revision:
- 8:92f6baeea027
- Parent:
- 4:25e028102625
Finished code for Motor class.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bobymicjohn | 4:25e028102625 | 1 | #include "Motor.h" |
Bobymicjohn | 4:25e028102625 | 2 | #include "mbed.h" |
Bobymicjohn | 8:92f6baeea027 | 3 | |
Bobymicjohn | 8:92f6baeea027 | 4 | #include "PinAssignment.h" |
Bobymicjohn | 4:25e028102625 | 5 | |
Bobymicjohn | 8:92f6baeea027 | 6 | Motor::Motor() : |
Bobymicjohn | 8:92f6baeea027 | 7 | m_dirL(DigitalOut(MC_DIR_L, 1)), |
Bobymicjohn | 8:92f6baeea027 | 8 | m_dirR(DigitalOut(MC_DIR_R, 1)), |
Bobymicjohn | 8:92f6baeea027 | 9 | m_pwmL(PwmOut(MC_SPEED_L)), |
Bobymicjohn | 8:92f6baeea027 | 10 | m_pwmR(PwmOut(MC_SPEED_R)) |
Bobymicjohn | 8:92f6baeea027 | 11 | |
Bobymicjohn | 8:92f6baeea027 | 12 | { |
Bobymicjohn | 8:92f6baeea027 | 13 | |
Bobymicjohn | 8:92f6baeea027 | 14 | } |
Bobymicjohn | 4:25e028102625 | 15 | |
Bobymicjohn | 8:92f6baeea027 | 16 | |
Bobymicjohn | 8:92f6baeea027 | 17 | void Motor::Update(float deltaTime) |
Bobymicjohn | 4:25e028102625 | 18 | { |
Bobymicjohn | 4:25e028102625 | 19 | |
Bobymicjohn | 4:25e028102625 | 20 | } |
Bobymicjohn | 8:92f6baeea027 | 21 | |
Bobymicjohn | 8:92f6baeea027 | 22 | void Motor::setLeftSpeed(float speed) |
Bobymicjohn | 8:92f6baeea027 | 23 | { |
Bobymicjohn | 8:92f6baeea027 | 24 | if(speed < -1.0f || speed > 1.0f) |
Bobymicjohn | 8:92f6baeea027 | 25 | return; |
Bobymicjohn | 4:25e028102625 | 26 | |
Bobymicjohn | 8:92f6baeea027 | 27 | m_pwmL.period_us(60); |
Bobymicjohn | 8:92f6baeea027 | 28 | |
Bobymicjohn | 8:92f6baeea027 | 29 | setLeftDirection(speed < 0 ? MDIR_Backward : MDIR_Forward); |
Bobymicjohn | 8:92f6baeea027 | 30 | speed = speed < 0 ? -speed : speed; |
Bobymicjohn | 8:92f6baeea027 | 31 | |
Bobymicjohn | 8:92f6baeea027 | 32 | m_pwmL = speed; |
Bobymicjohn | 4:25e028102625 | 33 | } |
Bobymicjohn | 4:25e028102625 | 34 | |
Bobymicjohn | 8:92f6baeea027 | 35 | void Motor::setRightSpeed(float speed) |
Bobymicjohn | 4:25e028102625 | 36 | { |
Bobymicjohn | 8:92f6baeea027 | 37 | if(speed < -1.0f || speed > 1.0f) |
Bobymicjohn | 8:92f6baeea027 | 38 | return; |
Bobymicjohn | 4:25e028102625 | 39 | |
Bobymicjohn | 8:92f6baeea027 | 40 | m_pwmR.period_us(60); |
Bobymicjohn | 8:92f6baeea027 | 41 | |
Bobymicjohn | 8:92f6baeea027 | 42 | setRightDirection(speed < 0 ? MDIR_Backward : MDIR_Forward); |
Bobymicjohn | 8:92f6baeea027 | 43 | speed = speed < 0 ? -speed : speed; |
Bobymicjohn | 4:25e028102625 | 44 | |
Bobymicjohn | 8:92f6baeea027 | 45 | m_pwmR = speed; |
Bobymicjohn | 4:25e028102625 | 46 | } |
Bobymicjohn | 4:25e028102625 | 47 | |
Bobymicjohn | 8:92f6baeea027 | 48 | void Motor::setSpeeds(float leftSpeed, float rightSpeed) |
Bobymicjohn | 4:25e028102625 | 49 | { |
Bobymicjohn | 4:25e028102625 | 50 | setLeftSpeed(leftSpeed); |
Bobymicjohn | 4:25e028102625 | 51 | setRightSpeed(rightSpeed); |
Bobymicjohn | 4:25e028102625 | 52 | } |
Bobymicjohn | 8:92f6baeea027 | 53 | |
Bobymicjohn | 8:92f6baeea027 | 54 | |
Bobymicjohn | 8:92f6baeea027 | 55 | void Motor::setLeftDirection(MotorDir dir) |
Bobymicjohn | 8:92f6baeea027 | 56 | { |
Bobymicjohn | 8:92f6baeea027 | 57 | m_dirL = static_cast<int>(dir); |
Bobymicjohn | 8:92f6baeea027 | 58 | } |
Bobymicjohn | 8:92f6baeea027 | 59 | |
Bobymicjohn | 8:92f6baeea027 | 60 | void Motor::setRightDirection(MotorDir dir) |
Bobymicjohn | 8:92f6baeea027 | 61 | { |
Bobymicjohn | 8:92f6baeea027 | 62 | m_dirR = static_cast<int>(dir); |
Bobymicjohn | 8:92f6baeea027 | 63 | } |
Bobymicjohn | 8:92f6baeea027 | 64 | |
Bobymicjohn | 8:92f6baeea027 | 65 | void Motor::setDirections(MotorDir dirL, MotorDir dirR) |
Bobymicjohn | 8:92f6baeea027 | 66 | { |
Bobymicjohn | 8:92f6baeea027 | 67 | setLeftDirection(dirL); |
Bobymicjohn | 8:92f6baeea027 | 68 | setRightDirection(dirR); |
Bobymicjohn | 8:92f6baeea027 | 69 | } |