-
Notifications
You must be signed in to change notification settings - Fork 15
/
StepLogic.pde
752 lines (668 loc) · 25.1 KB
/
StepLogic.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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
177
178
179
180
181
182
183
184
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/*
Copyright (C) 2009, 2010 Matt Reba, Jeremiah Dillingham
This file is part of BrewTroller.
BrewTroller is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
BrewTroller is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with BrewTroller. If not, see <http://www.gnu.org/licenses/>.
BrewTroller - Open Source Brewing Computer
Software Lead: Matt Reba (matt_AT_brewtroller_DOT_com)
Hardware Lead: Jeremiah Dillingham (jeremiah_AT_brewtroller_DOT_com)
Documentation, Forums and more information available at http://www.brewtroller.com
*/
unsigned long lastHop, grainInStart;
unsigned int boilAdds, triggered;
/**
* Used to determine if the given step is the active step in the program.
*/
boolean stepIsActive(byte brewStep) {
if (stepProgram[brewStep] != PROGRAM_IDLE) return true; else return false;
}
/**
* Usd to determine if the given ZONE is the active ZONE in the program.
* Returns true is any step in the given ZONE is the active step, false otherwise.
*/
boolean zoneIsActive(byte brewZone) {
if (brewZone == ZONE_MASH) {
if (stepIsActive(STEP_FILL)
|| stepIsActive(STEP_DELAY)
|| stepIsActive(STEP_PREHEAT)
|| stepIsActive(STEP_ADDGRAIN)
|| stepIsActive(STEP_REFILL)
|| stepIsActive(STEP_DOUGHIN)
|| stepIsActive(STEP_ACID)
|| stepIsActive(STEP_PROTEIN)
|| stepIsActive(STEP_SACCH)
|| stepIsActive(STEP_SACCH2)
|| stepIsActive(STEP_MASHOUT)
|| stepIsActive(STEP_MASHHOLD)
|| stepIsActive(STEP_SPARGE)
) return 1; else return 0;
} else if (brewZone == ZONE_BOIL) {
if (stepIsActive(STEP_BOIL)
|| stepIsActive(STEP_CHILL)
) return 1; else return 0;
}
}
//Returns 0 if start was successful or 1 if unable to start due to conflict with other step
//Performs any logic required at start of step
//TO DO: Power Loss Recovery Handling
boolean stepInit(byte pgm, byte brewStep) {
//Nothing more to do if starting 'Idle' program
if(pgm == PROGRAM_IDLE) return 1;
//Abort Fill/Mash step init if mash Zone is not free
if (brewStep >= STEP_FILL && brewStep <= STEP_MASHHOLD && zoneIsActive(ZONE_MASH)) return 1;
//Abort sparge init if either zone is currently active
else if (brewStep == STEP_SPARGE && (zoneIsActive(ZONE_MASH) || zoneIsActive(ZONE_BOIL))) return 1;
//Allow Boil step init while sparge is still going
//If we made it without an abort, save the program number for stepCore
setProgramStep(brewStep, pgm);
if (brewStep == STEP_FILL) {
//Step Init: Fill
//Set Target Volumes
tgtVol[VS_HLT] = calcSpargeVol(pgm);
tgtVol[VS_MASH] = calcStrikeVol(pgm);
if (getProgMLHeatSrc(pgm) == VS_HLT) {
tgtVol[VS_HLT] = min(tgtVol[VS_HLT] + tgtVol[VS_MASH], getCapacity(VS_HLT));
tgtVol[VS_MASH] = 0;
}
#ifdef AUTO_FILL_START
autoValve[AV_FILL] = 1;
#endif
} else if (brewStep == STEP_DELAY) {
//Step Init: Delay
//Load delay minutes from EEPROM if timer is not already populated via Power Loss Recovery
if (getDelayMins() && !timerValue[TIMER_MASH]) setTimer(TIMER_MASH, getDelayMins());
} else if (brewStep == STEP_PREHEAT) {
//Step Init: Preheat
#ifdef MASH_PREHEAT_NOVALVES
vlvConfig[VLV_MASHHEAT] = 0;
vlvConfig[VLV_MASHIDLE] = 0;
#endif
if (getProgMLHeatSrc(pgm) == VS_HLT) {
setSetpoint(TS_HLT, calcStrikeTemp(pgm));
#ifdef MASH_PREHEAT_STRIKE
setSetpoint(TS_MASH, calcStrikeTemp(pgm));
#elif defined MASH_PREHEAT_STEP1
setSetpoint(TS_MASH, getFirstStepTemp(pgm));
#else
setSetpoint(TS_MASH, 0);
#endif
} else {
setSetpoint(TS_HLT, getProgHLT(pgm));
setSetpoint(TS_MASH, calcStrikeTemp(pgm));
}
#ifndef PID_FLOW_CONTROL
setSetpoint(VS_STEAM, getSteamTgt());
#endif
preheated[VS_HLT] = 0;
preheated[VS_MASH] = 0;
//No timer used for preheat
clearTimer(TIMER_MASH);
#ifdef MASH_PREHEAT_SENSOR
//Overwrite mash temp sensor address from EEPROM using the memory location of the specified sensor (sensor element number * 8 bytes)
EEPROMreadBytes(MASH_PREHEAT_SENSOR * 8, tSensor[TS_MASH], 8);
#endif
} else if (brewStep == STEP_ADDGRAIN) {
//Step Init: Add Grain
grainInStart = 0;
//Disable HLT and Mash heat output during 'Add Grain' to avoid dry running heat elements and burns from HERMS recirc
resetHeatOutput(VS_HLT);
resetHeatOutput(VS_MASH);
#ifndef PID_FLOW_CONTROL
setSetpoint(VS_STEAM, getSteamTgt());
#endif
setAlarm(1);
bitSet(actProfiles, VLV_ADDGRAIN);
if(getProgMLHeatSrc(pgm) == VS_HLT) {
unsigned long spargeVol = calcSpargeVol(pgm);
unsigned long mashVol = calcStrikeVol(pgm);
tgtVol[VS_HLT] = (min((spargeVol + mashVol), getCapacity(VS_HLT)) - mashVol);
#ifdef VOLUME_MANUAL
// In manual volume mode show the target mash volume as a guide to the user
tgtVol[VS_MASH] = mashVol;
#endif
#ifdef AUTO_ML_XFER
autoValve[AV_SPARGEIN] = 1;
#endif
}
} else if (brewStep == STEP_REFILL) {
//Step Init: Refill
if (getProgMLHeatSrc(pgm) == VS_HLT) {
#ifdef HLT_MIN_REFILL
SpargeVol = calcSpargeVol(pgm);
tgtVol[VS_HLT] = max(SpargeVol, HLT_MIN_REFILL_VOL);
#else
tgtVol[VS_HLT] = calcSpargeVol(pgm);
#endif
tgtVol[VS_MASH] = 0;
}
#ifdef AUTO_REFILL_START
autoValve[AV_FILL] = 1;
#endif
} else if (brewStep == STEP_DOUGHIN) {
//Step Init: Dough In
setSetpoint(TS_HLT, getProgHLT(pgm));
#ifdef RIMS_MLT_SETPOINT_DELAY
starttime = millis(); // get current time
timetoset = starttime + RIMS_DELAY; //note that overflow of the milisecond timer is not covered here
steptoset = brewStep; //step that we need to set the setpoint to after the timer is done.
RIMStimeExpired = 0; //reset the boolean so that we know if the timer has expired for this program or not
autoValve[vesselAV(TS_MASH)] = 1; // turn on the mash recirc valve profile as if the setpoint had been set
#else
setSetpoint(TS_MASH, getProgMashTemp(pgm, MASH_DOUGHIN));
#endif
#ifndef PID_FLOW_CONTROL
setSetpoint(VS_STEAM, getSteamTgt());
#endif
preheated[VS_MASH] = 0;
//Set timer only if empty (for purposed of power loss recovery)
if (!timerValue[TIMER_MASH]) setTimer(TIMER_MASH, getProgMashMins(pgm, MASH_DOUGHIN));
//Leave timer paused until preheated
timerStatus[TIMER_MASH] = 0;
} else if (brewStep == STEP_ACID) {
//Step Init: Acid Rest
setSetpoint(TS_HLT, getProgHLT(pgm));
#ifdef RIMS_MLT_SETPOINT_DELAY
if(RIMStimeExpired == 0 && steptoset != 0) steptoset = brewStep;
else
#endif
setSetpoint(TS_MASH, getProgMashTemp(pgm, MASH_ACID));
#ifndef PID_FLOW_CONTROL
setSetpoint(VS_STEAM, getSteamTgt());
#endif
preheated[VS_MASH] = 0;
//Set timer only if empty (for purposed of power loss recovery)
if (!timerValue[TIMER_MASH]) setTimer(TIMER_MASH, getProgMashMins(pgm, MASH_ACID));
//Leave timer paused until preheated
timerStatus[TIMER_MASH] = 0;
} else if (brewStep == STEP_PROTEIN) {
//Step Init: Protein
setSetpoint(TS_HLT, getProgHLT(pgm));
#ifdef RIMS_MLT_SETPOINT_DELAY
if(RIMStimeExpired == 0 && steptoset != 0) steptoset = brewStep;
else
#endif
setSetpoint(TS_MASH, getProgMashTemp(pgm, MASH_PROTEIN));
#ifndef PID_FLOW_CONTROL
setSetpoint(VS_STEAM, getSteamTgt());
#endif
preheated[VS_MASH] = 0;
//Set timer only if empty (for purposed of power loss recovery)
if (!timerValue[TIMER_MASH]) setTimer(TIMER_MASH, getProgMashMins(pgm, MASH_PROTEIN));
//Leave timer paused until preheated
timerStatus[TIMER_MASH] = 0;
} else if (brewStep == STEP_SACCH) {
//Step Init: Sacch
setSetpoint(TS_HLT, getProgHLT(pgm));
#ifdef RIMS_MLT_SETPOINT_DELAY
if(RIMStimeExpired == 0 && steptoset != 0) steptoset = brewStep;
else
#endif
setSetpoint(TS_MASH, getProgMashTemp(pgm, MASH_SACCH));
#ifndef PID_FLOW_CONTROL
setSetpoint(VS_STEAM, getSteamTgt());
#endif
preheated[VS_MASH] = 0;
//Set timer only if empty (for purposed of power loss recovery)
if (!timerValue[TIMER_MASH]) setTimer(TIMER_MASH, getProgMashMins(pgm, MASH_SACCH));
//Leave timer paused until preheated
timerStatus[TIMER_MASH] = 0;
} else if (brewStep == STEP_SACCH2) {
//Step Init: Sacch2
setSetpoint(TS_HLT, getProgHLT(pgm));
#ifdef RIMS_MLT_SETPOINT_DELAY
if(RIMStimeExpired == 0 && steptoset != 0) steptoset = brewStep;
else
#endif
setSetpoint(TS_MASH, getProgMashTemp(pgm, MASH_SACCH2));
#ifndef PID_FLOW_CONTROL
setSetpoint(VS_STEAM, getSteamTgt());
#endif
preheated[VS_MASH] = 0;
//Set timer only if empty (for purposed of power loss recovery)
if (!timerValue[TIMER_MASH]) setTimer(TIMER_MASH, getProgMashMins(pgm, MASH_SACCH2));
//Leave timer paused until preheated
timerStatus[TIMER_MASH] = 0;
} else if (brewStep == STEP_MASHOUT) {
//Step Init: Mash Out
setSetpoint(TS_HLT, getProgHLT(pgm));
#ifdef RIMS_MLT_SETPOINT_DELAY
if(RIMStimeExpired == 0 && steptoset != 0) steptoset = brewStep;
else
#endif
setSetpoint(TS_MASH, getProgMashTemp(pgm, MASH_MASHOUT));
#ifndef PID_FLOW_CONTROL
setSetpoint(VS_STEAM, getSteamTgt());
#endif
preheated[VS_MASH] = 0;
//Set timer only if empty (for purposed of power loss recovery)
if (!timerValue[TIMER_MASH]) setTimer(TIMER_MASH, getProgMashMins(pgm, MASH_MASHOUT));
//Leave timer paused until preheated
timerStatus[TIMER_MASH] = 0;
} else if (brewStep == STEP_MASHHOLD) {
setAlarm(1);
//Set HLT to Sparge Temp
setSetpoint(TS_HLT, getProgSparge(pgm));
//Cycle through steps and use last non-zero step for mash setpoint
if (!setpoint[TS_MASH]) {
byte i = MASH_MASHOUT;
while (setpoint[TS_MASH] == 0 && i >= MASH_DOUGHIN && i <= MASH_MASHOUT) setSetpoint(TS_MASH, getProgMashTemp(pgm, i--));
}
#ifndef PID_FLOW_CONTROL
setSetpoint(VS_STEAM, getSteamTgt());
#endif
} else if (brewStep == STEP_SPARGE) {
//Step Init: Sparge
#ifdef HLT_HEAT_SPARGE
#ifdef HLT_MIN_SPARGE
if (volAvg[VS_HLT] >= HLT_MIN_SPARGE)
#endif
setSetpoint(TS_HLT, getProgSparge(pgm));
#endif
#ifdef BATCH_SPARGE
#else
#ifdef SPARGE_IN_PUMP_CONTROL
prevSpargeVol[1] = volAvg[VS_HLT]; // init the value at the start of sparge
prevSpargeVol[0] = 0;
#endif
tgtVol[VS_KETTLE] = calcPreboilVol(pgm);
#ifdef AUTO_SPARGE_START
autoValve[AV_FLYSPARGE] = 1;
#endif
#ifdef PID_FLOW_CONTROL
#ifdef USEMETRIC
// value is given in 10ths of a liter per min, so 1 liter/min would be 10, and 10 * 100 = 1000 which is 1 liter/min in flow rate calcs
setSetpoint(VS_PUMP, (getSteamTgt() * 100));
#else
//value is given in 10ths of a quart per min, so 1 quart/min would be 10, and 10 *25 = 250 which is 1 quart/min in flow rate calcs (1000ths of a gallon/min)
setSetpoint(VS_PUMP, getSteamTgt()* 25);
#endif
#endif
#endif
} else if (brewStep == STEP_BOIL) {
//Step Init: Boil
#ifdef PID_FLOW_CONTROL
resetHeatOutput(VS_PUMP); // turn off the pump if we are moving to boil.
#endif
setSetpoint(VS_KETTLE, getBoilTemp());
preheated[VS_KETTLE] = 0;
boilAdds = getProgAdds(pgm);
//Set timer only if empty (for purposes of power loss recovery)
if (!timerValue[TIMER_BOIL]) {
//Clean start of Boil
setTimer(TIMER_BOIL, getProgBoil(pgm));
triggered = 0;
setBoilAddsTrig(triggered);
} else {
//Assuming power loss recovery
triggered = getBoilAddsTrig();
}
//Leave timer paused until preheated
timerStatus[TIMER_BOIL] = 0;
lastHop = 0;
boilControlState = CONTROLSTATE_AUTO;
} else if (brewStep == STEP_CHILL) {
//Step Init: Chill
pitchTemp = getProgPitch(pgm);
}
//Call event handler
eventHandler(EVENT_STEPINIT, brewStep);
return 0;
}
void stepCore() {
if (stepIsActive(STEP_FILL)) stepFill(STEP_FILL);
if (stepIsActive(STEP_PREHEAT)) {
if ((setpoint[VS_MASH] && temp[VS_MASH] >= setpoint[VS_MASH])
|| (!setpoint[VS_MASH] && temp[VS_HLT] >= setpoint[VS_HLT])
) stepAdvance(STEP_PREHEAT);
#if defined SMART_HERMS_HLT && defined SMART_HERMS_PREHEAT
smartHERMSHLT();
#endif
}
if (stepIsActive(STEP_DELAY)) if (timerValue[TIMER_MASH] == 0) stepAdvance(STEP_DELAY);
if (stepIsActive(STEP_ADDGRAIN)) {
#ifdef AUTO_GRAININ_EXIT
if(!autoValve[AV_SPARGEIN]) {
if (!grainInStart) grainInStart = millis();
else if ((millis() - grainInStart) / 1000 > AUTO_GRAININ_EXIT) stepAdvance(STEP_ADDGRAIN);
}
#endif
//Turn off Sparge In AutoValve if tgtVol has been reached
//Because this function is called before processautovalves() if we clear the auto valve here the bit in the active profile will still be set until the
// user exits the grain in step, causing it to not shut off when target volume is reached.
if (autoValve[AV_SPARGEIN] && volAvg[VS_HLT] <= tgtVol[VS_HLT]) { autoValve[AV_SPARGEIN] = 0; bitClear(actProfiles, VLV_SPARGEIN);}
else if (volAvg[VS_HLT] <= tgtVol[VS_HLT]) bitClear(actProfiles, VLV_SPARGEIN);
}
if (stepIsActive(STEP_REFILL)) stepFill(STEP_REFILL);
for (byte brewStep = STEP_DOUGHIN; brewStep <= STEP_MASHOUT; brewStep++) if (stepIsActive(brewStep)) stepMash(brewStep);
if (stepIsActive(STEP_MASHHOLD)) {
#ifdef AUTO_MASH_HOLD_EXIT
if (!zoneIsActive(ZONE_BOIL) && temp[VS_HLT] >= setpoint[VS_HLT]) stepAdvance(STEP_MASHHOLD);
#endif
}
if (stepIsActive(STEP_SPARGE)) {
#ifdef HLT_HEAT_SPARGE
#ifdef HLT_MIN_SPARGE
if (volAvg[VS_HLT] < HLT_MIN_SPARGE) setSetpoint(TS_HLT, 0);
#endif
#endif
#ifdef BATCH_SPARGE
#else
#ifdef AUTO_SPARGE_EXIT
if (volAvg[VS_KETTLE] >= tgtVol[VS_KETTLE]) stepAdvance(STEP_SPARGE);
#endif
#endif
}
if (stepIsActive(STEP_BOIL)) {
#ifdef PREBOIL_ALARM
if (!(triggered & 32768) && temp[TS_KETTLE] != BAD_TEMP && temp[TS_KETTLE] >= PREBOIL_ALARM * 100) {
setAlarm(1);
triggered |= 32768;
setBoilAddsTrig(triggered);
}
#endif
if (!preheated[VS_KETTLE] && temp[TS_KETTLE] >= setpoint[VS_KETTLE] && setpoint[VS_KETTLE] > 0) {
preheated[VS_KETTLE] = 1;
//Unpause Timer
if (!timerStatus[TIMER_BOIL]) pauseTimer(TIMER_BOIL);
}
//Turn off hop valve profile after 5s
if (lastHop > 0 && millis() - lastHop > HOPADD_DELAY) {
bitClear(actProfiles, VLV_HOPADD);
lastHop = 0;
}
if (preheated[VS_KETTLE]) {
//Boil Addition
if ((boilAdds ^ triggered) & 1) {
bitSet(actProfiles, VLV_HOPADD);
lastHop = millis();
setAlarm(1);
triggered |= 1;
setBoilAddsTrig(triggered);
}
//Timed additions (See hoptimes[] array in BrewTroller.pde)
for (byte i = 0; i < 11; i++) {
if (((boilAdds ^ triggered) & (1<<(i + 1))) && timerValue[TIMER_BOIL] <= hoptimes[i] * 60000) {
bitSet(actProfiles, VLV_HOPADD);
lastHop = millis();
setAlarm(1);
triggered |= (1<<(i + 1));
setBoilAddsTrig(triggered);
}
}
#ifdef AUTO_BOIL_RECIRC
if (timerValue[TIMER_BOIL] <= AUTO_BOIL_RECIRC * 60000) bitSet(actProfiles, VLV_BOILRECIRC);
#endif
}
//Exit Condition
if(preheated[VS_KETTLE] && timerValue[TIMER_BOIL] == 0) {
//Kill Kettle power at end of timer...
boilControlState = CONTROLSTATE_OFF;
resetHeatOutput(VS_KETTLE);
//...but wait for last hop addition to complete before leaving step
if(lastHop == 0) stepAdvance(STEP_BOIL);
}
}
if (stepIsActive(STEP_CHILL)) {
if (temp[TS_KETTLE] != -1 && temp[TS_KETTLE] <= KETTLELID_THRESH) {
if (!vlvConfigIsActive(VLV_KETTLELID)) bitSet(actProfiles, VLV_KETTLELID);
} else {
if (vlvConfigIsActive(VLV_KETTLELID)) bitClear(actProfiles, VLV_KETTLELID);
}
}
}
//stepCore logic for Fill and Refill
void stepFill(byte brewStep) {
#ifdef AUTO_FILL_EXIT
if (volAvg[VS_HLT] >= tgtVol[VS_HLT] && volAvg[VS_MASH] >= tgtVol[VS_MASH]) stepAdvance(brewStep);
#else
#ifndef VOLUME_MANUAL
if (volAvg[VS_HLT] >= tgtVol[VS_HLT] && volAvg[VS_MASH] >= tgtVol[VS_MASH]) bitClear(actProfiles, VLV_FILLHLT);
#endif
#endif
}
//stepCore Logic for all mash steps
void stepMash(byte brewStep) {
#ifdef SMART_HERMS_HLT
smartHERMSHLT();
#endif
if (!preheated[VS_MASH] && temp[TS_MASH] >= setpoint[VS_MASH]) {
preheated[VS_MASH] = 1;
//Unpause Timer
if (!timerStatus[TIMER_MASH]) pauseTimer(TIMER_MASH);
}
//Exit Condition (and skip unused mash steps)
#ifdef RIMS_MLT_SETPOINT_DELAY
if (getProgMashTemp(stepProgram[brewStep], (brewStep - 5)) == 0 || (preheated[VS_MASH] && timerValue[TIMER_MASH] == 0)) stepAdvance(brewStep);
#else
if (setpoint[VS_MASH] == 0 || (preheated[VS_MASH] && timerValue[TIMER_MASH] == 0)) stepAdvance(brewStep);
#endif
}
//Advances program to next brew step
//Returns 0 if successful or 1 if unable to advance due to conflict with another step
boolean stepAdvance(byte brewStep) {
//Save program for next step/rollback
byte program = stepProgram[brewStep];
stepExit(brewStep);
//Advance step (if applicable)
if (brewStep + 1 < NUM_BREW_STEPS) {
if (stepInit(program, brewStep + 1)) {
//Init Failed: Rollback
stepExit(brewStep + 1); //Just to make sure we clean up a partial start
setProgramStep(program, brewStep); //Show the step we started with as active
return 1;
}
}
//Init Successful
return 0;
}
//Performs exit logic specific to each step
//Note: If called directly (as opposed through stepAdvance) acts as a program abort
void stepExit(byte brewStep) {
//Mark step idle
setProgramStep(brewStep, PROGRAM_IDLE);
//Perform step closeout functions
if (brewStep == STEP_FILL || brewStep == STEP_REFILL) {
//Step Exit: Fill/Refill
tgtVol[VS_HLT] = 0;
tgtVol[VS_MASH] = 0;
autoValve[AV_FILL] = 0;
bitClear(actProfiles, VLV_FILLHLT);
bitClear(actProfiles, VLV_FILLMASH);
} else if (brewStep == STEP_DELAY) {
//Step Exit: Delay
clearTimer(TIMER_MASH);
setAlarm(0);
} else if (brewStep == STEP_ADDGRAIN) {
//Step Exit: Add Grain
tgtVol[VS_HLT] = 0;
autoValve[AV_SPARGEIN] = 0;
bitClear(actProfiles, VLV_ADDGRAIN);
bitClear(actProfiles, VLV_SPARGEIN);
bitClear(actProfiles, VLV_MASHHEAT);
bitClear(actProfiles, VLV_MASHIDLE);
resetHeatOutput(VS_HLT);
#ifdef USESTEAM
resetHeatOutput(VS_STEAM);
#endif
} else if (brewStep == STEP_PREHEAT || (brewStep >= STEP_DOUGHIN && brewStep <= STEP_MASHHOLD)) {
//Step Exit: Preheat/Mash
clearTimer(TIMER_MASH);
bitClear(actProfiles, VLV_MASHHEAT);
bitClear(actProfiles, VLV_MASHIDLE);
resetHeatOutput(VS_HLT);
resetHeatOutput(VS_MASH);
#ifdef USESTEAM
resetHeatOutput(VS_STEAM);
#endif
#ifdef MASH_PREHEAT_SENSOR
//Restore mash temp sensor address from EEPROM (address 8)
EEPROMreadBytes(8, tSensor[TS_MASH], 8);
#endif
#ifdef MASH_PREHEAT_NOVALVES
loadVlvConfigs();
#endif
} else if (brewStep == STEP_SPARGE) {
//Step Exit: Sparge
#ifdef HLT_HEAT_SPARGE
setSetpoint(TS_HLT, 0);
#endif
tgtVol[VS_HLT] = 0;
tgtVol[VS_KETTLE] = 0;
resetSpargeValves();
} else if (brewStep == STEP_BOIL) {
//Step Exit: Boil
bitClear(actProfiles, VLV_HOPADD);
#ifdef AUTO_BOIL_RECIRC
bitClear(actProfiles, VLV_BOILRECIRC);
#endif
boilControlState = CONTROLSTATE_OFF;
resetHeatOutput(VS_KETTLE);
clearTimer(TIMER_BOIL);
} else if (brewStep == STEP_CHILL) {
//Step Exit: Chill
autoValve[AV_CHILL] = 0;
bitClear(actProfiles, VLV_CHILLBEER);
bitClear(actProfiles, VLV_CHILLH2O);
}
eventHandler(EVENT_STEPEXIT, brewStep);
}
void resetSpargeValves() {
autoValve[AV_SPARGEIN] = 0;
autoValve[AV_SPARGEOUT] = 0;
autoValve[AV_FLYSPARGE] = 0;
bitClear(actProfiles, VLV_SPARGEIN);
bitClear(actProfiles, VLV_SPARGEOUT);
bitClear(actProfiles, VLV_MASHHEAT);
bitClear(actProfiles, VLV_MASHIDLE);
}
#ifdef SMART_HERMS_HLT
void smartHERMSHLT() {
if (!setpoint[VS_MASH]) return;
setpoint[VS_HLT] = setpoint[VS_MASH] * 2 - temp[TS_MASH];
//Constrain HLT Setpoint to Mash Setpoint + MASH_HEAT_LOSS (minimum) and HLT_MAX_TEMP (Maximum)
setpoint[VS_HLT] = constrain(setpoint[VS_HLT], setpoint[VS_MASH] + MASH_HEAT_LOSS * 100, HLT_MAX_TEMP * 100);
}
#endif
unsigned long calcStrikeVol(byte pgm) {
unsigned int mashRatio = getProgRatio(pgm);
unsigned long retValue;
if (mashRatio) {
retValue = round(getProgGrain(pgm) * mashRatio / 100.0);
//Convert qts to gal for US
#ifndef USEMETRIC
retValue = round(retValue / 4.0);
#endif
retValue += getVolLoss(TS_MASH);
}
else {
//No Sparge Logic (Matio Ratio = 0)
retValue = calcPreboilVol(pgm);
//Add Water Lost in Spent Grain
retValue += calcGrainLoss(pgm);
//Add Loss from other Vessels
retValue += (getVolLoss(TS_HLT) + getVolLoss(TS_MASH));
}
#ifdef DEBUG_PROG_CALC_VOLS
logStart_P(LOGDEBUG);
logField_P(PSTR("StrikeVol:"));
logFieldI( retValue);
logEnd();
#endif
return retValue;
}
unsigned long calcSpargeVol(byte pgm) {
//Determine Preboil Volume Needed (Batch + Evap + Deadspace + Thermo Shrinkage)
unsigned long retValue = calcPreboilVol(pgm);
//Add Water Lost in Spent Grain
retValue += calcGrainLoss(pgm);
//Add Loss from other Vessels
retValue += (getVolLoss(TS_HLT) + getVolLoss(TS_MASH));
//Subtract Strike Water Volume
retValue -= calcStrikeVol(pgm);
#ifdef DEBUG_PROG_CALC_VOLS
logStart_P(LOGDEBUG);
logField_P(PSTR("SpargeVol:"));
logFieldI( retValue);
logEnd();
#endif
return retValue;
}
unsigned long calcPreboilVol(byte pgm) {
// Pre-Boil Volume is the total volume needed in the kettle to ensure you can collect your anticipated batch volume
// It is (((batch volume + kettle loss) / thermo shrinkage factor ) / evap loss factor )
//unsigned long retValue = (getProgBatchVol(pgm) / (1.0 - getEvapRate() / 100.0 * getProgBoil(pgm) / 60.0)) + getVolLoss(TS_KETTLE); // old logic
#ifdef BOIL_OFF_GALLONS
unsigned long retValue = (((getProgBatchVol(pgm) + getVolLoss(TS_KETTLE)) / VOL_SHRINKAGE) + (((unsigned long)getEvapRate() * EvapRateConversion) * getProgBoil(pgm) / 60.0));
#else
unsigned long retValue = (((getProgBatchVol(pgm) + getVolLoss(TS_KETTLE)) / VOL_SHRINKAGE) / (1.0 - getEvapRate() / 100.0 * getProgBoil(pgm) / 60.0));
#endif
#ifdef DEBUG_PROG_CALC_VOLS
logStart_P(LOGDEBUG);
logField_P(PSTR("PreBoilVol:"));
logFieldI( round(retValue));
logEnd();
#endif
return round(retValue);
}
unsigned long calcGrainLoss(byte pgm) {
unsigned long retValue;
retValue = round(getProgGrain(pgm) * GRAIN_VOL_LOSS);
#ifdef DEBUG_PROG_CALC_VOLS
logStart_P(LOGDEBUG);
logField_P(PSTR("GrainLoss"));
logFieldI(retValue);
logEnd();
#endif
return retValue;
}
unsigned long calcGrainVolume(byte pgm) {
return round (getProgGrain(pgm) * GRAIN2VOL);
}
/**
* Calculates the strike temperature for the mash.
*/
byte calcStrikeTemp(byte pgm) {
//Metric temps are stored as quantity of 0.5C increments
float strikeTemp = (float)getFirstStepTemp(pgm) / SETPOINT_DIV;
float grainTemp = (float)getGrainTemp() / SETPOINT_DIV;
//Imperial units must be converted from gallons to quarts
#ifdef USEMETRIC
const uint8_t kMashRatioVolumeFactor = 1;
#else
const uint8_t kMashRatioVolumeFactor = 4;
#endif
//Calculate mash ratio to include logic for no sparge recipes (Using mash ratio of 0 would not work in calcs)
float mashRatio = (float)calcStrikeVol(pgm) * kMashRatioVolumeFactor / getProgGrain(pgm);
#ifdef USEMETRIC
const float kGrainThermoDynamic = 0.41;
#else
const float kGrainThermoDynamic = 0.2;
#endif
//Calculate strike temp using the formula:
// Tw = (TDC/r)(T2 - T1) + T2
// where:
// TDC = Thermodynamic constant (0.2 for Imperial Units and 0.41 for Metric)
// r = The ratio of water to grain in quarts per pound or l per kg
// T1 = The initial temperature of the mash
// T2 = The target temperature of the mash
// Tw = The actual temperature of the infusion water
strikeTemp = (kGrainThermoDynamic / mashRatio) * (strikeTemp - grainTemp) + strikeTemp;
//Add Config.h value for adjustments if any
strikeTemp += STRIKE_TEMP_OFFSET;
//Return value in EEPROM format which is 0-255F or 0-255 x 0.5C
return strikeTemp * SETPOINT_DIV;
}
byte getFirstStepTemp(byte pgm) {
byte firstStep = 0;
byte i = MASH_DOUGHIN;
while (firstStep == 0 && i <= MASH_MASHOUT) firstStep = getProgMashTemp(pgm, i++);
return firstStep;
}