Sets motor currents based on input motorvalue
Diff: motorConfig.cpp
- Revision:
- 0:131a76b8848a
- Child:
- 1:84cb991c4d28
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/motorConfig.cpp Tue Oct 17 09:51:50 2017 +0000 @@ -0,0 +1,58 @@ +#include "motorConfig.h" +#include "mbed.h" +#include "FastPWM.h" + +// Member function definitions +motorConfig::motorConfig(PinName a, PinName b, PinName c, PinName d, PinName e):ledG(a),ledR(b),ledB(c),directionPin(e),pwmPin(d){ + currentState = KILLED; + pwmPin.period(1.0/5000.0); + directionPin = 0; + } + +void motorConfig::setMotor(float motorValue){ + switch(currentState){ + case KILLED: + pwmPin.write(0.0); + // Set motor direction + if (motorValue >=0){ + // corresponds to CW rotation of motor axle + directionPin = 0; + } else if(motorValue < 0){ + // corresponds to CCW rotation of motor axle + directionPin = 1; + } + ledR = 0; + ledG = 1; + ledB = 1; + break; + case ACTIVE: + // Set motor direction + if (motorValue >=0){ + // corresponds to CW rotation of motor axle + directionPin.write(0); + } else if(motorValue < 0){ + // corresponds to CCW rotation of motor axle + directionPin.write(1); + } + + // Set motor speed + if (fabs(motorValue)>1){ + pwmPin = 1.0; + } + else { + pwmPin.write(fabs(motorValue) + 0.4); + } + ledR = 1; + ledG = 1; + ledB = 0; + break; + } + } + +void motorConfig::turnMotorOn(){ + currentState = ACTIVE; + } + +void motorConfig::killSwitch(){ + currentState = KILLED; + } \ No newline at end of file