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

PulsedFilamentMonitor.cpp « FilamentMonitors « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8d894d2e6aded2e6bd3bf64bd1dbfc09a9c3c941 (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
/*
 * PulsedFilamentSensor.cpp
 *
 *  Created on: 9 Jan 2018
 *      Author: David
 */

#include "PulsedFilamentMonitor.h"
#include "GCodes/GCodeBuffer/GCodeBuffer.h"
#include "Platform.h"
#include "RepRap.h"
#include "Movement/Move.h"

// Unless we set the option to compare filament on all type of move, we reject readings if the last retract or reprime move wasn't completed
// well before the start bit was received. This is because those moves have high accelerations and decelerations, so the measurement delay
// is more likely to cause errors. This constant sets the delay required after a retract or reprime move before we accept the measurement.
const int32_t SyncDelayMillis = 10;

PulsedFilamentMonitor::PulsedFilamentMonitor(unsigned int extruder, unsigned int type) noexcept
	: FilamentMonitor(extruder, type),
	  mmPerPulse(DefaultMmPerPulse),
	  minMovementAllowed(DefaultMinMovementAllowed), maxMovementAllowed(DefaultMaxMovementAllowed),
	  minimumExtrusionCheckLength(DefaultMinimumExtrusionCheckLength), comparisonEnabled(false)
{
	Init();
}

void PulsedFilamentMonitor::Init() noexcept
{
	sensorValue = 0;
	calibrationStarted = false;
	samplesReceived = 0;
	lastMeasurementTime = 0;
	Reset();
}

void PulsedFilamentMonitor::Reset() noexcept
{
	extrusionCommandedThisSegment = extrusionCommandedSinceLastSync = movementMeasuredThisSegment = movementMeasuredSinceLastSync = 0.0;
	comparisonStarted = false;
	haveInterruptData = false;
	wasPrintingAtInterrupt = false;			// force a resync
}

// Configure this sensor, returning true if error and setting 'seen' if we processed any configuration parameters
bool PulsedFilamentMonitor::Configure(GCodeBuffer& gb, const StringRef& reply, bool& seen)
{
	if (ConfigurePin(gb, reply, INTERRUPT_MODE_RISING, seen))
	{
		return true;
	}

	gb.TryGetFValue('L', mmPerPulse, seen);
	gb.TryGetFValue('E', minimumExtrusionCheckLength, seen);

	if (gb.Seen('R'))
	{
		seen = true;
		size_t numValues = 2;
		uint32_t minMax[2];
		gb.GetUnsignedArray(minMax, numValues, false);
		if (numValues > 0)
		{
			minMovementAllowed = (float)minMax[0] * 0.01;
		}
		if (numValues > 1)
		{
			maxMovementAllowed = (float)minMax[1] * 0.01;
		}
	}

	if (gb.Seen('S'))
	{
		seen = true;
		comparisonEnabled = (gb.GetIValue() > 0);
	}

	if (seen)
	{
		Init();
	}
	else
	{
		reply.copy("Pulse-type filament monitor on pin ");
		GetPort().AppendPinName(reply);
		reply.catf(", %s, sensitivity %.3fmm/pulse, allowed movement %ld%% to %ld%%, check every %.1fmm, ",
					(comparisonEnabled) ? "enabled" : "disabled",
					(double)mmPerPulse,
					lrintf(minMovementAllowed * 100.0),
					lrintf(maxMovementAllowed * 100.0),
					(double)minimumExtrusionCheckLength);

		if (samplesReceived < 2)
		{
			reply.cat("no data received");
		}
		else
		{
			if (calibrationStarted && fabsf(totalMovementMeasured) > 1.0 && totalExtrusionCommanded > 20.0)
			{
				const float measuredMmPerPulse = totalExtrusionCommanded/totalMovementMeasured;
				reply.catf("measured sensitivity %.3fmm/pulse, measured minimum %ld%%, maximum %ld%% over %.1fmm\n",
					(double)measuredMmPerPulse,
					lrintf(100 * minMovementRatio),
					lrintf(100 * maxMovementRatio),
					(double)totalExtrusionCommanded);
			}
			else
			{
				reply.cat("no calibration data");
			}
		}
	}

	return false;
}

// ISR for when the pin state changes. It should return true if the ISR wants the commanded extrusion to be fetched.
bool PulsedFilamentMonitor::Interrupt() noexcept
{
	++sensorValue;
	if (samplesReceived < 100)
	{
		++samplesReceived;
	}

	// Most pulsed filament monitors have low resolution, but at least one user has a high-resolution one.
	// So don't automatically try to sync on every interrupt.
	const uint32_t now = millis();
	if (now - lastMeasurementTime >= 50)
	{
		lastMeasurementTime = millis();
		return true;
	}
	return false;
}

// Call the following regularly to keep the status up to date
void PulsedFilamentMonitor::Poll() noexcept
{
	cpu_irq_disable();
	const uint32_t locSensorVal = sensorValue;
	sensorValue = 0;
	cpu_irq_enable();
	movementMeasuredSinceLastSync += (float)locSensorVal;

	if (haveInterruptData)					// if we have a synchronised value for the amount of extrusion commanded
	{
		if (wasPrintingAtInterrupt && (int32_t)(lastSyncTime - reprap.GetMove().ExtruderPrintingSince()) > SyncDelayMillis)
		{
			// We can use this measurement
			extrusionCommandedThisSegment += extrusionCommandedAtInterrupt;
			movementMeasuredThisSegment += movementMeasuredSinceLastSync;
		}
		lastSyncTime = lastIsrTime;
		extrusionCommandedSinceLastSync -= extrusionCommandedAtInterrupt;
		movementMeasuredSinceLastSync = 0.0;

		haveInterruptData = false;
	}
}

// Call the following at intervals to check the status. This is only called when extrusion is in progress or imminent.
// 'filamentConsumed' is the net amount of extrusion since the last call to this function.
// 'isPrinting' is true unless a non-printing extruder move was in progress
// 'fromIsr' is true if this measurement was taken at the end of the ISR because the ISR returned true
FilamentSensorStatus PulsedFilamentMonitor::Check(bool isPrinting, bool fromIsr, uint32_t isrMillis, float filamentConsumed) noexcept
{
	// 1. Update the extrusion commanded
	extrusionCommandedSinceLastSync += filamentConsumed;

	// 2. If this call passes values synced to the interrupt, save the data
	if (fromIsr)
	{
		extrusionCommandedAtInterrupt = extrusionCommandedSinceLastSync;
		wasPrintingAtInterrupt = isPrinting;
		lastIsrTime = isrMillis;
		haveInterruptData = true;
	}

	// 3. Process the received data and update if we have received anything
	Poll();														// this may update movementMeasured

	// 4. Decide whether it is time to do a comparison, and return the status
	FilamentSensorStatus ret = FilamentSensorStatus::ok;
	if (extrusionCommandedThisSegment >= minimumExtrusionCheckLength)
	{
		ret = CheckFilament(extrusionCommandedThisSegment, movementMeasuredThisSegment, false);
		extrusionCommandedThisSegment = movementMeasuredThisSegment = 0.0;
	}
	else if (extrusionCommandedThisSegment + extrusionCommandedSinceLastSync >= minimumExtrusionCheckLength * 2 && millis() - lastMeasurementTime > 220)
	{
		// A sync is overdue
		ret = CheckFilament(extrusionCommandedThisSegment + extrusionCommandedSinceLastSync, movementMeasuredThisSegment + movementMeasuredSinceLastSync, true);
		extrusionCommandedThisSegment = extrusionCommandedSinceLastSync = movementMeasuredThisSegment = movementMeasuredSinceLastSync = 0.0;
	}

	return ret;
}

// Compare the amount commanded with the amount of extrusion measured, and set up for the next comparison
FilamentSensorStatus PulsedFilamentMonitor::CheckFilament(float amountCommanded, float amountMeasured, bool overdue) noexcept
{
	if (reprap.Debug(moduleFilamentSensors))
	{
		debugPrintf("Extr req %.3f meas %.3f%s\n", (double)amountCommanded, (double)amountMeasured, (overdue) ? " overdue" : "");
	}

	FilamentSensorStatus ret = FilamentSensorStatus::ok;
	const float extrusionMeasured = amountMeasured * mmPerPulse;

	if (!comparisonStarted)
	{
		// The first measurement after we start extruding is often a long way out, so discard it
		comparisonStarted = true;
		calibrationStarted = false;
	}
	else if (comparisonEnabled)
	{
		const float minExtrusionExpected = (amountCommanded >= 0.0)
											 ? amountCommanded * minMovementAllowed
												: amountCommanded * maxMovementAllowed;
		if (extrusionMeasured < minExtrusionExpected)
		{
			ret = FilamentSensorStatus::tooLittleMovement;
		}
		else
		{
			const float maxExtrusionExpected = (amountCommanded >= 0.0)
												 ? amountCommanded * maxMovementAllowed
													: amountCommanded * minMovementAllowed;
			if (extrusionMeasured > maxExtrusionExpected)
			{
				ret = FilamentSensorStatus::tooMuchMovement;
			}
		}
	}

	// Update the calibration accumulators, even if the user hasn't asked to do calibration
	const float ratio = extrusionMeasured/amountCommanded;
	if (calibrationStarted)
	{
		if (ratio < minMovementRatio)
		{
			minMovementRatio = ratio;
		}
		if (ratio > maxMovementRatio)
		{
			maxMovementRatio = ratio;
		}
		totalExtrusionCommanded += amountCommanded;
		totalMovementMeasured += amountMeasured;
	}
	else
	{
		minMovementRatio = maxMovementRatio = ratio;
		totalExtrusionCommanded = amountCommanded;
		totalMovementMeasured = amountMeasured;
		calibrationStarted = true;
	}

	return ret;
}

// Clear the measurement state - called when we are not printing a file. Return the present/not present status if available.
FilamentSensorStatus PulsedFilamentMonitor::Clear() noexcept
{
	Poll();								// to keep the diagnostics up to date
	Reset();
	return FilamentSensorStatus::ok;
}

// Print diagnostic info for this sensor
void PulsedFilamentMonitor::Diagnostics(MessageType mtype, unsigned int extruder) noexcept
{
	Poll();
	const char* const statusText = (samplesReceived < 2) ? "no data received" : "ok";
	reprap.GetPlatform().MessageF(mtype, "Extruder %u sensor: %s\n", extruder, statusText);
}

// End