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

GCodes5.cpp « GCodes « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 79db28f480de42592d6337fed64a9da67cfb6fe5 (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
/*
 * GCodes5.cpp
 *
 *  Created on: 8 Mar 2022
 *      Author: David
 *
 *  Purpose: Tool management
 */

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

// Check if the specified heater is used by a current tool other than the specified one
bool GCodes::IsHeaterUsedByDifferentCurrentTool(int heaterNumber, const Tool *tool) const noexcept
{
	for (const MovementState& ms : moveStates)
	{
		if (ms.currentTool != nullptr && ms.currentTool != tool && ms.currentTool->UsesHeater(heaterNumber))
		{
			return true;
		}
	}
	return false;
}

// Report the temperatures of one tool in M105 format
void GCodes::ReportToolTemperatures(const StringRef& reply, const Tool *tool, bool includeNumber) const noexcept
{
	if (tool != nullptr && tool->HeaterCount() != 0)
	{
		if (reply.strlen() != 0)
		{
			reply.cat(' ');
		}
		if (includeNumber)
		{
			reply.catf("T%u", tool->Number());
		}
		else
		{
			reply.cat("T");
		}

		Heat& heat = reprap.GetHeat();
		char sep = ':';
		for (size_t i = 0; i < tool->HeaterCount(); ++i)
		{
			const int heater = tool->GetHeater(i);
			reply.catf("%c%.1f /%.1f", sep, (double)heat.GetHeaterTemperature(heater), (double)heat.GetTargetTemperature(heater));
			sep = ' ';
		}
	}
}

#if SUPPORT_ASYNC_MOVES

// Handle M400
GCodeResult GCodes::ExecuteM400(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeException)
{
	const unsigned int param = (gb.Seen('P')) ? gb.GetLimitedUIValue('P', 2) : 0;
	const bool finished = (param == 1) ? LockAllMovementSystemsAndWaitForStandstill(gb) : LockCurrentMovementSystemAndWaitForStandstill(gb);
	return (finished) ? GCodeResult::ok : GCodeResult::notFinished;
}

// Handle M596
GCodeResult GCodes::SelectMovementQueue(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeException)
{
	if (gb.Seen('P'))
	{
		UnlockMovement(gb);							// in case we are in a macro - avoid unlocking the wrong movement system later
		const unsigned int queueNumber = gb.GetLimitedUIValue('P', ARRAY_SIZE(moveStates));
		gb.SetActiveQueueNumber(queueNumber);
		reprap.InputsUpdated();
	}
	else
	{
		reply.printf("Motion system %u is active", gb.GetActiveQueueNumber());
	}
	return GCodeResult::ok;
}

// Handle M597
GCodeResult GCodes::CollisionAvoidance(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeException)
{
	// Find the two specified axes
	int lowerAxisNumber = -1, upperAxisNumber = -1;
	float lowerValue, upperValue;
	for (unsigned int i = 0; i < numVisibleAxes; ++i)
	{
		if (gb.Seen(axisLetters[i]))
		{
			if (lowerAxisNumber < 0)
			{
				lowerAxisNumber = i;
				lowerValue = gb.GetFValue();
			}
			else
			{
				upperAxisNumber = i;
				upperValue = gb.GetFValue();
				break;
			}
		}
	}

	if (upperAxisNumber >= 0)
	{
		// Seen two axes, so go ahead
		if (upperValue == lowerValue)
		{
			reply.copy("Axis values must be different");
			return GCodeResult::error;
		}
		if (upperValue < lowerValue)
		{
			std::swap(upperValue, lowerValue);
			std::swap(upperAxisNumber, lowerAxisNumber);
			collisionChecker.Set(lowerAxisNumber, upperAxisNumber, upperValue - lowerValue, GetMovementState(gb).coords);
		}
	}
	else if (lowerAxisNumber >= 0)
	{
		reply.copy("Only one axis specified");
		return GCodeResult::error;
	}
	else if (collisionChecker.IsValid())
	{
		reply.printf("For collision avoidance, axis %c position must be at least %.1fmm higher than axis %c",
						axisLetters[collisionChecker.GetUpperAxis()], (double)collisionChecker.GetMinSeparation(), axisLetters[collisionChecker.GetLowerAxis()]);
	}
	else
	{
		reply.copy("Collision avoidance is not active");
	}
	return GCodeResult::ok;
}

// Handle M598
GCodeResult GCodes::SyncMovementSystems(GCodeBuffer& gb, const StringRef& reply) THROWS(GCodeException)
{
	return (DoSync(gb)) ? GCodeResult::ok : GCodeResult::notFinished;
}

#endif

GCodeResult GCodes::HandleM486(GCodeBuffer &gb, const StringRef &reply, OutputBuffer*& buf) THROWS(GCodeException)
{
	bool seen = false;
	if (gb.Seen('T'))
	{
		// Specify how many objects. May be useful for a user interface.
		seen = true;
		buildObjects.HandleM486T(gb.GetUIValue());
	}

	if (gb.Seen('S'))
	{
		// Specify which object we are about to print
		seen = true;
		buildObjects.UseM486Labelling();

		const int num = gb.GetIValue();
		if (num >= 0 && num < (int)MaxTrackedObjects && gb.Seen('A'))
		{
			String<StringLength50> objectName;
			gb.GetQuotedString(objectName.GetRef());
			buildObjects.SetM486Label(num, objectName.c_str());
		}
		ChangeToObject(gb, num);
	}

	const bool seenC = gb.Seen('C');
	if (seenC || gb.Seen('P'))
	{
		// Cancel an object
		seen = true;
		const int objectToCancel = (seenC) ? GetMovementState(gb).currentObjectNumber : (int)gb.GetUIValue();
		if (objectToCancel < 0)
		{
			reply.copy("No current object");
			return GCodeResult::error;
		}

		if (buildObjects.CancelObject((unsigned int)objectToCancel))
		{
			for (MovementState& ms : moveStates)
			{
				if (objectToCancel == ms.currentObjectNumber)
				{
					ms.StopPrinting(gb);
				}
			}
			reply.printf("Object %d cancelled", objectToCancel);
		}
	}

	if (gb.Seen('U'))
	{
		// Resume an object
		seen = true;
		const unsigned int objectToResume = gb.GetUIValue();
		if (buildObjects.ResumeObject(objectToResume))
		{
			for (MovementState& ms : moveStates)
			{
				if ((int)objectToResume == ms.currentObjectNumber)
				{
					ms.ResumePrinting(gb);
				}
			}
			reprap.JobUpdated();
		}
	}

	if (!seen)
	{
		// List objects on build plate
		if (!OutputBuffer::Allocate(buf))
		{
			return GCodeResult::notFinished;
		}

		buildObjects.ListObjects(buf);
	}

	return GCodeResult::ok;
}

// This is called when we have found an object label in a comment
void GCodes::StartObject(GCodeBuffer& gb, const char *_ecv_array label) noexcept
{
	if (!buildObjects.IsUsingM486Naming())
	{
		const size_t objectNumber = buildObjects.GetObjectNumber(label);
		ChangeToObject(gb, objectNumber);
	}
}

// This is called when we have found a "stop printing object" comment
void GCodes::StopObject(GCodeBuffer& gb) noexcept
{
	if (!buildObjects.IsUsingM486Naming())
	{
		ChangeToObject(gb, -1);
	}
}

void GCodes::ChangeToObject(GCodeBuffer& gb, int objectNumber) noexcept
{
	MovementState& ms = GetMovementState(gb);
	ms.currentObjectNumber = objectNumber;
	const bool cancelCurrentObject = buildObjects.CheckObject(objectNumber);
	if (cancelCurrentObject && !ms.currentObjectCancelled)
	{
		ms.StopPrinting(gb);
	}
	else if (!cancelCurrentObject && ms.currentObjectCancelled)
	{
		ms.ResumePrinting(gb);
	}
}

// Process M204
GCodeResult GCodes::ConfigureAccelerations(GCodeBuffer&gb, const StringRef& reply) THROWS(GCodeException)
{
	MovementState& ms = GetMovementState(gb);
	bool seen = false;
	if (gb.Seen('S'))
	{
		// For backwards compatibility with old versions of Marlin (e.g. for Cura and the Prusa fork of slic3r), set both accelerations
		seen = true;
		ms.maxTravelAcceleration = ms.maxPrintingAcceleration = gb.GetAcceleration();
	}
	if (gb.Seen('P'))
	{
		seen = true;
		ms.maxPrintingAcceleration = gb.GetAcceleration();
	}
	if (gb.Seen('T'))
	{
		seen = true;
		ms.maxTravelAcceleration = gb.GetAcceleration();
	}
	if (seen)
	{
		reprap.MoveUpdated();
	}
	else
	{
		reply.printf("Maximum printing acceleration %.1f, maximum travel acceleration %.1f mm/sec^2",
						(double)InverseConvertAcceleration(ms.maxPrintingAcceleration), (double)InverseConvertAcceleration(ms.maxTravelAcceleration));
	}
	return GCodeResult::ok;
}

#if HAS_MASS_STORAGE || HAS_SBC_INTERFACE

// Save some resume information, returning true if successful
// We assume that the tool configuration doesn't change, only the temperatures and the mix
bool GCodes::WriteToolSettings(FileStore *f, const MovementState& ms) const noexcept
{
	// First write the settings of all tools except the current one and the command to select them if they are on standby
	bool ok = true;
	ReadLocker lock(Tool::toolListLock);
	for (const Tool *t = Tool::GetToolList(); t != nullptr && ok; t = t->Next())
	{
		if (t != ms.currentTool)
		{
			ok = t->WriteSettings(f);
		}
	}

	// Finally write the settings of the active tool and the commands to select it. If no current tool, just deselect all tools.
	if (ok)
	{
		if (ms.currentTool == nullptr)
		{
			ok = f->Write("T-1 P0\n");
		}
		else
		{
			ok = ms.currentTool->WriteSettings(f);
			if (ok)
			{
				String<StringLength20> buf;
				buf.printf("T%u P0\n", ms.currentTool->Number());
				ok = f->Write(buf.c_str());
			}
		}
	}
	return ok;
}

// Save some information in config-override.g
bool GCodes::WriteToolParameters(FileStore *f, const bool forceWriteOffsets) const noexcept
{
	bool ok = true, written = false;
	ReadLocker lock(Tool::toolListLock);
	for (const Tool *t = Tool::GetToolList(); ok && t != nullptr; t = t->Next())
	{
		const AxesBitmap axesProbed = t->GetAxisOffsetsProbed();
		if (axesProbed.IsNonEmpty() || forceWriteOffsets)
		{
			String<StringLength256> scratchString;
			if (!written)
			{
				scratchString.copy("; Probed tool offsets\n");
				written = true;
			}
			scratchString.catf("G10 P%d", t->Number());
			for (size_t axis = 0; axis < MaxAxes; ++axis)
			{
				if (forceWriteOffsets || axesProbed.IsBitSet(axis))
				{
					scratchString.catf(" %c%.2f", GetAxisLetters()[axis], (double)(t->GetOffset(axis)));
				}
			}
			scratchString.cat('\n');
			ok = f->Write(scratchString.c_str());
		}
	}
	return ok;
}

#endif

// End