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

FilamentMonitor.cpp « FilamentMonitors « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d3d6e75168bbb83f0ce80b5132064fa4ac99ac70 (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
/*
 * FilamentSensor.cpp
 *
 *  Created on: 20 Jul 2017
 *      Author: David
 */

#include "FilamentMonitor.h"
#include "SimpleFilamentMonitor.h"
#include "RotatingMagnetFilamentMonitor.h"
#include "LaserFilamentMonitor.h"
#include "PulsedFilamentMonitor.h"
#include "RepRap.h"
#include "Platform.h"
#include "GCodes/GCodeBuffer/GCodeBuffer.h"
#include "Movement/Move.h"
#include "PrintMonitor.h"

// Static data
Mutex FilamentMonitor::filamentSensorsMutex;
FilamentMonitor *FilamentMonitor::filamentSensors[MaxExtruders] = { 0 };

// Default destructor
FilamentMonitor::~FilamentMonitor()
{
}

// Call this to disable the interrupt before deleting or re-configuring a filament monitor
void FilamentMonitor::Disable()
{
	port.Release();
}

// Try to get the pin number from the GCode command in the buffer, setting Seen if a pin number was provided and returning true if error.
// Also attaches the ISR.
bool FilamentMonitor::ConfigurePin(GCodeBuffer& gb, const StringRef& reply, InterruptMode interruptMode, bool& seen)
{
	if (gb.Seen('C'))
	{
		seen = true;
		if (!port.AssignPort(gb, reply, PinUsedBy::filamentMonitor, PinAccess::read))
		{
			return true;
		}

		haveIsrStepsCommanded = false;
		if (interruptMode != INTERRUPT_MODE_NONE && !port.AttachInterrupt(InterruptEntry, interruptMode, this))
		{
			reply.copy("unsuitable pin");
			return true;
		}
	}
	else if (seen)
	{
		// We already had a P parameter, therefore it is an error not to have a C parameter too
		reply.copy("no pin name given");
		return true;
	}
	return false;
}

// Static initialisation
/*static*/ void FilamentMonitor::InitStatic()
{
	filamentSensorsMutex.Create("FilamentSensors");
}

// Handle M591
/*static*/ GCodeResult FilamentMonitor::Configure(GCodeBuffer& gb, const StringRef& reply, unsigned int extruder)
{

	bool seen = false;
	uint32_t newSensorType;
	gb.TryGetUIValue('P', newSensorType, seen);

	MutexLocker lock(filamentSensorsMutex);
	FilamentMonitor*& sensor = filamentSensors[extruder];

	if (seen)
	{
		// We are setting the filament monitor type, so see if it has changed
		if (sensor != nullptr && newSensorType != sensor->GetType())
		{
			// We already have a sensor of a different type, so delete the old sensor
			sensor->Disable();
			delete sensor;
			sensor = nullptr;
		}

		if (sensor == nullptr)
		{
			sensor = Create(extruder, newSensorType);					// create the new sensor type, if any
		}
	}

	if (sensor != nullptr)
	{
		// Configure the sensor
		const bool error = sensor->Configure(gb, reply, seen);
		if (error)
		{
			sensor->Disable();
			delete sensor;
			sensor = nullptr;
		}
		return GetGCodeResultFromError(error);
	}
	else if (!seen)
	{
		reply.printf("Extruder %u has no filament sensor", extruder);
	}
	return GCodeResult::ok;
}

// Factory function
/*static*/ FilamentMonitor *FilamentMonitor::Create(unsigned int extruder, unsigned int type)
{
	switch (type)
	{
	case 1:		// active high switch
	case 2:		// active low switch
		return new SimpleFilamentMonitor(extruder, type);
		break;

	case 3:		// duet3d rotating magnet, no switch
	case 4:		// duet3d rotating magnet + switch
		return new RotatingMagnetFilamentMonitor(extruder, type);

	case 5:		// duet3d laser, no switch
	case 6:		// duet3d laser + switch
		return new LaserFilamentMonitor(extruder, type);

	case 7:		// simple pulse output sensor
		return new PulsedFilamentMonitor(extruder, type);
		break;

	default:	// no sensor, or unknown sensor
		return nullptr;
	}
}

// Return an error message corresponding to a status code
/*static*/ const char *FilamentMonitor::GetErrorMessage(FilamentSensorStatus f)
{
	switch(f)
	{
	case FilamentSensorStatus::ok:					return "no error";
	case FilamentSensorStatus::noFilament:			return "no filament";
	case FilamentSensorStatus::tooLittleMovement:	return "too little movement";
	case FilamentSensorStatus::tooMuchMovement:		return "too much movement";
	case FilamentSensorStatus::sensorError:			return "sensor not working";
	default:										return "unknown error";
	}
}

// ISR
/*static*/ void FilamentMonitor::InterruptEntry(CallbackParameter param)
{
	FilamentMonitor * const fm = static_cast<FilamentMonitor*>(param.vp);
	if (fm->Interrupt())
	{
		fm->isrExtruderStepsCommanded = reprap.GetMove().GetAccumulatedExtrusion(fm->extruderNumber, fm->isrWasPrinting);
		fm->haveIsrStepsCommanded = true;
		fm->isrMillis = millis();
	}
}

/*static*/ void FilamentMonitor::Spin()
{
	MutexLocker lock(filamentSensorsMutex);

	// Filament sensors
	for (size_t extruder = 0; extruder < MaxExtruders; ++extruder)
	{
		if (filamentSensors[extruder] != nullptr)
		{
			FilamentMonitor& fs = *filamentSensors[extruder];
			GCodes& gCodes = reprap.GetGCodes();
			bool isPrinting;
			bool fromIsr;
			int32_t extruderStepsCommanded;
			uint32_t isrMillis;
			cpu_irq_disable();
			if (fs.haveIsrStepsCommanded)
			{
				extruderStepsCommanded = fs.isrExtruderStepsCommanded;
				isPrinting = fs.isrWasPrinting;
				isrMillis = fs.isrMillis;
				fs.haveIsrStepsCommanded = false;
				cpu_irq_enable();
				fromIsr = true;
			}
			else
			{
				cpu_irq_enable();
				extruderStepsCommanded = reprap.GetMove().GetAccumulatedExtrusion(extruder, isPrinting);		// get and clear the net extrusion commanded
				fromIsr = false;
				isrMillis = 0;
			}
			if (gCodes.IsReallyPrinting() && !gCodes.IsSimulating())
			{
				const float extrusionCommanded = (float)extruderStepsCommanded/reprap.GetPlatform().DriveStepsPerUnit(ExtruderToLogicalDrive(extruder));
				const FilamentSensorStatus fstat = fs.Check(isPrinting, fromIsr, isrMillis, extrusionCommanded);
				if (fstat != FilamentSensorStatus::ok)
				{
					if (reprap.Debug(moduleFilamentSensors))
					{
						debugPrintf("Filament error: extruder %u reports %s\n", extruder, FilamentMonitor::GetErrorMessage(fstat));
					}
					else
					{
						gCodes.FilamentError(extruder, fstat);
					}
				}
			}
			else
			{
				fs.Clear();
			}
		}
	}
}

// Send diagnostics info
/*static*/ void FilamentMonitor::Diagnostics(MessageType mtype)
{
	bool first = true;
	for (size_t i = 0; i < MaxExtruders; ++i)
	{
		if (filamentSensors[i] != nullptr)
		{
			if (first)
			{
				reprap.GetPlatform().Message(mtype, "=== Filament sensors ===\n");
				first = false;
			}
			filamentSensors[i]->Diagnostics(mtype, i);
		}
	}
}

// End