Clownface / MotorDriver

Fork of MotorDriver by Matt Rogers

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 /** The state of the motor */
00036 typedef enum {
00037     MOTOR_ERROR=0,  /**< Motor Error Mode */
00038     DRIVING_CW=1,   /**< Driving Clockwise (forward) */
00039     DRIVING_CCW=2,  /**< Driving Counter-Clockwise (reverse) */
00040     BRAKING=3,      /**< Braking Mode */
00041     COASTING=4      /**< Coasting Mode */
00042 } Code_t;
00043 /** The code and value of the state of the motor. */
00044 typedef struct {
00045     Code_t code;    /**< The Code_t enum of the motor state */
00046     float  value;   /**< The numerical value of the motor state */
00047 } State_t;
00048 /** MotorDriver class.
00049  *  Used for DIY project "RC-AutoBot" by Clownface.space
00050  *
00051  * Example:
00052  * @code
00053  * #include "mbed.h"
00054  * #include "MotorDriver.h"
00055  *
00056  * MotorDriver m1(in1, in2, pwm);
00057  *
00058  * void dist(int distance)
00059  * {
00060  *  //put code here to happen when the distance is changed
00061  *  printf("\r\n** Distance changed to %dmm\r\n", distance);
00062  *  sensorDistance = distance;
00063  * }
00064  *
00065  * int main() {
00066  *     m1.setSpeed(1);
00067  * }
00068  * @endcode
00069  */
00070 class MotorDriver
00071 {
00072 public:
00073     /** Constructor of MotorDriver Objects
00074         @param in_1         Direction Input 1
00075         @param in_2         Direction Input 2
00076         @param pwm          PWM speed control input
00077         @param pwmFreq      PWM frequency, some motors may whine at lower freqs
00078         @param isBrakable   Boolean value indicating whether or not the
00079                             motor driver is brakeable(see your datasheet)
00080     */
00081     MotorDriver(DigitalOut in1, DigitalOut in2, PwmOut pwm, float pwmFreq, bool isBrakeable = false);
00082 
00083     /** Sets speed of motor normalized between -1.0 to 1.0
00084         @param speed    Value -1.0 to 1.0 (>0 CW at speed as percentage)
00085                                           (<0 CCW at speed as percentage)
00086                                           (=0 speed is zero)
00087         @return state of the motor
00088         NOTE: This method will NOT allow user to instantaneously swithch
00089         from CW to CCW or vise versa. Doing so will cause the motor to
00090         be put in to a BRAKE condition, while the motor_state.code will
00091         be updated to MOTOR_ERROR. User should avoid trying to do this, call
00092         first setSpeed(0) or brake().
00093     */
00094     State_t setSpeed(float speed);
00095 
00096     /** Same as setSpeed(float speed), however does not impose the safety disallowing
00097         instantaneous reversal of motor direction. It is up to the user to ensure they
00098         do not blow up their motor.
00099 
00100         @param speed How fast to run the motor
00101     */
00102     State_t forceSetSpeed(float speed);
00103 
00104     /** Put motor into braked config
00105         @param intensity How hard to brake (0.0 to 1.0)
00106         @return state of the motors
00107     */
00108     State_t brake(float intensity);
00109 
00110     /** Put motor into stop/coast config
00111     */
00112     State_t coast();
00113 
00114     /** Get state of the motor
00115         @return state of the motor
00116     */
00117     State_t getState();
00118 
00119 protected:                        // Protected so objects that inherit can access
00120     State_t    motor_state;
00121     DigitalOut _in1;
00122     DigitalOut _in2;
00123     PwmOut     _pwm;
00124     bool       isBrakeable;
00125     void determineState();        // Determine motor state based on in1, in2, and pwm
00126 };
00127 
00128 #endif