test

Dependencies:   ESP8266 HCSR04 PID

Fork of car_test_v1 by 涂 桂旺

Committer:
tgw
Date:
Sat Nov 25 03:36:58 2017 +0000
Revision:
3:9e51de1050a1
test

Who changed what in which revision?

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