Sets motor currents based on input motorvalue
motorConfig.cpp@1:84cb991c4d28, 2017-10-22 (annotated)
- Committer:
- tvlogman
- Date:
- Sun Oct 22 08:43:06 2017 +0000
- Revision:
- 1:84cb991c4d28
- Parent:
- 0:131a76b8848a
- Child:
- 2:91ea2963629a
Added calibrate state
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 | 1:84cb991c4d28 | 49 | case CALIBRATE: |
tvlogman | 1:84cb991c4d28 | 50 | pwmPin.write(0.0); |
tvlogman | 1:84cb991c4d28 | 51 | |
tvlogman | 1:84cb991c4d28 | 52 | |
tvlogman | 1:84cb991c4d28 | 53 | ledR = 1; |
tvlogman | 1:84cb991c4d28 | 54 | ledG = 0; |
tvlogman | 1:84cb991c4d28 | 55 | ledB = 0; |
tvlogman | 1:84cb991c4d28 | 56 | |
tvlogman | 1:84cb991c4d28 | 57 | break; |
tvlogman | 0:131a76b8848a | 58 | } |
tvlogman | 0:131a76b8848a | 59 | } |
tvlogman | 0:131a76b8848a | 60 | |
tvlogman | 0:131a76b8848a | 61 | void motorConfig::turnMotorOn(){ |
tvlogman | 0:131a76b8848a | 62 | currentState = ACTIVE; |
tvlogman | 0:131a76b8848a | 63 | } |
tvlogman | 0:131a76b8848a | 64 | |
tvlogman | 0:131a76b8848a | 65 | void motorConfig::killSwitch(){ |
tvlogman | 0:131a76b8848a | 66 | currentState = KILLED; |
tvlogman | 0:131a76b8848a | 67 | } |