j
Fork of Motordriver by
motordriver.cpp@7:cd1cd85b0eb2, 2017-05-09 (annotated)
- Committer:
- chris1996
- Date:
- Tue May 09 21:52:54 2017 +0000
- Revision:
- 7:cd1cd85b0eb2
- Parent:
- 6:a759f92efaac
SpiStuff;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
littlexc | 0:edc152f119b7 | 1 | #include "motordriver.h" |
littlexc | 1:3da7302dc9ae | 2 | |
littlexc | 0:edc152f119b7 | 3 | #include "mbed.h" |
littlexc | 1:3da7302dc9ae | 4 | |
littlexc | 0:edc152f119b7 | 5 | Motor::Motor(PinName pwm, PinName fwd, PinName rev, int brakeable): |
littlexc | 0:edc152f119b7 | 6 | _pwm(pwm), _fwd(fwd), _rev(rev) { |
littlexc | 0:edc152f119b7 | 7 | |
littlexc | 0:edc152f119b7 | 8 | // Set initial condition of PWM |
littlexc | 0:edc152f119b7 | 9 | _pwm.period(0.001); |
littlexc | 0:edc152f119b7 | 10 | _pwm = 0; |
littlexc | 0:edc152f119b7 | 11 | |
littlexc | 0:edc152f119b7 | 12 | // Initial condition of output enables |
littlexc | 0:edc152f119b7 | 13 | _fwd = 0; |
littlexc | 0:edc152f119b7 | 14 | _rev = 0; |
littlexc | 1:3da7302dc9ae | 15 | |
littlexc | 0:edc152f119b7 | 16 | //set if the motor dirver is capable of braking. (addition) |
littlexc | 0:edc152f119b7 | 17 | Brakeable= brakeable; |
littlexc | 1:3da7302dc9ae | 18 | sign = 0;//i.e nothing. |
littlexc | 0:edc152f119b7 | 19 | } |
littlexc | 1:3da7302dc9ae | 20 | |
littlexc | 2:2dc873322032 | 21 | float Motor::speed(float speed) { |
littlexc | 2:2dc873322032 | 22 | float temp = 0; |
littlexc | 1:3da7302dc9ae | 23 | if (sign == 0) { |
littlexc | 1:3da7302dc9ae | 24 | _fwd = (speed > 0.0); |
littlexc | 1:3da7302dc9ae | 25 | _rev = (speed < 0.0); |
littlexc | 2:2dc873322032 | 26 | temp = abs(speed); |
littlexc | 2:2dc873322032 | 27 | _pwm = temp; |
littlexc | 1:3da7302dc9ae | 28 | } else if (sign == 1) { |
littlexc | 1:3da7302dc9ae | 29 | if (speed < 0) { |
littlexc | 1:3da7302dc9ae | 30 | _fwd = (speed > 0.0); |
littlexc | 1:3da7302dc9ae | 31 | _rev = (speed < 0.0); |
littlexc | 1:3da7302dc9ae | 32 | _pwm = 0; |
littlexc | 2:2dc873322032 | 33 | temp = 0; |
littlexc | 4:5fb1296c0d60 | 34 | } else { |
littlexc | 1:3da7302dc9ae | 35 | _fwd = (speed > 0.0); |
littlexc | 1:3da7302dc9ae | 36 | _rev = (speed < 0.0); |
littlexc | 2:2dc873322032 | 37 | temp = abs(speed); |
littlexc | 2:2dc873322032 | 38 | _pwm = temp; |
littlexc | 1:3da7302dc9ae | 39 | } |
littlexc | 1:3da7302dc9ae | 40 | } else if (sign == -1) { |
littlexc | 1:3da7302dc9ae | 41 | if (speed > 0) { |
littlexc | 1:3da7302dc9ae | 42 | _fwd = (speed > 0.0); |
littlexc | 1:3da7302dc9ae | 43 | _rev = (speed < 0.0); |
littlexc | 1:3da7302dc9ae | 44 | _pwm = 0; |
littlexc | 2:2dc873322032 | 45 | temp = 0; |
littlexc | 1:3da7302dc9ae | 46 | } else { |
littlexc | 1:3da7302dc9ae | 47 | _fwd = (speed > 0.0); |
littlexc | 1:3da7302dc9ae | 48 | _rev = (speed < 0.0); |
littlexc | 2:2dc873322032 | 49 | temp = abs(speed); |
littlexc | 2:2dc873322032 | 50 | _pwm = temp; |
littlexc | 1:3da7302dc9ae | 51 | } |
littlexc | 1:3da7302dc9ae | 52 | } |
littlexc | 1:3da7302dc9ae | 53 | if (speed > 0) |
littlexc | 1:3da7302dc9ae | 54 | sign = 1; |
littlexc | 1:3da7302dc9ae | 55 | else if (speed < 0) { |
littlexc | 1:3da7302dc9ae | 56 | sign = -1; |
littlexc | 1:3da7302dc9ae | 57 | } else if (speed == 0) { |
littlexc | 1:3da7302dc9ae | 58 | sign = 0; |
littlexc | 1:3da7302dc9ae | 59 | } |
littlexc | 2:2dc873322032 | 60 | return temp; |
littlexc | 0:edc152f119b7 | 61 | } |
littlexc | 0:edc152f119b7 | 62 | // (additions) |
littlexc | 0:edc152f119b7 | 63 | void Motor::coast(void) { |
littlexc | 0:edc152f119b7 | 64 | _fwd = 0; |
littlexc | 0:edc152f119b7 | 65 | _rev = 0; |
littlexc | 0:edc152f119b7 | 66 | _pwm = 0; |
littlexc | 1:3da7302dc9ae | 67 | sign = 0; |
littlexc | 0:edc152f119b7 | 68 | } |
littlexc | 0:edc152f119b7 | 69 | |
littlexc | 2:2dc873322032 | 70 | float Motor::stop(float duty) { |
littlexc | 0:edc152f119b7 | 71 | if (Brakeable == 1) { |
littlexc | 0:edc152f119b7 | 72 | _fwd = 1; |
littlexc | 0:edc152f119b7 | 73 | _rev = 1; |
littlexc | 2:2dc873322032 | 74 | _pwm = duty; |
littlexc | 1:3da7302dc9ae | 75 | sign = 0; |
littlexc | 2:2dc873322032 | 76 | return duty; |
littlexc | 1:3da7302dc9ae | 77 | } else |
littlexc | 5:3110b9209d3c | 78 | Motor::coast(); |
littlexc | 3:8822f4955035 | 79 | return -1; |
littlexc | 0:edc152f119b7 | 80 | } |
littlexc | 1:3da7302dc9ae | 81 | |
littlexc | 4:5fb1296c0d60 | 82 | float Motor::state(void) { |
littlexc | 4:5fb1296c0d60 | 83 | if ((_fwd == _rev) && (_pwm > 0)) { |
littlexc | 4:5fb1296c0d60 | 84 | return -2;//braking |
littlexc | 4:5fb1296c0d60 | 85 | } else if (_pwm == 0) { |
littlexc | 4:5fb1296c0d60 | 86 | return 2;//coasting |
littlexc | 4:5fb1296c0d60 | 87 | } else if ((_fwd == 0) && (_rev == 1)) { |
littlexc | 4:5fb1296c0d60 | 88 | return -(_pwm);//reversing |
littlexc | 4:5fb1296c0d60 | 89 | } else if ((_fwd == 1) && (_rev == 0)) { |
littlexc | 4:5fb1296c0d60 | 90 | return _pwm;//fowards |
littlexc | 4:5fb1296c0d60 | 91 | } else |
littlexc | 4:5fb1296c0d60 | 92 | return -3;//error |
chris1996 | 6:a759f92efaac | 93 | } |