Commit c79ee807 authored by Philippe Kalaf's avatar Philippe Kalaf

Use thread flags to block brew worker thread when not brewing

parent b3d77922
Pipeline #55086335 failed with stages
in 58 seconds
......@@ -55,14 +55,12 @@ BrewControl::BrewControl( PinName brew_pin,
_temp_sensor(temp_sensor_pin),
_temp2_sensor(temp2_sensor_pin),
_boiler_pwm(boiler_pwm_pin),
_brew_worker_thread(osPriorityNormal, 512),
_brew_worker_thread(osPriorityNormal, 768),
_pid_worker_thread(osPriorityNormal, 256)
{
_preinfuse_time = 0;
_brew_switch = 0;
_state = STOPPED;
// at 60Hz, we got 120 zero-crosses per sec, we want to capture 100 of
// those within each PWM period
_boiler_pwm.period(BOILER_PWM_PERIOD);
......@@ -87,6 +85,8 @@ BrewControl::BrewControl( PinName brew_pin,
// Start pid worker thread
_pid_worker_thread.start(callback(this, &BrewControl::_boiler_pid_worker));
_set_state(STOPPED);
}
float BrewControl::get_current_temperature_side()
......@@ -317,25 +317,31 @@ void BrewControl::_log_brew_params()
}
#endif
// Internal helper function to set brew states properly
void BrewControl::_set_state(uint8_t state)
{
_state = state;
if (_state == BREWING || _state == STEAMING || _state == PRE_INFUSING)
_brew_worker_thread.flags_set(1);
}
void BrewControl::_brew_worker()
{
while(true)
{
ThisThread::sleep_for(BREW_WORKER_PERIOD);
// If we are not brewing, let's just wait until we start brewing
if (_state != PRE_INFUSING && _state != BREWING && _state != STEAMING)
{
// _set_state() will set the flag when we start brewing
ThisThread::flags_wait_any(1);
}
_brew_worker_mutex.lock();
#ifdef LOG
_log_brew_params();
#endif
if (_state == STOPPED || _state == SOFT_STOPPING)
// At each iteration, make sure we are in a brewing state, otherwise just wait
// ThisThread::flags_wait_any(PRE_INFUSING | BREWING | STEAMING);
{
_brew_worker_mutex.unlock();
continue;
}
if (_state == PRE_INFUSING)
{
// First let's fill up the portafilter
......@@ -359,7 +365,7 @@ void BrewControl::_brew_worker()
_pump_control.set_level(_prev_pressure);
_flow_sensor.reset_count();
_shot_clock.reset();
_state = BREWING;
_set_state(BREWING);
_stop_preinfuse = 0;
}
......@@ -432,7 +438,9 @@ void BrewControl::_brew_worker()
_stop();
}
ThisThread::sleep_for(BREW_WORKER_PERIOD);
_brew_worker_mutex.unlock();
}
}
......@@ -461,13 +469,13 @@ uint8_t BrewControl::start(uint8_t mode)
_prev_mode = _mode;
_prev_pressure = get_pump_level();
// set pre-infuse mode
_state = PRE_INFUSING;
_set_state(PRE_INFUSING);
// we pre-infuse at low pressure
_pump_control.set_level(60);
}
else
_state = BREWING;
_set_state(BREWING);
if(_mode == MODE_STEAM)
{
......@@ -499,7 +507,7 @@ void BrewControl::toggle_solenoid()
// This function is sometimes called from an ISR
void BrewControl::_stop()
{
_state = STOPPED;
_set_state(STOPPED);
// Close solenoid
_brew_switch = 0;
......@@ -538,7 +546,7 @@ void BrewControl::soft_stop()
_average_pressure = _pressure_sensor.stop_count();
_shot_clock.stop();
_state = SOFT_STOPPING;
_set_state(SOFT_STOPPING);
// Call stop() after SOFT_STOP_TIME_S
_soft_stop_timer.attach(callback(this, &BrewControl::_stop),
......
......@@ -32,12 +32,12 @@
#define MODE_MANUAL 4
#define MODE_STEAM 5
// Brew states, will also be used as thread flags
#define STOPPED static_cast<int>(1 << 0)
#define BREWING static_cast<int>(1 << 1)
#define PRE_INFUSING static_cast<int>(1 << 2)
#define SOFT_STOPPING static_cast<int>(1 << 3)
#define STEAMING static_cast<int>(1 << 4)
// Brew states
#define STOPPED 0
#define BREWING 1
#define PRE_INFUSING 2
#define SOFT_STOPPING 3
#define STEAMING 4
#define PORTAFILTER_VOLUME 100
......@@ -191,6 +191,8 @@ private:
Thread _pid_worker_thread;
void _boiler_pid_worker();
// Helper function to set state and thread flags at once
void _set_state(uint8_t state);
void _stop();
};
......
......@@ -742,11 +742,13 @@ int main()
brew_control.get_current_time(),
brew_control.get_current_flow_rate());
*/
/*
pc.printf("%.1f %.1f %d %d\n", brew_control.get_current_temperature_side(),
brew_control.get_current_temperature_top(),
brew_control.get_last_pulse_count_side(),
brew_control.get_last_pulse_count_top()
);
*/
#if MBED_HEAP_STATS_ENABLED
print_heap_stats();
......
......@@ -41,7 +41,7 @@ int PressureSensor::read_pa()
int pressure_pascal = (_input.read_u16() - 10300);
pressure_pascal = (pressure_pascal * 25);
return pressure_pascal;
return pressure_pascal;
}
void PressureSensor::worker()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment