Control an H-Bridge using a PwmOut (enable) and two DigitalOuts (direction select). (forked from simon/Motor and changed according to our needs)
Fork of Motor by
Motor.cpp@3:118c5f0675da, 2013-10-27 (annotated)
- Committer:
- narendraj9
- Date:
- Sun Oct 27 09:15:12 2013 +0000
- Revision:
- 3:118c5f0675da
- Parent:
- 2:f265e441bcd9
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
simon | 0:a470311addc4 | 1 | /* mbed simple H-bridge motor controller |
simon | 2:f265e441bcd9 | 2 | * Copyright (c) 2007-2010, sford, http://mbed.org |
simon | 0:a470311addc4 | 3 | * |
simon | 0:a470311addc4 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
simon | 0:a470311addc4 | 5 | * of this software and associated documentation files (the "Software"), to deal |
simon | 0:a470311addc4 | 6 | * in the Software without restriction, including without limitation the rights |
simon | 0:a470311addc4 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
simon | 0:a470311addc4 | 8 | * copies of the Software, and to permit persons to whom the Software is |
simon | 0:a470311addc4 | 9 | * furnished to do so, subject to the following conditions: |
simon | 0:a470311addc4 | 10 | * |
simon | 0:a470311addc4 | 11 | * The above copyright notice and this permission notice shall be included in |
simon | 0:a470311addc4 | 12 | * all copies or substantial portions of the Software. |
simon | 0:a470311addc4 | 13 | * |
simon | 0:a470311addc4 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
simon | 0:a470311addc4 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
simon | 0:a470311addc4 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
simon | 0:a470311addc4 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
simon | 0:a470311addc4 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
simon | 0:a470311addc4 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
simon | 0:a470311addc4 | 20 | * THE SOFTWARE. |
simon | 0:a470311addc4 | 21 | */ |
simon | 0:a470311addc4 | 22 | |
simon | 0:a470311addc4 | 23 | #include "Motor.h" |
simon | 0:a470311addc4 | 24 | |
simon | 0:a470311addc4 | 25 | #include "mbed.h" |
simon | 0:a470311addc4 | 26 | |
narendraj9 | 3:118c5f0675da | 27 | Motor::Motor(PinName en, PinName fwd, PinName rev): |
narendraj9 | 3:118c5f0675da | 28 | _en(en), _fwd(fwd), _rev(rev) { |
simon | 0:a470311addc4 | 29 | |
narendraj9 | 3:118c5f0675da | 30 | // Set initial condition for the enable pin |
narendraj9 | 3:118c5f0675da | 31 | _en = 0; |
narendraj9 | 3:118c5f0675da | 32 | |
simon | 0:a470311addc4 | 33 | // Initial condition of output enables |
simon | 0:a470311addc4 | 34 | _fwd = 0; |
simon | 0:a470311addc4 | 35 | _rev = 0; |
simon | 0:a470311addc4 | 36 | } |
simon | 0:a470311addc4 | 37 | |
narendraj9 | 3:118c5f0675da | 38 | // direct the motor in a specific direction or stop it |
narendraj9 | 3:118c5f0675da | 39 | void Motor::direct(int dir) { |
narendraj9 | 3:118c5f0675da | 40 | _fwd = (dir > 0); |
narendraj9 | 3:118c5f0675da | 41 | _rev = (dir < 0); |
narendraj9 | 3:118c5f0675da | 42 | _en = !(dir == 0); |
simon | 0:a470311addc4 | 43 | } |
simon | 0:a470311addc4 | 44 | |
narendraj9 | 3:118c5f0675da | 45 | /* emulate pwm for a specified time interval */ |
narendraj9 | 3:118c5f0675da | 46 | void Motor::speed(float timeval, float speed) { |
narendraj9 | 3:118c5f0675da | 47 | float wait_time = 1/(speed * 10); |
narendraj9 | 3:118c5f0675da | 48 | for (int i = 0; i < timeval; i += 0.1 ) { |
narendraj9 | 3:118c5f0675da | 49 | _en = 0; |
narendraj9 | 3:118c5f0675da | 50 | wait(wait_time/2); |
narendraj9 | 3:118c5f0675da | 51 | _en = 1; |
narendraj9 | 3:118c5f0675da | 52 | wait(wait_time); |
narendraj9 | 3:118c5f0675da | 53 | } |
simon | 0:a470311addc4 | 54 | |
narendraj9 | 3:118c5f0675da | 55 | } |
simon | 0:a470311addc4 | 56 | |
narendraj9 | 3:118c5f0675da | 57 | mState Motor::getState() |
narendraj9 | 3:118c5f0675da | 58 | { |
narendraj9 | 3:118c5f0675da | 59 | mState temp = { _en, _fwd, _rev }; |
narendraj9 | 3:118c5f0675da | 60 | return temp; |
narendraj9 | 3:118c5f0675da | 61 | } |
narendraj9 | 3:118c5f0675da | 62 | |
narendraj9 | 3:118c5f0675da | 63 | void Motor::setState(mState pinState) |
narendraj9 | 3:118c5f0675da | 64 | { |
narendraj9 | 3:118c5f0675da | 65 | _en = pinState.en; |
narendraj9 | 3:118c5f0675da | 66 | _fwd = pinState.fwd; |
narendraj9 | 3:118c5f0675da | 67 | _rev = pinState.rev; |
narendraj9 | 3:118c5f0675da | 68 | } |
narendraj9 | 3:118c5f0675da | 69 |