motor drive libary to use speed control, coasting and dynamic braking. NOTE, dynamic braking my result in large currents. this may or may not set the motor driver on fire/break it/other undesired effects. so read the data sheet folks.
Dependents: motordrivertestprogram Initialmbedrobotprogram mbedrobot ipod ... more
motordriver.cpp@0:edc152f119b7, 2010-11-06 (annotated)
- Committer:
- littlexc
- Date:
- Sat Nov 06 13:05:17 2010 +0000
- Revision:
- 0:edc152f119b7
- Child:
- 1:3da7302dc9ae
V1.0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
littlexc | 0:edc152f119b7 | 1 | /*motor driver libary modified from the following libary, |
littlexc | 0:edc152f119b7 | 2 | * |
littlexc | 0:edc152f119b7 | 3 | * mbed simple H-bridge motor controller |
littlexc | 0:edc152f119b7 | 4 | * Copyright (c) 2007-2010, sford |
littlexc | 0:edc152f119b7 | 5 | * |
littlexc | 0:edc152f119b7 | 6 | * by Christopher Hasler. |
littlexc | 0:edc152f119b7 | 7 | * |
littlexc | 0:edc152f119b7 | 8 | * from sford's libary, |
littlexc | 0:edc152f119b7 | 9 | * |
littlexc | 0:edc152f119b7 | 10 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
littlexc | 0:edc152f119b7 | 11 | * of this software and associated documentation files (the "Software"), to deal |
littlexc | 0:edc152f119b7 | 12 | * in the Software without restriction, including without limitation the rights |
littlexc | 0:edc152f119b7 | 13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
littlexc | 0:edc152f119b7 | 14 | * copies of the Software, and to permit persons to whom the Software is |
littlexc | 0:edc152f119b7 | 15 | * furnished to do so, subject to the following conditions: |
littlexc | 0:edc152f119b7 | 16 | * |
littlexc | 0:edc152f119b7 | 17 | * The above copyright notice and this permission notice shall be included in |
littlexc | 0:edc152f119b7 | 18 | * all copies or substantial portions of the Software. |
littlexc | 0:edc152f119b7 | 19 | * |
littlexc | 0:edc152f119b7 | 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
littlexc | 0:edc152f119b7 | 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
littlexc | 0:edc152f119b7 | 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
littlexc | 0:edc152f119b7 | 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
littlexc | 0:edc152f119b7 | 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
littlexc | 0:edc152f119b7 | 25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
littlexc | 0:edc152f119b7 | 26 | * THE SOFTWARE. |
littlexc | 0:edc152f119b7 | 27 | */ |
littlexc | 0:edc152f119b7 | 28 | |
littlexc | 0:edc152f119b7 | 29 | #include "motordriver.h" |
littlexc | 0:edc152f119b7 | 30 | |
littlexc | 0:edc152f119b7 | 31 | #include "mbed.h" |
littlexc | 0:edc152f119b7 | 32 | |
littlexc | 0:edc152f119b7 | 33 | Motor::Motor(PinName pwm, PinName fwd, PinName rev, int brakeable): |
littlexc | 0:edc152f119b7 | 34 | _pwm(pwm), _fwd(fwd), _rev(rev) { |
littlexc | 0:edc152f119b7 | 35 | |
littlexc | 0:edc152f119b7 | 36 | // Set initial condition of PWM |
littlexc | 0:edc152f119b7 | 37 | _pwm.period(0.001); |
littlexc | 0:edc152f119b7 | 38 | _pwm = 0; |
littlexc | 0:edc152f119b7 | 39 | |
littlexc | 0:edc152f119b7 | 40 | // Initial condition of output enables |
littlexc | 0:edc152f119b7 | 41 | _fwd = 0; |
littlexc | 0:edc152f119b7 | 42 | _rev = 0; |
littlexc | 0:edc152f119b7 | 43 | |
littlexc | 0:edc152f119b7 | 44 | //set if the motor dirver is capable of braking. (addition) |
littlexc | 0:edc152f119b7 | 45 | Brakeable= brakeable; |
littlexc | 0:edc152f119b7 | 46 | } |
littlexc | 0:edc152f119b7 | 47 | |
littlexc | 0:edc152f119b7 | 48 | void Motor::speed(float speed) { |
littlexc | 0:edc152f119b7 | 49 | _fwd = (speed > 0.0); |
littlexc | 0:edc152f119b7 | 50 | _rev = (speed < 0.0); |
littlexc | 0:edc152f119b7 | 51 | _pwm = abs(speed); |
littlexc | 0:edc152f119b7 | 52 | } |
littlexc | 0:edc152f119b7 | 53 | // (additions) |
littlexc | 0:edc152f119b7 | 54 | void Motor::coast(void) { |
littlexc | 0:edc152f119b7 | 55 | _fwd = 0; |
littlexc | 0:edc152f119b7 | 56 | _rev = 0; |
littlexc | 0:edc152f119b7 | 57 | _pwm = 0; |
littlexc | 0:edc152f119b7 | 58 | } |
littlexc | 0:edc152f119b7 | 59 | |
littlexc | 0:edc152f119b7 | 60 | void Motor::stop(void) { |
littlexc | 0:edc152f119b7 | 61 | if (Brakeable == 1) { |
littlexc | 0:edc152f119b7 | 62 | _fwd = 1; |
littlexc | 0:edc152f119b7 | 63 | _rev = 1; |
littlexc | 0:edc152f119b7 | 64 | _pwm = 0.5; |
littlexc | 0:edc152f119b7 | 65 | } |
littlexc | 0:edc152f119b7 | 66 | else |
littlexc | 0:edc152f119b7 | 67 | return; |
littlexc | 0:edc152f119b7 | 68 | } |
littlexc | 0:edc152f119b7 | 69 | |
littlexc | 0:edc152f119b7 | 70 | /* |
littlexc | 0:edc152f119b7 | 71 | test code, this demonstrates working motor drivers. |
littlexc | 0:edc152f119b7 | 72 | |
littlexc | 0:edc152f119b7 | 73 | Motor A(p22, p6, p5, 1); // pwm, fwd, rev, can break |
littlexc | 0:edc152f119b7 | 74 | Motor B(p21, p7, p8, 1); // pwm, fwd, rev, can break |
littlexc | 0:edc152f119b7 | 75 | int main() { |
littlexc | 0:edc152f119b7 | 76 | for (float s=-1.0; s < 1.0 ; s += 0.01) { |
littlexc | 0:edc152f119b7 | 77 | A.speed(s); |
littlexc | 0:edc152f119b7 | 78 | B.speed(s); |
littlexc | 0:edc152f119b7 | 79 | wait(0.02); |
littlexc | 0:edc152f119b7 | 80 | } |
littlexc | 0:edc152f119b7 | 81 | A.stop(); |
littlexc | 0:edc152f119b7 | 82 | B.stop(); |
littlexc | 0:edc152f119b7 | 83 | wait(1); |
littlexc | 0:edc152f119b7 | 84 | A.coast(); |
littlexc | 0:edc152f119b7 | 85 | B.coast(); |
littlexc | 0:edc152f119b7 | 86 | } |
littlexc | 0:edc152f119b7 | 87 | */ |