-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasterpiec.ino
197 lines (183 loc) · 6.07 KB
/
masterpiec.ino
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
#include "masterpiec.h"
#include <assert.h>
#include <MD_DS1307.h>
#include "ui_handler.h"
#include "burn_control.h"
#include "piec_sensors.h"
#include "boiler_control.h"
void updateDallasSensorAssignmentFromConfig();
/**
* struktura programu
* - sterowanie silnikiem dmuchawy - na przerwaniu 0
* - pętla sterowania grzaniem (tj załączanie dmuchawy i podajnika w odpowiednim reżimie) - na przerwaniu zegarowym, aktualizacja statusu co 1 sek. Pętla sterowania grzaniem obsługuje też wyłączanie i załączanie pomp.
* - obsługa UI. Aktualizacja ekranu w głównym loop-ie, natomiast obsługa inputu - z przerwań. Nie powinno być wyswietlania czegokolwiek w przerwaniu.
* - zewnętrzny termostat - podpięty do jednego z pinów. Odczytywany tryb (grzej / nie grzej) i obsługiwany odpowiednio w pętli sterowania grzaniem
* - odczyt czujników temperatury - też powinien następować w pętli, nie wiem czy tak często jak pętla sterowania grzaniem
*
*
* pompy - działają niezależnie. Jeśli grzanie=ON to pompa CO załącza się gdy temp. jest powyżej minimalnej i wyłącza się gdy jest poniżej. Jeśli grzanie = off to pompa CO załącza się wyłącznie po to żeby pozbyć się nadmiaru ciepła, czyli gdy temp aktualna CO jest wyższa
* niż TZadana + delta. Pozbywanie się nadmiaru ciepła trwa do momentu gdy temp spadnie albo gdy przestanie rosnąć albo przez ustalony cykl. Możemy też pompę co skonfigurować do załączania się cyklicznie na x minut co y minut.
* Pompa CWU - działa gdy temp pieca jest wyższa niż temp bojlera+delta CWU (czyli to jest nasza minimalna temp załączenia pompy). Załącza się gdy temp bojlera spadnie poniżej tCWUZadana - hist1. W momencie załączenia przestawiamy TZadana pieca na tZadanaCWU+delta CWU
* pompa CWU wyłącza się gdy temp bojlera osiągnie docelową (wtedy tez przywracamy temp zadana pieca) albo gdy temp spadnie poniżej tBojler+delta cwu.
*
*/
unsigned long _lastDebugTime = 0;
uint8_t _debugTime = 0;
void setup() {
//initialize interrupts etc
//initialize hardware
delay(500);
Serial.begin(115200);
if (!RTC.isRunning()) {
//Serial.println("RTC not runnning, starting the clock");
RTC.control(DS1307_CLOCK_HALT, DS1307_OFF);
}
RTC.readTime();
// put your setup code here, to run once:
readInitialConfig();
Serial.print("CFG size:");
Serial.println(sizeof(TControlConfiguration));
if (sizeof(TControlConfiguration) > CFG_SLOT_SIZE) {
exit(0);
}
initializeEncoder();
initializeDisplay();
g_CurrentUIView = 0;
updateView();
initializeDallasSensors();
initializeMax6675Sensors();
initializeBlowerControl();
initializeFlowMeter();
loggingInit();
Serial.print("Hw inited. Time:");
Serial.print(RTC.yyyy);
Serial.print('-');
Serial.print(RTC.mm);
Serial.print('-');
Serial.print(RTC.dd);
Serial.print('T');
Serial.print(RTC.h);
Serial.print(':');
Serial.println(RTC.m);
updateDallasSensorAssignmentFromConfig();
for(int i=0;i<6;i++) {
delay(500);
refreshSensorReadings();
}
initializeBurningLoop();
changeUIState('0');
_lastDebugTime = millis();
}
void loop() {
uint64_t m = millis();
_debugTime = 0;
if (m - _lastDebugTime >= 30000) {
_debugTime = 1;
_lastDebugTime = m;
}
void (*pf)(void*) = g_uiBottomHalf;
if (pf != NULL)
{
pf(g_uiBottomHalfCtx);
if (pf == g_uiBottomHalf)
{
g_uiBottomHalf = NULL;
g_uiBottomHalfCtx = NULL;
}
}
RTC.readTime();
refreshSensorReadings();
burnControlTask();
circulationControlTask();
gatherStatsTask();
updateView();
periodicDumpControlState();
#ifdef MPIEC_ENABLE_SCRIPT
handleSerialShellTask();
#endif
loggingTask();
commandHandlingTask();
int m2 = millis();
int d = 150 - (m2 - m);
if (d > 0)
{
delay(d);
}
else
{
Serial.print(F("zabraklo mi ms "));
Serial.println(-d);
}
}
extern uint32_t counter;
bool isDebugTime() {
return _debugTime != 0;
}
void periodicDumpControlState() {
static TSTATE pstate = STATE_UNDEFINED;
unsigned long t = millis();
if (isDebugTime() || g_BurnState != pstate)
{
pstate = g_BurnState;
Serial.print("T+");
Serial.print(t / 1000);
Serial.print(F(" s:"));
Serial.print(BURN_STATES[g_BurnState].Code);
Serial.print(F(", dm:"));
Serial.print(getCurrentBlowerPower());
Serial.print(F(" ("));
Serial.print(getCurrentBlowerCycle());
Serial.print(F("), pd:"));
Serial.print(isFeederOn() ? "ON": "OFF");
Serial.print(F(", co:"));
Serial.print(isPumpOn(PUMP_CO1));
Serial.print(F(", cwu:"));
Serial.print(isPumpOn(PUMP_CWU1));
Serial.print(F(", stime:"));
Serial.print((t - g_CurStateStart) / 1000);
Serial.print(F(", btime:"));
Serial.print((t - g_CurBurnCycleStart) / 1000);
Serial.print(F(", tspalin:"));
Serial.print(g_TempSpaliny);
Serial.print(F(", tpiec:"));
Serial.print(g_TempCO);
Serial.print(F(", need:"));
Serial.print(g_needHeat);
Serial.print(F(", dT60:"));
Serial.print(g_dT60);
Serial.print(F(", dTl3:"));
Serial.print(g_dTl3);
Serial.print(F(", dTEx:"));
Serial.print(g_dTExh);
Serial.print(F(", floV:"));
Serial.print(g_AirFlow);
Serial.print(F(":"));
Serial.print(g_AirFlowNormal);
Serial.print(F(", lastT:"));
Serial.print(g_lastCOReads.IsEmpty() ? 0.0f : *g_lastCOReads.GetAt(-1));
Serial.print(F(", CNT:"));
Serial.print(counter);
Serial.println();
}
}
void updateDallasSensorAssignmentFromConfig() {
uint8_t zbuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
char buf[20];
for(uint8_t i=0; i<8; i++) {
uint8_t*p = g_DeviceConfig.DallasAddress[i];
if (memcmp(p, zbuf, 8) == 0) continue;
if (!ensureDallasSensorAtIndex(i, p)) {
Serial.print(F("Invalid dallas address "));
sprintf(buf, "%d: %02X%02X%02X%02X%02X%02X%02X%02X", i, p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8]);
Serial.println(buf);
}
}
for(uint8_t i=0; i<8; i++)
{
printDallasInfo(i, buf);
Serial.print(F("czujnik "));
Serial.print(i);
Serial.print(" ");
Serial.println(buf);
}
}