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

Committer:
littlexc
Date:
Thu Nov 25 13:34:15 2010 +0000
Revision:
5:3110b9209d3c
Parent:
4:5fb1296c0d60
set the stop function to coast if not able to dynamicaly brake.

Who changed what in which revision?

UserRevisionLine numberNew 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 #ifndef MBED_MOTOR_H
littlexc 0:edc152f119b7 30 #define MBED_MOTOR_H
littlexc 0:edc152f119b7 31
littlexc 0:edc152f119b7 32 #include "mbed.h"
littlexc 0:edc152f119b7 33
littlexc 0:edc152f119b7 34 /** Interface to control a standard DC motor
littlexc 0:edc152f119b7 35 * with an H-bridge using a PwmOut and 2 DigitalOuts
littlexc 0:edc152f119b7 36 */
littlexc 0:edc152f119b7 37 class Motor {
littlexc 0:edc152f119b7 38 public:
littlexc 0:edc152f119b7 39
littlexc 0:edc152f119b7 40 /** Create a motor control interface
littlexc 0:edc152f119b7 41 *
littlexc 0:edc152f119b7 42 * @param pwm A PwmOut pin, driving the H-bridge enable line to control the speed
littlexc 0:edc152f119b7 43 * @param fwd A DigitalOut, set high when the motor should go forward
littlexc 0:edc152f119b7 44 * @param rev A DigitalOut, set high when the motor should go backwards
littlexc 2:2dc873322032 45 * @param set if the motor driver is able to do braking 0 false 1 true.
littlexc 0:edc152f119b7 46 */
littlexc 0:edc152f119b7 47 Motor(PinName pwm, PinName fwd, PinName rev, int brakeable);
littlexc 0:edc152f119b7 48
littlexc 2:2dc873322032 49 /** Set the speed of the motor
littlexc 0:edc152f119b7 50 *
littlexc 2:2dc873322032 51 * @param speed The speed of the motor as a normalised value between -1.0 and 1.0.
littlexc 2:2dc873322032 52 * @return the applied speed to the motor after checking to ensure motor doesn't switch from forward to reverse without stopping.
littlexc 0:edc152f119b7 53 */
littlexc 2:2dc873322032 54 float speed(float speed);
littlexc 0:edc152f119b7 55
littlexc 0:edc152f119b7 56 /** Set the the motor to coast
littlexc 0:edc152f119b7 57 *
littlexc 2:2dc873322032 58 * @param void
littlexc 2:2dc873322032 59 * @return motor coasts until another instruction is recived.
littlexc 0:edc152f119b7 60 */
littlexc 0:edc152f119b7 61
littlexc 0:edc152f119b7 62 void coast(void);
littlexc 0:edc152f119b7 63
littlexc 2:2dc873322032 64 /** Set the motor to dynamicaly brake
littlexc 0:edc152f119b7 65 *
littlexc 2:2dc873322032 66 * @param float 0 - 1.0 provides some control over how hard the motor brakes.
littlexc 2:2dc873322032 67 * @return duty applied to motor driver. -1 is error, motor driver can't brake.
littlexc 0:edc152f119b7 68 */
littlexc 0:edc152f119b7 69
littlexc 2:2dc873322032 70 float stop(float duty);
littlexc 4:5fb1296c0d60 71 /** return the current state of the motor
littlexc 4:5fb1296c0d60 72 *
littlexc 4:5fb1296c0d60 73 * @param void
littlexc 4:5fb1296c0d60 74 * @return state of motor, -1 to 1 is speed, -2 is braking, 2 is coasting. -3 is error.
littlexc 4:5fb1296c0d60 75 */
littlexc 4:5fb1296c0d60 76 float state(void);
littlexc 0:edc152f119b7 77
littlexc 0:edc152f119b7 78 protected:
littlexc 0:edc152f119b7 79 PwmOut _pwm;
littlexc 0:edc152f119b7 80 DigitalOut _fwd;
littlexc 0:edc152f119b7 81 DigitalOut _rev;
littlexc 2:2dc873322032 82 int Brakeable; // cna the motor driver break
littlexc 1:3da7302dc9ae 83 int sign; //prevents throwing the motor from full foward to full reverse and stuff melting.
littlexc 0:edc152f119b7 84
littlexc 0:edc152f119b7 85 };
littlexc 0:edc152f119b7 86
littlexc 4:5fb1296c0d60 87
littlexc 4:5fb1296c0d60 88
littlexc 4:5fb1296c0d60 89
littlexc 4:5fb1296c0d60 90
littlexc 0:edc152f119b7 91 #endif