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