Skip to content

Commit 0c77425

Browse files
committed
feat(drinks-machine): implement sequential pump activation and PWM speed control
1 parent 460fffb commit 0c77425

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

server/firmware/serverEspReact/apps/drinks machine/services/PumpsService.hpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ class PumpsService {
1818
int pins[] = {PIN_PUMP_1, PIN_PUMP_2, PIN_PUMP_3, PIN_PUMP_4};
1919
for(int p : pins) {
2020
pinMode(p, OUTPUT);
21-
digitalWrite(p, 0);
21+
analogWrite(p, 0);
2222
}
2323
}
2424

25-
// Calculates duration based on ingredients and 20ml calibration
25+
// Calculates total duration by summing sequential ingredient times
2626
unsigned long calculateServingDuration(const MenuEntry& entry) {
27-
unsigned long maxTime = 0;
27+
unsigned long totalTime = 0;
2828

29-
Serial.printf("\n[Pumps] --- Analyzing Recipe: %s ---\n", entry.name.c_str());
29+
Serial.printf("\n[Pumps] --- Analyzing Recipe (Sequential): %s ---\n", entry.name.c_str());
3030

3131
if (entry.index >= 0 && entry.index < (int)MenuService::getInstance().cocktails.size()) {
3232
const auto& recipe = MenuService::getInstance().cocktails[entry.index].ingredients;
@@ -36,17 +36,13 @@ class PumpsService {
3636
bool matched = false;
3737
for (const auto& b : bottles) {
3838
if (b.liquid == ing.name) {
39-
// ratePerMl = Seconds for 1ml (e.g. 1.6s / 20 = 0.08s)
4039
float ratePerMl = b.timeCalibration / 20.0f;
41-
// durationMs = ml * rate * 1000 (e.g. 150ml * 0.08s * 1000 = 12000ms)
4240
unsigned long durationMs = (unsigned long)(ing.quantity * ratePerMl * 1000.0f);
4341

4442
Serial.printf(" - %s: %dml (Pump %d, %.2fs/20ml) -> Duration: %lums\n",
4543
ing.name.c_str(), ing.quantity, b.id, b.timeCalibration, durationMs);
4644

47-
if (durationMs > maxTime) {
48-
maxTime = durationMs;
49-
}
45+
totalTime += durationMs;
5046
matched = true;
5147
break;
5248
}
@@ -56,18 +52,18 @@ class PumpsService {
5652
}
5753
}
5854

59-
// Safety limit (2 minutes)
60-
if (maxTime == 0 || maxTime > 120000) {
61-
Serial.printf(" ! Unusual duration calculated (%lums). Forcing to 3000ms for safety.\n", maxTime);
62-
maxTime = 3000;
55+
// Safety limit (4 minutes for sequential)
56+
if (totalTime == 0 || totalTime > 240000) {
57+
Serial.printf(" ! Unusual duration calculated (%lums). Forcing to 3000ms for safety.\n", totalTime);
58+
totalTime = 3000;
6359
}
6460
} else {
6561
Serial.println(" ! Error: Invalid recipe index.");
66-
maxTime = 3000;
62+
totalTime = 3000;
6763
}
6864

69-
Serial.printf("[Pumps] Total cycle duration: %lums\n\n", maxTime);
70-
return maxTime;
65+
Serial.printf("[Pumps] Total sequence duration: %lums\n\n", totalTime);
66+
return totalTime;
7167
}
7268

7369
void processPumps(bool isServing, const MenuEntry& entry, unsigned long elapsed) {
@@ -81,22 +77,34 @@ class PumpsService {
8177
const auto& recipe = MenuService::getInstance().cocktails[entry.index].ingredients;
8278
const auto& bottles = MenuService::getInstance().bottles;
8379

80+
unsigned long cumulativeTime = 0;
81+
bool anyPumpActive = false;
82+
8483
for (const auto& ing : recipe) {
84+
bool found = false;
8585
for (const auto& b : bottles) {
8686
if (b.liquid == ing.name) {
8787
float ratePerMl = b.timeCalibration / 20.0f;
8888
unsigned long durationMs = (unsigned long)(ing.quantity * ratePerMl * 1000.0f);
8989

90-
if (elapsed < durationMs) {
91-
pinMode(b.gpio, OUTPUT);
92-
digitalWrite(b.gpio, HIGH);
90+
if (!anyPumpActive && elapsed >= cumulativeTime && elapsed < (cumulativeTime + durationMs)) {
91+
analogWrite(b.gpio, b.pwm);
92+
anyPumpActive = true;
9393
} else {
94-
pinMode(b.gpio, OUTPUT);
95-
digitalWrite(b.gpio, LOW);
94+
analogWrite(b.gpio, 0);
9695
}
96+
cumulativeTime += durationMs;
97+
found = true;
9798
break;
9899
}
99100
}
101+
if (!found) {
102+
// Should not happen as it was checked in calculateServingDuration
103+
}
104+
}
105+
106+
if (!anyPumpActive) {
107+
offAllPumps();
100108
}
101109
}
102110

0 commit comments

Comments
 (0)