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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
/****************************************************************************************************
RepRapFirmware - Tool
This class implements a tool in the RepRap machine, usually (though not necessarily) an extruder.
Tools may have zero or more drives associated with them and zero or more heaters. There are a fixed number
of tools in a given RepRap, with fixed heaters and drives. All this is specified on reboot, and cannot
be altered dynamically. This restriction may be lifted in the future. Tool descriptions are stored in
GCode macros that are loaded on reboot.
-----------------------------------------------------------------------------------------------------
Version 0.1
Created on: Apr 11, 2014
Adrian Bowyer
RepRap Professional Ltd
http://reprappro.com
Licence: GPL
****************************************************************************************************/
#include "RepRapFirmware.h"
Tool * Tool::freelist = nullptr;
/*static*/Tool * Tool::Create(int toolNumber, long d[], size_t dCount, long h[], size_t hCount, long xMap[], size_t xCount, uint32_t fanMap)
{
const size_t numExtruders = reprap.GetGCodes()->GetNumExtruders();
if (dCount > ARRAY_SIZE(Tool::drives))
{
reprap.GetPlatform()->Message(GENERIC_MESSAGE, "Error: Tool creation: too many drives");
return nullptr;
}
if (hCount > ARRAY_SIZE(Tool::heaters))
{
reprap.GetPlatform()->Message(GENERIC_MESSAGE, "Error: Tool creation: too many heaters");
return nullptr;
}
// Validate the heater and extruder numbers
for (size_t i = 0; i < dCount; ++i)
{
if (d[i] < 0 || d[i] >= (int)numExtruders)
{
reprap.GetPlatform()->Message(GENERIC_MESSAGE, "Error: Tool creation: bad drive number");
return nullptr;
}
}
for (size_t i = 0; i < hCount; ++i)
{
if (h[i] < 0 || h[i] >= HEATERS)
{
reprap.GetPlatform()->Message(GENERIC_MESSAGE, "Error: Tool creation: bad heater number");
return nullptr;
}
}
Tool *t;
if (freelist != nullptr)
{
t = freelist;
freelist = t->next;
}
else
{
t = new Tool;
}
t->myNumber = toolNumber;
t->next = nullptr;
t->active = false;
t->driveCount = dCount;
t->heaterCount = hCount;
t->xmapCount = xCount;
t->fanMapping = fanMap;
t->heaterFault = false;
t->mixing = false;
t->displayColdExtrudeWarning = false;
for (size_t axis = 0; axis < MAX_AXES; axis++)
{
t->offset[axis] = 0.0;
}
if (t->driveCount > 0)
{
const float r = 1.0 / (float) (t->driveCount);
for (size_t drive = 0; drive < t->driveCount; drive++)
{
t->drives[drive] = d[drive];
t->mix[drive] = r;
}
}
for (size_t heater = 0; heater < t->heaterCount; heater++)
{
t->heaters[heater] = h[heater];
t->activeTemperatures[heater] = ABS_ZERO;
t->standbyTemperatures[heater] = ABS_ZERO;
}
for (size_t xi = 0; xi < t->xmapCount; ++xi)
{
t->xMapping[xi] = xMap[xi];
}
return t;
}
/*static*/void Tool::Delete(Tool *t)
{
if (t != nullptr)
{
t->next = freelist;
freelist = t;
}
}
void Tool::Print(StringRef& reply)
{
reply.printf("Tool %d - drives:", myNumber);
char sep = ' ';
for (size_t drive = 0; drive < driveCount; drive++)
{
reply.catf("%c%d", sep, drives[drive]);
sep = ',';
}
reply.cat("; heaters (active/standby temps):");
sep = ' ';
for (size_t heater = 0; heater < heaterCount; heater++)
{
reply.catf("%c%d (%.1f/%.1f)", sep, heaters[heater], activeTemperatures[heater], standbyTemperatures[heater]);
sep = ',';
}
reply.cat("; xmap:");
sep = ' ';
for (size_t xi = 0; xi < xmapCount; ++xi)
{
reply.catf("%c%c", sep, GCodes::axisLetters[xi]);
sep = ',';
}
reply.cat("; fans:");
sep = ' ';
for (size_t fi = 0; fi < NUM_FANS; ++fi)
{
if ((fanMapping & (1u << fi)) != 0)
{
reply.catf("%c%u", sep, fi);
sep = ',';
}
}
reply.catf("; status: %s", active ? "selected" : "standby");
}
float Tool::MaxFeedrate() const
{
if (driveCount <= 0)
{
reprap.GetPlatform()->Message(GENERIC_MESSAGE,
"Error: Attempt to get maximum feedrate for a tool with no drives.\n");
return 1.0;
}
float result = 0.0;
const size_t numAxes = reprap.GetGCodes()->GetNumAxes();
for (size_t d = 0; d < driveCount; d++)
{
const float mf = reprap.GetPlatform()->MaxFeedrate(drives[d] + numAxes);
if (mf > result)
{
result = mf;
}
}
return result;
}
float Tool::InstantDv() const
{
if (driveCount <= 0)
{
reprap.GetPlatform()->Message(GENERIC_MESSAGE, "Error: Attempt to get InstantDv for a tool with no drives.\n");
return 1.0;
}
float result = FLT_MAX;
const size_t numAxes = reprap.GetGCodes()->GetNumAxes();
for (size_t d = 0; d < driveCount; d++)
{
const float idv = reprap.GetPlatform()->ActualInstantDv(drives[d] + numAxes);
if (idv < result)
{
result = idv;
}
}
return result;
}
// There is a temperature fault on a heater.
// Disable all tools using that heater.
// This function must be called for the first
// entry in the linked list.
void Tool::FlagTemperatureFault(int8_t heater)
{
Tool* n = this;
while (n != nullptr)
{
n->SetTemperatureFault(heater);
n = n->Next();
}
}
void Tool::ClearTemperatureFault(int8_t heater)
{
Tool* n = this;
while (n != nullptr)
{
n->ResetTemperatureFault(heater);
n = n->Next();
}
}
void Tool::SetTemperatureFault(int8_t dudHeater)
{
for (size_t heater = 0; heater < heaterCount; heater++)
{
if (dudHeater == heaters[heater])
{
heaterFault = true;
return;
}
}
}
void Tool::ResetTemperatureFault(int8_t wasDudHeater)
{
for (size_t heater = 0; heater < heaterCount; heater++)
{
if (wasDudHeater == heaters[heater])
{
heaterFault = false;
return;
}
}
}
bool Tool::AllHeatersAtHighTemperature(bool forExtrusion) const
{
for (size_t heater = 0; heater < heaterCount; heater++)
{
const float temperature = reprap.GetHeat()->GetTemperature(heaters[heater]);
if (temperature < HOT_ENOUGH_TO_RETRACT || (temperature < HOT_ENOUGH_TO_EXTRUDE && forExtrusion))
{
return false;
}
}
return true;
}
void Tool::Activate(Tool* currentlyActive)
{
if (!active)
{
if (currentlyActive != nullptr && currentlyActive != this)
{
currentlyActive->Standby();
}
for (size_t heater = 0; heater < heaterCount; heater++)
{
reprap.GetHeat()->SetActiveTemperature(heaters[heater], activeTemperatures[heater]);
reprap.GetHeat()->SetStandbyTemperature(heaters[heater], standbyTemperatures[heater]);
reprap.GetHeat()->Activate(heaters[heater]);
}
active = true;
}
}
void Tool::Standby()
{
if (active)
{
for (size_t heater = 0; heater < heaterCount; heater++)
{
reprap.GetHeat()->SetStandbyTemperature(heaters[heater], standbyTemperatures[heater]);
reprap.GetHeat()->Standby(heaters[heater]);
}
active = false;
}
}
void Tool::SetVariables(const float* standby, const float* active)
{
for (size_t heater = 0; heater < heaterCount; heater++)
{
if (active[heater] < NEARLY_ABS_ZERO && standby[heater] < NEARLY_ABS_ZERO)
{
// Temperatures close to ABS_ZERO turn off all associated heaters
reprap.GetHeat()->SwitchOff(heaters[heater]);
}
else
{
const float temperatureLimit = reprap.GetHeat()->GetTemperatureLimit(heaters[heater]);
if (active[heater] < temperatureLimit)
{
activeTemperatures[heater] = active[heater];
reprap.GetHeat()->SetActiveTemperature(heaters[heater], activeTemperatures[heater]);
}
if (standby[heater] < temperatureLimit)
{
standbyTemperatures[heater] = standby[heater];
reprap.GetHeat()->SetStandbyTemperature(heaters[heater], standbyTemperatures[heater]);
}
}
}
}
void Tool::GetVariables(float* standby, float* active) const
{
for (size_t heater = 0; heater < heaterCount; heater++)
{
active[heater] = activeTemperatures[heater];
standby[heater] = standbyTemperatures[heater];
}
}
// May be called from ISR
bool Tool::ToolCanDrive(bool extrude)
{
if (!heaterFault && AllHeatersAtHighTemperature(extrude))
{
return true;
}
displayColdExtrudeWarning = true;
return false;
}
// Update the number of active drives and extruders in use to reflect what this tool uses
void Tool::UpdateExtruderAndHeaterCount(uint16_t &numExtruders, uint16_t &numHeaters) const
{
for (size_t drive = 0; drive < driveCount; drive++)
{
if (drives[drive] >= numExtruders)
{
numExtruders = drives[drive] + 1;
}
}
const int8_t bedHeater = reprap.GetHeat()->GetBedHeater();
for (size_t heater = 0; heater < heaterCount; heater++)
{
if (heaters[heater] != bedHeater && heaters[heater] >= numHeaters)
{
numHeaters = heaters[heater] + 1;
}
}
}
bool Tool::DisplayColdExtrudeWarning()
{
bool result = displayColdExtrudeWarning;
displayColdExtrudeWarning = false;
return result;
}
void Tool::DefineMix(const float m[])
{
for(size_t drive = 0; drive < driveCount; drive++)
{
mix[drive] = m[drive];
}
}
// End
|