Skip to content

Commit 963682f

Browse files
Copilotdorkmo
andcommitted
Extract flow current calculation to helper function with constants
- Added flow control constants: BASE_CURRENT, MIN_ACTIVE_CURRENT, CURRENT_RANGE - Created flowCurrentFromInput() helper to eliminate duplication - Updated all three flow calculation locations to use the helper - Easier to maintain and adjust calibration in the future Co-authored-by: dorkmo <[email protected]>
1 parent 8e6caa0 commit 963682f

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

LifeTrac-v25/arduino_opta_controller/lifetrac_v25_controller.ino

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ PubSubClient client(espClient);
132132
// Deadzone for joystick input (0.1 = 10% of range)
133133
const float DEADZONE = 0.1;
134134

135+
// Flow control current constants (4-20mA current loop)
136+
const int BASE_CURRENT = 4; // 4mA = no flow
137+
const int MIN_ACTIVE_CURRENT = 6; // 6mA = minimum active flow (~12.5%)
138+
const int CURRENT_RANGE = 16; // 16mA range (4-20mA span)
139+
135140
// Safety timeout (stop all movement if no commands received)
136141
unsigned long lastCommandTime = 0;
137142
const unsigned long SAFETY_TIMEOUT = 1000; // 1 second
@@ -377,6 +382,20 @@ void controlValve(float control, int upPin, int downPin) {
377382
}
378383
}
379384

385+
// Helper function to convert input magnitude to flow control current (4-20mA)
386+
// magnitude: Input magnitude (0.0 to 1.0)
387+
// hasInput: Whether there is active input above deadzone
388+
// Returns: Current value in mA (4-20 range)
389+
int flowCurrentFromInput(float magnitude, bool hasInput) {
390+
if (!hasInput) {
391+
return BASE_CURRENT; // 4mA = no flow
392+
}
393+
394+
int currentValue = BASE_CURRENT + (int)(magnitude * CURRENT_RANGE);
395+
currentValue = max(currentValue, MIN_ACTIVE_CURRENT); // Minimum active flow
396+
return currentValue;
397+
}
398+
380399
void setFlowControl() {
381400
if (flowConfig == ONE_VALVE) {
382401
// Single valve mode: One valve controls all hydraulics
@@ -403,19 +422,13 @@ void setFlowControl() {
403422
hasInput = true;
404423
}
405424

406-
// Convert to 4-20mA value (4mA = minimum, 20mA = maximum)
407-
int currentValue;
408-
if (hasInput) {
409-
currentValue = 4 + (int)(minInput * 16.0);
410-
currentValue = max(currentValue, 6); // Minimum ~12.5% flow (6mA)
411-
} else {
412-
currentValue = 4; // 4mA = no flow
413-
}
425+
// Convert to 4-20mA value using helper function
426+
int currentValue = flowCurrentFromInput(minInput, hasInput);
414427

415428
// Output to primary flow valve
416429
OptaController.analogWriteCurrent(FLOW_CONTROL_PIN_1, currentValue);
417430
// Disable secondary valve in single valve mode
418-
OptaController.analogWriteCurrent(FLOW_CONTROL_PIN_2, 4);
431+
OptaController.analogWriteCurrent(FLOW_CONTROL_PIN_2, BASE_CURRENT);
419432

420433
} else {
421434
// Dual valve mode: Two independent valves
@@ -431,25 +444,15 @@ void setFlowControl() {
431444
float maxInput1 = 0.0;
432445
maxInput1 = max(maxInput1, abs(leftTrackSpeed)); // Left track speed
433446
maxInput1 = max(maxInput1, abs(currentInput.right_y)); // Arms
434-
435-
int currentValue1 = 4 + (int)(maxInput1 * 16.0);
436-
if (maxInput1 > DEADZONE) {
437-
currentValue1 = max(currentValue1, 6);
438-
} else {
439-
currentValue1 = 4;
440-
}
447+
bool hasInput1 = maxInput1 > DEADZONE;
448+
int currentValue1 = flowCurrentFromInput(maxInput1, hasInput1);
441449

442450
// Calculate flow for Valve 2 (right track + bucket)
443451
float maxInput2 = 0.0;
444452
maxInput2 = max(maxInput2, abs(rightTrackSpeed)); // Right track speed
445453
maxInput2 = max(maxInput2, abs(currentInput.right_x)); // Bucket
446-
447-
int currentValue2 = 4 + (int)(maxInput2 * 16.0);
448-
if (maxInput2 > DEADZONE) {
449-
currentValue2 = max(currentValue2, 6);
450-
} else {
451-
currentValue2 = 4;
452-
}
454+
bool hasInput2 = maxInput2 > DEADZONE;
455+
int currentValue2 = flowCurrentFromInput(maxInput2, hasInput2);
453456

454457
// Output to both flow valves
455458
OptaController.analogWriteCurrent(FLOW_CONTROL_PIN_1, currentValue1);
@@ -468,9 +471,9 @@ void stopAllMovement() {
468471
digitalWrite(BUCKET_UP_PIN, LOW);
469472
digitalWrite(BUCKET_DOWN_PIN, LOW);
470473

471-
// Stop flow control - set to 4mA (no flow) for both valves
472-
OptaController.analogWriteCurrent(FLOW_CONTROL_PIN_1, 4);
473-
OptaController.analogWriteCurrent(FLOW_CONTROL_PIN_2, 4);
474+
// Stop flow control - set to BASE_CURRENT (no flow) for both valves
475+
OptaController.analogWriteCurrent(FLOW_CONTROL_PIN_1, BASE_CURRENT);
476+
OptaController.analogWriteCurrent(FLOW_CONTROL_PIN_2, BASE_CURRENT);
474477
}
475478

476479
void publishStatus() {

0 commit comments

Comments
 (0)