Skip to content

Commit

Permalink
make upgrade to float pressure automatic (Zer0-bit#391)
Browse files Browse the repository at this point in the history
- Lots of fixes.
- Additional startup checks( ie brew switch ON = system enters fail state).
- Refactored descale mode(needs more testing re time vs water tank levels)
- Refactored and improved temperature control based on pump flow volumes(still needs additional work but already way more promising than how it was before).
- Nextion fixes(discovered some doubling page numbers which might introduce inconsistencies)
- Possibly more stuff i can't recall cause shit's been stashed and unstashed far too many times at this point lol.
  • Loading branch information
Zer0-bit committed Jan 29, 2023
1 parent 60c8429 commit 9db7bf0
Show file tree
Hide file tree
Showing 13 changed files with 274 additions and 215 deletions.
Binary file modified lcd-hmi/nextion-lcd.HMI
Binary file not shown.
8 changes: 4 additions & 4 deletions lib/Common/profiling_phases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ bool Phase::isStopConditionReached(SensorState& currentState, uint32_t timeInSho
//----------------------------------------------------------------------//
bool PhaseStopConditions::isReached(SensorState& state, long timeInShot, ShotSnapshot stateAtPhaseStart) const {
float flow = state.weight > 0.4f ? state.weightFlow : state.smoothedPumpFlow;
float stopDelta = flow / 2.f;
float stopDelta = flow * (state.shotWeight / 100.f);

return (time >= 0L && timeInShot - stateAtPhaseStart.timeInShot >= (uint32_t) time) ||
(weight > 0.f && state.shotWeight > weight - stopDelta) ||
(pressureAbove > 0.f && state.pressure > pressureAbove) ||
(pressureBelow > 0.f && state.pressure < pressureBelow) ||
(pressureAbove > 0.f && state.smoothedPressure > pressureAbove) ||
(pressureBelow > 0.f && state.smoothedPressure < pressureBelow) ||
(waterPumpedInPhase > 0.f && state.waterPumped - stateAtPhaseStart.waterPumped > waterPumpedInPhase - stopDelta) ||
(flowAbove > 0.f && state.smoothedPumpFlow > flowAbove) ||
(flowBelow > 0.f && state.smoothedPumpFlow < flowBelow);
}

bool GlobalStopConditions::isReached(SensorState& state, long timeInShot) {
float flow = state.weight > 0.4f ? state.weightFlow : state.smoothedPumpFlow;
float stopDelta = flow / 2.f;
float stopDelta = flow * (state.shotWeight / 100.f);

return (weight > 0.f && state.shotWeight > weight - stopDelta) ||
(waterPumped > 0.f && state.waterPumped > waterPumped) ||
Expand Down
Binary file modified nextion-basic-lcd.tft
Binary file not shown.
Binary file modified nextion-discovery-lcd.tft
Binary file not shown.
109 changes: 56 additions & 53 deletions src/functional/descale.cpp
Original file line number Diff line number Diff line change
@@ -1,68 +1,71 @@
#include "../peripherals/internal_watchdog.h"
#include "descale.h"

short flushCounter;
DescalingState descalingState = IDLE;

void deScale(eepromValues_t &runningCfg, SensorState &currentState) {
static bool blink = true;
static long timer = millis();
static int currentCycleRead = 0;
static int lastCycleRead = 5;
static bool descaleFinished = false;
short flushCounter = 0;
uint8_t counter = 0;
unsigned long descalingTimer = 0;
int descalingCycle = 0;

if (brewState() && !descaleFinished) {
openValve();
if (currentCycleRead < lastCycleRead) { // descale in cycles of 5 then wait according to the below conditions
if (blink == true) { // Logic that switches between modes depending on the $blink value
setPumpToRawValue(10);
if (millis() - timer > DESCALE_PHASE1_EVERY) { //set dimmer power to min descale value for 10 sec
if(currentCycleRead < 100) lcdSetDescaleCycle(currentCycleRead);
else descaleFinished = true;
blink = false;
timer = millis();
}
} else {
setPumpOff();
if ((millis() - timer) > DESCALE_PHASE2_EVERY) { //nothing for 5 minutes
currentCycleRead++;
if(currentCycleRead < 100) lcdSetDescaleCycle(currentCycleRead);
else descaleFinished = true;
blink = true;
timer = millis();
void deScale(eepromValues_t &runningCfg, SensorState &currentState) {
switch (descalingState) {
case IDLE:
if (brewState()) {
runningCfg.setpoint = 9;
openValve();
descalingState = DESCALING_PHASE1;
descalingCycle = 0;
descalingTimer = millis();
}
break;
case DESCALING_PHASE1: // Slowly penetrating that scale
brewState() ? descalingState : descalingState = FINISHED;
setPumpToRawValue(10);
if (millis() - descalingTimer > DESCALE_PHASE1_EVERY) {
lcdSetDescaleCycle(descalingCycle++);
if (descalingCycle < 100) {
descalingTimer = millis();
descalingState = DESCALING_PHASE2;
} else {
descalingState = FINISHED;
}
}
} else {
break;
case DESCALING_PHASE2: // Softening the f outta that scale
brewState() ? descalingState : descalingState = FINISHED;
setPumpOff();
if (millis() - descalingTimer > DESCALE_PHASE2_EVERY) {
descalingTimer = millis();
lcdSetDescaleCycle(descalingCycle++);
descalingState = DESCALING_PHASE3;
}
break;
case DESCALING_PHASE3: // Fucking up that scale big time
brewState() ? descalingState : descalingState = FINISHED;
setPumpToRawValue(30);
if (millis() - timer > DESCALE_PHASE3_EVERY) { //set dimmer power to max descale value for 20 sec
if (millis() - descalingTimer > DESCALE_PHASE3_EVERY) {
solenoidBeat();
blink = true;
currentCycleRead++;
lastCycleRead = currentCycleRead*3;
if (lastCycleRead < 100) lcdSetDescaleCycle(currentCycleRead);
else {
lcdSetDescaleCycle(100);
descaleFinished = true;
lcdSetDescaleCycle(descalingCycle++);
if (descalingCycle < 100) {
descalingTimer = millis();
descalingState = DESCALING_PHASE1;
} else {
descalingState = FINISHED;
}
timer = millis();
}
}
} else if (brewState() && descaleFinished == true){
setPumpOff();
closeValve();
if ((millis() - timer) > 1000) {
lcdBrewTimerStop();
lcdShowPopup("FINISHED");
timer=millis();
}
} else {
setPumpOff();
closeValve();
currentCycleRead = 0;
lastCycleRead = 10;
descaleFinished = false;
timer = millis();
break;
case FINISHED: // Scale successufuly fucked
setPumpOff();
closeValve();
if (millis() - descalingTimer > 1000) {
lcdBrewTimerStop();
lcdShowPopup("FINISHED");
descalingState = IDLE;
descalingTimer = millis();
}
break;
}
//keeping it at temp
justDoCoffee(runningCfg, currentState, false, false);
}

Expand Down
14 changes: 11 additions & 3 deletions src/functional/descale.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@
#include "just_do_coffee.h"
#include <Arduino.h>

#define DESCALE_PHASE1_EVERY 30000 //60000 // short pump pulses during descale
#define DESCALE_PHASE2_EVERY 60000 //120000 // long pause for scale softening
#define DESCALE_PHASE3_EVERY 10000 //4000 // short burst for efficiency
#define DESCALE_PHASE1_EVERY 30000UL //30000 // short pump pulses during descale
#define DESCALE_PHASE2_EVERY 60000UL //60000 // long pause for scale softening
#define DESCALE_PHASE3_EVERY 10000UL //10000 // short burst for descale efficiency

//#############################################################################################
//###############################____DESCALE__CONTROL____######################################
//#############################################################################################

typedef enum {
IDLE,
DESCALING_PHASE1,
DESCALING_PHASE2,
DESCALING_PHASE3,
FINISHED
} DescalingState;

void deScale(eepromValues_t &runningCfg, SensorState &currentState);
void solenoidBeat(void);
void backFlush(SensorState &currentState);
Expand Down
131 changes: 60 additions & 71 deletions src/functional/just_do_coffee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,87 +2,67 @@
#include "../lcd/lcd.h"

//delta stuff
inline static float TEMP_DELTA(float d) { return (d*DELTA_RANGE); }
// inline static float TEMP_DELTA(float d) { return (d*DELTA_RANGE); }
inline static float TEMP_DELTA(float d, SensorState &currentState) {
return (
d * (currentState.pumpFlow < 1.f
? currentState.pumpFlow / 9.f
: currentState.pumpFlow / 10.f
)
);
}

void justDoCoffee(eepromValues_t &runningCfg, SensorState &currentState, bool brewActive, bool preinfusionFinished) {
int HPWR_LOW = runningCfg.hpwr / runningCfg.mainDivider;
static double heaterWave;
static bool heaterState;
float BREW_TEMP_DELTA;
float sensorTemperature = currentState.temperature + runningCfg.offsetTemp;
// Calculating the boiler heating power range based on the below input values
int HPWR_OUT = mapRange(sensorTemperature, runningCfg.setpoint - 10, runningCfg.setpoint, runningCfg.hpwr, HPWR_LOW, 0);
HPWR_OUT = constrain(HPWR_OUT, HPWR_LOW, runningCfg.hpwr); // limits range of sensor values to HPWR_LOW and HPWR
BREW_TEMP_DELTA = mapRange(sensorTemperature, runningCfg.setpoint, runningCfg.setpoint + TEMP_DELTA(runningCfg.setpoint), TEMP_DELTA(runningCfg.setpoint), 0, 0);
BREW_TEMP_DELTA = constrain(BREW_TEMP_DELTA, 0, TEMP_DELTA(runningCfg.setpoint));
BREW_TEMP_DELTA = mapRange(sensorTemperature, runningCfg.setpoint, runningCfg.setpoint + TEMP_DELTA(runningCfg.setpoint, currentState), TEMP_DELTA(runningCfg.setpoint, currentState), 0, 0);
BREW_TEMP_DELTA = constrain(BREW_TEMP_DELTA, 0, TEMP_DELTA(runningCfg.setpoint, currentState));
lcdTargetState(0); // setting the target mode to "brew temp"

if (brewActive) {
// Applying the HPWR_OUT variable as part of the relay switching logic
if (sensorTemperature > runningCfg.setpoint && sensorTemperature < runningCfg.setpoint + 0.25f && !preinfusionFinished ) {
if (millis() - heaterWave > HPWR_OUT * runningCfg.brewDivider && !heaterState ) {
setBoilerOff();
heaterState=true;
heaterWave=millis();
} else if (millis() - heaterWave > HPWR_LOW * runningCfg.mainDivider && heaterState ) {
setBoilerOn();
heaterState=false;
heaterWave=millis();
}
} else if (sensorTemperature > runningCfg.setpoint - 1.5f && sensorTemperature < runningCfg.setpoint + (runningCfg.brewDeltaState ? BREW_TEMP_DELTA : 0.f) && preinfusionFinished ) {
if (millis() - heaterWave > runningCfg.hpwr * runningCfg.brewDivider && !heaterState ) {
setBoilerOn();
heaterState=true;
heaterWave=millis();
} else if (millis() - heaterWave > runningCfg.hpwr && heaterState ) {
setBoilerOff();
heaterState=false;
heaterWave=millis();
}
} else if (runningCfg.brewDeltaState && sensorTemperature >= (runningCfg.setpoint + BREW_TEMP_DELTA) && sensorTemperature <= (runningCfg.setpoint + BREW_TEMP_DELTA + 2.5f) && preinfusionFinished ) {
if (millis() - heaterWave > runningCfg.hpwr * runningCfg.mainDivider && !heaterState ) {
setBoilerOn();
heaterState=true;
heaterWave=millis();
} else if (millis() - heaterWave > runningCfg.hpwr && heaterState ) {
setBoilerOff();
heaterState=false;
heaterWave=millis();
}
} else if(sensorTemperature <= runningCfg.setpoint - 1.5f) {
if (brewActive) { //if brewState == true
if(sensorTemperature <= runningCfg.setpoint) {
setBoilerOn();
} else {
}
else if (sensorTemperature <= runningCfg.setpoint + (runningCfg.brewDeltaState ? BREW_TEMP_DELTA : 0.f)) {
pulseHeaters(runningCfg.hpwr, runningCfg.mainDivider, runningCfg.brewDivider, brewActive);
}
else {
setBoilerOff();
}
} else { //if brewState == 0
if (sensorTemperature < ((float)runningCfg.setpoint - 10.00f)) {
} else { //if brewState == false
if (sensorTemperature <= ((float)runningCfg.setpoint - 10.00f)) {
setBoilerOn();
} else if (sensorTemperature >= ((float)runningCfg.setpoint - 10.f) && sensorTemperature < ((float)runningCfg.setpoint - 5.f)) {
if (millis() - heaterWave > HPWR_OUT && !heaterState) {
setBoilerOn();
heaterState=true;
heaterWave=millis();
} else if (millis() - heaterWave > HPWR_OUT / runningCfg.brewDivider && heaterState ) {
setBoilerOff();
heaterState=false;
heaterWave=millis();
}
} else if ((sensorTemperature >= ((float)runningCfg.setpoint - 5.f)) && (sensorTemperature <= ((float)runningCfg.setpoint - 0.25f))) {
if (millis() - heaterWave > HPWR_OUT * runningCfg.brewDivider && !heaterState) {
setBoilerOn();
heaterState=!heaterState;
heaterWave=millis();
} else if (millis() - heaterWave > HPWR_OUT / runningCfg.brewDivider && heaterState ) {
setBoilerOff();
heaterState=!heaterState;
heaterWave=millis();
}
} else {
}
else if (sensorTemperature <= ((float)runningCfg.setpoint - 5.f)) {
pulseHeaters(HPWR_OUT, 1, runningCfg.mainDivider, brewActive);
}
else if (sensorTemperature < ((float)runningCfg.setpoint)) {
pulseHeaters(HPWR_OUT, runningCfg.brewDivider, runningCfg.brewDivider, brewActive);
}
else {
setBoilerOff();
}
}
}

void pulseHeaters(uint32_t pulseLength, int factor_1, int factor_2, bool brewActive) {
static uint32_t heaterWave;
static bool heaterState;
if (millis() - heaterWave > pulseLength * factor_1 && !heaterState ) {
brewActive ? setBoilerOff() : setBoilerOn();
heaterState=!heaterState;
heaterWave=millis();
} else if (millis() - heaterWave > pulseLength / factor_2 && heaterState ) {
brewActive ? setBoilerOn() : setBoilerOff();
heaterState=!heaterState;
heaterWave=millis();
}
}

//#############################################################################################
//################################____STEAM_POWER_CONTROL____##################################
//#############################################################################################
Expand All @@ -92,22 +72,20 @@ void steamCtrl(eepromValues_t &runningCfg, SensorState &currentState, bool brewA
// steam temp control, needs to be aggressive to keep steam pressure acceptable
float sensorTemperature = currentState.temperature + runningCfg.offsetTemp;
if (steamState() && brewState()) {
closeValve();
setPumpToRawValue(80);
setBoilerOn();
} else if ((currentState.smoothedPressure <= 9.f)
&& (sensorTemperature > runningCfg.setpoint - 10.f)
&& (sensorTemperature <= runningCfg.steamSetPoint))
{
hotWaterMode();
}
else if ((currentState.smoothedPressure <= 9.f)
&& (sensorTemperature > runningCfg.setpoint - 10.f)
&& (sensorTemperature <= runningCfg.steamSetPoint)
) {
setBoilerOn();
if (currentState.smoothedPressure < 1.8f) {
#if not defined (SINGLE_BOARD) // not ENABLED if using the PCB
#if not defined (DREAM_STEAM_DISABLED) // disabled for bigger boilers which have no need of adjusting the pressure
openValve();
#endif
#endif

#ifndef DREAM_STEAM_DISABLED
#ifndef DREAM_STEAM_DISABLED //
setPumpToRawValue(3);
#endif
} else setPumpOff();
Expand All @@ -126,3 +104,14 @@ void steamCtrl(eepromValues_t &runningCfg, SensorState &currentState, bool brewA
: currentState.isSteamForgottenON = false;
} else steamTime = millis();
}
/*Water mode and all that*/
void hotWaterMode() {
#ifndef SINGLE_BOARD
openValve();
#else
closeValve();
#endif
setPumpToRawValue(80);
setBoilerOn();
}

3 changes: 2 additions & 1 deletion src/functional/just_do_coffee.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
#define DELTA_RANGE 0.25f // % to apply as delta

void justDoCoffee(eepromValues_t &runningCfg, SensorState &currentState, bool brewActive, bool preinfusionFinished);
void pulseHeaters(uint32_t pulseLength, int factor_1, int factor_2, bool brewActive);
void steamCtrl(eepromValues_t &runningCfg, SensorState &currentState, bool brewActive, unsigned long steamTime);

void hotWaterMode();
#endif

6 changes: 3 additions & 3 deletions src/functional/predictive_weight.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class PredictiveWeight {
// Through empirical testing it's been observed that ~2 bars is the indicator of the pf headspace being full
// as well as there being enough pressure for water to wet the puck enough to start the output
bool phaseTypePressure = phase.getType() == PHASE_TYPE_PRESSURE;
float pressureTarget = phaseTypePressure ? phase.getTarget() : phase.getRestriction();
pressureTarget = (pressureTarget == 0.f || pressureTarget > 1.2f) ? 1.2f : pressureTarget;
// float pressureTarget = phaseTypePressure ? phase.getTarget() : phase.getRestriction();
// pressureTarget = (pressureTarget == 0.f || pressureTarget > 2.f) ? 2.f : pressureTarget;
// We need to watch when pressure goes above the PI pressure which is a better indicator of headspace being filled.
// float preinfusionPressure = cfg.preinfusionFlowState ? cfg.preinfusionFlowPressureTarget : cfg.preinfusionBar;

Expand All @@ -49,7 +49,7 @@ class PredictiveWeight {
}

// Pressure has to reach at least half way to pressureTarget
if (pressure < pressureTarget) {
if (pressure < 1.1f/*pressureTarget*/) {
return;
}

Expand Down
Loading

0 comments on commit 9db7bf0

Please sign in to comment.