Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Tool.cpp « Tools « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8408b458f372eca4aaa033d352e0b01a9895f9ca (plain)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
/****************************************************************************************************

 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 "Tool.h"
#include "Filament.h"

#include <GCodes/GCodes.h>
#include <GCodes/GCodeBuffer/GCodeBuffer.h>
#include <Heating/Heat.h>
#include <Platform/Platform.h>
#include <Platform/RepRap.h>

#if SUPPORT_OBJECT_MODEL

// Object model table and functions
// Note: if using GCC version 7.3.1 20180622 and lambda functions are used in this table, you must compile this file with option -std=gnu++17.
// Otherwise the table will be allocated in RAM instead of flash, which wastes too much RAM.

// Macro to build a standard lambda function that includes the necessary type conversions
#define OBJECT_MODEL_FUNC(...) OBJECT_MODEL_FUNC_BODY(Tool, __VA_ARGS__)

constexpr ObjectModelArrayDescriptor Tool::activeTempsArrayDescriptor =
{
	nullptr,					// no lock needed
	[] (const ObjectModel *self, const ObjectExplorationContext&) noexcept -> size_t { return ((const Tool*)self)->heaterCount; },
	[] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue(((const Tool*)self)->activeTemperatures[context.GetLastIndex()], 1); }
};

constexpr ObjectModelArrayDescriptor Tool::standbyTempsArrayDescriptor =
{
	nullptr,					// no lock needed
	[] (const ObjectModel *self, const ObjectExplorationContext&) noexcept -> size_t { return ((const Tool*)self)->heaterCount; },
	[] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue(((const Tool*)self)->standbyTemperatures[context.GetLastIndex()], 1); }
};

constexpr ObjectModelArrayDescriptor Tool::heatersArrayDescriptor =
{
	nullptr,					// no lock needed
	[] (const ObjectModel *self, const ObjectExplorationContext&) noexcept -> size_t { return ((const Tool*)self)->heaterCount; },
	[] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue((int32_t)((const Tool*)self)->heaters[context.GetLastIndex()]); }
};

constexpr ObjectModelArrayDescriptor Tool::feedForwardArrayDescriptor =
{
	nullptr,					// no lock needed
	[] (const ObjectModel *self, const ObjectExplorationContext&) noexcept -> size_t { return ((const Tool*)self)->heaterCount; },
	[] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue(((const Tool*)self)->heaterFeedForward[context.GetLastIndex()], 3); }
};

constexpr ObjectModelArrayDescriptor Tool::extrudersArrayDescriptor =
{
	nullptr,					// no lock needed
	[] (const ObjectModel *self, const ObjectExplorationContext&) noexcept -> size_t { return ((const Tool*)self)->driveCount; },
	[] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue((int32_t)((const Tool*)self)->drives[context.GetLastIndex()]); }
};

constexpr ObjectModelArrayDescriptor Tool::mixArrayDescriptor =
{
	nullptr,					// no lock needed
	[] (const ObjectModel *self, const ObjectExplorationContext&) noexcept -> size_t { return ((const Tool*)self)->driveCount; },
	[] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue(((const Tool*)self)->mix[context.GetLastIndex()], 2); }
};

constexpr ObjectModelArrayDescriptor Tool::offsetsArrayDescriptor =
{
	nullptr,					// no lock needed
	[] (const ObjectModel *self, const ObjectExplorationContext&) noexcept -> size_t { return reprap.GetGCodes().GetVisibleAxes(); },
	[] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue(((const Tool*)self)->offset[context.GetLastIndex()], 3); }
};

constexpr ObjectModelArrayDescriptor Tool::axesArrayDescriptor =
{
	nullptr,					// no lock needed
	[] (const ObjectModel *self, const ObjectExplorationContext&) noexcept -> size_t { return 2; },
	[] (const ObjectModel *self, ObjectExplorationContext& context) noexcept -> ExpressionValue { return ExpressionValue(((const Tool*)self)->axisMapping[context.GetLastIndex()]); }
};

constexpr ObjectModelTableEntry Tool::objectModelTable[] =
{
	// Within each group, these entries must be in alphabetical order
	// 0. Tool members
	{ "active",				OBJECT_MODEL_FUNC_NOSELF(&activeTempsArrayDescriptor), 						ObjectModelEntryFlags::live },
	{ "axes",				OBJECT_MODEL_FUNC_NOSELF(&axesArrayDescriptor), 							ObjectModelEntryFlags::none },
	{ "extruders",			OBJECT_MODEL_FUNC_NOSELF(&extrudersArrayDescriptor), 						ObjectModelEntryFlags::none },
	{ "fans",				OBJECT_MODEL_FUNC(self->fanMapping), 										ObjectModelEntryFlags::none },
	{ "feedForward",		OBJECT_MODEL_FUNC_NOSELF(&feedForwardArrayDescriptor), 						ObjectModelEntryFlags::none },
	{ "filamentExtruder",	OBJECT_MODEL_FUNC((int32_t)self->filamentExtruder),							ObjectModelEntryFlags::none },
	{ "heaters",			OBJECT_MODEL_FUNC_NOSELF(&heatersArrayDescriptor), 							ObjectModelEntryFlags::none },
	{ "isRetracted",		OBJECT_MODEL_FUNC(self->IsRetracted()), 									ObjectModelEntryFlags::live },
	{ "mix",				OBJECT_MODEL_FUNC_NOSELF(&mixArrayDescriptor), 								ObjectModelEntryFlags::none },
	{ "name",				OBJECT_MODEL_FUNC(self->name),						 						ObjectModelEntryFlags::none },
	{ "number",				OBJECT_MODEL_FUNC((int32_t)self->myNumber),									ObjectModelEntryFlags::none },
	{ "offsets",			OBJECT_MODEL_FUNC_NOSELF(&offsetsArrayDescriptor), 							ObjectModelEntryFlags::none },
	{ "offsetsProbed",		OBJECT_MODEL_FUNC((int32_t)self->axisOffsetsProbed.GetRaw()),				ObjectModelEntryFlags::none },
	{ "retraction",			OBJECT_MODEL_FUNC(self, 1),													ObjectModelEntryFlags::none },
	{ "spindle",			OBJECT_MODEL_FUNC((int32_t)self->spindleNumber),							ObjectModelEntryFlags::none },
	{ "spindleRpm",			OBJECT_MODEL_FUNC((int32_t)self->spindleRpm),								ObjectModelEntryFlags::none },
	{ "standby",			OBJECT_MODEL_FUNC_NOSELF(&standbyTempsArrayDescriptor), 					ObjectModelEntryFlags::live },
	{ "state",				OBJECT_MODEL_FUNC(self->state.ToString()), 									ObjectModelEntryFlags::live },

	// 1. Tool.retraction members
	{ "extraRestart",		OBJECT_MODEL_FUNC(self->retractExtra, 1),									ObjectModelEntryFlags::none },
	{ "length",				OBJECT_MODEL_FUNC(self->retractLength, 1),									ObjectModelEntryFlags::none },
	{ "speed" ,				OBJECT_MODEL_FUNC(InverseConvertSpeedToMmPerSec(self->retractSpeed), 1),	ObjectModelEntryFlags::none },
	{ "unretractSpeed",		OBJECT_MODEL_FUNC(InverseConvertSpeedToMmPerSec(self->unRetractSpeed), 1),	ObjectModelEntryFlags::none },
	{ "zHop",				OBJECT_MODEL_FUNC(self->retractHop, 2),										ObjectModelEntryFlags::none },
};

constexpr uint8_t Tool::objectModelTableDescriptor[] = { 2, 18, 5 };

DEFINE_GET_OBJECT_MODEL_TABLE(Tool)

#endif

// Create a new tool and return a pointer to it. If an error occurs, put an error message in 'reply' and return nullptr.
/*static*/ Tool *Tool::Create(unsigned int toolNumber, const char *toolName, int32_t d[], size_t dCount, int32_t h[], size_t hCount, AxesBitmap xMap, AxesBitmap yMap, FansBitmap fanMap, int filamentDrive, size_t sCount, int8_t spindleNo, const StringRef& reply) noexcept
{
	const size_t numExtruders = reprap.GetGCodes().GetNumExtruders();
	if (dCount > ARRAY_SIZE(Tool::drives))
	{
		reply.copy("too many drives");
		return nullptr;
	}

	if (hCount > ARRAY_SIZE(Tool::heaters))
	{
		reply.copy("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)
		{
			reply.copy("bad drive number");
			return nullptr;
		}
	}
	for (size_t i = 0; i < hCount; ++i)
	{
		if (h[i] < 0 || h[i] >= (int)MaxHeaters)
		{
			reply.copy("bad heater number");
			return nullptr;
		}
	}

	// Check that the spindle - if given - is configured
	if (sCount > 0 && spindleNo > -1)
	{
		if (spindleNo >= (int)MaxSpindles)
		{
			reply.copy("bad spindle number");
			return nullptr;
		}
		if (reprap.GetPlatform().AccessSpindle(spindleNo).GetState() == SpindleState::unconfigured)
		{
			reply.copy("unconfigured spindle");
			return nullptr;
		}
	}

	Tool * const t = new Tool;

	if (filamentDrive >= 0 && filamentDrive < (int)MaxExtruders)
	{
		// Use exactly only one Filament instance per extruder drive
		Filament * const filament = Filament::GetFilamentByExtruder(filamentDrive);
		t->filament = (filament == nullptr) ? new Filament(d[0]) : filament;
		t->filamentExtruder = filamentDrive;
	}
	else
	{
		// Don't support filament codes for other tools
		t->filament = nullptr;
		t->filamentExtruder = -1;
	}

	const size_t nameLength = strlen(toolName);
	if (nameLength != 0)
	{
		char *tName = new char[nameLength + 1];
		SafeStrncpy(tName, toolName, nameLength + 1);
		t->name = tName;
	}
	else
	{
		t->name = nullptr;
	}

	t->next = nullptr;
	t->myNumber = (uint16_t)toolNumber;
	t->state = ToolState::off;
	t->driveCount = (uint8_t)dCount;
	t->heaterCount = (uint8_t)hCount;
	t->axisMapping[0] = xMap;
	t->axisMapping[1] = yMap;
	t->fanMapping = fanMap;
	t->heaterFault = false;
	t->axisOffsetsProbed.Clear();
	t->displayColdExtrudeWarning = false;
	t->retractLength = DefaultRetractLength;
	t->retractExtra = 0.0;
	t->retractHop = 0.0;
	t->retractSpeed = t->unRetractSpeed = ConvertSpeedFromMmPerMin(DefaultRetractSpeed);
	t->isRetracted = false;
	t->spindleNumber = spindleNo;
	t->spindleRpm = 0;

	for (size_t axis = 0; axis < MaxAxes; axis++)
	{
		t->offset[axis] = 0.0;
	}

	for (size_t drive = 0; drive < t->driveCount; drive++)
	{
		t->drives[drive] = d[drive];
		t->mix[drive] = (drive == 0) ? 1.0 : 0.0;		// initial mix ratio is 1:0:0
	}

	for (size_t heater = 0; heater < t->heaterCount; heater++)
	{
		const int8_t heaterNumber = (int8_t)h[heater];
		reprap.GetHeat().SetAsToolHeater(heaterNumber);
		t->heaters[heater] = heaterNumber;
		t->activeTemperatures[heater] = ABS_ZERO;
		t->standbyTemperatures[heater] = ABS_ZERO;
		t->heaterFeedForward[heater] = 0.0;
	}

	if (t->filament != nullptr)
	{
		t->filament->LoadAssignment();
	}

	return t;
}

/*static*/ AxesBitmap Tool::GetXAxes(const Tool *tool) noexcept
{
	return (tool == nullptr) ? DefaultXAxisMapping : tool->axisMapping[0];
}

/*static*/ AxesBitmap Tool::GetYAxes(const Tool *tool) noexcept
{
	return (tool == nullptr) ? DefaultYAxisMapping : tool->axisMapping[1];
}

/*static*/ AxesBitmap Tool::GetAxisMapping(const Tool *tool, unsigned int axis) noexcept
{
	return (tool != nullptr && axis < ARRAY_SIZE(tool->axisMapping)) ? tool->axisMapping[axis] : AxesBitmap::MakeFromBits(axis);
}

/*static*/ float Tool::GetOffset(const Tool *tool, size_t axis) noexcept
{
	return (tool == nullptr) ? 0.0 : tool->offset[axis];
}

void Tool::Print(const StringRef& reply) const noexcept
{
	reply.printf("Tool %u - ", myNumber);
	if (name != nullptr)
	{
		reply.catf("name: %s; ", name);
	}

	if (driveCount == 0)
	{
		reply.cat("no drives");
	}
	else
	{
		reply.cat("drives:");
		char sep = ' ';
		for (size_t drive = 0; drive < driveCount; drive++)
		{
			reply.catf("%c%d", sep, drives[drive]);
			sep = ',';
		}
	}

	if (heaterCount == 0)
	{
		reply.cat("; no heaters");
	}
	else
	{
		reply.cat("; heaters (active/standby temps):");
		char sep = ' ';
		for (size_t heater = 0; heater < heaterCount; heater++)
		{
			reply.catf("%c%d (%.1f/%.1f)", sep, heaters[heater], (double)activeTemperatures[heater], (double)standbyTemperatures[heater]);
			sep = ',';
		}
	}

	reply.cat("; xmap:");
	char sep = ' ';
	for (size_t xi = 0; xi < MaxAxes; ++xi)
	{
		if (axisMapping[0].IsBitSet(xi))
		{
			reply.catf("%c%c", sep, reprap.GetGCodes().GetAxisLetters()[xi]);
			sep = ',';
		}
	}

	reply.cat("; ymap:");
	sep = ' ';
	for (size_t yi = 0; yi < MaxAxes; ++yi)
	{
		if (axisMapping[1].IsBitSet(yi))
		{
			reply.catf("%c%c", sep, reprap.GetGCodes().GetAxisLetters()[yi]);
			sep = ',';
		}
	}

	reply.cat("; fans:");
	sep = ' ';
	for (size_t fi = 0; fi < MaxFans; ++fi)
	{
		if (fanMapping.IsBitSet(fi))
		{
			reply.catf("%c%u", sep, fi);
			sep = ',';
		}
	}

	if (spindleNumber == -1)
	{
		reply.cat("; no spindle");
	}
	else
	{
		reply.catf("; spindle: %d@%" PRIi32 "RPM", spindleNumber, spindleRpm);
	}

	reply.catf("; status: %s", (state == ToolState::active) ? "selected" : (state == ToolState::standby) ? "standby" : "off");
}

// There is a temperature fault on a heater, so 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) noexcept
{
	Tool* n = this;
	while (n != nullptr)
	{
		n->SetTemperatureFault(heater);
		n = n->Next();
	}
}

void Tool::ClearTemperatureFault(int8_t heater) noexcept
{
	Tool* n = this;
	while (n != nullptr)
	{
		n->ResetTemperatureFault(heater);
		n = n->Next();
	}
}

void Tool::SetTemperatureFault(int8_t dudHeater) noexcept
{
	for (size_t heater = 0; heater < heaterCount; heater++)
	{
		if (dudHeater == heaters[heater])
		{
			heaterFault = true;
			return;
		}
	}
}

void Tool::ResetTemperatureFault(int8_t wasDudHeater) noexcept
{
	for (size_t heater = 0; heater < heaterCount; heater++)
	{
		if (wasDudHeater == heaters[heater])
		{
			heaterFault = false;
			return;
		}
	}
}

bool Tool::AllHeatersAtHighTemperature(bool forExtrusion) const noexcept
{
	for (size_t heater = 0; heater < heaterCount; heater++)
	{
		const float temperature = reprap.GetHeat().GetHeaterTemperature(heaters[heater]);
		if (temperature < reprap.GetHeat().GetRetractionMinTemp() || (forExtrusion && temperature < reprap.GetHeat().GetExtrusionMinTemp()))
		{
			return false;
		}
	}
	return true;
}

// Activate this tool
void Tool::Activate() noexcept
{
	for (size_t heater = 0; heater < heaterCount; heater++)
	{
		String<StringLength100> message;
		GCodeResult ret;
		try
		{
			reprap.GetHeat().SetActiveTemperature(heaters[heater], activeTemperatures[heater]);
			reprap.GetHeat().SetStandbyTemperature(heaters[heater], standbyTemperatures[heater]);
			ret = reprap.GetHeat().Activate(heaters[heater], message.GetRef());
		}
		catch (const GCodeException& exc)
		{
			exc.GetMessage(message.GetRef(), nullptr);
			ret = GCodeResult::error;
		}
		if (ret != GCodeResult::ok)
		{
			reprap.GetPlatform().MessageF((ret == GCodeResult::warning) ? WarningMessage : ErrorMessage, "%s\n", message.c_str());
		}
	}

	if (spindleNumber > -1)
	{
		Spindle& spindle = reprap.GetPlatform().AccessSpindle(spindleNumber);

		// NIST Standard M6 says "When the tool change is complete: * The spindle will be stopped. [...]"
		spindle.SetState(SpindleState::stopped);

		// Restore the configured RPM of this tool only after we made sure the spindle is not running
		spindle.SetConfiguredRpm(spindleRpm, false);
	}
	state = ToolState::active;
}

void Tool::HeatersToStandby() const noexcept
{
	const Tool * const currentTool = reprap.GetCurrentTool();
	for (size_t heater = 0; heater < heaterCount; heater++)
	{
		// Don't switch a heater to standby if the active tool is using it and is different from this tool
		if (currentTool == this || currentTool == nullptr || !currentTool->UsesHeater(heater))
		{
			try
			{
				reprap.GetHeat().SetStandbyTemperature(heaters[heater], standbyTemperatures[heater]);
				reprap.GetHeat().Standby(heaters[heater], this);
			}
			catch (const GCodeException& exc)
			{
				String<StringLength100> message;
				exc.GetMessage(message.GetRef(), nullptr);
				reprap.GetPlatform().Message(ErrorMessage, message.c_str());
			}
		}
	}
}

void Tool::HeatersToActive() const noexcept
{
	const Tool * const currentTool = reprap.GetCurrentTool();
	for (size_t heater = 0; heater < heaterCount; heater++)
	{
		// Don't switch a heater to active if the active tool is using it and is different from this tool
		if (currentTool == this || currentTool == nullptr || !currentTool->UsesHeater(heater))
		{
			String<StringLength100> message;
			GCodeResult ret;
			try
			{
				reprap.GetHeat().SetActiveTemperature(heaters[heater], activeTemperatures[heater]);
				ret = reprap.GetHeat().Activate(heaters[heater], message.GetRef());
			}
			catch (const GCodeException& exc)
			{
				exc.GetMessage(message.GetRef(), nullptr);
				ret = GCodeResult::error;
			}
			if (ret != GCodeResult::ok)
			{
				reprap.GetPlatform().MessageF((ret == GCodeResult::warning) ? WarningMessage : ErrorMessage, "%s\n", message.c_str());
			}
		}
	}
}

void Tool::HeatersToOff() const noexcept
{
	const Tool * const currentTool = reprap.GetCurrentTool();
	for (size_t heater = 0; heater < heaterCount; heater++)
	{
		// Don't switch a heater to standby if the active tool is using it and is different from this tool
		if (currentTool == this || currentTool == nullptr || !currentTool->UsesHeater(heater))
		{
			reprap.GetHeat().SwitchOff(heaters[heater]);
		}
	}
}

void Tool::Standby() noexcept
{
	HeatersToStandby();

	// NIST Standard M6 says "When the tool change is complete: * The spindle will be stopped. [...]"
	// We don't have M6 but Tn already does tool change so we need
	// to make sure the spindle is off
	if (spindleNumber > -1)
	{
		Spindle& spindle = reprap.GetPlatform().AccessSpindle(spindleNumber);
		spindle.SetState(SpindleState::stopped);
	}

	state = ToolState::standby;
}

// May be called from ISR
bool Tool::ToolCanDrive(bool extrude) noexcept
{
	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, uint16_t &numToolsToReport) const noexcept
{
	for (size_t drive = 0; drive < driveCount; drive++)
	{
		if (drives[drive] >= numExtruders)
		{
			numExtruders = drives[drive] + 1;
		}
	}

	for (size_t heater = 0; heater < heaterCount; heater++)
	{
		if (!reprap.GetHeat().IsBedOrChamberHeater(heaters[heater]) && heaters[heater] >= numHeaters)
		{
			numHeaters = heaters[heater] + 1;
		}
	}

	if (myNumber >= numToolsToReport)
	{
		numToolsToReport = myNumber + 1;
	}
}

bool Tool::DisplayColdExtrudeWarning() noexcept
{
	bool result = displayColdExtrudeWarning;
	displayColdExtrudeWarning = false;
	return result;
}

void Tool::DefineMix(const float m[]) noexcept
{
	for (size_t drive = 0; drive < driveCount; drive++)
	{
		mix[drive] = m[drive];
	}
	reprap.ToolsUpdated();
}

#if HAS_MASS_STORAGE || HAS_SBC_INTERFACE

// Write the tool's settings to file returning true if successful. The settings written leave the tool selected unless it is off.
bool Tool::WriteSettings(FileStore *f) const noexcept
{
	String<StringLength50> buf;
	bool ok = true;

	// Set up active and standby heater temperatures
	if (heaterCount != 0)
	{
		buf.printf("G10 P%d ", myNumber);
		char c = 'S';
		for (size_t i = 0; i < heaterCount; ++i)
		{
			buf.catf("%c%d", c, (int)activeTemperatures[i]);
			c = ':';
		}
		buf.cat(' ');
		c = 'R';
		for (size_t i = 0; i < heaterCount; ++i)
		{
			buf.catf("%c%d", c, (int)standbyTemperatures[i]);
			c = ':';
		}
		buf.cat('\n');
		ok = f->Write(buf.c_str());
	}

	if (ok && state != ToolState::off)
	{
		ok = buf.printf("T%d P0\n", myNumber);
	}

	return ok;
}

#endif

void Tool::SetOffset(size_t axis, float offs, bool byProbing) noexcept
{
	offset[axis] = offs;
	if (byProbing)
	{
		axisOffsetsProbed.SetBit(axis);
	}
	ToolUpdated();
}

float Tool::GetToolHeaterActiveTemperature(size_t heaterNumber) const noexcept
{
	return (heaterNumber < heaterCount) ? activeTemperatures[heaterNumber] : 0.0;
}

float Tool::GetToolHeaterStandbyTemperature(size_t heaterNumber) const noexcept
{
	return (heaterNumber < heaterCount) ? standbyTemperatures[heaterNumber] : 0.0;
}

void Tool::SetToolHeaterActiveTemperature(size_t heaterNumber, float temp) THROWS(GCodeException)
{
	if (heaterNumber < heaterCount)
	{
		const int8_t heater = heaters[heaterNumber];
		const Tool * const currentTool = reprap.GetCurrentTool();
		const bool setHeater = (currentTool == nullptr || currentTool == this);
		if (temp <= NEARLY_ABS_ZERO)								// temperatures close to ABS_ZERO turn off the heater
		{
			activeTemperatures[heaterNumber] = 0;
			if (setHeater)
			{
				reprap.GetHeat().SwitchOff(heater);
			}
		}
		else
		{
			if (temp <= reprap.GetHeat().GetLowestTemperatureLimit(heater) || temp >= reprap.GetHeat().GetHighestTemperatureLimit(heater))
			{
				throw GCodeException(-1, -1, "Requested temperature out of range");
			}
			activeTemperatures[heaterNumber] = temp;
			if (setHeater)
			{
				reprap.GetHeat().SetActiveTemperature(heater, temp);
			}
		}
	}
}

void Tool::SetToolHeaterStandbyTemperature(size_t heaterNumber, float temp) THROWS(GCodeException)
{
	if (heaterNumber < heaterCount)
	{
		const int8_t heater = heaters[heaterNumber];
		const Tool * const currentTool = reprap.GetCurrentTool();
		const Tool * const lastStandbyTool = reprap.GetHeat().GetLastStandbyTool(heater);
		const bool setHeater = (currentTool == nullptr || currentTool == this || lastStandbyTool == nullptr || lastStandbyTool == this);
		if (temp <= NEARLY_ABS_ZERO)								// temperatures close to ABS_ZERO turn off the heater
		{
			standbyTemperatures[heaterNumber] = 0;
			if (setHeater)
			{
				reprap.GetHeat().SwitchOff(heater);
			}
		}
		else
		{
			if (temp <= reprap.GetHeat().GetLowestTemperatureLimit(heater) || temp >= reprap.GetHeat().GetHighestTemperatureLimit(heater))
			{
				throw GCodeException(-1, -1, "Requested temperature out of range");
			}
			standbyTemperatures[heaterNumber] = temp;
			if (setHeater)
			{
				reprap.GetHeat().SetStandbyTemperature(heater, temp);
			}
		}
	}
}

void Tool::SetSpindleRpm(uint32_t rpm) THROWS(GCodeException)
{
	if (spindleNumber > -1)
	{
		Spindle& spindle = reprap.GetPlatform().AccessSpindle(spindleNumber);
		if (rpm == 0)
		{
			spindleRpm = 0;
			spindle.SetState(SpindleState::stopped);
			spindle.SetConfiguredRpm(spindleRpm, false);
		}
		else if (!spindle.IsValidRpm(rpm))
		{
			throw GCodeException(-1, -1, "Requested spindle RPM out of range");
		}
		else
		{
			spindleRpm = rpm;
			if (reprap.GetCurrentTool() == this)
			{
				spindle.SetConfiguredRpm(spindleRpm, true);
			}
		}
		reprap.ToolsUpdated();
	}
}

void Tool::IterateExtruders(function_ref<void(unsigned int)> f) const noexcept
{
	for (size_t i = 0; i < driveCount; ++i)
	{
		f(drives[i]);
	}
}

void Tool::IterateHeaters(function_ref<void(int)> f) const noexcept
{
	for (size_t i = 0; i < heaterCount; ++i)
	{
		f(heaters[i]);
	}
}

void Tool::SetFansPwm(float f) const noexcept
{
	const float pwmChange = reprap.GetFansManager().SetFansValue(fanMapping, f);
	if (pwmChange != 0.0)
	{
		IterateHeaters([pwmChange](unsigned int heater) { reprap.GetHeat().FeedForwardAdjustment(heater, pwmChange, 0.0); });
	}
}

// Return true if this tool uses the specified heater
bool Tool::UsesHeater(int8_t heater) const noexcept
{
	for (size_t i = 0; i < heaterCount; ++i)
	{
		if (heaters[i] == heater)
		{
			return true;
		}
	}
	return false;
}

const char *Tool::GetFilamentName() const noexcept
{
	return (filament == nullptr) ? "" : filament->GetName();
}

GCodeResult Tool::SetFirmwareRetraction(GCodeBuffer &gb, const StringRef &reply, OutputBuffer*& outBuf) THROWS(GCodeException)
{
	bool seen = false;
	if (gb.Seen('S'))
	{
		retractLength = max<float>(gb.GetFValue(), 0.0);
		seen = true;
	}
	if (gb.Seen('R'))	// must do this one after 'S'
	{
		retractExtra = max<float>(gb.GetFValue(), -retractLength);
		seen = true;
	}
	if (gb.Seen('F'))
	{
		unRetractSpeed = retractSpeed = max<float>(gb.GetSpeedFromMm(false), ConvertSpeedFromMmPerMin(MinRetractSpeed));
		seen = true;
	}
	if (gb.Seen('T'))	// must do this one after 'F'
	{
		unRetractSpeed = max<float>(gb.GetSpeedFromMm(false), ConvertSpeedFromMmPerMin(MinRetractSpeed));
		seen = true;
	}
	if (gb.Seen('Z'))
	{
		retractHop = max<float>(gb.GetFValue(), 0.0);
		seen = true;
	}

	if (seen)
	{
		ToolUpdated();
	}
	else
	{
		// Use an output buffer because M207 can report on all tools
		if (outBuf == nullptr && !OutputBuffer::Allocate(outBuf))
		{
			return GCodeResult::notFinished;
		}
		outBuf->lcatf("Tool %u retract/reprime: length %.2f/%.2fmm, speed %.1f/%.1fmm/sec, Z hop %.2fmm",
			myNumber, (double)retractLength, (double)(retractLength + retractExtra), (double)InverseConvertSpeedToMmPerSec(retractSpeed), (double)InverseConvertSpeedToMmPerSec(unRetractSpeed), (double)retractHop);
	}
	return GCodeResult::ok;
}

GCodeResult Tool::GetSetFeedForward(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeException)
{
	if (gb.Seen('S'))
	{
		size_t numValues = heaterCount;
		gb.GetFloatArray(heaterFeedForward, numValues, false);
		ToolUpdated();
	}
	else
	{
		reply.printf("Tool %u heater feedforward:", myNumber);
		for (size_t i = 0; i < heaterCount; ++i)
		{
			reply.catf(" %.3f", (double)heaterFeedForward[i]);
		}
	}

	return GCodeResult::ok;
}

// Apply feedforward to the current tool. Called from an ISR context or with BASEPRI set high.
void Tool::ApplyFeedForward(float extrusionSpeed) const noexcept
{
	Heat& heat = reprap.GetHeat();
	for (size_t i = 0; i < heaterCount; ++i)
	{
		heat.SetExtrusionFeedForward(heaters[i], extrusionSpeed * heaterFeedForward[i]);
	}
}

// Stop applying feedforward to the current tool. Called from an ISR context or with BASEPRI set high.
void Tool::StopFeedForward() const noexcept
{
	Heat& heat = reprap.GetHeat();
	for (size_t i = 0; i < heaterCount; ++i)
	{
		heat.SetExtrusionFeedForward(heaters[i], 0.0);
	}
}

// End