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@4:25e028102625, 2017-02-02 (annotated)
- Committer:
- Bobymicjohn
- Date:
- Thu Feb 02 23:30:41 2017 +0000
- Revision:
- 4:25e028102625
- Child:
- 8:92f6baeea027
Added 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 | 4:25e028102625 | 3 | /* |
Bobymicjohn | 4:25e028102625 | 4 | DigitalOut DIR_L(PTA13); |
Bobymicjohn | 4:25e028102625 | 5 | DigitalOut DIR_R(PTC9); |
Bobymicjohn | 4:25e028102625 | 6 | |
Bobymicjohn | 4:25e028102625 | 7 | PwmOut PWM_L(PTD0); |
Bobymicjohn | 4:25e028102625 | 8 | PwmOut PWM_R(PTD5); |
Bobymicjohn | 4:25e028102625 | 9 | |
Bobymicjohn | 4:25e028102625 | 10 | Motor::Motor() |
Bobymicjohn | 4:25e028102625 | 11 | { |
Bobymicjohn | 4:25e028102625 | 12 | |
Bobymicjohn | 4:25e028102625 | 13 | } |
Bobymicjohn | 4:25e028102625 | 14 | |
Bobymicjohn | 4:25e028102625 | 15 | void Motor::initializeMotor() |
Bobymicjohn | 4:25e028102625 | 16 | { |
Bobymicjohn | 4:25e028102625 | 17 | } |
Bobymicjohn | 4:25e028102625 | 18 | |
Bobymicjohn | 4:25e028102625 | 19 | void Motor::setLeftSpeed(int speed) |
Bobymicjohn | 4:25e028102625 | 20 | { |
Bobymicjohn | 4:25e028102625 | 21 | init(); |
Bobymicjohn | 4:25e028102625 | 22 | PWM_L.period_us(60); |
Bobymicjohn | 4:25e028102625 | 23 | |
Bobymicjohn | 4:25e028102625 | 24 | bool reverse = 0; |
Bobymicjohn | 4:25e028102625 | 25 | |
Bobymicjohn | 4:25e028102625 | 26 | if(speed < 0) |
Bobymicjohn | 4:25e028102625 | 27 | { |
Bobymicjohn | 4:25e028102625 | 28 | speed = -speed; |
Bobymicjohn | 4:25e028102625 | 29 | reverse = 1; |
Bobymicjohn | 4:25e028102625 | 30 | } |
Bobymicjohn | 4:25e028102625 | 31 | //Set Max Speed |
Bobymicjohn | 4:25e028102625 | 32 | if(speed > 60) |
Bobymicjohn | 4:25e028102625 | 33 | speed = 60; |
Bobymicjohn | 4:25e028102625 | 34 | |
Bobymicjohn | 4:25e028102625 | 35 | if(reverse) |
Bobymicjohn | 4:25e028102625 | 36 | DIR_L = 1; |
Bobymicjohn | 4:25e028102625 | 37 | else |
Bobymicjohn | 4:25e028102625 | 38 | DIR_L = 0; |
Bobymicjohn | 4:25e028102625 | 39 | |
Bobymicjohn | 4:25e028102625 | 40 | PWM_L.pulsewidth_us(speed); |
Bobymicjohn | 4:25e028102625 | 41 | } |
Bobymicjohn | 4:25e028102625 | 42 | |
Bobymicjohn | 4:25e028102625 | 43 | void Motor::setRightSpeed(int speed) |
Bobymicjohn | 4:25e028102625 | 44 | { |
Bobymicjohn | 4:25e028102625 | 45 | init(); |
Bobymicjohn | 4:25e028102625 | 46 | PWM_R.period_us(60); |
Bobymicjohn | 4:25e028102625 | 47 | |
Bobymicjohn | 4:25e028102625 | 48 | bool reverse = 0; |
Bobymicjohn | 4:25e028102625 | 49 | |
Bobymicjohn | 4:25e028102625 | 50 | if(speed < 0) |
Bobymicjohn | 4:25e028102625 | 51 | { |
Bobymicjohn | 4:25e028102625 | 52 | speed = -speed; |
Bobymicjohn | 4:25e028102625 | 53 | reverse = 1; |
Bobymicjohn | 4:25e028102625 | 54 | } |
Bobymicjohn | 4:25e028102625 | 55 | //Set Max Speed |
Bobymicjohn | 4:25e028102625 | 56 | if(speed > 60) |
Bobymicjohn | 4:25e028102625 | 57 | speed = 60; |
Bobymicjohn | 4:25e028102625 | 58 | |
Bobymicjohn | 4:25e028102625 | 59 | if(reverse) |
Bobymicjohn | 4:25e028102625 | 60 | DIR_R = 1; |
Bobymicjohn | 4:25e028102625 | 61 | else |
Bobymicjohn | 4:25e028102625 | 62 | DIR_R = 0; |
Bobymicjohn | 4:25e028102625 | 63 | |
Bobymicjohn | 4:25e028102625 | 64 | PWM_R.pulsewidth_us(speed); |
Bobymicjohn | 4:25e028102625 | 65 | } |
Bobymicjohn | 4:25e028102625 | 66 | |
Bobymicjohn | 4:25e028102625 | 67 | void Motor::setSpeeds(int leftSpeed, int rightSpeed) |
Bobymicjohn | 4:25e028102625 | 68 | { |
Bobymicjohn | 4:25e028102625 | 69 | setLeftSpeed(leftSpeed); |
Bobymicjohn | 4:25e028102625 | 70 | setRightSpeed(rightSpeed); |
Bobymicjohn | 4:25e028102625 | 71 | } |
Bobymicjohn | 4:25e028102625 | 72 | */ |