Sets motor currents based on input motorvalue
motorConfig.cpp@2:91ea2963629a, 2017-10-23 (annotated)
- Committer:
- tvlogman
- Date:
- Mon Oct 23 13:47:38 2017 +0000
- Revision:
- 2:91ea2963629a
- Parent:
- 1:84cb991c4d28
Works now
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 | 2:91ea2963629a | 5 | // Constructor |
tvlogman | 2:91ea2963629a | 6 | motorConfig::motorConfig(PinName _directionPin, PinName _pwmPin):directionPin(_directionPin),pwmPin(_pwmPin){ |
tvlogman | 0:131a76b8848a | 7 | pwmPin.period(1.0/5000.0); |
tvlogman | 0:131a76b8848a | 8 | directionPin = 0; |
tvlogman | 0:131a76b8848a | 9 | } |
tvlogman | 0:131a76b8848a | 10 | |
tvlogman | 2:91ea2963629a | 11 | // Other member function definitions |
tvlogman | 0:131a76b8848a | 12 | void motorConfig::setMotor(float motorValue){ |
tvlogman | 2:91ea2963629a | 13 | // Set motor direction |
tvlogman | 2:91ea2963629a | 14 | if (motorValue >=0){ |
tvlogman | 2:91ea2963629a | 15 | // corresponds to CW rotation of motor axle |
tvlogman | 2:91ea2963629a | 16 | directionPin.write(0); |
tvlogman | 2:91ea2963629a | 17 | } else if(motorValue < 0){ |
tvlogman | 2:91ea2963629a | 18 | // corresponds to CCW rotation of motor axle |
tvlogman | 2:91ea2963629a | 19 | directionPin.write(1); |
tvlogman | 2:91ea2963629a | 20 | } |
tvlogman | 0:131a76b8848a | 21 | |
tvlogman | 2:91ea2963629a | 22 | // Set motor speed |
tvlogman | 2:91ea2963629a | 23 | if (fabs(motorValue)>1){ |
tvlogman | 2:91ea2963629a | 24 | pwmPin = 1.0; |
tvlogman | 2:91ea2963629a | 25 | } |
tvlogman | 2:91ea2963629a | 26 | else { |
tvlogman | 2:91ea2963629a | 27 | pwmPin.write(fabs(motorValue) + 0.4); |
tvlogman | 2:91ea2963629a | 28 | } |
tvlogman | 2:91ea2963629a | 29 | |
tvlogman | 0:131a76b8848a | 30 | } |
tvlogman | 0:131a76b8848a | 31 | |
tvlogman | 2:91ea2963629a | 32 | void motorConfig::kill(){ |
tvlogman | 2:91ea2963629a | 33 | // currentState = KILLED; |
tvlogman | 2:91ea2963629a | 34 | pwmPin.write(0.0); |
tvlogman | 2:91ea2963629a | 35 | |
tvlogman | 0:131a76b8848a | 36 | } |