forked from reprappro/RepRapFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heat.cpp
294 lines (249 loc) · 7.5 KB
/
Heat.cpp
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
/****************************************************************************************************
RepRapFirmware - Heat
This is all the code to deal with heat and temperature.
-----------------------------------------------------------------------------------------------------
Version 0.1
18 November 2012
Adrian Bowyer
RepRap Professional Ltd
http://reprappro.com
Licence: GPL
****************************************************************************************************/
#include "RepRapFirmware.h"
const float invHeatPwmAverageCount = HEAT_SAMPLE_TIME/HEAT_PWM_AVERAGE_TIME;
Heat::Heat(Platform* p, GCodes* g)
{
platform = p;
gCodes = g;
for(size_t heater=0; heater < HEATERS; heater++)
{
pids[heater] = new PID(platform, heater);
}
active = false;
}
void Heat::Init()
{
for(size_t heater=0; heater < HEATERS; heater++)
{
pids[heater]->Init();
}
lastTime = platform->Time();
longWait = lastTime;
active = true;
}
void Heat::Exit()
{
for(size_t heater=0; heater < HEATERS; heater++)
{
pids[heater]->SwitchOff();
}
platform->Message(HOST_MESSAGE, "Heat class exited.\n");
active = false;
}
void Heat::Spin()
{
if (!active)
return;
float t = platform->Time();
if (t - lastTime < platform->HeatSampleTime())
return;
lastTime = t;
for(size_t heater=0; heater < HEATERS; heater++)
{
pids[heater]->Spin();
}
platform->ClassReport(longWait);
}
void Heat::Diagnostics()
{
platform->AppendMessage(BOTH_MESSAGE, "Heat Diagnostics:\n");
for(size_t heater=0; heater < HEATERS; heater++)
{
if (pids[heater]->active)
{
platform->AppendMessage(BOTH_MESSAGE, "Heater %d: I-accumulator = %.1f\n", heater, pids[heater]->temp_iState);
}
}
}
bool Heat::AllHeatersAtSetTemperatures(bool includingBed) const
{
#if HOT_BED != -1
for(size_t heater = (includingBed) ? HOT_BED : E0_HEATER; heater < HEATERS; heater++)
#else
for(size_t heater = E0_HEATER; heater < HEATERS; heater++)
#endif
{
if(!HeaterAtSetTemperature(heater))
{
return false;
}
}
return true;
}
//query an individual heater
bool Heat::HeaterAtSetTemperature(int8_t heater) const
{
// If it hasn't anything to do, it must be right wherever it is...
if (heater < 0 || pids[heater]->SwitchedOff() || pids[heater]->FaultOccurred())
return true;
float dt = GetTemperature(heater);
float target = (pids[heater]->Active()) ? GetActiveTemperature(heater) : GetStandbyTemperature(heater);
return (target < TEMPERATURE_LOW_SO_DONT_CARE) || (fabs(dt - target) <= TEMPERATURE_CLOSE_ENOUGH);
}
//******************************************************************************************************
PID::PID(Platform* p, int8_t h)
{
platform = p;
heater = h;
}
void PID::Init()
{
platform->SetHeater(heater, 0.0);
temperature = platform->GetTemperature(heater);
activeTemperature = ABS_ZERO;
standbyTemperature = ABS_ZERO;
lastTemperature = temperature;
temp_iState = 0.0;
badTemperatureCount = 0;
temperatureFault = false;
active = false; // Default to standby temperature
switchedOff = true;
heatingUp = false;
averagePWM = 0.0;
}
void PID::SwitchOn()
{
// if(reprap.Debug())
// {
// snprintf(scratchString, STRING_LENGTH, "Heater %d switched on.\n", heater);
// platform->Message(BOTH_MESSAGE, scratchString);
// }
switchedOff = temperatureFault;
}
void PID::Spin()
{
// Always know our temperature, regardless of whether we have been switched on or not
temperature = platform->GetTemperature(heater);
// If we're not switched on, or there's a fault, turn the power off and go home.
// If we're not switched on, then nothing is using us. This probably means that
// we don't even have a thermistor connected. So don't even check for faults if we
// are not switched on. This is safe, as the next bit of code always turns our
// heater off in that case anyway.
if (temperatureFault || switchedOff)
{
platform->SetHeater(heater, 0.0); // Make sure...
averagePWM *= (1.0 - invHeatPwmAverageCount);
return;
}
// We are switched on. Check for faults. Temperature silly-low or silly-high mean open-circuit
// or shorted thermistor respectively.
if (temperature < BAD_LOW_TEMPERATURE || temperature > BAD_HIGH_TEMPERATURE)
{
badTemperatureCount++;
if (badTemperatureCount > MAX_BAD_TEMPERATURE_COUNT)
{
platform->SetHeater(heater, 0.0);
temperatureFault = true;
// switchedOff = true;
platform->Message(BOTH_MESSAGE, "Temperature fault on heater %d, T = %.1f\n", heater, temperature);
reprap.FlagTemperatureFault(heater);
}
}
else
{
badTemperatureCount = 0;
}
// Now check how long it takes to warm up. If too long, maybe the thermistor is not in contact with the heater
if (heatingUp && heater != HOT_BED) // FIXME - also check bed warmup time?
{
float tmp = (active) ? activeTemperature : standbyTemperature;
if (temperature < tmp - TEMPERATURE_CLOSE_ENOUGH)
{
float tim = platform->Time() - timeSetHeating;
float limit = platform->TimeToHot();
if (tim > platform->TimeToHot() && limit > 0.0)
{
platform->SetHeater(heater, 0.0);
temperatureFault = true;
// switchedOff = true;
platform->Message(BOTH_MESSAGE, "Heating fault on heater %d, T = %.1f C; still not at temperature %.1f after %f seconds.\n",heater, temperature, tmp, tim);
reprap.FlagTemperatureFault(heater);
}
}
else
{
heatingUp = false;
}
}
float targetTemperature = (active) ? activeTemperature : standbyTemperature;
float error = targetTemperature - temperature;
const PidParameters& pp = platform->GetPidParameters(heater);
if (!pp.UsePID())
{
platform->SetHeater(heater, (error > 0.0) ? pp.kS : 0.0);
if(error > 0.0)
{
platform->SetHeater(heater, pp.kS);
averagePWM = averagePWM * (1.0 - invHeatPwmAverageCount) + pp.kS;
}
else
{
platform->SetHeater(heater, 0.0);
averagePWM *= (1.0 - invHeatPwmAverageCount);
}
return;
}
if (error < -pp.fullBand)
{
// actual temperature is well above target
temp_iState = (targetTemperature + pp.fullBand - 25.0) * pp.kT; // set the I term to our estimate of what will be needed ready for the switch to PID
platform->SetHeater(heater, 0.0);
averagePWM *= (1.0 - invHeatPwmAverageCount);
lastTemperature = temperature;
return;
}
if (error > pp.fullBand)
{
// actual temperature is well below target
temp_iState = (targetTemperature - pp.fullBand - 25.0) * pp.kT; // set the I term to our estimate of what will be needed ready for the switch to PID
platform->SetHeater(heater, pp.kS);
averagePWM *= (1.0 - invHeatPwmAverageCount) + pp.kS;
lastTemperature = temperature;
return;
}
float sampleInterval = platform->HeatSampleTime();
temp_iState += error * pp.kI * sampleInterval;
if (temp_iState < pp.pidMin)
{
temp_iState = pp.pidMin;
}
else if (temp_iState > pp.pidMax)
{
temp_iState = pp.pidMax;
}
float temp_dState = pp.kD * (temperature - lastTemperature) / sampleInterval;
float result = pp.kP * error + temp_iState - temp_dState;
lastTemperature = temperature;
// Legacy - old RepRap PID parameters were set to give values in [0, 255] for 1 byte PWM control
// TODO - maybe change them to give [0.0, 1.0]?
if (result < 0.0)
{
result = 0.0;
}
else if (result > 255.0)
{
result = 255.0;
}
result = result / 255.0;
if (!temperatureFault)
{
platform->SetHeater(heater, result * pp.kS);
}
averagePWM = averagePWM * (1.0 - invHeatPwmAverageCount) + result;
// debugPrintf("Heater %d: e=%f, P=%f, I=%f, d=%f, r=%f\n", heater, error, pp.kP*error, temp_iState, temp_dState, result);
}
float PID::GetAveragePWM() const
{
return averagePWM * invHeatPwmAverageCount;
}
// End