-
Notifications
You must be signed in to change notification settings - Fork 120
/
Heat.h
310 lines (252 loc) · 8.13 KB
/
Heat.h
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
/****************************************************************************************************
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
****************************************************************************************************/
#ifndef HEAT_H
#define HEAT_H
/**
* This class implements a PID controller for the heaters
*/
class PID
{
friend class Heat;
protected:
PID(Platform* p, int8_t h);
void Init(); // (Re)Set everything to start
void Spin(); // Called in a tight loop to keep things running
void SetActiveTemperature(float t);
float GetActiveTemperature() const;
void SetStandbyTemperature(float t);
float GetStandbyTemperature() const;
void Activate(); // Switch from idle to active
void Standby(); // Switch from active to idle
bool Active() const; // Are we active?
void SwitchOff(); // Not even standby - all heater power off
bool SwitchedOff() const; // Are we switched off?
bool FaultOccurred() const; // Has a heater fault occurred?
void ResetFault(); // Reset a fault condition - only call this if you know what you are doing
float GetTemperature() const; // Get the current temperature
float GetAveragePWM() const; // Return the running average PWM to the heater. Answer is a fraction in [0, 1].
private:
void SwitchOn();
Platform* platform; // The instance of the class that is the RepRap hardware
float activeTemperature; // The required active temperature
float standbyTemperature; // The required standby temperature
float temperature; // The current temperature
float lastTemperature; // The previous current temperature
float temp_iState; // The integral PID component
bool active; // Are we active or standby?
bool switchedOff; // Becomes false when someone tells us our active or standby temperatures
int8_t heater; // The index of our heater
int8_t badTemperatureCount; // Count of sequential dud readings
bool temperatureFault; // Has our heater developed a fault?
float timeSetHeating; // When we were switched on
bool heatingUp; // Are we heating up?
float averagePWM; // The running average of the PWM.
};
/**
* The master class that controls all the heaters in the RepRap machine
*/
class Heat
{
public:
// Enumeration to describe the status of a heater. Note that the web interface returns the numerical values, so don't change them.
enum HeaterStatus { HS_off = 0, HS_standby = 1, HS_active = 2, HS_fault = 3 };
Heat(Platform* p, GCodes* g);
void Spin(); // Called in a tight loop to keep everything going
void Init(); // Set everything up
void Exit(); // Shut everything down
void SetActiveTemperature(int8_t heater, float t);
float GetActiveTemperature(int8_t heater) const;
void SetStandbyTemperature(int8_t heater, float t);
float GetStandbyTemperature(int8_t heater) const;
void Activate(int8_t heater); // Turn on a heater
void Standby(int8_t heater); // Set a heater idle
float GetTemperature(int8_t heater) const; // Get the temperature of a heater
HeaterStatus GetStatus(int8_t heater) const; // Get the off/standby/active status
void SwitchOff(int8_t heater); // Turn off a specific heater
void SwitchOffAll(); // Turn all heaters off
void ResetFault(int8_t heater); // Reset a heater fault - only call this if you know what you are doing
bool AllHeatersAtSetTemperatures(bool includingBed) const; // Is everything at temperature within tolerance?
bool HeaterAtSetTemperature(int8_t heater) const; // Is a specific heater at temperature within tolerance?
void Diagnostics(); // Output useful information
float GetAveragePWM(int8_t heater) const; // Return the running average PWM to the heater. Answer is a fraction in [0, 1].
private:
Platform* platform; // The instance of the RepRap hardware class
GCodes* gCodes; // The instance of the G Code interpreter class
bool active; // Are we active?
PID* pids[HEATERS]; // A PID controller for each heater
float lastTime; // The last time our Spin() was called
float longWait; // Long time for things that happen occasionally
};
//***********************************************************************************************************
inline bool PID::Active() const
{
return active;
}
inline void PID::SetActiveTemperature(float t)
{
if (t > BAD_HIGH_TEMPERATURE)
{
platform->Message(BOTH_ERROR_MESSAGE, "Temperature %.1f too high for heater %d!\n", t, heater);
}
SwitchOn();
activeTemperature = t;
}
inline float PID::GetActiveTemperature() const
{
return activeTemperature;
}
inline void PID::SetStandbyTemperature(float t)
{
if (t > BAD_HIGH_TEMPERATURE)
{
platform->Message(BOTH_ERROR_MESSAGE, "Temperature %.1f too high for heater %d!\n", t, heater);
}
SwitchOn();
standbyTemperature = t;
}
inline float PID::GetStandbyTemperature() const
{
return standbyTemperature;
}
inline float PID::GetTemperature() const
{
return temperature;
}
inline void PID::Activate()
{
if (temperatureFault)
{
return;
}
SwitchOn();
active = true;
if (!heatingUp)
{
timeSetHeating = platform->Time();
}
heatingUp = activeTemperature > temperature;
}
inline void PID::Standby()
{
if (temperatureFault)
{
return;
}
SwitchOn();
active = false;
if (!heatingUp)
{
timeSetHeating = platform->Time();
}
heatingUp = standbyTemperature > temperature;
}
inline bool PID::FaultOccurred() const
{
return temperatureFault;
}
inline void PID::ResetFault()
{
temperatureFault = false;
timeSetHeating = platform->Time(); // otherwise we will get another timeout immediately
badTemperatureCount = 0;
}
inline void PID::SwitchOff()
{
platform->SetHeater(heater, 0.0);
active = false;
switchedOff = true;
heatingUp = false;
}
inline bool PID::SwitchedOff() const
{
return switchedOff;
}
inline float Heat::GetAveragePWM(int8_t heater) const
{
return pids[heater]->GetAveragePWM();
}
//**********************************************************************************
// Heat
inline Heat::HeaterStatus Heat::GetStatus(int8_t heater) const
{
if (heater < 0 || heater >= HEATERS)
return HS_off;
if (pids[heater]->FaultOccurred())
return HS_fault;
if (pids[heater]->SwitchedOff())
return HS_off;
if (pids[heater]->Active())
return HS_active;
return HS_standby;
}
inline void Heat::SetActiveTemperature(int8_t heater, float t)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->SetActiveTemperature(t);
}
}
inline float Heat::GetActiveTemperature(int8_t heater) const
{
return (heater >= 0 && heater < HEATERS) ? pids[heater]->GetActiveTemperature() : ABS_ZERO;
}
inline void Heat::SetStandbyTemperature(int8_t heater, float t)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->SetStandbyTemperature(t);
}
}
inline float Heat::GetStandbyTemperature(int8_t heater) const
{
return (heater >= 0 && heater < HEATERS) ? pids[heater]->GetStandbyTemperature() : ABS_ZERO;
}
inline float Heat::GetTemperature(int8_t heater) const
{
return (heater >= 0 && heater < HEATERS) ? pids[heater]->GetTemperature() : ABS_ZERO;
}
inline void Heat::Activate(int8_t heater)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->Activate();
}
}
inline void Heat::SwitchOff(int8_t heater)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->SwitchOff();
}
}
inline void Heat::SwitchOffAll()
{
for(size_t heater = 0; heater < HEATERS; ++heater)
{
pids[heater]->SwitchOff();
}
}
inline void Heat::Standby(int8_t heater)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->Standby();
}
}
inline void Heat::ResetFault(int8_t heater)
{
if (heater >= 0 && heater < HEATERS)
{
pids[heater]->ResetFault();
}
}
#endif