alpha a
motor.cpp@0:52cb035f23fa, 2019-01-17 (annotated)
- Committer:
- martinsimpson
- Date:
- Thu Jan 17 17:01:11 2019 +0000
- Revision:
- 0:52cb035f23fa
try3;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
martinsimpson | 0:52cb035f23fa | 1 | /** A Motor Driver, used for setting the pins for PWM Outpu |
martinsimpson | 0:52cb035f23fa | 2 | * for use with ROCO104 |
martinsimpson | 0:52cb035f23fa | 3 | * |
martinsimpson | 0:52cb035f23fa | 4 | * @note Synchronization level: Interrupt safe |
martinsimpson | 0:52cb035f23fa | 5 | * |
martinsimpson | 0:52cb035f23fa | 6 | * Example: |
martinsimpson | 0:52cb035f23fa | 7 | * @code |
martinsimpson | 0:52cb035f23fa | 8 | * // Toggle a LED |
martinsimpson | 0:52cb035f23fa | 9 | * #include "mbed.h" |
martinsimpson | 0:52cb035f23fa | 10 | * |
martinsimpson | 0:52cb035f23fa | 11 | * Motor Wheels(D15,D14,D13,D12); |
martinsimpson | 0:52cb035f23fa | 12 | * |
martinsimpson | 0:52cb035f23fa | 13 | * int main() |
martinsimpson | 0:52cb035f23fa | 14 | * { |
martinsimpson | 0:52cb035f23fa | 15 | * Wheel.Period_in_ms(2);//Set frequency of the PWMs 500Hz |
martinsimpson | 0:52cb035f23fa | 16 | * while(true) |
martinsimpson | 0:52cb035f23fa | 17 | * { |
martinsimpson | 0:52cb035f23fa | 18 | * Wheel.Speed(0.8,0.8);//Forward 80% |
martinsimpson | 0:52cb035f23fa | 19 | * wait(5.0); |
martinsimpson | 0:52cb035f23fa | 20 | * Wheel.stop(); |
martinsimpson | 0:52cb035f23fa | 21 | * wait(1.0); |
martinsimpson | 0:52cb035f23fa | 22 | * Wheel.Speed(-0.8,-0.8);//Reverse 80% |
martinsimpson | 0:52cb035f23fa | 23 | * wait(5.0); |
martinsimpson | 0:52cb035f23fa | 24 | * Wheel.stop(); |
martinsimpson | 0:52cb035f23fa | 25 | * wait(1.0); |
martinsimpson | 0:52cb035f23fa | 26 | * } |
martinsimpson | 0:52cb035f23fa | 27 | * } |
martinsimpson | 0:52cb035f23fa | 28 | * @endcode |
martinsimpson | 0:52cb035f23fa | 29 | */ |
martinsimpson | 0:52cb035f23fa | 30 | |
martinsimpson | 0:52cb035f23fa | 31 | #include "motor.h" |
martinsimpson | 0:52cb035f23fa | 32 | Motor::Motor(PinName pinName1, PinName pinName2, PinName pinName3, PinName pinName4) : pin1(pinName1), pin2(pinName2), pin3(pinName3), pin4(pinName4) |
martinsimpson | 0:52cb035f23fa | 33 | { |
martinsimpson | 0:52cb035f23fa | 34 | } |
martinsimpson | 0:52cb035f23fa | 35 | void Motor::Fwd(float duty) |
martinsimpson | 0:52cb035f23fa | 36 | { |
martinsimpson | 0:52cb035f23fa | 37 | this->pin1 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 38 | this->pin2 = duty; |
martinsimpson | 0:52cb035f23fa | 39 | this->pin3 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 40 | this->pin4 = duty; |
martinsimpson | 0:52cb035f23fa | 41 | } |
martinsimpson | 0:52cb035f23fa | 42 | void Motor::Rev(float duty) |
martinsimpson | 0:52cb035f23fa | 43 | { |
martinsimpson | 0:52cb035f23fa | 44 | this->pin1 = duty; |
martinsimpson | 0:52cb035f23fa | 45 | this->pin2 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 46 | this->pin3 = duty; |
martinsimpson | 0:52cb035f23fa | 47 | this->pin4 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 48 | } |
martinsimpson | 0:52cb035f23fa | 49 | void Motor::Stop(void) |
martinsimpson | 0:52cb035f23fa | 50 | { |
martinsimpson | 0:52cb035f23fa | 51 | this->pin1 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 52 | this->pin2 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 53 | this->pin3 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 54 | this->pin4 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 55 | } |
martinsimpson | 0:52cb035f23fa | 56 | int Motor::Speed(float speedA, float speedB) |
martinsimpson | 0:52cb035f23fa | 57 | { |
martinsimpson | 0:52cb035f23fa | 58 | if(speedA>1.0f||speedA<-1.0f){ //CHECK speedA Value is in Range! |
martinsimpson | 0:52cb035f23fa | 59 | return -1; //return ERROR code -1=speedA Value out of range! EXIT Function |
martinsimpson | 0:52cb035f23fa | 60 | } |
martinsimpson | 0:52cb035f23fa | 61 | if(speedB>1.0f||speedA<-1.0f){ //CHECK speedB Value is in Range! |
martinsimpson | 0:52cb035f23fa | 62 | return -2; //return ERROR code -2=speedB Value out of range! EXIT Function |
martinsimpson | 0:52cb035f23fa | 63 | } |
martinsimpson | 0:52cb035f23fa | 64 | |
martinsimpson | 0:52cb035f23fa | 65 | //If speed values have passed the checks above then the following code will be executed |
martinsimpson | 0:52cb035f23fa | 66 | |
martinsimpson | 0:52cb035f23fa | 67 | if(speedA<0.0f) |
martinsimpson | 0:52cb035f23fa | 68 | { //Reverse A motor |
martinsimpson | 0:52cb035f23fa | 69 | this->pin1 = -speedA; |
martinsimpson | 0:52cb035f23fa | 70 | this->pin2 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 71 | } |
martinsimpson | 0:52cb035f23fa | 72 | |
martinsimpson | 0:52cb035f23fa | 73 | else |
martinsimpson | 0:52cb035f23fa | 74 | { //Forward A motor |
martinsimpson | 0:52cb035f23fa | 75 | this->pin1 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 76 | this->pin2 = speedA; |
martinsimpson | 0:52cb035f23fa | 77 | } |
martinsimpson | 0:52cb035f23fa | 78 | |
martinsimpson | 0:52cb035f23fa | 79 | if(speedB<0.0f) |
martinsimpson | 0:52cb035f23fa | 80 | { //Reverse B motor |
martinsimpson | 0:52cb035f23fa | 81 | this->pin3 = -speedB; |
martinsimpson | 0:52cb035f23fa | 82 | this->pin4 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 83 | } |
martinsimpson | 0:52cb035f23fa | 84 | else |
martinsimpson | 0:52cb035f23fa | 85 | { //Forward B motor |
martinsimpson | 0:52cb035f23fa | 86 | this->pin3 = 0.0f; |
martinsimpson | 0:52cb035f23fa | 87 | this->pin4 = speedB; |
martinsimpson | 0:52cb035f23fa | 88 | } |
martinsimpson | 0:52cb035f23fa | 89 | return 0; //Return ERROR code Zero i.e. NO ERROR success! |
martinsimpson | 0:52cb035f23fa | 90 | } |
martinsimpson | 0:52cb035f23fa | 91 | |
martinsimpson | 0:52cb035f23fa | 92 | void Motor::Period_in_ms(int msPeriod) |
martinsimpson | 0:52cb035f23fa | 93 | { |
martinsimpson | 0:52cb035f23fa | 94 | this->pin1.period_ms(msPeriod); |
martinsimpson | 0:52cb035f23fa | 95 | this->pin2.period_ms(msPeriod); |
martinsimpson | 0:52cb035f23fa | 96 | this->pin3.period_ms(msPeriod); |
martinsimpson | 0:52cb035f23fa | 97 | this->pin4.period_ms(msPeriod); |
martinsimpson | 0:52cb035f23fa | 98 | } |