Basic Motor Driver library. Being used with BTS7970 on the RC-AutoBot project. The latest DIY project created by your favorite open-source company: Clownface.space

Fork of MotorDriver by Matt Rogers

Committer:
knotbeer
Date:
Sat Apr 30 05:38:43 2016 +0000
Revision:
4:9283a3f684af
Parent:
3:7a6365365ee2
API Documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
electromotivated 0:871adb9cf798 1 /*
electromotivated 0:871adb9cf798 2 Bryce Williams
electromotivated 0:871adb9cf798 3 09/14/2014
electromotivated 0:871adb9cf798 4
electromotivated 0:871adb9cf798 5 General/Basic Motor Driver Class providing Motor Control using a COTS Motor Driver
electromotivated 0:871adb9cf798 6
electromotivated 0:871adb9cf798 7 Class based off of Christopher Hasler's Motordriver library found at
knotbeer 4:9283a3f684af 8 https://developer.mbed.org/cookbook/Motor
knotbeer 4:9283a3f684af 9
knotbeer 4:9283a3f684af 10
electromotivated 0:871adb9cf798 11 * Permission is hereby granted, free of charge, to any person obtaining a copy
electromotivated 0:871adb9cf798 12 * of this software and associated documentation files (the "Software"), to deal
electromotivated 0:871adb9cf798 13 * in the Software without restriction, including without limitation the rights
electromotivated 0:871adb9cf798 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
electromotivated 0:871adb9cf798 15 * copies of the Software, and to permit persons to whom the Software is
electromotivated 0:871adb9cf798 16 * furnished to do so, subject to the following conditions:
electromotivated 0:871adb9cf798 17 *
electromotivated 0:871adb9cf798 18 * The above copyright notice and this permission notice shall be included in
electromotivated 0:871adb9cf798 19 * all copies or substantial portions of the Software.
electromotivated 0:871adb9cf798 20 *
electromotivated 0:871adb9cf798 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
electromotivated 0:871adb9cf798 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
electromotivated 0:871adb9cf798 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
electromotivated 0:871adb9cf798 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
electromotivated 0:871adb9cf798 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
electromotivated 0:871adb9cf798 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
electromotivated 0:871adb9cf798 27 * THE SOFTWARE.
electromotivated 0:871adb9cf798 28 */
electromotivated 0:871adb9cf798 29
electromotivated 0:871adb9cf798 30 #ifndef MOTORDRIVER_H
electromotivated 0:871adb9cf798 31 #define MOTORDRIVER_H
electromotivated 0:871adb9cf798 32
electromotivated 0:871adb9cf798 33 #include "mbed.h"
electromotivated 0:871adb9cf798 34
knotbeer 4:9283a3f684af 35 /** The state of the motor */
knotbeer 4:9283a3f684af 36 typedef enum {
knotbeer 4:9283a3f684af 37 MOTOR_ERROR=0, /**< Motor Error Mode */
knotbeer 4:9283a3f684af 38 DRIVING_CW=1, /**< Driving Clockwise (forward) */
knotbeer 4:9283a3f684af 39 DRIVING_CCW=2, /**< Driving Counter-Clockwise (reverse) */
knotbeer 4:9283a3f684af 40 BRAKING=3, /**< Braking Mode */
knotbeer 4:9283a3f684af 41 COASTING=4 /**< Coasting Mode */
knotbeer 4:9283a3f684af 42 } Code_t;
knotbeer 4:9283a3f684af 43 /** The code and value of the state of the motor. */
knotbeer 4:9283a3f684af 44 typedef struct {
knotbeer 4:9283a3f684af 45 Code_t code; /**< The Code_t enum of the motor state */
knotbeer 4:9283a3f684af 46 float value; /**< The numerical value of the motor state */
knotbeer 4:9283a3f684af 47 } State_t;
knotbeer 2:5470035811dd 48 /** MotorDriver class.
knotbeer 2:5470035811dd 49 * Used for DIY project "RC-AutoBot" by Clownface.space
knotbeer 2:5470035811dd 50 *
knotbeer 2:5470035811dd 51 * Example:
knotbeer 2:5470035811dd 52 * @code
knotbeer 2:5470035811dd 53 * #include "mbed.h"
knotbeer 2:5470035811dd 54 * #include "MotorDriver.h"
knotbeer 2:5470035811dd 55 *
knotbeer 2:5470035811dd 56 * MotorDriver m1(in1, in2, pwm);
knotbeer 4:9283a3f684af 57 *
knotbeer 3:7a6365365ee2 58 * void dist(int distance)
knotbeer 3:7a6365365ee2 59 * {
knotbeer 3:7a6365365ee2 60 * //put code here to happen when the distance is changed
knotbeer 3:7a6365365ee2 61 * printf("\r\n** Distance changed to %dmm\r\n", distance);
knotbeer 3:7a6365365ee2 62 * sensorDistance = distance;
knotbeer 3:7a6365365ee2 63 * }
knotbeer 3:7a6365365ee2 64 *
knotbeer 2:5470035811dd 65 * int main() {
knotbeer 2:5470035811dd 66 * m1.setSpeed(1);
knotbeer 2:5470035811dd 67 * }
knotbeer 2:5470035811dd 68 * @endcode
knotbeer 2:5470035811dd 69 */
knotbeer 4:9283a3f684af 70 class MotorDriver
knotbeer 4:9283a3f684af 71 {
knotbeer 4:9283a3f684af 72 public:
knotbeer 4:9283a3f684af 73 /** Constructor of MotorDriver Objects
knotbeer 4:9283a3f684af 74 @param in_1 Direction Input 1
knotbeer 4:9283a3f684af 75 @param in_2 Direction Input 2
knotbeer 4:9283a3f684af 76 @param pwm PWM speed control input
knotbeer 4:9283a3f684af 77 @param pwmFreq PWM frequency, some motors may whine at lower freqs
knotbeer 4:9283a3f684af 78 @param isBrakable Boolean value indicating whether or not the
knotbeer 4:9283a3f684af 79 motor driver is brakeable(see your datasheet)
knotbeer 4:9283a3f684af 80 */
knotbeer 4:9283a3f684af 81 MotorDriver(DigitalOut in1, DigitalOut in2, PwmOut pwm, float pwmFreq, bool isBrakeable = false);
knotbeer 4:9283a3f684af 82
knotbeer 4:9283a3f684af 83 /** Sets speed of motor normalized between -1.0 to 1.0
knotbeer 4:9283a3f684af 84 @param speed Value -1.0 to 1.0 (>0 CW at speed as percentage)
knotbeer 4:9283a3f684af 85 (<0 CCW at speed as percentage)
knotbeer 4:9283a3f684af 86 (=0 speed is zero)
knotbeer 4:9283a3f684af 87 @return state of the motor
knotbeer 4:9283a3f684af 88 NOTE: This method will NOT allow user to instantaneously swithch
knotbeer 4:9283a3f684af 89 from CW to CCW or vise versa. Doing so will cause the motor to
knotbeer 4:9283a3f684af 90 be put in to a BRAKE condition, while the motor_state.code will
knotbeer 4:9283a3f684af 91 be updated to MOTOR_ERROR. User should avoid trying to do this, call
knotbeer 4:9283a3f684af 92 first setSpeed(0) or brake().
knotbeer 4:9283a3f684af 93 */
knotbeer 4:9283a3f684af 94 State_t setSpeed(float speed);
knotbeer 4:9283a3f684af 95
knotbeer 4:9283a3f684af 96 /** Same as setSpeed(float speed), however does not impose the safety disallowing
knotbeer 4:9283a3f684af 97 instantaneous reversal of motor direction. It is up to the user to ensure they
knotbeer 4:9283a3f684af 98 do not blow up their motor.
knotbeer 4:9283a3f684af 99
knotbeer 4:9283a3f684af 100 @param speed How fast to run the motor
knotbeer 4:9283a3f684af 101 */
knotbeer 4:9283a3f684af 102 State_t forceSetSpeed(float speed);
knotbeer 4:9283a3f684af 103
knotbeer 4:9283a3f684af 104 /** Put motor into braked config
knotbeer 4:9283a3f684af 105 @param intensity How hard to brake (0.0 to 1.0)
knotbeer 4:9283a3f684af 106 @return state of the motors
knotbeer 4:9283a3f684af 107 */
knotbeer 4:9283a3f684af 108 State_t brake(float intensity);
knotbeer 4:9283a3f684af 109
knotbeer 4:9283a3f684af 110 /** Put motor into stop/coast config
knotbeer 4:9283a3f684af 111 */
knotbeer 4:9283a3f684af 112 State_t coast();
knotbeer 4:9283a3f684af 113
knotbeer 4:9283a3f684af 114 /** Get state of the motor
knotbeer 4:9283a3f684af 115 @return state of the motor
knotbeer 4:9283a3f684af 116 */
knotbeer 4:9283a3f684af 117 State_t getState();
knotbeer 4:9283a3f684af 118
knotbeer 4:9283a3f684af 119 protected: // Protected so objects that inherit can access
knotbeer 4:9283a3f684af 120 State_t motor_state;
knotbeer 4:9283a3f684af 121 DigitalOut _in1;
knotbeer 4:9283a3f684af 122 DigitalOut _in2;
knotbeer 4:9283a3f684af 123 PwmOut _pwm;
knotbeer 4:9283a3f684af 124 bool isBrakeable;
knotbeer 4:9283a3f684af 125 void determineState(); // Determine motor state based on in1, in2, and pwm
electromotivated 0:871adb9cf798 126 };
electromotivated 0:871adb9cf798 127
electromotivated 0:871adb9cf798 128 #endif