Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
motor.cpp@1:3ca91ad8e927, 2018-12-14 (annotated)
- Committer:
- martinsimpson
- Date:
- Fri Dec 14 14:24:52 2018 +0000
- Revision:
- 1:3ca91ad8e927
- Parent:
- 0:51c12cc34baf
Version Alpha 0.1a
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| martinsimpson | 0:51c12cc34baf | 1 | #include "motor.h" |
| martinsimpson | 0:51c12cc34baf | 2 | |
| martinsimpson | 1:3ca91ad8e927 | 3 | Motor::Motor(PinName pinName1, PinName pinName2, PinName pinName3, PinName pinName4) : pin1(pinName1), pin2(pinName2), pin3(pinName3), pin4(pinName4) |
| martinsimpson | 0:51c12cc34baf | 4 | { |
| martinsimpson | 0:51c12cc34baf | 5 | } |
| martinsimpson | 0:51c12cc34baf | 6 | void Motor::Fwd(float duty) |
| martinsimpson | 0:51c12cc34baf | 7 | { |
| martinsimpson | 0:51c12cc34baf | 8 | this->pin1 = 0.0f; |
| martinsimpson | 0:51c12cc34baf | 9 | this->pin2 = duty; |
| martinsimpson | 1:3ca91ad8e927 | 10 | this->pin3 = 0.0f; |
| martinsimpson | 1:3ca91ad8e927 | 11 | this->pin4 = duty; |
| martinsimpson | 0:51c12cc34baf | 12 | } |
| martinsimpson | 0:51c12cc34baf | 13 | void Motor::Rev(float duty) |
| martinsimpson | 0:51c12cc34baf | 14 | { |
| martinsimpson | 0:51c12cc34baf | 15 | this->pin1 = duty; |
| martinsimpson | 0:51c12cc34baf | 16 | this->pin2 = 0.0f; |
| martinsimpson | 1:3ca91ad8e927 | 17 | this->pin3 = duty; |
| martinsimpson | 1:3ca91ad8e927 | 18 | this->pin4 = 0.0f; |
| martinsimpson | 0:51c12cc34baf | 19 | } |
| martinsimpson | 1:3ca91ad8e927 | 20 | void Motor::Stop(void) |
| martinsimpson | 1:3ca91ad8e927 | 21 | { |
| martinsimpson | 1:3ca91ad8e927 | 22 | this->pin1 = 0.0f; |
| martinsimpson | 1:3ca91ad8e927 | 23 | this->pin2 = 0.0f; |
| martinsimpson | 1:3ca91ad8e927 | 24 | this->pin3 = 0.0f; |
| martinsimpson | 1:3ca91ad8e927 | 25 | this->pin4 = 0.0f; |
| martinsimpson | 1:3ca91ad8e927 | 26 | } |
| martinsimpson | 1:3ca91ad8e927 | 27 | int Motor::Speed(float speedA, float speedB) |
| martinsimpson | 1:3ca91ad8e927 | 28 | { |
| martinsimpson | 1:3ca91ad8e927 | 29 | if(speedA>1.0f||speedA<-1.0f){ //CHECK speedA Value is in Range! |
| martinsimpson | 1:3ca91ad8e927 | 30 | return -1; //return ERROR code -1=speedA Value out of range! EXIT Function |
| martinsimpson | 1:3ca91ad8e927 | 31 | } |
| martinsimpson | 1:3ca91ad8e927 | 32 | if(speedB>1.0f||speedA<-1.0f){ //CHECK speedB Value is in Range! |
| martinsimpson | 1:3ca91ad8e927 | 33 | return -2; //return ERROR code -2=speedB Value out of range! EXIT Function |
| martinsimpson | 1:3ca91ad8e927 | 34 | } |
| martinsimpson | 1:3ca91ad8e927 | 35 | |
| martinsimpson | 1:3ca91ad8e927 | 36 | //If speed values have passed the checks above then the following code will be executed |
| martinsimpson | 1:3ca91ad8e927 | 37 | |
| martinsimpson | 1:3ca91ad8e927 | 38 | if(speedA<0.0f) |
| martinsimpson | 1:3ca91ad8e927 | 39 | { //Reverse A motor |
| martinsimpson | 1:3ca91ad8e927 | 40 | this->pin1 = -speedA; |
| martinsimpson | 1:3ca91ad8e927 | 41 | this->pin2 = 0.0f; |
| martinsimpson | 1:3ca91ad8e927 | 42 | } |
| martinsimpson | 1:3ca91ad8e927 | 43 | |
| martinsimpson | 1:3ca91ad8e927 | 44 | else |
| martinsimpson | 1:3ca91ad8e927 | 45 | { //Forward A motor |
| martinsimpson | 1:3ca91ad8e927 | 46 | this->pin1 = 0.0f; |
| martinsimpson | 1:3ca91ad8e927 | 47 | this->pin2 = speedA; |
| martinsimpson | 1:3ca91ad8e927 | 48 | } |
| martinsimpson | 1:3ca91ad8e927 | 49 | |
| martinsimpson | 1:3ca91ad8e927 | 50 | if(speedB<0.0f) |
| martinsimpson | 1:3ca91ad8e927 | 51 | { //Reverse B motor |
| martinsimpson | 1:3ca91ad8e927 | 52 | this->pin3 = -speedB; |
| martinsimpson | 1:3ca91ad8e927 | 53 | this->pin4 = 0.0f; |
| martinsimpson | 1:3ca91ad8e927 | 54 | } |
| martinsimpson | 1:3ca91ad8e927 | 55 | else |
| martinsimpson | 1:3ca91ad8e927 | 56 | { //Forward B motor |
| martinsimpson | 1:3ca91ad8e927 | 57 | this->pin3 = 0.0f; |
| martinsimpson | 1:3ca91ad8e927 | 58 | this->pin4 = speedB; |
| martinsimpson | 1:3ca91ad8e927 | 59 | } |
| martinsimpson | 1:3ca91ad8e927 | 60 | return 0; //Return ERROR code Zero i.e. NO ERROR success! |
| martinsimpson | 1:3ca91ad8e927 | 61 | } |
| martinsimpson | 1:3ca91ad8e927 | 62 | |
| martinsimpson | 0:51c12cc34baf | 63 | void Motor::Period_in_ms(int msPeriod) |
| martinsimpson | 0:51c12cc34baf | 64 | { |
| martinsimpson | 0:51c12cc34baf | 65 | this->pin1.period_ms(msPeriod); |
| martinsimpson | 0:51c12cc34baf | 66 | this->pin2.period_ms(msPeriod); |
| martinsimpson | 1:3ca91ad8e927 | 67 | this->pin3.period_ms(msPeriod); |
| martinsimpson | 1:3ca91ad8e927 | 68 | this->pin4.period_ms(msPeriod); |
| martinsimpson | 0:51c12cc34baf | 69 | } |