-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqController.cpp
More file actions
360 lines (277 loc) · 10.4 KB
/
Copy pathqController.cpp
File metadata and controls
360 lines (277 loc) · 10.4 KB
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
/*
QControl PID controller for BBQ
Copyright (c) 2016 David Paiva (david@nailbuster.com). All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "qController.h"
#include "qGlobals.h"
template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
const byte* p = (const byte*)(const void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++)
EEPROM.write(ee++, *p++);
return i;
}
template <class T> int EEPROM_readAnything(int ee, T& value)
{
byte* p = (byte*)(void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++)
*p++ = EEPROM.read(ee++);
return i;
}
void ReadEEPROMString(int &curPos,char eData[]) {
//String tmp;
char ch;
int dp=0;
eData[0] = '\0';
ch = EEPROM.read(curPos);
curPos++;
//Serial.print(ch);
while (ch != '\0')
{
eData[dp] = ch;
ch = EEPROM.read(curPos);
curPos++;
dp++;
//Serial.print(char(ch));
}
eData[dp] = '\0'; //null terminate
}
void WriteEEPROMString(int &curPos, char InData[]) {
for (int i = 0; i < strlen(InData); i++)
{
EEPROM.write(curPos, InData[i]);
curPos++;
}
EEPROM.write(curPos, '\0'); //null it
curPos++;
}
QControllerClass::QControllerClass()
{
}
void QControllerClass::ReadConfig()
{
int curPos = 0;
char chke;
char jData[300];
curPos += EEPROM_readAnything(0, chke); //good eprom with Q? //write just quick when setpoint is changed....don't need to rewrite entire flash...
//debug to reinit flash
// chke='Z';
if (chke != 'A') {
noConfig = true;
}
else //good eeprom
{
curPos += EEPROM_readAnything(curPos, Status); //good eprom with Q?
curPos += EEPROM_readAnything(curPos, setpoint); //good eprom with Q?
curPos += EEPROM_readAnything(curPos, timestart); //good eprom with Q?
curPos += EEPROM_readAnything(curPos, totalseconds); //good eprom with Q?
ReadEEPROMString(curPos, jData);SetValuesJson(String (jData));
ReadEEPROMString(curPos, jData);curPID.SetValuesJson(String (jData));
ReadEEPROMString(curPos, jData);curFan.SetValuesJson(String (jData));
for (int fx = 0; fx < MaxProbes; fx++)
{
ReadEEPROMString(curPos, jData);
curProbes[fx].SetValuesJson(String (jData));
} //read to real curStatus
noConfig = false;
}
}
void QControllerClass::writeConfig(bool AllData){
int curPos = 0;
char chke = 'A';
char tmpChars[300];
curPos += EEPROM_writeAnything(0, chke); //good eprom with Q? //write just quick when setpoint is changed....don't need to rewrite entire flash...
curPos += EEPROM_writeAnything(curPos, Status); //good eprom with Q?
curPos += EEPROM_writeAnything(curPos, setpoint); //good eprom with Q?
curPos += EEPROM_writeAnything(curPos, timestart); //good eprom with Q?
curPos += EEPROM_writeAnything(curPos, totalseconds); //good eprom with Q?
if (AllData)
{
AsJson(tmpChars); WriteEEPROMString(curPos, tmpChars); //Qcontroller object set write jsons as null term
curPID.AsJson(tmpChars); WriteEEPROMString(curPos, tmpChars); //PID
curFan.AsJson(tmpChars); WriteEEPROMString(curPos, tmpChars); //FAN
for (int fx = 0; fx<MaxProbes; fx++)
{
curProbes[fx].AsJson(tmpChars);
WriteEEPROMString(curPos, tmpChars); //PROBES
} //read to real curStatus
}
}
void QControllerClass::Begin()
{
//EEPROM.write(0, 0); reset eeprom
ReadConfig(); //load and setup variables
if (noConfig) Serial.println(F("NEW"));
curFan.begin();
curPID.begin();
curLink.begin(); //startup link
//debug must remove manually set the pins for now....
//curProbes[0].gpioPin = 2; curProbes[1].gpioPin = 3; curProbes[2].gpioPin = 4; curProbes[3].gpioPin = 5; //my homemade thingy
// curProbes[0].gpioPin = 13; curProbes[1].gpioPin = 14; curProbes[2].gpioPin = 15; curProbes[3].gpioPin = 5; // MEGA testing ramps board
// curProbes[0].resistor = 4700; curProbes[1].resistor = 4700; curProbes[2].resistor = 4700;
//max testing
// curProbes[0].gpioPin = 8; curProbes[1].gpioPin = 9; curProbes[2].gpioPin = 1; curProbes[3].gpioPin = 2; // MEGA testing ramps board
// curProbes[0].ProbeType = 1; curProbes[1].ProbeType = 1;
// curFan.minPWM = 150;
// curFan.fanStartDuty = 200; //use servo upto 200
// curFan.ServoLow = 85;
// curFan.ServoHi = 180;
//lidOpenTime = 60;
for (int fx = 0; fx < MaxProbes; fx++) //go through each probe and begin
{
curProbes[fx].begin();
}
}
void QControllerClass::Run()
{
if ((millis() - lastTimerChk >= msReadInterval) && (curLink.inProgMode==false)) //process every interval (1 second for now)
{
lastTimerChk = millis();
//char line[60];
int PIDresult;
for (int fx = 0; fx < MaxProbes; fx++) //go through each probe and grab current temps in F
{
curProbes[fx].ReadTemperature();
if (LidOpened == false && targetReached) curProbes[fx].CheckAlarm();
} //read all probes temperatures
pit_temp = curProbes[pitProbeIndex].curTemp; //pitProbeIndex is pit probe;
if (setpoint <= 0) setpoint = 1; //safety and division errors.
/*qa testing
setpoint = 140;
qatemp = (qatemp *1.0) + (((curFan.curDuty) / 255.0) * 4.0);
if (curFan.curDuty > 0) qatemp += 1; else qatemp -= 0.30;
pit_temp = int (qatemp);
curProbes[pitProbeIndex].runningAvg = pit_temp; //so no lid detect....
*/
if (LidOpened == false)
{
PIDresult = curPID.DoControlAlgorithm(setpoint, pit_temp); //call PID
curFan.SetFanSpeed(PIDresult); //set fanspeed from PIDresult; 0-255....
}
else {
curFan.SetFanSpeed(0); //no fan during lidopenmode
//Serial.println("in lidopen");
}
if (abs(setpoint - pit_temp) <= 10) { //target is reached within XX degrees of setpoint?
targetReached = true;
preHeat = false;
}
// else targetReached = false; //for alarms and such.....
// }
//else if (pit_temp<setpoint) curFan.SetFanSpeed(preHeatMaxSpeed/100 * 255); //when preHeat then use the preheat max speed...
//saftey?
if (pit_temp >(setpoint + 25)) curFan.SetFanSpeed(0); //don't think...but who knows?
//lidopencheck timer
if (LidOpened) {
LidOpenCountdown -= 1;
if (LidOpenCountdown <= 0) { LidOpened = false; }
}
//lidopen detect
int lod = (curProbes[pitProbeIndex].runningAvg - pit_temp) * 100 / curProbes[pitProbeIndex].runningAvg;
if (lod >= lidOpenOffset && LidOpened==false && targetReached)
{
LidOpened = true;
LidOpenCountdown = lidOpenTime; //seconds to countdown
}
} //main interval loop
QGUI.run(); //run GUI object...does nothing yet...
curLink.run(); //link object run
}
void QControllerClass::StartCook()
{
Status = 1;
writeConfig(false); //write status;
preHeat = true;
}
void QControllerClass::StopCook()
{
Status = 2;
writeConfig(false); //write status;
preHeat = false;
}
void QControllerClass::AsJson(char *jData)
{
fBuf(jData, 300, F("{lo:%i,lt:%i,ph:%i,pr:%i,aX:%i,aN:%i,ri:%i,pt:%i}"),
lidOpenOffset, lidOpenTime, preHeatMaxSpeed, preHeatTemp, MaxSetPoint, MinTempFan, msReadInterval, pit_temp);
}
void QControllerClass::SetValuesJson(const String& msgStr)
{
String curPart, curField, curVal;
for (int fy = 0; fy <= 50; fy++)
{
curPart = String(getValue(msgStr, fy, ',')); //grab first node separated by ,
if (curPart == "") { break; } //exit if nothing found
curField = getValue(curPart, 0, ':');
curVal = getValue(curPart, 1, ':');
if (curField == "lo") { lidOpenOffset = curVal.toInt(); }
else if (curField == "lt") { lidOpenTime = curVal.toInt(); }
else if (curField == "ph") { preHeatMaxSpeed = curVal.toInt(); }
else if (curField == "pr") { preHeatTemp = curVal.toInt(); }
else if (curField == "aX") { MaxSetPoint = curVal.toInt(); }
else if (curField == "aN") { MinTempFan = curVal.toInt(); }
// else if (curField == "ri") { msReadInterval = curVal.toInt(); }
else if (curField == "pt") { pit_temp = curVal.toInt(); }
}
}
void QControllerClass::CheckAlarms()
{/* changed to method on probe
int alarmBuf = 10; //degrees to reset alarm hi/lo (so we don't repeat alarms when very close swings)
//Check High Alarms;
if (LidOpened) exit; //don't check during lidOpen detect.
if (targetReached == false) {
curProbes[pitProbeIndex].AlarmActiveMax = true; //if pitprob hasn't reached target then don't fire alarms for it (set that alarms are already active)
curProbes[pitProbeIndex].AlarmActiveMin = true;
}
for (int fx = 0; fx < MaxProbes; fx++) //go through each probe and check for alarm hi
{
if (curProbes[fx].curTemp > curProbes[fx].AlarmMaxTemp && curProbes[fx].AlarmMaxTemp > 0 && curProbes[fx].isConnected)
{
if (curProbes[fx].AlarmActiveMax == false) //new alarm
{
//fire alarm high!
curLink.SendAlarm(fx + 1, "High", curProbes[fx].AlarmMaxTemp, curProbes[fx].curTemp);
curProbes[fx].AlarmActiveMax = true;
}
}
else if (curProbes[fx].curTemp < (curProbes[fx].AlarmMaxTemp-alarmBuf)) { curProbes[fx].AlarmActiveMax = false; } //reset alarm if target reached
}
//Check Low Alarms;
for (int fx = 0; fx < MaxProbes; fx++) //go through each probe and check for alarm lo
{
if (curProbes[fx].curTemp < curProbes[fx].AlarmMinTemp && curProbes[fx].AlarmMinTemp > 0 && curProbes[fx].isConnected)
{
if (curProbes[fx].AlarmActiveMin == false) //new alarm
{
//fire alarm low!
curLink.SendAlarm(fx + 1, "Low", curProbes[fx].AlarmMinTemp, curProbes[fx].curTemp);
curProbes[fx].AlarmActiveMin = true;
}
}
else if (curProbes[fx].curTemp > (curProbes[fx].AlarmMinTemp + alarmBuf)) { curProbes[fx].AlarmActiveMin = false; } //reset alarm if target reached
}
*/
}
void QControllerClass::LinkSetTemp(int NewTemp) //when setpoint is changed
{
//if (NewTemp>=0 && NewTemp<curProbes[pitProbeIndex].maxTemp)
if (NewTemp >= 0 && NewTemp<MaxSetPoint)
{
setpoint = NewTemp; //set temperature from bluetooth
targetReached = false; //reset target for alarms;
curPID.reset();
writeConfig(false); //write status eeprom;
}
}