Motor driver library. Based on Christopher Hasler\'s Motordriver library.

Dependents:   project1 motoresMicromouse micromouse coba_lib_lcd ... more

Committer:
osmeest
Date:
Wed Feb 23 23:12:44 2011 +0000
Revision:
0:83245b45ea70
Slightly improvement over original: brakeable is a bool and state() should be const.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
osmeest 0:83245b45ea70 1 /*motor driver libary modified from the following libary,
osmeest 0:83245b45ea70 2 *
osmeest 0:83245b45ea70 3 * mbed simple H-bridge motor controller
osmeest 0:83245b45ea70 4 * Copyright (c) 2007-2010, sford
osmeest 0:83245b45ea70 5 *
osmeest 0:83245b45ea70 6 * by Christopher Hasler.
osmeest 0:83245b45ea70 7 *
osmeest 0:83245b45ea70 8 * from sford's libary,
osmeest 0:83245b45ea70 9 *
osmeest 0:83245b45ea70 10 * Permission is hereby granted, free of charge, to any person obtaining a copy
osmeest 0:83245b45ea70 11 * of this software and associated documentation files (the "Software"), to deal
osmeest 0:83245b45ea70 12 * in the Software without restriction, including without limitation the rights
osmeest 0:83245b45ea70 13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
osmeest 0:83245b45ea70 14 * copies of the Software, and to permit persons to whom the Software is
osmeest 0:83245b45ea70 15 * furnished to do so, subject to the following conditions:
osmeest 0:83245b45ea70 16 *
osmeest 0:83245b45ea70 17 * The above copyright notice and this permission notice shall be included in
osmeest 0:83245b45ea70 18 * all copies or substantial portions of the Software.
osmeest 0:83245b45ea70 19 *
osmeest 0:83245b45ea70 20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
osmeest 0:83245b45ea70 21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
osmeest 0:83245b45ea70 22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
osmeest 0:83245b45ea70 23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
osmeest 0:83245b45ea70 24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
osmeest 0:83245b45ea70 25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
osmeest 0:83245b45ea70 26 * THE SOFTWARE.
osmeest 0:83245b45ea70 27 */
osmeest 0:83245b45ea70 28
osmeest 0:83245b45ea70 29 #ifndef MBED_MOTOR_H
osmeest 0:83245b45ea70 30 #define MBED_MOTOR_H
osmeest 0:83245b45ea70 31
osmeest 0:83245b45ea70 32 #include "mbed.h"
osmeest 0:83245b45ea70 33
osmeest 0:83245b45ea70 34 /** Interface to control a standard DC motor
osmeest 0:83245b45ea70 35 * with an H-bridge using a PwmOut and 2 DigitalOuts
osmeest 0:83245b45ea70 36 */
osmeest 0:83245b45ea70 37 class Motor {
osmeest 0:83245b45ea70 38 public:
osmeest 0:83245b45ea70 39
osmeest 0:83245b45ea70 40 /** Create a motor control interface
osmeest 0:83245b45ea70 41 *
osmeest 0:83245b45ea70 42 * @param pwm A PwmOut pin, driving the H-bridge enable line to control the speed
osmeest 0:83245b45ea70 43 * @param fwd A DigitalOut, set high when the motor should go forward
osmeest 0:83245b45ea70 44 * @param rev A DigitalOut, set high when the motor should go backwards
osmeest 0:83245b45ea70 45 * @param set if the motor driver is able to do braking 0 false 1 true.
osmeest 0:83245b45ea70 46 */
osmeest 0:83245b45ea70 47 Motor(PinName pwm, PinName fwd, PinName rev, bool brakeable = false);
osmeest 0:83245b45ea70 48
osmeest 0:83245b45ea70 49 /** Set the speed of the motor
osmeest 0:83245b45ea70 50 *
osmeest 0:83245b45ea70 51 * @param speed The speed of the motor as a normalised value between -1.0 and 1.0.
osmeest 0:83245b45ea70 52 * @return the applied speed to the motor after checking to ensure motor doesn't switch from forward to reverse without stopping.
osmeest 0:83245b45ea70 53 */
osmeest 0:83245b45ea70 54 float speed(float speed);
osmeest 0:83245b45ea70 55
osmeest 0:83245b45ea70 56 /** Set the the motor to coast
osmeest 0:83245b45ea70 57 *
osmeest 0:83245b45ea70 58 * @param void
osmeest 0:83245b45ea70 59 * @return motor coasts until another instruction is recived.
osmeest 0:83245b45ea70 60 */
osmeest 0:83245b45ea70 61
osmeest 0:83245b45ea70 62 void coast(void);
osmeest 0:83245b45ea70 63
osmeest 0:83245b45ea70 64 /** Set the motor to dynamicaly brake
osmeest 0:83245b45ea70 65 *
osmeest 0:83245b45ea70 66 * @param float 0 - 1.0 provides some control over how hard the motor brakes.
osmeest 0:83245b45ea70 67 * @return duty applied to motor driver. -1 is error, motor driver can't brake.
osmeest 0:83245b45ea70 68 */
osmeest 0:83245b45ea70 69
osmeest 0:83245b45ea70 70 float stop(float duty);
osmeest 0:83245b45ea70 71 /** return the current state of the motor
osmeest 0:83245b45ea70 72 *
osmeest 0:83245b45ea70 73 * @param void
osmeest 0:83245b45ea70 74 * @return state of motor, -1 to 1 is speed, -2 is braking, 2 is coasting. -3 is error.
osmeest 0:83245b45ea70 75 */
osmeest 0:83245b45ea70 76 float state(void) const;
osmeest 0:83245b45ea70 77
osmeest 0:83245b45ea70 78 protected:
osmeest 0:83245b45ea70 79 mutable PwmOut _pwm;
osmeest 0:83245b45ea70 80 mutable DigitalOut _fwd;
osmeest 0:83245b45ea70 81 mutable DigitalOut _rev;
osmeest 0:83245b45ea70 82 bool _brakeable; // cna the motor driver break
osmeest 0:83245b45ea70 83 int _sign; //prevents throwing the motor from full foward to full reverse and stuff melting.
osmeest 0:83245b45ea70 84
osmeest 0:83245b45ea70 85 };
osmeest 0:83245b45ea70 86
osmeest 0:83245b45ea70 87
osmeest 0:83245b45ea70 88
osmeest 0:83245b45ea70 89
osmeest 0:83245b45ea70 90
osmeest 0:83245b45ea70 91 #endif