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
|
/****************************************************************************************************
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"
#include "Pid.h"
Heat::Heat(Platform* p)
: platform(p), active(false), coldExtrude(false), bedHeater(BED_HEATER), chamberHeater(-1), heaterBeingTuned(-1), lastHeaterTuned(-1)
{
for (size_t heater = 0; heater < HEATERS; heater++)
{
pids[heater] = new PID(platform, heater);
}
}
void Heat::Init()
{
for (int heater = 0; heater < HEATERS; heater++)
{
if (heater == bedHeater || heater == chamberHeater)
{
pids[heater]->Init(DefaultBedHeaterGain, DefaultBedHeaterTimeConstant, DefaultBedHeaterDeadTime, false);
}
#ifndef DUET_NG
else if (heater == HEATERS - 1)
{
// Heater 6 pin is shared with fan 1. By default we support fan 1, so disable heater 6.
pids[heater]->Init(-1.0, -1.0, -1.0, true);
}
#endif
else
{
pids[heater]->Init(DefaultHotEndHeaterGain, DefaultHotEndHeaterTimeConstant, DefaultHotEndHeaterDeadTime, true);
}
}
lastTime = millis() - platform->HeatSampleInterval(); // flag the PIDS as due for spinning
longWait = platform->Time();
coldExtrude = false;
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)
{
// See if it is time to spin the PIDs
const uint32_t now = millis();
if (now - lastTime >= platform->HeatSampleInterval())
{
lastTime = now;
for (size_t heater=0; heater < HEATERS; heater++)
{
pids[heater]->Spin();
}
// See if we have finished tuning a PID
if (heaterBeingTuned != -1 && !pids[heaterBeingTuned]->IsTuning())
{
lastHeaterTuned = heaterBeingTuned;
heaterBeingTuned = -1;
}
}
}
platform->ClassReport(longWait);
}
void Heat::Diagnostics(MessageType mtype)
{
platform->MessageF(mtype, "=== Heat ===\nBed heater = %d, chamber heater = %d\n", bedHeater, chamberHeater);
for (size_t heater=0; heater < HEATERS; heater++)
{
if (pids[heater]->Active())
{
platform->MessageF(mtype, "Heater %d is on, I-accum = %.1f\n", heater, pids[heater]->GetAccumulator());
}
}
}
bool Heat::AllHeatersAtSetTemperatures(bool includingBed) const
{
size_t firstHeater = (bedHeater == -1) ? E0_HEATER :
(includingBed) ? min<int8_t>(bedHeater, E0_HEATER) : E0_HEATER;
for(size_t heater = firstHeater; heater < HEATERS; heater++)
{
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 || heater >= HEATERS || pids[heater]->SwitchedOff() || pids[heater]->FaultOccurred())
{
return true;
}
const float dt = GetTemperature(heater);
const float target = (pids[heater]->Active()) ? GetActiveTemperature(heater) : GetStandbyTemperature(heater);
return (target < TEMPERATURE_LOW_SO_DONT_CARE) || (fabsf(dt - target) <= TEMPERATURE_CLOSE_ENOUGH);
}
Heat::HeaterStatus Heat::GetStatus(int8_t heater) const
{
if (heater < 0 || heater >= HEATERS)
{
return HS_off;
}
return (pids[heater]->FaultOccurred()) ? HS_fault
: (pids[heater]->SwitchedOff()) ? HS_off
: (pids[heater]->IsTuning()) ? HS_tuning
: (pids[heater]->Active()) ? HS_active
: HS_standby;
}
void Heat::SetActiveTemperature(int8_t heater, float t)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->SetActiveTemperature(t);
}
}
float Heat::GetActiveTemperature(int8_t heater) const
{
return (heater >= 0 && heater < HEATERS) ? pids[heater]->GetActiveTemperature() : ABS_ZERO;
}
void Heat::SetStandbyTemperature(int8_t heater, float t)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->SetStandbyTemperature(t);
}
}
float Heat::GetStandbyTemperature(int8_t heater) const
{
return (heater >= 0 && heater < HEATERS) ? pids[heater]->GetStandbyTemperature() : ABS_ZERO;
}
float Heat::GetTemperature(int8_t heater) const
{
return (heater >= 0 && heater < HEATERS) ? pids[heater]->GetTemperature() : ABS_ZERO;
}
void Heat::Activate(int8_t heater)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->Activate();
}
}
void Heat::SwitchOff(int8_t heater)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->SwitchOff();
}
}
void Heat::SwitchOffAll()
{
for (size_t heater = 0; heater < HEATERS; ++heater)
{
pids[heater]->SwitchOff();
}
}
void Heat::Standby(int8_t heater)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->Standby();
}
}
void Heat::ResetFault(int8_t heater)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->ResetFault();
}
}
float Heat::GetAveragePWM(size_t heater) const
{
return pids[heater]->GetAveragePWM();
}
uint32_t Heat::GetLastSampleTime(size_t heater) const
{
return pids[heater]->GetLastSampleTime();
}
bool Heat::UseSlowPwm(int8_t heater) const
{
return heater == bedHeater || heater == chamberHeater;
}
// Auto tune a PID
void Heat::StartAutoTune(size_t heater, float temperature, float maxPwm, StringRef& reply)
{
if (heaterBeingTuned == -1)
{
heaterBeingTuned = (int8_t)heater;
pids[heater]->StartAutoTune(temperature, maxPwm, reply);
}
else
{
// Trying to start a new auto tune, but we are already tuning a heater
reply.printf("Error: cannot start auto tuning heater %u because heater %d is being tuned", heater, heaterBeingTuned);
}
}
bool Heat::IsTuning(size_t heater) const
{
return pids[heater]->IsTuning();
}
void Heat::GetAutoTuneStatus(StringRef& reply) const
{
int8_t whichPid = (heaterBeingTuned == -1) ? lastHeaterTuned : heaterBeingTuned;
if (whichPid != -1)
{
pids[whichPid]->GetAutoTuneStatus(reply);
}
else
{
reply.copy("No heater has been tuned yet");
}
}
// End
|