Sets motor currents based on input motorvalue
motorConfig.cpp@0:131a76b8848a, 2017-10-17 (annotated)
- Committer:
- tvlogman
- Date:
- Tue Oct 17 09:51:50 2017 +0000
- Revision:
- 0:131a76b8848a
- Child:
- 1:84cb991c4d28
Now works!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tvlogman | 0:131a76b8848a | 1 | #include "motorConfig.h" |
tvlogman | 0:131a76b8848a | 2 | #include "mbed.h" |
tvlogman | 0:131a76b8848a | 3 | #include "FastPWM.h" |
tvlogman | 0:131a76b8848a | 4 | |
tvlogman | 0:131a76b8848a | 5 | // Member function definitions |
tvlogman | 0:131a76b8848a | 6 | motorConfig::motorConfig(PinName a, PinName b, PinName c, PinName d, PinName e):ledG(a),ledR(b),ledB(c),directionPin(e),pwmPin(d){ |
tvlogman | 0:131a76b8848a | 7 | currentState = KILLED; |
tvlogman | 0:131a76b8848a | 8 | pwmPin.period(1.0/5000.0); |
tvlogman | 0:131a76b8848a | 9 | directionPin = 0; |
tvlogman | 0:131a76b8848a | 10 | } |
tvlogman | 0:131a76b8848a | 11 | |
tvlogman | 0:131a76b8848a | 12 | void motorConfig::setMotor(float motorValue){ |
tvlogman | 0:131a76b8848a | 13 | switch(currentState){ |
tvlogman | 0:131a76b8848a | 14 | case KILLED: |
tvlogman | 0:131a76b8848a | 15 | pwmPin.write(0.0); |
tvlogman | 0:131a76b8848a | 16 | // Set motor direction |
tvlogman | 0:131a76b8848a | 17 | if (motorValue >=0){ |
tvlogman | 0:131a76b8848a | 18 | // corresponds to CW rotation of motor axle |
tvlogman | 0:131a76b8848a | 19 | directionPin = 0; |
tvlogman | 0:131a76b8848a | 20 | } else if(motorValue < 0){ |
tvlogman | 0:131a76b8848a | 21 | // corresponds to CCW rotation of motor axle |
tvlogman | 0:131a76b8848a | 22 | directionPin = 1; |
tvlogman | 0:131a76b8848a | 23 | } |
tvlogman | 0:131a76b8848a | 24 | ledR = 0; |
tvlogman | 0:131a76b8848a | 25 | ledG = 1; |
tvlogman | 0:131a76b8848a | 26 | ledB = 1; |
tvlogman | 0:131a76b8848a | 27 | break; |
tvlogman | 0:131a76b8848a | 28 | case ACTIVE: |
tvlogman | 0:131a76b8848a | 29 | // Set motor direction |
tvlogman | 0:131a76b8848a | 30 | if (motorValue >=0){ |
tvlogman | 0:131a76b8848a | 31 | // corresponds to CW rotation of motor axle |
tvlogman | 0:131a76b8848a | 32 | directionPin.write(0); |
tvlogman | 0:131a76b8848a | 33 | } else if(motorValue < 0){ |
tvlogman | 0:131a76b8848a | 34 | // corresponds to CCW rotation of motor axle |
tvlogman | 0:131a76b8848a | 35 | directionPin.write(1); |
tvlogman | 0:131a76b8848a | 36 | } |
tvlogman | 0:131a76b8848a | 37 | |
tvlogman | 0:131a76b8848a | 38 | // Set motor speed |
tvlogman | 0:131a76b8848a | 39 | if (fabs(motorValue)>1){ |
tvlogman | 0:131a76b8848a | 40 | pwmPin = 1.0; |
tvlogman | 0:131a76b8848a | 41 | } |
tvlogman | 0:131a76b8848a | 42 | else { |
tvlogman | 0:131a76b8848a | 43 | pwmPin.write(fabs(motorValue) + 0.4); |
tvlogman | 0:131a76b8848a | 44 | } |
tvlogman | 0:131a76b8848a | 45 | ledR = 1; |
tvlogman | 0:131a76b8848a | 46 | ledG = 1; |
tvlogman | 0:131a76b8848a | 47 | ledB = 0; |
tvlogman | 0:131a76b8848a | 48 | break; |
tvlogman | 0:131a76b8848a | 49 | } |
tvlogman | 0:131a76b8848a | 50 | } |
tvlogman | 0:131a76b8848a | 51 | |
tvlogman | 0:131a76b8848a | 52 | void motorConfig::turnMotorOn(){ |
tvlogman | 0:131a76b8848a | 53 | currentState = ACTIVE; |
tvlogman | 0:131a76b8848a | 54 | } |
tvlogman | 0:131a76b8848a | 55 | |
tvlogman | 0:131a76b8848a | 56 | void motorConfig::killSwitch(){ |
tvlogman | 0:131a76b8848a | 57 | currentState = KILLED; |
tvlogman | 0:131a76b8848a | 58 | } |