A library to control a linear actuator box
linearActuatorControl.cpp@1:cf533adb862c, 2020-02-24 (annotated)
- Committer:
- crussell21
- Date:
- Mon Feb 24 08:34:01 2020 +0000
- Revision:
- 1:cf533adb862c
- Parent:
- 0:1a5f4c492b1a
inverted conditional so switch up and down are flipped to a more logical interpretation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
crussell21 | 0:1a5f4c492b1a | 1 | #include "linearActuatorControl.h" |
crussell21 | 0:1a5f4c492b1a | 2 | |
crussell21 | 0:1a5f4c492b1a | 3 | LinearActuatorControl::LinearActuatorControl() : _controlMode(D7), _runMode(D8), |
crussell21 | 0:1a5f4c492b1a | 4 | _linAct1(D9), _linAct2(D10), _linAct3(D11), _linAct4(D12), |
crussell21 | 0:1a5f4c492b1a | 5 | _actBtn1(D3), _actBtn2(D4), _actBtn3(D5), _actBtn4(D6) { |
crussell21 | 0:1a5f4c492b1a | 6 | |
crussell21 | 0:1a5f4c492b1a | 7 | for (int i = 0; i < 4; i++) { |
crussell21 | 0:1a5f4c492b1a | 8 | _idlePositions[i] = 0; |
crussell21 | 0:1a5f4c492b1a | 9 | _actPositions[i] = 0; |
crussell21 | 0:1a5f4c492b1a | 10 | _manualPositionsDown[i] = 0; |
crussell21 | 0:1a5f4c492b1a | 11 | _manualPositionsUp[i] = 1; |
crussell21 | 0:1a5f4c492b1a | 12 | } |
crussell21 | 0:1a5f4c492b1a | 13 | |
crussell21 | 0:1a5f4c492b1a | 14 | _linAct1.period_ms(20); |
crussell21 | 0:1a5f4c492b1a | 15 | _linAct2.period_ms(20); |
crussell21 | 0:1a5f4c492b1a | 16 | _linAct3.period_ms(20); |
crussell21 | 0:1a5f4c492b1a | 17 | _linAct4.period_ms(20); |
crussell21 | 0:1a5f4c492b1a | 18 | } |
crussell21 | 0:1a5f4c492b1a | 19 | |
crussell21 | 0:1a5f4c492b1a | 20 | void LinearActuatorControl::refresh() { |
crussell21 | 0:1a5f4c492b1a | 21 | if (!_controlMode) { |
crussell21 | 1:cf533adb862c | 22 | _linAct1.pulsewidth_us(!_actBtn1 ? (1000*(1+_manualPositionsDown[0])) : (1000*(1+_manualPositionsUp[0]))); |
crussell21 | 1:cf533adb862c | 23 | _linAct2.pulsewidth_us(!_actBtn2 ? (1000*(1+_manualPositionsDown[1])) : (1000*(1+_manualPositionsUp[1]))); |
crussell21 | 1:cf533adb862c | 24 | _linAct3.pulsewidth_us(!_actBtn3 ? (1000*(1+_manualPositionsDown[2])) : (1000*(1+_manualPositionsUp[2]))); |
crussell21 | 1:cf533adb862c | 25 | _linAct4.pulsewidth_us(!_actBtn4 ? (1000*(1+_manualPositionsDown[3])) : (1000*(1+_manualPositionsUp[3]))); |
crussell21 | 0:1a5f4c492b1a | 26 | |
crussell21 | 0:1a5f4c492b1a | 27 | } else { |
crussell21 | 0:1a5f4c492b1a | 28 | if (_runMode) { |
crussell21 | 0:1a5f4c492b1a | 29 | //update PWM to sequence positions |
crussell21 | 0:1a5f4c492b1a | 30 | _linAct1.pulsewidth_us(1000*(1+_actPositions[0])); |
crussell21 | 0:1a5f4c492b1a | 31 | _linAct2.pulsewidth_us(1000*(1+_actPositions[1])); |
crussell21 | 0:1a5f4c492b1a | 32 | _linAct3.pulsewidth_us(1000*(1+_actPositions[2])); |
crussell21 | 0:1a5f4c492b1a | 33 | _linAct4.pulsewidth_us(1000*(1+_actPositions[3])); |
crussell21 | 0:1a5f4c492b1a | 34 | } else { |
crussell21 | 0:1a5f4c492b1a | 35 | //change to idle positions |
crussell21 | 0:1a5f4c492b1a | 36 | _linAct1.pulsewidth_us(1000*(1+_idlePositions[0])); |
crussell21 | 0:1a5f4c492b1a | 37 | _linAct2.pulsewidth_us(1000*(1+_idlePositions[1])); |
crussell21 | 0:1a5f4c492b1a | 38 | _linAct3.pulsewidth_us(1000*(1+_idlePositions[2])); |
crussell21 | 0:1a5f4c492b1a | 39 | _linAct4.pulsewidth_us(1000*(1+_idlePositions[3])); |
crussell21 | 0:1a5f4c492b1a | 40 | } |
crussell21 | 0:1a5f4c492b1a | 41 | } |
crussell21 | 0:1a5f4c492b1a | 42 | } |
crussell21 | 0:1a5f4c492b1a | 43 | |
crussell21 | 0:1a5f4c492b1a | 44 | void LinearActuatorControl::setIdlePositions(float pos1, float pos2, float pos3, float pos4) { |
crussell21 | 0:1a5f4c492b1a | 45 | _idlePositions[0] = pos1; |
crussell21 | 0:1a5f4c492b1a | 46 | _idlePositions[1] = pos2; |
crussell21 | 0:1a5f4c492b1a | 47 | _idlePositions[2] = pos3; |
crussell21 | 0:1a5f4c492b1a | 48 | _idlePositions[3] = pos4; |
crussell21 | 0:1a5f4c492b1a | 49 | } |
crussell21 | 0:1a5f4c492b1a | 50 | |
crussell21 | 0:1a5f4c492b1a | 51 | void LinearActuatorControl::setManualPositionsDown(float pos1, float pos2, float pos3, float pos4) { |
crussell21 | 0:1a5f4c492b1a | 52 | _manualPositionsDown[0] = pos1; |
crussell21 | 0:1a5f4c492b1a | 53 | _manualPositionsDown[1] = pos2; |
crussell21 | 0:1a5f4c492b1a | 54 | _manualPositionsDown[2] = pos3; |
crussell21 | 0:1a5f4c492b1a | 55 | _manualPositionsDown[3] = pos4; |
crussell21 | 0:1a5f4c492b1a | 56 | } |
crussell21 | 0:1a5f4c492b1a | 57 | |
crussell21 | 0:1a5f4c492b1a | 58 | void LinearActuatorControl::setManualPositionsUp(float pos1, float pos2, float pos3, float pos4) { |
crussell21 | 0:1a5f4c492b1a | 59 | _manualPositionsUp[0] = pos1; |
crussell21 | 0:1a5f4c492b1a | 60 | _manualPositionsUp[1] = pos2; |
crussell21 | 0:1a5f4c492b1a | 61 | _manualPositionsUp[2] = pos3; |
crussell21 | 0:1a5f4c492b1a | 62 | _manualPositionsUp[3] = pos4; |
crussell21 | 0:1a5f4c492b1a | 63 | } |
crussell21 | 0:1a5f4c492b1a | 64 | |
crussell21 | 0:1a5f4c492b1a | 65 | void LinearActuatorControl::move(float pos1, float pos2, float pos3, float pos4) { |
crussell21 | 0:1a5f4c492b1a | 66 | _actPositions[0] = pos1; |
crussell21 | 0:1a5f4c492b1a | 67 | _actPositions[1] = pos2; |
crussell21 | 0:1a5f4c492b1a | 68 | _actPositions[2] = pos3; |
crussell21 | 0:1a5f4c492b1a | 69 | _actPositions[3] = pos4; |
crussell21 | 0:1a5f4c492b1a | 70 | refresh(); |
crussell21 | 0:1a5f4c492b1a | 71 | } |
crussell21 | 0:1a5f4c492b1a | 72 | |
crussell21 | 0:1a5f4c492b1a | 73 | bool LinearActuatorControl::inUserSequence() { |
crussell21 | 0:1a5f4c492b1a | 74 | if (_controlMode && _runMode) { |
crussell21 | 0:1a5f4c492b1a | 75 | return true; |
crussell21 | 0:1a5f4c492b1a | 76 | } |
crussell21 | 0:1a5f4c492b1a | 77 | return false; |
crussell21 | 0:1a5f4c492b1a | 78 | } |
crussell21 | 0:1a5f4c492b1a | 79 | |
crussell21 | 0:1a5f4c492b1a | 80 | float _convertToPulseWidth_us(float input) { |
crussell21 | 0:1a5f4c492b1a | 81 | return 1000*(1+input); |
crussell21 | 0:1a5f4c492b1a | 82 | } |