Wed, 10 Jun 2020 09:43:51 +0200
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
0 | 1 | /** |
2 | * @file task_driver.c | |
3 | * @brief BrewBoard relays driver. Control the hardware outputs with the | |
4 | * Solid State relays for the Mash/Boil kettle (MLT, the Hot | |
5 | * Liquer Tank (HLT) and the pump. The MLT has a PID controller | |
6 | * during mashing, and a simple bang on/off control during the | |
7 | * boil. | |
8 | * The HLT output can be off, bang on/off, or just on if the MLT | |
9 | * is off, depending on the configuration. | |
10 | * Use SSR modules that switch during zero crossing of the mains | |
11 | * power, so that when one is turned on and on the same time the | |
12 | * other is turned off, they won't be active at the same time. | |
13 | */ | |
14 | ||
15 | #include "config.h" | |
16 | ||
17 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
18 | #define SSR_MLT CONFIG_SSR_MLT_GPIO ///< GPIO SSR MLT pin |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
19 | #define SSR_HLT CONFIG_SSR_HLT_GPIO ///< GPIO SSR HLT pin |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
20 | #define SSR_PUMP CONFIG_SSR_PUMP_GPIO ///< GPIO Pump relay pin |
0 | 21 | |
22 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
23 | bool outEnable = false; ///< Enable outputs flag |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
24 | DRIVER_State * driver_state; ///< Driver state |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
25 | SemaphoreHandle_t xSemaphoreDriver = NULL; ///< Driver state lock |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
26 | int MLT_pin = 0; ///< MLT state |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
27 | int HLT_pin = 0; ///< HLT state |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
28 | int Pump_pin = 0; ///< Pump state |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
29 | double Input = 0; ///< PID input value |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
30 | double Output = 0; ///< PID output value |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
31 | double Setpoint = 0; ///< PID setpoint value |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
32 | int MLT_Mode = MLT_MODE_NONE; ///< MLT mode flag |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
33 | double HLT_Input = 0; ///< HLT input value |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
34 | double HLT_Setpoint = 0; ///< HLT setpoint values |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
35 | int HLT_Output = 0; ///< HLT output value |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
36 | int HLT_Mode = HLT_MODE_NONE; ///< HLT mode flag |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
37 | TickType_t MLT_time, HLT_time; |
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
38 | |
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
39 | |
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
40 | static const char *MLTTypes[] = { "None", "Bang", "PID", "Off", "Ext" }; |
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
41 | static const char *HLTTypes[] = { "None", "Bang", "Off", "On" }; |
0 | 42 | |
43 | static const char *TAG = "task_driver"; | |
44 | ||
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
45 | extern SemaphoreHandle_t xSemaphoreDS18B20; |
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
46 | extern DS18B20_State *ds18b20_state; |
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
47 | extern unsigned long lastTime; |
0 | 48 | |
49 | ||
50 | /** | |
51 | * @brief Turn the MLT SSR on or off. | |
52 | */ | |
53 | void MLT(int onoff); | |
54 | ||
55 | /** | |
56 | * @brief Turn the HLT SSR on or off. | |
57 | */ | |
58 | void HLT(int onoff); | |
59 | ||
60 | /** | |
61 | * @brief Turn the Pump on or off. | |
62 | */ | |
63 | void Pump(int onoff); | |
64 | ||
65 | ||
66 | ||
67 | void MLT(int onoff) { | |
68 | ||
69 | if (onoff && outEnable) { | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
70 | if (MLT_pin != 1) |
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
71 | MLT_time = xTaskGetTickCount(); |
0 | 72 | gpio_set_level(SSR_MLT, 1); |
73 | MLT_pin = 1; | |
74 | } else { | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
75 | if (MLT_pin) |
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
76 | runtime.MLT_usage += xTaskGetTickCount() - MLT_time; |
0 | 77 | gpio_set_level(SSR_MLT, 0); |
78 | MLT_pin = 0; | |
79 | } | |
80 | } | |
81 | ||
82 | ||
83 | ||
84 | void HLT(int onoff) { | |
85 | ||
86 | if (onoff && outEnable) { | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
87 | if (HLT_pin != 1) |
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
88 | HLT_time = xTaskGetTickCount(); |
0 | 89 | gpio_set_level(SSR_HLT, 1); |
90 | HLT_pin = 1; | |
91 | } else { | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
92 | if (HLT_pin) |
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
93 | runtime.HLT_usage += xTaskGetTickCount() - HLT_time; |
0 | 94 | gpio_set_level(SSR_HLT, 0); |
95 | HLT_pin = 0; | |
96 | } | |
97 | } | |
98 | ||
99 | ||
100 | ||
101 | void Pump(int onoff) { | |
102 | ||
103 | if (onoff && outEnable) { | |
104 | gpio_set_level(SSR_PUMP, 1); | |
105 | Pump_pin = 1; | |
106 | } else { | |
107 | gpio_set_level(SSR_PUMP, 0); | |
108 | Pump_pin = 0; | |
109 | } | |
110 | } | |
111 | ||
112 | ||
113 | ||
1
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
114 | /** |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
115 | * @brief Load PID settings from equipment record. |
ad2c8b13eb88
Updated lots of doxygen comments
Michiel Broek <mbroek@mbse.eu>
parents:
0
diff
changeset
|
116 | */ |
0 | 117 | void LoadPIDsettings() { |
82 | 118 | PID_SetTunings(equipment.PID_kP, equipment.PID_kI, equipment.PID_kD); |
0 | 119 | PID_SetSampleTime(equipment.SampleTime); |
120 | ||
121 | /* | |
122 | * Initialize the PID | |
123 | */ | |
124 | Output = 0.0; // Reset internal Iterm. | |
125 | PID_SetMode(PID_MANUAL); | |
126 | PID_SetMode(PID_AUTOMATIC); | |
127 | } | |
128 | ||
129 | ||
130 | ||
131 | void task_driver(void *pvParameter) | |
132 | { | |
133 | TickType_t wait_ticks, last_tick, now_tick; | |
134 | bool rc; | |
135 | unsigned long now, RealTime, w_StartTime = 0; | |
136 | ||
137 | ESP_LOGI(TAG, "Starting output drivers"); | |
138 | ||
139 | /* | |
140 | * Configure IOMUX register. | |
141 | */ | |
142 | gpio_pad_select_gpio(SSR_MLT); | |
143 | gpio_set_direction(SSR_MLT, GPIO_MODE_OUTPUT); | |
144 | gpio_pad_select_gpio(SSR_HLT); | |
145 | gpio_set_direction(SSR_HLT, GPIO_MODE_OUTPUT); | |
146 | gpio_pad_select_gpio(SSR_PUMP); | |
147 | gpio_set_direction(SSR_PUMP, GPIO_MODE_OUTPUT); | |
148 | ||
149 | /* | |
150 | * Initialize state | |
151 | */ | |
152 | driver_state = malloc(sizeof(DRIVER_State)); | |
153 | driver_state->enable = outEnable = false; | |
154 | driver_state->mlt_gpio = SSR_MLT; | |
155 | driver_state->mlt_mode = MLT_MODE_NONE; | |
156 | driver_state->mlt_sp = driver_state->mlt_pv = 0.0; | |
157 | driver_state->mlt_power = 0; | |
158 | driver_state->hlt_gpio = SSR_HLT; | |
159 | driver_state->hlt_mode = HLT_MODE_NONE; | |
160 | driver_state->hlt_sp = driver_state->hlt_pv = 0.0; | |
161 | driver_state->hlt_power = 0; | |
162 | driver_state->hlt_and_mlt = false; | |
163 | driver_state->pump_gpio = SSR_PUMP; | |
164 | driver_state->pump_run = 0; | |
165 | ||
86 | 166 | PID(&Input, &Output, &Setpoint, 200, 2.0, 1.5, PID_DIRECT); |
0 | 167 | |
168 | /* | |
169 | * One loop must complete in 20 mSecs, that is one mains | |
170 | * frequency period cycle in 50 Hz countries. | |
171 | */ | |
172 | while (1) { | |
173 | ||
174 | last_tick = xTaskGetTickCount(); | |
175 | ||
176 | if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) { | |
177 | /* | |
178 | * Get the current temperature readings | |
179 | */ | |
180 | if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) { | |
181 | if (ds18b20_state->mlt_valid) | |
182 | driver_state->mlt_pv = ds18b20_state->mlt_temperature; | |
183 | if (ds18b20_state->hlt_valid) | |
184 | driver_state->hlt_pv = ds18b20_state->hlt_temperature; | |
185 | xSemaphoreGive(xSemaphoreDS18B20); | |
186 | } | |
187 | ||
188 | /* | |
189 | * Other values that we need | |
190 | */ | |
191 | Input = driver_state->mlt_pv; | |
192 | Setpoint = driver_state->mlt_sp; | |
193 | if (driver_state->mlt_mode != MLT_Mode) { | |
194 | if (driver_state->mlt_mode == MLT_MODE_BANG) { | |
195 | PID_SetMode(PID_MANUAL); | |
196 | } else if (driver_state->mlt_mode == MLT_MODE_PID) { | |
197 | LoadPIDsettings(); | |
198 | } | |
199 | MLT_Mode = driver_state->mlt_mode; | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
200 | ESP_LOGI(TAG, "MLT mode set to %s", MLTTypes[MLT_Mode]); |
0 | 201 | } |
202 | if (driver_state->hlt_mode != HLT_Mode) { | |
203 | HLT_Mode = driver_state->hlt_mode; | |
87
47253f294a9f
SDK settings to reduce bin size. Some log messages to debug level. Added KWH usage registration. Added equipment power usage for HLT and MLT. Equipment database upgraded to version 2, expandable. Fixed some screen errors during temperature mash steps.
Michiel Broek <mbroek@mbse.eu>
parents:
86
diff
changeset
|
204 | ESP_LOGI(TAG, "HLT mode set to %s", HLTTypes[HLT_Mode]); |
0 | 205 | } |
206 | outEnable = driver_state->enable; | |
207 | HLT_Input = driver_state->hlt_pv; | |
208 | HLT_Setpoint = driver_state->hlt_sp; | |
209 | xSemaphoreGive(xSemaphoreDriver); | |
210 | } | |
211 | ||
212 | rc = false; | |
213 | now = xTaskGetTickCount() * portTICK_PERIOD_MS; | |
214 | ||
215 | if ((PID_GetMode() == PID_AUTOMATIC) && (MLT_Mode == MLT_MODE_PID)) { | |
216 | rc = PID_Compute(); | |
217 | RealTime = (equipment.SampleTime * equipment.MashPower) / 100; | |
218 | } else { | |
219 | /* | |
220 | * Schedule the loop ourself. | |
221 | */ | |
222 | unsigned long timeChange = (now - lastTime); | |
223 | if (timeChange >= equipment.SampleTime) { | |
224 | lastTime = now; | |
225 | rc = true; | |
226 | } | |
227 | RealTime = equipment.SampleTime; | |
228 | if (driver_state->mlt_mode == MLT_MODE_BANG) { | |
229 | Output = (Input < Setpoint) ? 255:0; | |
230 | } | |
231 | if (driver_state->mlt_mode == MLT_MODE_NONE || driver_state->mlt_mode == MLT_MODE_OFF) { | |
232 | Output = 0; | |
233 | } | |
234 | } | |
235 | ||
236 | if (rc) { | |
237 | w_StartTime = now; | |
238 | } | |
239 | ||
240 | if ((int)((Output / 255.0) * RealTime) > (now - w_StartTime)) { | |
241 | MLT(1); | |
242 | if ((equipment.SSR2 == SSR2_HLT_SHARE) || (equipment.SSR2 == SSR2_ON_IDLE)) { | |
243 | HLT(0); | |
244 | HLT_Output = 0; | |
245 | } | |
246 | } else { | |
247 | MLT(0); | |
248 | if (equipment.SSR2 == SSR2_HLT_SHARE) { | |
249 | if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) { | |
250 | HLT_Output = 0; | |
251 | if (driver_state->hlt_mode == HLT_MODE_BANG) { | |
252 | HLT_Output = (HLT_Input < HLT_Setpoint) ? 1:0; | |
253 | } else if (driver_state->hlt_mode == HLT_MODE_ON) { | |
254 | HLT_Output = 1; | |
255 | } | |
256 | xSemaphoreGive(xSemaphoreDriver); | |
257 | } | |
258 | HLT(HLT_Output); | |
259 | } else if (equipment.SSR2 == SSR2_ON_IDLE) { | |
260 | HLT_Output = 1; | |
261 | HLT(1); | |
262 | } | |
263 | } | |
264 | ||
265 | /* | |
266 | * Independant HLT temperature control | |
267 | */ | |
268 | if (equipment.SSR2 == SSR2_HLT_IND) { | |
269 | if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) { | |
270 | HLT_Output = 0; | |
271 | if (driver_state->hlt_mode == HLT_MODE_BANG) { | |
272 | HLT_Output = (HLT_Input < HLT_Setpoint) ? 1:0; | |
273 | } else if (driver_state->hlt_mode == HLT_MODE_ON) { | |
274 | HLT_Output = 1; | |
275 | } | |
276 | xSemaphoreGive(xSemaphoreDriver); | |
277 | } | |
278 | HLT(HLT_Output); | |
279 | } | |
280 | ||
281 | /* | |
282 | * Update the driver results. | |
283 | */ | |
284 | if (xSemaphoreTake(xSemaphoreDriver, 10) == pdTRUE) { | |
285 | driver_state->mlt_power = (int)((Output * 100) / 255.0); | |
286 | if (HLT_Output) { | |
287 | if (equipment.SSR2 == SSR2_HLT_SHARE) { | |
288 | driver_state->hlt_power = 100 - driver_state->mlt_power; | |
289 | } else if (equipment.SSR2 == SSR2_HLT_IND) { | |
290 | driver_state->hlt_power = 100; | |
291 | } | |
292 | } else { | |
293 | driver_state->hlt_power = 0; | |
294 | } | |
295 | if (driver_state->pump_run != Pump_pin) | |
296 | Pump(driver_state->pump_run); | |
297 | xSemaphoreGive(xSemaphoreDriver); | |
298 | } | |
299 | ||
300 | #if 0 | |
301 | if (rc) { | |
302 | printf("ST: %s MLT[In: %7.3f Out: %3.0f Sp: %6.2f %s RT: %lu] HLT[In: %7.3f Out: %d Sp: %5.1f]\n", outEnable ? "E":"D", | |
303 | Input, Output, Setpoint, PID_GetMode() ? "AUTOMATIC" : "MANUAL ", RealTime, | |
304 | HLT_Input, HLT_Output, HLT_Setpoint); | |
305 | } | |
306 | #endif | |
307 | ||
308 | // Not reliable, so do it manually. | |
309 | //vTaskDelayUntil(&last_wake_time, (1000 / 50) / portTICK_PERIOD_MS); | |
310 | now_tick = xTaskGetTickCount(); | |
311 | if ((now_tick - last_tick) > (1000 / 50)) { | |
312 | // This happens one or two times during a brew. | |
313 | wait_ticks = (1000 / 50); | |
314 | } else { | |
315 | wait_ticks = (1000 / 50) - (now_tick - last_tick); | |
316 | } | |
317 | if (wait_ticks == 0) { | |
318 | // This is rare, but it happens. | |
319 | wait_ticks = 1; | |
320 | } | |
321 | ||
322 | vTaskDelay(wait_ticks); | |
323 | } | |
324 | } | |
325 | ||
326 |