Andrea Faustinelli / espresso-for-geeks-master
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers brewcontrol.h Source File

brewcontrol.h

00001 /* Copyright (c) 2017 Philippe Kalaf, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or 
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #ifndef brewcontrol_h
00020 #define brewcontrol_h
00021 
00022 #include "mbed.h"
00023 #include "fhksc.h"
00024 #include "phasecontrol.h"
00025 #include "lmt01.h"
00026 #include "pidcontrol.h"
00027 #include "pressuresensor.h"
00028 
00029 #define MODE_TIME 1
00030 #define MODE_YIELD 2
00031 #define MODE_TIME_YIELD 3
00032 #define MODE_MANUAL 4
00033 #define MODE_STEAM 5
00034 
00035 // Brew states
00036 #define STOPPED         0        
00037 #define BREWING         1
00038 #define PRE_INFUSING    2
00039 #define SOFT_STOPPING   3
00040 #define STEAMING        4
00041 
00042 #define PORTAFILTER_VOLUME 100
00043 
00044 /// This is the main class where all brew functions and sensors are handled
00045 class BrewControl 
00046 {
00047 public:
00048     /// Constructor requires the pin numbers for all inputs and outputs
00049     BrewControl(PinName brew_pin, 
00050                 PinName flow_sensor_pin, 
00051                 PinName zcd_input_pin,
00052                 PinName pump_control_pin,
00053                 PinName pressure_sensor_pin,
00054                 PinName temp_sensor_pin,
00055         PinName temp2_sensor_pin,
00056                 PinName boiler_pwm_pin);
00057 
00058     /// Set the target shot temperature in C
00059     void set_shot_temperature(float temperature);
00060     /// Get the target shot temperature in C
00061     float get_shot_temperature();
00062     /// Get the live temperature of water in the boiler in C
00063     float get_current_temperature();
00064     float get_current_temperature_side();
00065     float get_current_temperature_top();
00066 
00067     /// Set the target shot time in s
00068     void set_shot_time(int time);
00069     /// Get the target shot time in s
00070     int get_shot_time();
00071     /// Get the live shot clock time in s
00072     float get_current_time();
00073 
00074     /// Set the required pre-infuse time in s, 0 for no pre-infuse
00075     void set_preinfuse_time(int time);
00076     /// Get the current set pre-infuse time in s, 0 if no pre-infuse is set
00077     int get_preinfuse_time();
00078     /// Function call to manually set pre-infuse quantity
00079     void stop_preinfuse_now();
00080 
00081     /// Set the target shot volume in ml (default 60ml)
00082     void set_shot_volume(int volume);
00083     /// Get the target shot volume in ml
00084     int get_shot_volume();
00085     /// Get the live shot volume in ml
00086     float get_current_volume();
00087 
00088     /// Set the target shot flow rate in ml/s
00089     void set_shot_flow_rate(float flow_rate);
00090     /// Get the target shot flow rate in ml/s 
00091     float get_shot_flow_rate();
00092     /// Get the live shot flow rate in ml/s
00093     float get_current_flow_rate();
00094     
00095     
00096     /// Set the target pressure in bars 
00097     void set_shot_pressure(float pressure);
00098     /// Get the target pressure in bars
00099     float get_shot_pressure();
00100     /// Get the current measured pressure in bars
00101     float get_current_pressure();
00102     void pressure_up(uint8_t value = 1);
00103     void pressure_down(uint8_t value = 1);
00104     /// Get the currently set pump level (0 to 100%)
00105     uint8_t get_pump_level();
00106 
00107     /// Return 1 if currently brewing, 0 if not
00108     uint8_t get_state();
00109     /// Toggle brewing
00110     uint8_t toggle(uint8_t mode);
00111     /// Start brewing, this resets all live variables and based on set targets initiates the right brew mode, returns set mode
00112     uint8_t start(uint8_t mode);
00113     /// Stop brewing with reduced backflush
00114     void soft_stop();
00115 
00116     /// Enable PID boiler control
00117     void enable_boiler();
00118     /// Disable PID boiler control, this means water remains cold
00119     void disable_boiler();
00120     /// Toggle PID boiler control
00121     bool toggle_boiler();
00122 
00123     /// Get average pressure of last shot
00124     float get_average_pressure();
00125 
00126     PhaseControl *get_pump_control_ptr();
00127     void toggle_solenoid();
00128     uint16_t get_last_pulse_count_side();
00129     uint16_t get_last_pulse_count_top();
00130 
00131 private:
00132     Mutex _brew_worker_mutex;
00133 
00134     // Signal to turn on/off pump
00135     DigitalOut _brew_switch;
00136     
00137     // Flow sensor
00138     FHKSC _flow_sensor;
00139     
00140     // Pump phase controller for pressure control
00141     PhaseControl _pump_control;
00142 
00143     // Soft stop reduces backflush
00144     Timeout _soft_stop_timer;
00145 
00146     // Pressure Sensor
00147     PressureSensor _pressure_sensor;
00148     
00149     // Temperature control
00150     LMT01 _temp_sensor;
00151     LMT01 _temp2_sensor;
00152     PwmOut _boiler_pwm;
00153     PIDControl _boiler_pid;
00154     float _latest_temp;
00155 
00156     // Previous temp used for steam mode
00157     float _prev_temp;
00158     
00159     // Boiler state
00160     bool _enable_boiler;
00161     
00162     // The shot clock is the time spent brewing
00163     Timer _shot_clock;
00164 
00165     // Desired shot time, volume and temperature during a brew
00166     int _target_shot_time;
00167     int _target_shot_volume;
00168     float _target_shot_temperature;
00169     float _target_flow_rate;
00170     float _target_shot_pressure;
00171 
00172     // Operating modes
00173     uint8_t _state;
00174     int _mode;
00175 
00176     // The average pressure of previous shot
00177     float _average_pressure;
00178     
00179     // These are used for pre-infuse mode to go back to initial settings after
00180     // pre-infuse is done
00181     int _prev_mode;
00182     int _preinfuse_time;
00183 
00184     // This is for manual pre-infuse control
00185     bool _stop_preinfuse;
00186     
00187     // Workers
00188     Thread _brew_worker_thread;
00189     void _brew_worker();
00190     Thread _pid_worker_thread;
00191     void _boiler_pid_worker();
00192 
00193     // Helper function to set state and thread flags at once
00194     void _set_state(uint8_t state);
00195     void _stop();
00196 };
00197 
00198 #endif// brewcontrol_h