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 haofan Zheng

Committer:
Bobymicjohn
Date:
Tue Feb 07 21:58:20 2017 +0000
Revision:
11:676ea42afd56
Parent:
9:b72e18f80f49
Child:
13:7dcb1642ef99
Finished Core, and Servo classes.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bobymicjohn 4:25e028102625 1 #include "Motor.h"
Bobymicjohn 4:25e028102625 2 #include "mbed.h"
Bobymicjohn 8:92f6baeea027 3
Bobymicjohn 11:676ea42afd56 4 #include "Core.h"
Bobymicjohn 11:676ea42afd56 5
Bobymicjohn 8:92f6baeea027 6 #include "PinAssignment.h"
Bobymicjohn 4:25e028102625 7
Bobymicjohn 11:676ea42afd56 8 Motor::Motor(SW::Core& core) :
Bobymicjohn 11:676ea42afd56 9 m_core(core),
Bobymicjohn 8:92f6baeea027 10 m_dirL(DigitalOut(MC_DIR_L, 1)),
Bobymicjohn 8:92f6baeea027 11 m_dirR(DigitalOut(MC_DIR_R, 1)),
Bobymicjohn 8:92f6baeea027 12 m_pwmL(PwmOut(MC_SPEED_L)),
Bobymicjohn 8:92f6baeea027 13 m_pwmR(PwmOut(MC_SPEED_R))
Bobymicjohn 8:92f6baeea027 14
Bobymicjohn 8:92f6baeea027 15 {
Bobymicjohn 8:92f6baeea027 16
Bobymicjohn 8:92f6baeea027 17 }
Bobymicjohn 4:25e028102625 18
Bobymicjohn 8:92f6baeea027 19
Bobymicjohn 8:92f6baeea027 20 void Motor::Update(float deltaTime)
Bobymicjohn 4:25e028102625 21 {
Bobymicjohn 4:25e028102625 22
Bobymicjohn 4:25e028102625 23 }
Bobymicjohn 8:92f6baeea027 24
Bobymicjohn 8:92f6baeea027 25 void Motor::setLeftSpeed(float speed)
Bobymicjohn 8:92f6baeea027 26 {
Bobymicjohn 8:92f6baeea027 27 if(speed < -1.0f || speed > 1.0f)
Bobymicjohn 8:92f6baeea027 28 return;
Bobymicjohn 4:25e028102625 29
Bobymicjohn 8:92f6baeea027 30 m_pwmL.period_us(60);
Bobymicjohn 8:92f6baeea027 31
Bobymicjohn 8:92f6baeea027 32 setLeftDirection(speed < 0 ? MDIR_Backward : MDIR_Forward);
Bobymicjohn 8:92f6baeea027 33 speed = speed < 0 ? -speed : speed;
Bobymicjohn 8:92f6baeea027 34
Bobymicjohn 8:92f6baeea027 35 m_pwmL = speed;
Bobymicjohn 4:25e028102625 36 }
Bobymicjohn 4:25e028102625 37
Bobymicjohn 8:92f6baeea027 38 void Motor::setRightSpeed(float speed)
Bobymicjohn 4:25e028102625 39 {
Bobymicjohn 8:92f6baeea027 40 if(speed < -1.0f || speed > 1.0f)
Bobymicjohn 8:92f6baeea027 41 return;
Bobymicjohn 4:25e028102625 42
Bobymicjohn 8:92f6baeea027 43 m_pwmR.period_us(60);
Bobymicjohn 8:92f6baeea027 44
Bobymicjohn 8:92f6baeea027 45 setRightDirection(speed < 0 ? MDIR_Backward : MDIR_Forward);
Bobymicjohn 8:92f6baeea027 46 speed = speed < 0 ? -speed : speed;
Bobymicjohn 4:25e028102625 47
Bobymicjohn 8:92f6baeea027 48 m_pwmR = speed;
Bobymicjohn 4:25e028102625 49 }
Bobymicjohn 4:25e028102625 50
Bobymicjohn 8:92f6baeea027 51 void Motor::setSpeeds(float leftSpeed, float rightSpeed)
Bobymicjohn 4:25e028102625 52 {
Bobymicjohn 4:25e028102625 53 setLeftSpeed(leftSpeed);
Bobymicjohn 4:25e028102625 54 setRightSpeed(rightSpeed);
Bobymicjohn 4:25e028102625 55 }
Bobymicjohn 8:92f6baeea027 56
Bobymicjohn 8:92f6baeea027 57
Bobymicjohn 8:92f6baeea027 58 void Motor::setLeftDirection(MotorDir dir)
Bobymicjohn 8:92f6baeea027 59 {
Bobymicjohn 8:92f6baeea027 60 m_dirL = static_cast<int>(dir);
Bobymicjohn 8:92f6baeea027 61 }
Bobymicjohn 8:92f6baeea027 62
Bobymicjohn 8:92f6baeea027 63 void Motor::setRightDirection(MotorDir dir)
Bobymicjohn 8:92f6baeea027 64 {
Bobymicjohn 8:92f6baeea027 65 m_dirR = static_cast<int>(dir);
Bobymicjohn 8:92f6baeea027 66 }
Bobymicjohn 8:92f6baeea027 67
Bobymicjohn 8:92f6baeea027 68 void Motor::setDirections(MotorDir dirL, MotorDir dirR)
Bobymicjohn 8:92f6baeea027 69 {
Bobymicjohn 8:92f6baeea027 70 setLeftDirection(dirL);
Bobymicjohn 8:92f6baeea027 71 setRightDirection(dirR);
Bobymicjohn 8:92f6baeea027 72 }