brewcontrol.cpp 10.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2017 Philippe Kalaf, MIT License
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
 * and associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
 
// brew worker to run 10 times per second
#define BREW_WORKER_PERIOD 0.1

22
// Boiler PID worker to run 1 times per second
23 24 25 26 27 28
#define PID_WORKER_PERIOD 1

// PWM period
// 0.8333 for 60Hz, 1 for 50Hz for 1% resolution
#define BOILER_PWM_PERIOD 0.8333

Philippe Kalaf's avatar
Philippe Kalaf committed
29 30 31
// Soft stop time
#define SOFT_STOP_TIME_S 7.0

32 33 34 35 36 37 38 39 40
// Manage different brew modes and timings
#include "brewcontrol.h"

BrewControl::BrewControl(   PinName brew_pin, 
                            PinName flow_sensor_pin,
                            PinName zcd_input_pin,
                            PinName pump_control_pin,
                            PinName pressure_sensor_pin,
                            PinName temp_sensor_pin,
41
			    //PinName temp2_sensor_pin,
42 43 44 45 46 47 48
                            PinName boiler_pwm_pin
                            ) : 
                            _brew_switch(brew_pin, 0), 
                            _flow_sensor(flow_sensor_pin), 
                            _pump_control(zcd_input_pin, pump_control_pin),
                            _pressure_sensor(pressure_sensor_pin),
                            _temp_sensor(temp_sensor_pin),
49
			    //_temp2_sensor(temp2_sensor_pin),
50 51 52 53 54 55 56 57 58 59 60 61
                            _boiler_pwm(boiler_pwm_pin)
{
    _preinfuse_time = 0;
    _brew_switch = 0;
    
    // 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);
    
    // let's start at 93 C
    _target_shot_temperature = 93;

62 63 64 65 66 67
    // 9 bars is default
    _target_shot_pressure = 9;

    _boiler_pid.setPIDGains(0.075, 0.1, 0.9);
    _boiler_pid.setIntegratorLimits(0, 1);

68 69 70 71
    // Boiler is on by default
    enable_boiler();
}

72
float BrewControl::get_current_temperature_side()
73 74 75 76
{
    return _temp_sensor.read();
}

77 78 79 80 81 82 83 84 85 86 87 88 89
float BrewControl::get_current_temperature_top()
{
 //   return _temp2_sensor.read();

    return 0;
}

float BrewControl::get_current_temperature()
{
    //return (_temp_sensor.read() + _temp2_sensor.read())/2;
    return get_current_temperature_side();
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
void BrewControl::set_shot_temperature(float shot_temp)
{
    if (shot_temp <= 20)
        _target_shot_temperature = 20;
    else if (shot_temp >= 110)
        _target_shot_temperature = 110;
    else
        _target_shot_temperature = shot_temp;
}

float BrewControl::get_shot_temperature()
{
    return _target_shot_temperature;
}

105 106 107 108 109 110 111 112
void BrewControl::set_shot_pressure(float pressure)
{
    if(pressure <= 0)
	_target_shot_pressure = 0;
    else if(pressure >= 12)
	_target_shot_pressure = 12;
    _target_shot_pressure = pressure;
}
113 114 115 116

// Return pressure in bars
float BrewControl::get_current_pressure()
{
117
    return _pressure_sensor.read_bars();
118 119 120 121 122 123 124 125 126 127 128
}

int BrewControl::get_shot_volume()
{
    return _target_shot_volume;
}


void BrewControl::boiler_pid_worker()
{
    // take temperature measurement
129
    float latestTemp = get_current_temperature(); 
130
        
131
    float power = 0.0;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

    // 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
150
    _latest_temp = latestTemp;
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
}

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;
}

bool BrewControl::toggle_boiler()
{
    if (_enable_boiler)
        disable_boiler();
    else
        enable_boiler();
    
    return _enable_boiler;
}

177
void BrewControl::pressure_up(uint8_t value)
178
{
179
    _pump_control.level_up(value);
180 181
}

182
void BrewControl::pressure_down(uint8_t value)
183
{
184
    _pump_control.level_down(value);
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
}

// pre-infuse time in seconds
// set to 0 to disable
void BrewControl::set_preinfuse_time(int time)
{
    if (time > 0)
        _preinfuse_time = time;
    else
        _preinfuse_time = 0;
}

int BrewControl::get_preinfuse_time()
{
    return _preinfuse_time;
}

uint8_t BrewControl::get_pump_level()
{
    return _pump_control.get_level();
}

// This is to set the wanted shot time
void BrewControl::set_shot_time(int time)
{
    _target_shot_time = time;
}

// This is to set the wanted shot volume
void BrewControl::set_shot_volume(int volume)
{
216
    if (volume >= 0)
217 218 219
        _target_shot_volume = volume;
}

