Basic Motor Driver library. Tested on Pololu's TB6612FNG Dual Motor Driver Carrier breakout board Part# 713, but should work with any Motor H- Bridge Driver

Dependents:   ESP8266_pid_redbot_webserver 4180_lab4_project

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MotorDriver.h Source File

MotorDriver.h

00001 /*
00002 Bryce Williams
00003 09/14/2014
00004 
00005 General/Basic Motor Driver Class providing Motor Control using a COTS Motor Driver
00006 
00007     Class based off of Christopher Hasler's Motordriver library found at
00008     https://developer.mbed.org/cookbook/Motor 
00009     
00010     
00011 * Permission is hereby granted, free of charge, to any person obtaining a copy
00012 * of this software and associated documentation files (the "Software"), to deal
00013 * in the Software without restriction, including without limitation the rights
00014 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00015 * copies of the Software, and to permit persons to whom the Software is
00016 * furnished to do so, subject to the following conditions:
00017 *
00018 * The above copyright notice and this permission notice shall be included in
00019 * all copies or substantial portions of the Software.
00020 *
00021 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00022 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00023 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00024 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00025 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00027 * THE SOFTWARE.
00028 */
00029 
00030 #ifndef MOTORDRIVER_H
00031 #define MOTORDRIVER_H
00032 
00033 #include "mbed.h"
00034 
00035 typedef enum {ERROR=0, DRIVING_CW=1, DRIVING_CCW=2, BRAKING=3, COASTING=4}Code_t;
00036 typedef struct{
00037     Code_t code;     
00038     float  value;
00039 }State_t;
00040 
00041 class MotorDriver{
00042     public:
00043         /*
00044             Constructor of MotorDriver Objects
00045             @param in_1         Direction Input 1
00046             @param in_2         Direction Input 2
00047             @param pwm          PWM speed control input
00048             @param pwmFreq      PWM frequency, some motors may whine at lower freqs
00049             @param isBrakable   Boolean value indicating whether or not the 
00050                                 motor driver is brakeable(see your datasheet)
00051         */
00052         MotorDriver(DigitalOut in1, DigitalOut in2, PwmOut pwm, float pwmFreq, bool isBrakeable = false);
00053         
00054         /*
00055             Sets speed of motor normalized between -1.0 to 1.0 
00056             @param speed    Value -1.0 to 1.0 (>0 CW at speed as percentage)
00057                                               (<0 CCW at speed as percentage)
00058                                               (=0 speed is zero)
00059             @return state of the motor
00060             NOTE: This method will NOT allow user to instantaneously swithch 
00061             from CW to CCW or vise versa. Doing so will cause the motor to 
00062             be put in to a BRAKE condition, while the motor_state.code will 
00063             be updated to ERROR. User should avoid trying to do this, call 
00064             first setSpeed(0) or brake().  
00065         */
00066         State_t setSpeed(float speed);
00067         
00068         /*
00069             Same as setSpeed(float speed), however does not impose the safety disallowing
00070             instantaneous reversal of motor direction. It is up to the user to ensure they
00071             do not blow up their motor. 
00072         */
00073         State_t forceSetSpeed(float speed);
00074         
00075         /*
00076             Put motor into braked config
00077             @param intensity How hard to brake (0.0 to 1.0)
00078             @return state of the motors
00079         */
00080         State_t brake(float intensity);
00081         
00082         /*
00083             Put motor into stop/coast config
00084         */
00085         State_t coast();
00086         
00087         
00088         /*
00089             Get state of the motor
00090             @return state of the motor
00091         */
00092         State_t getState();
00093         
00094     protected:                        // Protected so objects that inherit can access
00095         State_t    motor_state;
00096         DigitalOut _in1;
00097         DigitalOut _in2;
00098         PwmOut     _pwm;
00099         bool       isBrakeable;
00100         void determineState();        // Determine motor state based on in1, in2, and pwm
00101 };
00102 
00103 #endif