![](/media/cache/profiles/1f47cef5e38c952f94c5d61726027439.50x50_q85.jpg)
Based on PS3_BlueUSB with reference to http://blog.goo.ne.jp/roboz80/e/10e7bf38d3a63b996ca2894e9fb5e3b6
motordriver.cpp@0:44619612f575, 2011-07-05 (annotated)
- Committer:
- kenbumono
- Date:
- Tue Jul 05 08:25:40 2011 +0000
- Revision:
- 0:44619612f575
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kenbumono | 0:44619612f575 | 1 | /*motor driver libary modified from the following libary, |
kenbumono | 0:44619612f575 | 2 | * |
kenbumono | 0:44619612f575 | 3 | * mbed simple H-bridge motor controller |
kenbumono | 0:44619612f575 | 4 | * Copyright (c) 2007-2010, sford |
kenbumono | 0:44619612f575 | 5 | * |
kenbumono | 0:44619612f575 | 6 | * by Christopher Hasler. |
kenbumono | 0:44619612f575 | 7 | * |
kenbumono | 0:44619612f575 | 8 | * from sford's libary, |
kenbumono | 0:44619612f575 | 9 | * |
kenbumono | 0:44619612f575 | 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
kenbumono | 0:44619612f575 | 11 | * of this software and associated documentation files (the "Software"), to deal |
kenbumono | 0:44619612f575 | 12 | * in the Software without restriction, including without limitation the rights |
kenbumono | 0:44619612f575 | 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
kenbumono | 0:44619612f575 | 14 | * copies of the Software, and to permit persons to whom the Software is |
kenbumono | 0:44619612f575 | 15 | * furnished to do so, subject to the following conditions: |
kenbumono | 0:44619612f575 | 16 | * |
kenbumono | 0:44619612f575 | 17 | * The above copyright notice and this permission notice shall be included in |
kenbumono | 0:44619612f575 | 18 | * all copies or substantial portions of the Software. |
kenbumono | 0:44619612f575 | 19 | * |
kenbumono | 0:44619612f575 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
kenbumono | 0:44619612f575 | 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
kenbumono | 0:44619612f575 | 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
kenbumono | 0:44619612f575 | 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
kenbumono | 0:44619612f575 | 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
kenbumono | 0:44619612f575 | 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
kenbumono | 0:44619612f575 | 26 | * THE SOFTWARE. |
kenbumono | 0:44619612f575 | 27 | */ |
kenbumono | 0:44619612f575 | 28 | |
kenbumono | 0:44619612f575 | 29 | #include "motordriver.h" |
kenbumono | 0:44619612f575 | 30 | |
kenbumono | 0:44619612f575 | 31 | #include "mbed.h" |
kenbumono | 0:44619612f575 | 32 | |
kenbumono | 0:44619612f575 | 33 | Motor::Motor(PinName pwm, PinName fwd, PinName rev, bool brakeable): |
kenbumono | 0:44619612f575 | 34 | _pwm(pwm), _fwd(fwd), _rev(rev), _brakeable(brakeable), _sign(0) { |
kenbumono | 0:44619612f575 | 35 | |
kenbumono | 0:44619612f575 | 36 | // Set initial condition of PWM |
kenbumono | 0:44619612f575 | 37 | _pwm.period(0.001); |
kenbumono | 0:44619612f575 | 38 | _pwm = 0; |
kenbumono | 0:44619612f575 | 39 | |
kenbumono | 0:44619612f575 | 40 | // Initial condition of output enables |
kenbumono | 0:44619612f575 | 41 | _fwd = 0; |
kenbumono | 0:44619612f575 | 42 | _rev = 0; |
kenbumono | 0:44619612f575 | 43 | } |
kenbumono | 0:44619612f575 | 44 | |
kenbumono | 0:44619612f575 | 45 | float Motor::speed(float speed) { |
kenbumono | 0:44619612f575 | 46 | float temp = 0; |
kenbumono | 0:44619612f575 | 47 | if (_sign == 0) { |
kenbumono | 0:44619612f575 | 48 | _fwd = (speed > 0.0); |
kenbumono | 0:44619612f575 | 49 | _rev = (speed < 0.0); |
kenbumono | 0:44619612f575 | 50 | temp = abs(speed); |
kenbumono | 0:44619612f575 | 51 | _pwm = temp; |
kenbumono | 0:44619612f575 | 52 | } else if (_sign == 1) { |
kenbumono | 0:44619612f575 | 53 | if (speed < 0) { |
kenbumono | 0:44619612f575 | 54 | _fwd = (speed > 0.0); |
kenbumono | 0:44619612f575 | 55 | _rev = (speed < 0.0); |
kenbumono | 0:44619612f575 | 56 | _pwm = 0; |
kenbumono | 0:44619612f575 | 57 | temp = 0; |
kenbumono | 0:44619612f575 | 58 | } else { |
kenbumono | 0:44619612f575 | 59 | _fwd = (speed > 0.0); |
kenbumono | 0:44619612f575 | 60 | _rev = (speed < 0.0); |
kenbumono | 0:44619612f575 | 61 | temp = abs(speed); |
kenbumono | 0:44619612f575 | 62 | _pwm = temp; |
kenbumono | 0:44619612f575 | 63 | } |
kenbumono | 0:44619612f575 | 64 | } else if (_sign == -1) { |
kenbumono | 0:44619612f575 | 65 | if (speed > 0) { |
kenbumono | 0:44619612f575 | 66 | _fwd = (speed > 0.0); |
kenbumono | 0:44619612f575 | 67 | _rev = (speed < 0.0); |
kenbumono | 0:44619612f575 | 68 | _pwm = 0; |
kenbumono | 0:44619612f575 | 69 | temp = 0; |
kenbumono | 0:44619612f575 | 70 | } else { |
kenbumono | 0:44619612f575 | 71 | _fwd = (speed > 0.0); |
kenbumono | 0:44619612f575 | 72 | _rev = (speed < 0.0); |
kenbumono | 0:44619612f575 | 73 | temp = abs(speed); |
kenbumono | 0:44619612f575 | 74 | _pwm = temp; |
kenbumono | 0:44619612f575 | 75 | } |
kenbumono | 0:44619612f575 | 76 | } |
kenbumono | 0:44619612f575 | 77 | if (speed > 0) |
kenbumono | 0:44619612f575 | 78 | _sign = 1; |
kenbumono | 0:44619612f575 | 79 | else if (speed < 0) { |
kenbumono | 0:44619612f575 | 80 | _sign = -1; |
kenbumono | 0:44619612f575 | 81 | } else if (speed == 0) { |
kenbumono | 0:44619612f575 | 82 | _sign = 0; |
kenbumono | 0:44619612f575 | 83 | } |
kenbumono | 0:44619612f575 | 84 | return temp; |
kenbumono | 0:44619612f575 | 85 | } |
kenbumono | 0:44619612f575 | 86 | // (additions) |
kenbumono | 0:44619612f575 | 87 | void Motor::coast(void) { |
kenbumono | 0:44619612f575 | 88 | _fwd = 0; |
kenbumono | 0:44619612f575 | 89 | _rev = 0; |
kenbumono | 0:44619612f575 | 90 | _pwm = 0; |
kenbumono | 0:44619612f575 | 91 | _sign = 0; |
kenbumono | 0:44619612f575 | 92 | } |
kenbumono | 0:44619612f575 | 93 | |
kenbumono | 0:44619612f575 | 94 | float Motor::stop(float duty) { |
kenbumono | 0:44619612f575 | 95 | if (_brakeable == 1) { |
kenbumono | 0:44619612f575 | 96 | _fwd = 1; |
kenbumono | 0:44619612f575 | 97 | _rev = 1; |
kenbumono | 0:44619612f575 | 98 | _pwm = duty; |
kenbumono | 0:44619612f575 | 99 | _sign = 0; |
kenbumono | 0:44619612f575 | 100 | return duty; |
kenbumono | 0:44619612f575 | 101 | } else |
kenbumono | 0:44619612f575 | 102 | Motor::coast(); |
kenbumono | 0:44619612f575 | 103 | return -1; // error, can't brake |
kenbumono | 0:44619612f575 | 104 | } |
kenbumono | 0:44619612f575 | 105 | |
kenbumono | 0:44619612f575 | 106 | float Motor::state(void) const { |
kenbumono | 0:44619612f575 | 107 | int fwd = _fwd.read(); |
kenbumono | 0:44619612f575 | 108 | int rev = _rev.read(); |
kenbumono | 0:44619612f575 | 109 | float pwm = _pwm.read(); |
kenbumono | 0:44619612f575 | 110 | |
kenbumono | 0:44619612f575 | 111 | if ((fwd == rev) && (pwm > 0)) { |
kenbumono | 0:44619612f575 | 112 | return -2;//braking |
kenbumono | 0:44619612f575 | 113 | } else if (pwm == 0) { |
kenbumono | 0:44619612f575 | 114 | return 2;//coasting |
kenbumono | 0:44619612f575 | 115 | } else if ((fwd == 0) && (rev == 1)) { |
kenbumono | 0:44619612f575 | 116 | return -pwm;//reversing |
kenbumono | 0:44619612f575 | 117 | } else if ((fwd == 1) && (rev == 0)) { |
kenbumono | 0:44619612f575 | 118 | return pwm;//fowards |
kenbumono | 0:44619612f575 | 119 | } else |
kenbumono | 0:44619612f575 | 120 | return -3;//error |
kenbumono | 0:44619612f575 | 121 | } |
kenbumono | 0:44619612f575 | 122 | |
kenbumono | 0:44619612f575 | 123 | //test code, this demonstrates working motor drivers. |
kenbumono | 0:44619612f575 | 124 | |
kenbumono | 0:44619612f575 | 125 | //Motor A(p22, p6, p5, 1); // pwm, fwd, rev, can break |
kenbumono | 0:44619612f575 | 126 | /* |
kenbumono | 0:44619612f575 | 127 | Motor A(p22, p7, p8, 1); // pwm, fwd, rev, can break |
kenbumono | 0:44619612f575 | 128 | Motor B(p21, p5, p6, 1); // pwm, fwd, rev, can break |
kenbumono | 0:44619612f575 | 129 | int main() { |
kenbumono | 0:44619612f575 | 130 | for (float s=-1.0; s < 1.0 ; s += 0.01) { |
kenbumono | 0:44619612f575 | 131 | //A.speed(s); |
kenbumono | 0:44619612f575 | 132 | A.speed(s); |
kenbumono | 0:44619612f575 | 133 | B.speed(s); |
kenbumono | 0:44619612f575 | 134 | wait(0.02); |
kenbumono | 0:44619612f575 | 135 | } |
kenbumono | 0:44619612f575 | 136 | A.stop(0); |
kenbumono | 0:44619612f575 | 137 | B.stop(0); |
kenbumono | 0:44619612f575 | 138 | wait(1); |
kenbumono | 0:44619612f575 | 139 | A.coast(); |
kenbumono | 0:44619612f575 | 140 | B.coast(); |
kenbumono | 0:44619612f575 | 141 | } |
kenbumono | 0:44619612f575 | 142 | */ |