220 221 222 223 224 225
// This is to set the wanted flow rate (ml/s)
void BrewControl::set_shot_flow_rate(float flow_rate)
{
    _target_flow_rate = flow_rate;
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
// return current shot_clock in seconds
float BrewControl::get_current_time()
{
    return _shot_clock;
}

// return current volume in ml
float BrewControl::get_current_volume()
{
    return _flow_sensor.read();
}

// return the current flow rate
float BrewControl::get_current_flow_rate()
{
    return _flow_sensor.get_flow_rate();
}

// read brew on/off state
245
uint8_t BrewControl::get_state()
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
{
    return _state;
}

void BrewControl::brew_worker()
{
    _shot_clock = _shot_clock + BREW_WORKER_PERIOD;
    
    if (_mode == MODE_PRE_INFUSE)
    {
        // First let's fill up the portafilter
        if (_flow_sensor.read() <= PORTAFILTER_VOLUME)
            return;
        
        // It's full, let's stop the pump for set pre-infuse time
261
        if (_pump_control.get_level() != 0)
262
        {
263
            _pump_control.set_level(0);
264 265 266 267 268 269 270 271 272 273
            _shot_clock = 0;
        }
        
        // Once pre-infuse time runs out, set brew mode
        if (_shot_clock >= _preinfuse_time)
        {
            _mode = _prev_mode;
            _pump_control.set_level(_prev_pressure);
            _shot_clock = 0;
            _flow_sensor.reset_count();
274
	    _state = BREWING;
275 276 277 278
        }
    }
    else if(_mode == MODE_TIME)
    {
279 280 281 282 283 284 285
	// Auto-adjust pressure to target
	float error = _target_shot_pressure - get_current_pressure();
	if(error < -0.25)
	    pressure_down();
	else if(error > 0.25) 
	    pressure_up();

286
        if(_shot_clock >= _target_shot_time)
Philippe Kalaf's avatar
Philippe Kalaf committed
287
            soft_stop();
288
    }_target_shot_pressure - get_current_pressure()
289 290
    else if(_mode == MODE_VOLUME)
    {
291 292 293 294 295 296 297 298
	// Auto-adjust pressure to target
	float error = _target_shot_pressure - get_current_pressure();
        if(error < -0.25)
	    pressure_down();
	else if(error > 0.25)
	    pressure_up();

	if(_flow_sensor.read() >= _target_shot_volume)
Philippe Kalaf's avatar
Philippe Kalaf committed
299
            soft_stop();
300
    }
301
    else if(_mode == MODE_FLOW_RATE)
302
    {
303 304 305 306 307 308 309 310 311 312 313 314
	// Re-calculate target flowrate
	_target_flow_rate = (_target_shot_volume - _flow_sensor.read())
	    / (_target_shot_time - _shot_clock);
	// Auto-adjust flow-rate
	if(_target_flow_rate - _flow_sensor.get_flow_rate() < 0)
	    pressure_down();
	else
	    pressure_up();

	// Stop when target shot volume is reached
	if(_flow_sensor.read() >= _target_shot_volume)
            soft_stop();
315
    }
316 317 318
    else if(_mode == MODE_MANUAL)
    {
    }
319 320 321 322 323 324
}

uint8_t BrewControl::start()
{
    if(_state) return _mode;
    
325
    _state = BREWING;
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    
    // reset shot clock and flow sensor
    _flow_sensor.reset_count();
    _shot_clock = 0;
    _brew_ticker.detach();
    
    // We have 4 operating modes based on which values are set or unset
    
    // Time mode - constant pressure, constant time, variable volume
    if (_target_shot_time && !_target_shot_volume)
        _mode = MODE_TIME;
        
    // Volume mode - constant pressure, constant volume, variable time
    else if(!_target_shot_time && _target_shot_volume)
        _mode = MODE_VOLUME;
        
342
    // Flowrate mode - constant volume, constant time, variable flow-rate 
343
    else if(_target_shot_time && _target_shot_volume)
344
        _mode = MODE_FLOW_RATE;
345 346 347 348 349 350 351 352 353 354 355 356
        
    // Manual on/off via start(), stop(), toggle()
    else
        _mode = MODE_MANUAL;
    
    // Let's save settings before we set pre-infuse mode
    if (_preinfuse_time && _mode != MODE_MANUAL)
    {
        _prev_mode = _mode;
        _prev_pressure = get_pump_level();
	// set pre-infuse mode
        _mode = MODE_PRE_INFUSE;
357 358 359
	_state = PRE_INFUSING;

	// we pre-infuse at low pressure
360 361 362 363 364 365 366 367 368 369 370 371 372 373
        _pump_control.set_level(45);
    }
    
    // Turn on brewing circuits
    _brew_switch = 1;
    
    // Run worker at defined period
    if(_mode != MODE_MANUAL)
	    _brew_ticker.attach(callback(this, &BrewControl::brew_worker),
			    BREW_WORKER_PERIOD);

    return _mode;
}

374 375 376 377 378 379 380 381
void BrewControl::toggle_solenoid()
{
    if(_brew_switch)
	_brew_switch = 0;
    else
	_brew_switch = 1;
}

382 383
void BrewControl::stop()
{
384
    _state = STOPPED;
385
    _brew_switch = 0;
386
    _shot_clock = 0;
387 388 389 390 391
    set_shot_volume(0);
    set_shot_time(0);
    _flow_sensor.reset_count();
}

Philippe Kalaf's avatar
Philippe Kalaf committed
392 393 394
void BrewControl::soft_stop()
{
    _pump_control.set_level(0);
395 396 397 398
    _brew_ticker.detach();
    _soft_stop_timer.attach(callback(this, &BrewControl::stop),
			SOFT_STOP_TIME_S);
    _state = SOFT_STOPPING;
Philippe Kalaf's avatar
Philippe Kalaf committed
399 400
}

401
uint8_t BrewControl::toggle()
402
{
403
    if(_state == BREWING || _state == PRE_INFUSING)
Philippe Kalaf's avatar
Philippe Kalaf committed
404
        soft_stop();
405
    else if(_state == STOPPED)
406 407 408 409 410 411 412 413 414
        start();
        
    return _state;
}

PhaseControl *BrewControl::get_pump_control_ptr()
{
    return &_pump_control;
}