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 Aaron Berk

Committer:
aberk
Date:
Thu Sep 02 16:32:57 2010 +0000
Revision:
0:f05e09f8f5d9
Child:
1:c75b234558af
Added period(...) method to allow the period on the PwmOut pin to be changed.

Who changed what in which revision?

UserRevisionLine numberNew 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 0:f05e09f8f5d9 28 /** Interface to control a standard DC motor
aberk 0:f05e09f8f5d9 29 * with an H-bridge using a PwmOut and 2 DigitalOuts
aberk 0:f05e09f8f5d9 30 */
aberk 0:f05e09f8f5d9 31 class Motor {
aberk 0:f05e09f8f5d9 32 public:
aberk 0:f05e09f8f5d9 33
aberk 0:f05e09f8f5d9 34 /** Create a motor control interface
aberk 0:f05e09f8f5d9 35 *
aberk 0:f05e09f8f5d9 36 * @param pwm A PwmOut pin, driving the H-bridge enable line to control the speed
aberk 0:f05e09f8f5d9 37 * @param fwd A DigitalOut, set high when the motor should go forward
aberk 0:f05e09f8f5d9 38 * @param rev A DigitalOut, set high when the motor should go backwards
aberk 0:f05e09f8f5d9 39 */
aberk 0:f05e09f8f5d9 40 Motor(PinName pwm, PinName fwd, PinName rev);
aberk 0:f05e09f8f5d9 41
aberk 0:f05e09f8f5d9 42 /** Set the speed of the motor
aberk 0:f05e09f8f5d9 43 *
aberk 0:f05e09f8f5d9 44 * @param speed The speed of the motor as a normalised value between -1.0 and 1.0
aberk 0:f05e09f8f5d9 45 */
aberk 0:f05e09f8f5d9 46 void speed(float speed);
aberk 0:f05e09f8f5d9 47
aberk 0:f05e09f8f5d9 48 /** Set the period of the pwm duty cycle.
aberk 0:f05e09f8f5d9 49 *
aberk 0:f05e09f8f5d9 50 * Wrapper for PwmOut::period()
aberk 0:f05e09f8f5d9 51 *
aberk 0:f05e09f8f5d9 52 * @param seconds - Pwm duty cycle in seconds.
aberk 0:f05e09f8f5d9 53 */
aberk 0:f05e09f8f5d9 54 void period(float period);
aberk 0:f05e09f8f5d9 55
aberk 0:f05e09f8f5d9 56 protected:
aberk 0:f05e09f8f5d9 57 PwmOut _pwm;
aberk 0:f05e09f8f5d9 58 DigitalOut _fwd;
aberk 0:f05e09f8f5d9 59 DigitalOut _rev;
aberk 0:f05e09f8f5d9 60
aberk 0:f05e09f8f5d9 61 };
aberk 0:f05e09f8f5d9 62
aberk 0:f05e09f8f5d9 63 #endif