Control a DC motor via a standard H-bridge using a PwmOut pin to control speed and two DigitalOut pins to control direction. Can change pwm period on the PwmOut pin, and also brake high or low.
Fork of Motor by
Motor.h@1:c75b234558af, 2010-09-07 (annotated)
- Committer:
- aberk
- Date:
- Tue Sep 07 11:21:42 2010 +0000
- Revision:
- 1:c75b234558af
- Parent:
- 0:f05e09f8f5d9
Added functionality to brake high or low.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aberk | 0:f05e09f8f5d9 | 1 | /* mbed simple H-bridge motor controller |
aberk | 0:f05e09f8f5d9 | 2 | * Copyright (c) 2007-2010, sford |
aberk | 0:f05e09f8f5d9 | 3 | * |
aberk | 0:f05e09f8f5d9 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
aberk | 0:f05e09f8f5d9 | 5 | * of this software and associated documentation files (the "Software"), to deal |
aberk | 0:f05e09f8f5d9 | 6 | * in the Software without restriction, including without limitation the rights |
aberk | 0:f05e09f8f5d9 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
aberk | 0:f05e09f8f5d9 | 8 | * copies of the Software, and to permit persons to whom the Software is |
aberk | 0:f05e09f8f5d9 | 9 | * furnished to do so, subject to the following conditions: |
aberk | 0:f05e09f8f5d9 | 10 | * |
aberk | 0:f05e09f8f5d9 | 11 | * The above copyright notice and this permission notice shall be included in |
aberk | 0:f05e09f8f5d9 | 12 | * all copies or substantial portions of the Software. |
aberk | 0:f05e09f8f5d9 | 13 | * |
aberk | 0:f05e09f8f5d9 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
aberk | 0:f05e09f8f5d9 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
aberk | 0:f05e09f8f5d9 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
aberk | 0:f05e09f8f5d9 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
aberk | 0:f05e09f8f5d9 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
aberk | 0:f05e09f8f5d9 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
aberk | 0:f05e09f8f5d9 | 20 | * THE SOFTWARE. |
aberk | 0:f05e09f8f5d9 | 21 | */ |
aberk | 0:f05e09f8f5d9 | 22 | |
aberk | 0:f05e09f8f5d9 | 23 | #ifndef MBED_MOTOR_H |
aberk | 0:f05e09f8f5d9 | 24 | #define MBED_MOTOR_H |
aberk | 0:f05e09f8f5d9 | 25 | |
aberk | 0:f05e09f8f5d9 | 26 | #include "mbed.h" |
aberk | 0:f05e09f8f5d9 | 27 | |
aberk | 1:c75b234558af | 28 | #define BRAKE_HIGH 1 |
aberk | 1:c75b234558af | 29 | #define BRAKE_LOW 0 |
aberk | 1:c75b234558af | 30 | |
aberk | 0:f05e09f8f5d9 | 31 | /** Interface to control a standard DC motor |
aberk | 0:f05e09f8f5d9 | 32 | * with an H-bridge using a PwmOut and 2 DigitalOuts |
aberk | 0:f05e09f8f5d9 | 33 | */ |
aberk | 0:f05e09f8f5d9 | 34 | class Motor { |
aberk | 0:f05e09f8f5d9 | 35 | public: |
aberk | 0:f05e09f8f5d9 | 36 | |
aberk | 0:f05e09f8f5d9 | 37 | /** Create a motor control interface |
aberk | 0:f05e09f8f5d9 | 38 | * |
aberk | 0:f05e09f8f5d9 | 39 | * @param pwm A PwmOut pin, driving the H-bridge enable line to control the speed |
aberk | 0:f05e09f8f5d9 | 40 | * @param fwd A DigitalOut, set high when the motor should go forward |
aberk | 0:f05e09f8f5d9 | 41 | * @param rev A DigitalOut, set high when the motor should go backwards |
aberk | 0:f05e09f8f5d9 | 42 | */ |
aberk | 0:f05e09f8f5d9 | 43 | Motor(PinName pwm, PinName fwd, PinName rev); |
aberk | 0:f05e09f8f5d9 | 44 | |
aberk | 0:f05e09f8f5d9 | 45 | /** Set the speed of the motor |
aberk | 0:f05e09f8f5d9 | 46 | * |
aberk | 0:f05e09f8f5d9 | 47 | * @param speed The speed of the motor as a normalised value between -1.0 and 1.0 |
aberk | 0:f05e09f8f5d9 | 48 | */ |
aberk | 0:f05e09f8f5d9 | 49 | void speed(float speed); |
aberk | 0:f05e09f8f5d9 | 50 | |
aberk | 0:f05e09f8f5d9 | 51 | /** Set the period of the pwm duty cycle. |
aberk | 0:f05e09f8f5d9 | 52 | * |
aberk | 0:f05e09f8f5d9 | 53 | * Wrapper for PwmOut::period() |
aberk | 0:f05e09f8f5d9 | 54 | * |
aberk | 0:f05e09f8f5d9 | 55 | * @param seconds - Pwm duty cycle in seconds. |
aberk | 0:f05e09f8f5d9 | 56 | */ |
aberk | 0:f05e09f8f5d9 | 57 | void period(float period); |
aberk | 1:c75b234558af | 58 | |
aberk | 1:c75b234558af | 59 | /** Brake the H-bridge to GND or VCC. |
aberk | 1:c75b234558af | 60 | * |
aberk | 1:c75b234558af | 61 | * Defaults to breaking to VCC. |
aberk | 1:c75b234558af | 62 | * |
aberk | 1:c75b234558af | 63 | * Brake to GND => inA = inB = 0 |
aberk | 1:c75b234558af | 64 | * Brake to VCC => inA = inB = 1 |
aberk | 1:c75b234558af | 65 | */ |
aberk | 1:c75b234558af | 66 | void brake(int highLow = BRAKE_HIGH); |
aberk | 0:f05e09f8f5d9 | 67 | |
aberk | 0:f05e09f8f5d9 | 68 | protected: |
aberk | 0:f05e09f8f5d9 | 69 | PwmOut _pwm; |
aberk | 0:f05e09f8f5d9 | 70 | DigitalOut _fwd; |
aberk | 0:f05e09f8f5d9 | 71 | DigitalOut _rev; |
aberk | 0:f05e09f8f5d9 | 72 | |
aberk | 0:f05e09f8f5d9 | 73 | }; |
aberk | 0:f05e09f8f5d9 | 74 | |
aberk | 0:f05e09f8f5d9 | 75 | #endif |