Commit aae02739 authored by Philippe Kalaf's avatar Philippe Kalaf

Move boiler pid worker from isr to a thread

parent 1d49d759
Pipeline #41767606 failed with stages
in 47 seconds
......@@ -20,8 +20,8 @@
// brew worker period in ms
#define BREW_WORKER_PERIOD 100
// Boiler PID worker to run 1 times per second
#define PID_WORKER_PERIOD 1
// Boiler PID worker in ms
#define PID_WORKER_PERIOD 500
// PWM period
// 0.8333 for 60Hz, 1 for 50Hz for 1% resolution
......@@ -55,7 +55,8 @@ BrewControl::BrewControl( PinName brew_pin,
_temp_sensor(temp_sensor_pin),
_temp2_sensor(temp2_sensor_pin),
_boiler_pwm(boiler_pwm_pin),
_brew_worker_thread(osPriorityNormal, 1024)
_brew_worker_thread(osPriorityNormal, 512),
_pid_worker_thread(osPriorityNormal, 256)
{
_preinfuse_time = 0;
_brew_switch = 0;
......@@ -83,6 +84,9 @@ BrewControl::BrewControl( PinName brew_pin,
// Start main worker thread of brewing
_brew_worker_thread.start(callback(this, &BrewControl::_brew_worker));
// Start pid worker thread
_pid_worker_thread.start(callback(this, &BrewControl::_boiler_pid_worker));
}
float BrewControl::get_current_temperature_side()
......@@ -145,45 +149,49 @@ int BrewControl::get_shot_volume()
return _target_shot_volume;
}
// ISR
void BrewControl::_boiler_pid_worker()
{
// take temperature measurement
float latestTemp = get_current_temperature();
float power = 0.0;
while(true)
{
ThisThread::sleep_for(PID_WORKER_PERIOD);
// if the temperature is near zero, we assume there's an error
if ( latestTemp > 0.5 ) {
// calculate PID update
power = _boiler_pid.update(_target_shot_temperature - latestTemp,
latestTemp);
}
if(!_enable_boiler)
continue;
// Validate number
if ( power > 1 )
power = 1;
else if ( power < 0 )
power = 0;
// take temperature measurement
float latestTemp = get_current_temperature();
// let's set new power on boiler
_boiler_pwm = power;
float power = 0.0;
// store the latest temperature reading
_latest_temp = latestTemp;
// if the temperature is near zero, we assume there's an error
if ( latestTemp > 0.5 ) {
// calculate PID update
power = _boiler_pid.update(_target_shot_temperature - latestTemp,
latestTemp);
}
// Validate number
if ( power > 1 )
power = 1;
else if ( power < 0 )
power = 0;
// let's set new power on boiler
_boiler_pwm = power;
// store the latest temperature reading
_latest_temp = latestTemp;
}
}
void BrewControl::enable_boiler()
{
_enable_boiler = 1;
_boiler_pid_ticker.attach(callback(this, &BrewControl::_boiler_pid_worker),
PID_WORKER_PERIOD);
}
void BrewControl::disable_boiler()
{
_enable_boiler = 0;
_boiler_pid_ticker.detach();
_boiler_pwm = 0;
}
......@@ -426,7 +434,6 @@ void BrewControl::_brew_worker()
}
_brew_worker_mutex.unlock();
}
}
......
......@@ -188,7 +188,7 @@ private:
// Workers
Thread _brew_worker_thread;
void _brew_worker();
Ticker _boiler_pid_ticker;
Thread _pid_worker_thread;
void _boiler_pid_worker();
void _stop();
......
......@@ -27,7 +27,7 @@ const int LMT01::_pulse_temp_table[20][2] = {
{110, 2569}, {120, 2731}, {130, 2894}, {140, 3058}, {150, 3220}
};
LMT01::LMT01(PinName pin) : _interrupt(pin), _worker_thread(osPriorityNormal, 1024) { // create the InterruptIn on the pin specified to LMT01
LMT01::LMT01(PinName pin) : _interrupt(pin), _worker_thread(osPriorityNormal, 256) { // create the InterruptIn on the pin specified to LMT01
_interrupt.mode(PullUp); // an internal pull up is used, otherwise disable here
......
......@@ -364,8 +364,8 @@ void draw_menu()
void enter_power_save()
{
// let's set ambient temp to keep things cool
brew_control.set_shot_temperature(23);
// let's disable PID temperature control
brew_control.disable_boiler();
// turn on LCD power saving
//LCD.power_save(1);
......@@ -377,8 +377,8 @@ void exit_power_save()
{
power_save = 0;
power_save_timer.reset();
// go back to previously set temperature
set_params(M_BREW_SETTINGS);
// enabled PID temperature control
brew_control.enable_boiler();
// turn off LCD power saving
//LCD.power_save(0);
}
......
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