forked from reprappro/RepRapFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeat.cpp
More file actions
228 lines (185 loc) · 5.85 KB
/
Heat.cpp
File metadata and controls
228 lines (185 loc) · 5.85 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
/****************************************************************************************************
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"
Heat::Heat(Platform* p, GCodes* g)
{
platform = p;
gCodes = g;
for(int8_t heater=0; heater < HEATERS; heater++)
pids[heater] = new PID(platform, heater);
active = false;
}
void Heat::Init()
{
for(int8_t heater=0; heater < HEATERS; heater++)
pids[heater]->Init();
lastTime = platform->Time();
longWait = lastTime;
active = true;
}
void Heat::Exit()
{
for(int8_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(int8_t heater=0; heater < HEATERS; heater++)
pids[heater]->Spin();
platform->ClassReport("Heat", longWait);
}
void Heat::Diagnostics()
{
platform->Message(HOST_MESSAGE, "Heat Diagnostics:\n");
// TODO - Put something useful in here
}
bool Heat::AllHeatersAtSetTemperatures()
{
for(int8_t heater = 0; heater < HEATERS; heater++)
{
if(!HeaterAtSetTemperature(heater))
return false;
}
return true;
}
//query an individual heater
bool Heat::HeaterAtSetTemperature(int8_t heater)
{
if(pids[heater]->SwitchedOff()) // If it hasn't anything to do, it must be right wherever it is...
return true;
float dt = GetTemperature(heater);
if(pids[heater]->Active())
{
if(GetActiveTemperature(heater) < TEMPERATURE_LOW_SO_DONT_CARE)
dt = 0.0;
else
dt = fabs(dt - GetActiveTemperature(heater));
} else
{
if(GetStandbyTemperature(heater) < TEMPERATURE_LOW_SO_DONT_CARE)
dt = 0.0;
else
dt = fabs(dt - GetStandbyTemperature(heater));
}
if(dt > TEMPERATURE_CLOSE_ENOUGH)
return false;
return true;
}
//******************************************************************************************************
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;
temp_dState = 0.0;
badTemperatureCount = 0;
temperatureFault = false;
active = false; // Default to standby temperature
switchedOff = true;
}
void PID::SwitchOn()
{
// if(reprap.Debug())
// {
// snprintf(scratchString, STRING_LENGTH, "Heater %d switched on.\n", heater);
// platform->Message(BOTH_MESSAGE, scratchString);
// }
switchedOff = false;
}
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...
return;
}
// We are switched on. Check for faults.
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(HOST_MESSAGE, "Temperature measurement fault on heater ");
snprintf(scratchString, STRING_LENGTH, "%d", heater);
platform->Message(HOST_MESSAGE, scratchString);
platform->Message(HOST_MESSAGE, ", T = ");
snprintf(scratchString, STRING_LENGTH, "%f", temperature);
platform->Message(HOST_MESSAGE, scratchString);
platform->Message(HOST_MESSAGE, "\n");
}
} else
{
badTemperatureCount = 0;
}
float error = ((active) ? activeTemperature : standbyTemperature) - temperature;
if(!platform->UsePID(heater))
{
platform->SetHeater(heater, (error > 0.0) ? 1.0 : 0.0);
return;
}
if(error < -platform->FullPidBand(heater))
{
temp_iState = 0.0;
platform->SetHeater(heater, 0.0);
lastTemperature = temperature;
return;
}
if(error > platform->FullPidBand(heater))
{
temp_iState = 0.0;
platform->SetHeater(heater, 1.0);
lastTemperature = temperature;
return;
}
temp_iState += error * platform->PidKi(heater);
if (temp_iState < platform->PidMin(heater)) temp_iState = platform->PidMin(heater);
else if (temp_iState > platform->PidMax(heater)) temp_iState = platform->PidMax(heater);
temp_dState = platform->PidKd(heater)*(temperature - lastTemperature)*(1.0 - platform->DMix(heater)) + platform->DMix(heater)*temp_dState;
float result = platform->PidKp(heater)*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);
// char buffer[100];
// snprintf(buffer, ARRAY_SIZE(buffer), "Heat: e=%f, P=%f, I=%f, d=%f, r=%f\n", error, platform->PidKp(heater)*error, temp_iState, temp_dState, result);
// platform->Message(HOST_MESSAGE, buffer);
}