Clownface / MotorDriver

Fork of MotorDriver by Matt Rogers

Committer:
knotbeer
Date:
Thu Apr 28 01:43:44 2016 +0000
Revision:
1:15e17abb9f29
Parent:
0:871adb9cf798
Child:
3:7a6365365ee2
Changed items to suit our needs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
electromotivated 0:871adb9cf798 1 #include "MotorDriver.h"
electromotivated 0:871adb9cf798 2 #include "mbed.h"
electromotivated 0:871adb9cf798 3 #include <cmath>
electromotivated 0:871adb9cf798 4
electromotivated 0:871adb9cf798 5 // See .h file for function descriptions and details
electromotivated 0:871adb9cf798 6
knotbeer 1:15e17abb9f29 7 MotorDriver::MotorDriver(DigitalOut in1, DigitalOut in2, PwmOut pwm, float pwmFreq, bool isBrakeable)
knotbeer 1:15e17abb9f29 8 : _in1(in1), _in2(in2), _pwm(pwm)
electromotivated 0:871adb9cf798 9 {
electromotivated 0:871adb9cf798 10 // Initialize motor and variables
knotbeer 1:15e17abb9f29 11 _in1 = 1;
knotbeer 1:15e17abb9f29 12 _in2 = 0;
knotbeer 1:15e17abb9f29 13 _pwm = 0.0;
knotbeer 1:15e17abb9f29 14 this->isBrakeable = isBrakeable;
knotbeer 1:15e17abb9f29 15 _pwm.period(1.0f/pwmFreq);
electromotivated 0:871adb9cf798 16 determineState();
electromotivated 0:871adb9cf798 17 }
electromotivated 0:871adb9cf798 18
knotbeer 1:15e17abb9f29 19 void MotorDriver::determineState()
knotbeer 1:15e17abb9f29 20 {
knotbeer 1:15e17abb9f29 21 if(_in1==1 && _in2==1) {
electromotivated 0:871adb9cf798 22 motor_state.code = BRAKING;
knotbeer 1:15e17abb9f29 23 motor_state.value = _pwm;
knotbeer 1:15e17abb9f29 24 } else if(_in1==1 && _in2==0) {
electromotivated 0:871adb9cf798 25 motor_state.code = DRIVING_CW;
electromotivated 0:871adb9cf798 26 motor_state.value = _pwm;
knotbeer 1:15e17abb9f29 27 } else if(_in1==0 && _in2==1) {
electromotivated 0:871adb9cf798 28 motor_state.code = DRIVING_CCW;
knotbeer 1:15e17abb9f29 29 motor_state.value = _pwm;
knotbeer 1:15e17abb9f29 30 } else if(_in1==0 && _in2==0) {
electromotivated 0:871adb9cf798 31 motor_state.code = COASTING;
electromotivated 0:871adb9cf798 32 motor_state.value = _pwm;
knotbeer 1:15e17abb9f29 33 } else {
knotbeer 1:15e17abb9f29 34 motor_state.code = MOTOR_ERROR; // Undefined config found
knotbeer 1:15e17abb9f29 35 motor_state.value = _pwm;
electromotivated 0:871adb9cf798 36 }
electromotivated 0:871adb9cf798 37 }
electromotivated 0:871adb9cf798 38
knotbeer 1:15e17abb9f29 39 State_t MotorDriver::setSpeed(float speed)
knotbeer 1:15e17abb9f29 40 {
electromotivated 0:871adb9cf798 41 // Prevent instantaneous reversal; put into brake if requested to do so
knotbeer 1:15e17abb9f29 42 bool isTryingToInstantReverse =
knotbeer 1:15e17abb9f29 43 (bool)((speed < 0.0f) && (motor_state.code == DRIVING_CW) && (_pwm > 0.05f)) ||
knotbeer 1:15e17abb9f29 44 (bool)((speed > 0.0f) && (motor_state.code == DRIVING_CCW)&& (_pwm > 0.05f));
knotbeer 1:15e17abb9f29 45 if(isTryingToInstantReverse) {
electromotivated 0:871adb9cf798 46 // Set motor to brake, set state to error
electromotivated 0:871adb9cf798 47 coast();
knotbeer 1:15e17abb9f29 48 motor_state.code = MOTOR_ERROR;
knotbeer 1:15e17abb9f29 49 motor_state.value = _pwm;
electromotivated 0:871adb9cf798 50 return motor_state;
knotbeer 1:15e17abb9f29 51 } else {
knotbeer 1:15e17abb9f29 52 if(speed == 0.0f) {
electromotivated 0:871adb9cf798 53 // No effect to _in pins
knotbeer 1:15e17abb9f29 54 } else {
knotbeer 1:15e17abb9f29 55 _in1 = (speed>0.0f);
knotbeer 1:15e17abb9f29 56 _in2 = (speed<0.0f);
electromotivated 0:871adb9cf798 57 }
electromotivated 0:871adb9cf798 58 _pwm = std::abs(speed);
electromotivated 0:871adb9cf798 59 determineState();
electromotivated 0:871adb9cf798 60 return motor_state;
electromotivated 0:871adb9cf798 61 }
electromotivated 0:871adb9cf798 62 }
electromotivated 0:871adb9cf798 63
knotbeer 1:15e17abb9f29 64 State_t MotorDriver::forceSetSpeed(float speed)
knotbeer 1:15e17abb9f29 65 {
knotbeer 1:15e17abb9f29 66 if(speed == 0.0f) {
knotbeer 1:15e17abb9f29 67 // No effect to _in pins
knotbeer 1:15e17abb9f29 68 } else {
knotbeer 1:15e17abb9f29 69 _in1 = (speed>0.0f);
knotbeer 1:15e17abb9f29 70 _in2 = (speed<0.0f);
knotbeer 1:15e17abb9f29 71 }
knotbeer 1:15e17abb9f29 72 _pwm = std::abs(speed);
knotbeer 1:15e17abb9f29 73 determineState();
knotbeer 1:15e17abb9f29 74 return motor_state;
electromotivated 0:871adb9cf798 75 }
electromotivated 0:871adb9cf798 76
knotbeer 1:15e17abb9f29 77 State_t MotorDriver::brake(float intensity)
knotbeer 1:15e17abb9f29 78 {
electromotivated 0:871adb9cf798 79 if(!isBrakeable) coast();
knotbeer 1:15e17abb9f29 80 else {
knotbeer 1:15e17abb9f29 81 _in1=1;
knotbeer 1:15e17abb9f29 82 _in2=1;
knotbeer 1:15e17abb9f29 83 _pwm = _pwm * (1.0f - intensity);
electromotivated 0:871adb9cf798 84 }
electromotivated 0:871adb9cf798 85 determineState();
electromotivated 0:871adb9cf798 86 return motor_state;
electromotivated 0:871adb9cf798 87 }
electromotivated 0:871adb9cf798 88
knotbeer 1:15e17abb9f29 89 State_t MotorDriver::coast()
knotbeer 1:15e17abb9f29 90 {
knotbeer 1:15e17abb9f29 91 _in1=0;
knotbeer 1:15e17abb9f29 92 _in2=0;
knotbeer 1:15e17abb9f29 93 _pwm = 1.0;
electromotivated 0:871adb9cf798 94 determineState();
electromotivated 0:871adb9cf798 95 return motor_state;
electromotivated 0:871adb9cf798 96 }
electromotivated 0:871adb9cf798 97
knotbeer 1:15e17abb9f29 98 State_t MotorDriver::getState()
knotbeer 1:15e17abb9f29 99 {
electromotivated 0:871adb9cf798 100 return motor_state;
electromotivated 0:871adb9cf798 101 }