Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 18e5ed2

Browse files
committedMar 22, 2023
cleanup, add test
1 parent a7ce62d commit 18e5ed2

File tree

4 files changed

+71
-164
lines changed

4 files changed

+71
-164
lines changed
 

‎Marlin/Configuration.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -1894,12 +1894,13 @@
18941894
// large enough to avoid false positives.)
18951895
//#define FILAMENT_MOTION_SENSOR
18961896

1897-
#ifdef FILAMENT_MOTION_SENSOR
1898-
//#define FILAMENT_BOTH_SWITCH_AND_MOTION
1899-
#ifdef FILAMENT_BOTH_SWITCH_AND_MOTION
1897+
#if ENABLED(FILAMENT_MOTION_SENSOR)
1898+
//#define FILAMENT_SWITCH_AND_MOTION
1899+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
19001900
#define NUM_MOTION_SENSORS 1 // Number of sensors, up to one per extruder. Define a FIL_MOTION#_PIN for each.
1901-
#define FIL_MOTION1_PIN PA0
1902-
// Override individually if the runout sensors vary
1901+
//#define FIL_MOTION1_PIN -1
1902+
1903+
// Override individually if the motion sensors vary
19031904
//#define FIL_MOTION1_STATE LOW
19041905
//#define FIL_MOTION1_PULLUP
19051906
//#define FIL_MOTION1_PULLDOWN

‎Marlin/src/feature/runout.h

+61-155
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,10 @@
4444
#endif
4545

4646
#if ENABLED(FILAMENT_MOTION_SENSOR)
47-
#define FILAMENT_HAS_MOTION
48-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
49-
#define FILAMENT_HAS_SWITCH
50-
#endif
51-
#else
52-
#define FILAMENT_HAS_SWITCH
47+
#define HAS_FILAMENT_MOTION 1
48+
#endif
49+
#if DISABLED(FILAMENT_MOTION_SENSOR) || ENABLED(FILAMENT_SWITCH_AND_MOTION)
50+
#define HAS_FILAMENT_SWITCH 1
5351
#endif
5452

5553
void event_filament_runout(const uint8_t extruder);
@@ -106,11 +104,11 @@ class TFilamentMonitor : public FilamentMonitorBase {
106104
static void filament_present(const uint8_t extruder) {
107105
response.filament_present(extruder);
108106
}
109-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
110-
static void filament_motion_present(const uint8_t extruder) {
111-
response.filament_motion_present(extruder);
112-
}
113-
#endif
107+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
108+
static void filament_motion_present(const uint8_t extruder) {
109+
response.filament_motion_present(extruder);
110+
}
111+
#endif
114112

115113
#if HAS_FILAMENT_RUNOUT_DISTANCE
116114
static float& runout_distance() { return response.runout_distance_mm; }
@@ -184,71 +182,25 @@ class FilamentSensorBase {
184182
static void filament_present(const uint8_t extruder) {
185183
runout.filament_present(extruder); // ...which calls response.filament_present(extruder)
186184
}
187-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
188-
static void filament_motion_present(const uint8_t extruder) {
189-
runout.filament_motion_present(extruder);
190-
}
191-
#endif
185+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
186+
static void filament_motion_present(const uint8_t extruder) {
187+
runout.filament_motion_present(extruder); // ...which calls response.filament_motion_present(extruder)
188+
}
189+
#endif
192190

193191
public:
194192
static void setup() {
195-
#define _INIT_RUNOUT_PIN(P,S,U,D) do{ if (ENABLED(U)) SET_INPUT_PULLUP(P); else if (ENABLED(D)) SET_INPUT_PULLDOWN(P); else SET_INPUT(P); }while(0)
193+
#define _INIT_RUNOUT_PIN(P,S,U,D) do{ if (ENABLED(U)) SET_INPUT_PULLUP(P); else if (ENABLED(D)) SET_INPUT_PULLDOWN(P); else SET_INPUT(P); }while(0);
196194
#define INIT_RUNOUT_PIN(N) _INIT_RUNOUT_PIN(FIL_RUNOUT##N##_PIN, FIL_RUNOUT##N##_STATE, FIL_RUNOUT##N##_PULLUP, FIL_RUNOUT##N##_PULLDOWN)
197-
#if NUM_RUNOUT_SENSORS >= 1
198-
INIT_RUNOUT_PIN(1);
199-
#endif
200-
#if NUM_RUNOUT_SENSORS >= 2
201-
INIT_RUNOUT_PIN(2);
202-
#endif
203-
#if NUM_RUNOUT_SENSORS >= 3
204-
INIT_RUNOUT_PIN(3);
205-
#endif
206-
#if NUM_RUNOUT_SENSORS >= 4
207-
INIT_RUNOUT_PIN(4);
208-
#endif
209-
#if NUM_RUNOUT_SENSORS >= 5
210-
INIT_RUNOUT_PIN(5);
211-
#endif
212-
#if NUM_RUNOUT_SENSORS >= 6
213-
INIT_RUNOUT_PIN(6);
214-
#endif
215-
#if NUM_RUNOUT_SENSORS >= 7
216-
INIT_RUNOUT_PIN(7);
217-
#endif
218-
#if NUM_RUNOUT_SENSORS >= 8
219-
INIT_RUNOUT_PIN(8);
220-
#endif
195+
REPEAT_1(NUM_RUNOUT_SENSORS, INIT_RUNOUT_PIN);
221196
#undef INIT_RUNOUT_PIN
222197

223-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
198+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
224199
#define INIT_MOTION_PIN(N) _INIT_RUNOUT_PIN(FIL_MOTION##N##_PIN, FIL_MOTION##N##_STATE, FIL_MOTION##N##_PULLUP, FIL_MOTION##N##_PULLDOWN)
225-
#if NUM_MOTION_SENSORS >= 1
226-
INIT_MOTION_PIN(1);
227-
#endif
228-
#if NUM_MOTION_SENSORS >= 2
229-
INIT_MOTION_PIN(2);
230-
#endif
231-
#if NUM_MOTION_SENSORS >= 3
232-
INIT_MOTION_PIN(3);
233-
#endif
234-
#if NUM_MOTION_SENSORS >= 4
235-
INIT_MOTION_PIN(4);
236-
#endif
237-
#if NUM_MOTION_SENSORS >= 5
238-
INIT_MOTION_PIN(5);
239-
#endif
240-
#if NUM_MOTION_SENSORS >= 6
241-
INIT_MOTION_PIN(6);
242-
#endif
243-
#if NUM_MOTION_SENSORS >= 7
244-
INIT_MOTION_PIN(7);
245-
#endif
246-
#if NUM_MOTION_SENSORS >= 8
247-
INIT_MOTION_PIN(8);
248-
#endif
200+
REPEAT_1(NUM_MOTION_SENSORS, INIT_MOTION_PIN);
201+
#undef INIT_MOTION_PIN
249202
#endif
250203
#undef _INIT_RUNOUT_PIN
251-
#undef INIT_MOTION_PIN
252204
}
253205

254206
// Return a bitmask of runout pin states
@@ -260,35 +212,12 @@ class FilamentSensorBase {
260212

261213
// Return a bitmask of runout flag states (1 bits always indicates runout)
262214
static uint8_t poll_runout_states() {
263-
return poll_runout_pins() ^ uint8_t(0
264-
#if NUM_RUNOUT_SENSORS >= 1
265-
| (FIL_RUNOUT1_STATE ? 0 : _BV(1 - 1))
266-
#endif
267-
#if NUM_RUNOUT_SENSORS >= 2
268-
| (FIL_RUNOUT2_STATE ? 0 : _BV(2 - 1))
269-
#endif
270-
#if NUM_RUNOUT_SENSORS >= 3
271-
| (FIL_RUNOUT3_STATE ? 0 : _BV(3 - 1))
272-
#endif
273-
#if NUM_RUNOUT_SENSORS >= 4
274-
| (FIL_RUNOUT4_STATE ? 0 : _BV(4 - 1))
275-
#endif
276-
#if NUM_RUNOUT_SENSORS >= 5
277-
| (FIL_RUNOUT5_STATE ? 0 : _BV(5 - 1))
278-
#endif
279-
#if NUM_RUNOUT_SENSORS >= 6
280-
| (FIL_RUNOUT6_STATE ? 0 : _BV(6 - 1))
281-
#endif
282-
#if NUM_RUNOUT_SENSORS >= 7
283-
| (FIL_RUNOUT7_STATE ? 0 : _BV(7 - 1))
284-
#endif
285-
#if NUM_RUNOUT_SENSORS >= 8
286-
| (FIL_RUNOUT8_STATE ? 0 : _BV(8 - 1))
287-
#endif
288-
);
215+
#define _OR_RUNOUT(N) | (FIL_RUNOUT##N##_STATE ? 0 : _BV(N - 1))
216+
return poll_runout_pins() ^ uint8_t(0 REPEAT_1(NUM_RUNOUT_SENSORS, _OR_RUNOUT));
217+
#undef _OR_RUNOUT
289218
}
290219

291-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
220+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
292221
// Return a bitmask of motion pin states
293222
static uint8_t poll_motion_pins() {
294223
#define _OR_MOTION(N) | (READ(FIL_MOTION##N##_PIN) ? _BV((N) - 1) : 0)
@@ -298,37 +227,14 @@ class FilamentSensorBase {
298227

299228
// Return a bitmask of motion flag states (1 bits always indicates runout)
300229
static uint8_t poll_motion_states() {
301-
return poll_motion_pins() ^ uint8_t(0
302-
#if NUM_MOTION_SENSORS >= 1
303-
| (FIL_MOTION1_STATE ? 0 : _BV(1 - 1))
304-
#endif
305-
#if NUM_MOTION_SENSORS >= 2
306-
| (FIL_MOTION2_STATE ? 0 : _BV(2 - 1))
307-
#endif
308-
#if NUM_MOTION_SENSORS >= 3
309-
| (FIL_MOTION3_STATE ? 0 : _BV(3 - 1))
310-
#endif
311-
#if NUM_MOTION_SENSORS >= 4
312-
| (FIL_MOTION4_STATE ? 0 : _BV(4 - 1))
313-
#endif
314-
#if NUM_MOTION_SENSORS >= 5
315-
| (FIL_MOTION5_STATE ? 0 : _BV(5 - 1))
316-
#endif
317-
#if NUM_MOTION_SENSORS >= 6
318-
| (FIL_MOTION6_STATE ? 0 : _BV(6 - 1))
319-
#endif
320-
#if NUM_MOTION_SENSORS >= 7
321-
| (FIL_MOTION7_STATE ? 0 : _BV(7 - 1))
322-
#endif
323-
#if NUM_MOTION_SENSORS >= 8
324-
| (FIL_MOTION8_STATE ? 0 : _BV(8 - 1))
325-
#endif
326-
);
230+
#define _OR_MOTION(N) | (FIL_MOTION##N##_STATE ? 0 : _BV(N - 1))
231+
return poll_motion_pins() ^ uint8_t(0 REPEAT_1(NUM_MOTION_SENSORS, _OR_MOTION));
232+
#undef _OR_MOTION
327233
}
328234
#endif
329235
};
330236

331-
#if ENABLED(FILAMENT_HAS_MOTION)
237+
#if HAS_FILAMENT_MOTION
332238

333239
/**
334240
* This sensor uses a magnetic encoder disc and a Hall effect
@@ -342,14 +248,14 @@ class FilamentSensorBase {
342248

343249
static void poll_motion_sensor() {
344250
static uint8_t old_state;
345-
const uint8_t new_state = TERN(FILAMENT_BOTH_SWITCH_AND_MOTION, poll_motion_pins, poll_runout_pins)(),
251+
const uint8_t new_state = TERN(FILAMENT_SWITCH_AND_MOTION, poll_motion_pins, poll_runout_pins)(),
346252
change = old_state ^ new_state;
347253
old_state = new_state;
348254

349255
#if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
350256
if (change) {
351257
SERIAL_ECHOPGM("Motion detected:");
352-
LOOP_L_N(e, TERN(FILAMENT_BOTH_SWITCH_AND_MOTION, NUM_MOTION_SENSORS, NUM_RUNOUT_SENSORS))
258+
LOOP_L_N(e, TERN(FILAMENT_SWITCH_AND_MOTION, NUM_MOTION_SENSORS, NUM_RUNOUT_SENSORS))
353259
if (TEST(change, e)) SERIAL_CHAR(' ', '0' + e);
354260
SERIAL_EOL();
355261
}
@@ -363,7 +269,7 @@ class FilamentSensorBase {
363269
// If the sensor wheel has moved since the last call to
364270
// this method reset the runout counter for the extruder.
365271
if (TEST(motion_detected, b->extruder))
366-
TERN(FILAMENT_BOTH_SWITCH_AND_MOTION, filament_motion_present, filament_present)(b->extruder);
272+
TERN(FILAMENT_SWITCH_AND_MOTION, filament_motion_present, filament_present)(b->extruder);
367273

368274
// Clear motion triggers for next block
369275
motion_detected = 0;
@@ -372,9 +278,9 @@ class FilamentSensorBase {
372278
static void run() { poll_motion_sensor(); }
373279
};
374280

375-
#endif // FILAMENT_HAS_MOTION
281+
#endif // HAS_FILAMENT_MOTION
376282

377-
#if ENABLED(FILAMENT_HAS_SWITCH)
283+
#if HAS_FILAMENT_SWITCH
378284

379285
/**
380286
* This is a simple endstop switch in the path of the filament.
@@ -412,26 +318,26 @@ class FilamentSensorBase {
412318
}
413319
};
414320

415-
#endif // FILAMENT_HAS_SWITCH
321+
#endif // HAS_FILAMENT_SWITCH
416322

417323
/**
418324
* This is a simple endstop switch in the path of the filament.
419325
* It can detect filament runout, but not stripouts or jams.
420326
*/
421327
class FilamentSensor : public FilamentSensorBase {
422328
private:
423-
TERN_(FILAMENT_HAS_MOTION, static FilamentSensorEncoder encoder_sensor);
424-
TERN_(FILAMENT_HAS_SWITCH, static FilamentSensorSwitch switch_sensor);
329+
TERN_(HAS_FILAMENT_MOTION, static FilamentSensorEncoder encoder_sensor);
330+
TERN_(HAS_FILAMENT_SWITCH, static FilamentSensorSwitch switch_sensor);
425331

426332
public:
427333
static void block_completed(const block_t * const b) {
428-
TERN_(FILAMENT_HAS_MOTION, encoder_sensor.block_completed(b));
429-
TERN_(FILAMENT_HAS_SWITCH, switch_sensor.block_completed(b));
334+
TERN_(HAS_FILAMENT_MOTION, encoder_sensor.block_completed(b));
335+
TERN_(HAS_FILAMENT_SWITCH, switch_sensor.block_completed(b));
430336
}
431337

432338
static void run() {
433-
TERN_(FILAMENT_HAS_MOTION, encoder_sensor.run());
434-
TERN_(FILAMENT_HAS_SWITCH, switch_sensor.run());
339+
TERN_(HAS_FILAMENT_MOTION, encoder_sensor.run());
340+
TERN_(HAS_FILAMENT_SWITCH, switch_sensor.run());
435341
}
436342
};
437343

@@ -442,9 +348,9 @@ class FilamentSensorBase {
442348

443349
typedef struct {
444350
float runout[NUM_RUNOUT_SENSORS];
445-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
446-
float motion[NUM_MOTION_SENSORS];
447-
#endif
351+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
352+
float motion[NUM_MOTION_SENSORS];
353+
#endif
448354
} countdown_t;
449355

450356
// RunoutResponseDelayed triggers a runout event only if the length
@@ -459,9 +365,9 @@ class FilamentSensorBase {
459365

460366
static void reset() {
461367
LOOP_L_N(i, NUM_RUNOUT_SENSORS) filament_present(i);
462-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
463-
LOOP_L_N(i, NUM_MOTION_SENSORS) filament_motion_present(i);
464-
#endif
368+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
369+
LOOP_L_N(i, NUM_MOTION_SENSORS) filament_motion_present(i);
370+
#endif
465371
}
466372

467373
static void run() {
@@ -472,10 +378,10 @@ class FilamentSensorBase {
472378
t = millis() + 1000UL;
473379
LOOP_L_N(i, NUM_RUNOUT_SENSORS)
474380
SERIAL_ECHOF(i ? F(", ") : F("Runout remaining mm: "), mm_countdown.runout[i]);
475-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
476-
LOOP_L_N(i, NUM_MOTION_SENSORS)
477-
SERIAL_ECHOF(i ? F(", ") : F("Motion remaining mm: "), mm_countdown.motion[i]);
478-
#endif
381+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
382+
LOOP_L_N(i, NUM_MOTION_SENSORS)
383+
SERIAL_ECHOF(i ? F(", ") : F("Motion remaining mm: "), mm_countdown.motion[i]);
384+
#endif
479385
SERIAL_EOL();
480386
}
481387
#endif
@@ -484,21 +390,21 @@ class FilamentSensorBase {
484390
static uint8_t has_run_out() {
485391
uint8_t runout_flags = 0;
486392
LOOP_L_N(i, NUM_RUNOUT_SENSORS) if (mm_countdown.runout[i] < 0) SBI(runout_flags, i);
487-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
488-
LOOP_L_N(i, NUM_MOTION_SENSORS) if (mm_countdown.motion[i] < 0) SBI(runout_flags, i);
489-
#endif
393+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
394+
LOOP_L_N(i, NUM_MOTION_SENSORS) if (mm_countdown.motion[i] < 0) SBI(runout_flags, i);
395+
#endif
490396
return runout_flags;
491397
}
492398

493399
static void filament_present(const uint8_t extruder) {
494400
mm_countdown.runout[extruder] = runout_distance_mm;
495401
}
496402

497-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
498-
static void filament_motion_present(const uint8_t extruder) {
499-
mm_countdown.motion[extruder] = runout_distance_mm;
500-
}
501-
#endif
403+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
404+
static void filament_motion_present(const uint8_t extruder) {
405+
mm_countdown.motion[extruder] = runout_distance_mm;
406+
}
407+
#endif
502408

503409
static void block_completed(const block_t * const b) {
504410
if (b->steps.x || b->steps.y || b->steps.z || did_pause_print) { // Allow pause purge move to re-trigger runout state
@@ -508,10 +414,10 @@ class FilamentSensorBase {
508414
float mm = (TEST(b->direction_bits, E_AXIS) ? -steps : steps) * planner.mm_per_step[E_AXIS_N(e)];
509415
if (e < NUM_RUNOUT_SENSORS)
510416
mm_countdown.runout[e] -= mm;
511-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
512-
if (e < NUM_MOTION_SENSORS)
513-
mm_countdown.motion[e] -= mm;
514-
#endif
417+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
418+
if (e < NUM_MOTION_SENSORS)
419+
mm_countdown.motion[e] -= mm;
420+
#endif
515421
}
516422
}
517423
};

‎Marlin/src/inc/Conditionals_LCD.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,7 @@
12041204
#endif
12051205
#endif // FILAMENT_RUNOUT_SENSOR
12061206

1207-
#if ENABLED(FILAMENT_BOTH_SWITCH_AND_MOTION)
1207+
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
12081208
#if NUM_MOTION_SENSORS >= 1
12091209
#ifndef FIL_MOTION1_STATE
12101210
#define FIL_MOTION1_STATE FIL_RUNOUT_STATE
@@ -1293,7 +1293,7 @@
12931293
#define FILMOTION8_PULLDOWN FIL_RUNOUT_PULLDOWN
12941294
#endif
12951295
#endif
1296-
#endif // FILAMENT_BOTH_SWITCH_AND_MOTION
1296+
#endif // FILAMENT_SWITCH_AND_MOTION
12971297

12981298
// Homing to Min or Max
12991299
#if X_HOME_DIR > 0

‎buildroot/tests/rambo

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ opt_set MOTHERBOARD BOARD_RAMBO \
1515
TEMP_SENSOR_PROBE 1 TEMP_PROBE_PIN 12 \
1616
TEMP_SENSOR_CHAMBER 3 TEMP_CHAMBER_PIN 3 HEATER_CHAMBER_PIN 45 \
1717
GRID_MAX_POINTS_X 16 AUTO_POWER_E_TEMP 80 \
18-
FANMUX0_PIN 53
18+
FANMUX0_PIN 53 FIL_MOTION1_PIN 45
1919
opt_disable Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN USE_WATCHDOG
2020
opt_enable USE_ZMAX_PLUG REPRAP_DISCOUNT_SMART_CONTROLLER LCD_PROGRESS_BAR LCD_PROGRESS_BAR_TEST \
2121
FIX_MOUNTED_PROBE CODEPENDENT_XY_HOMING PIDTEMPBED PTC_PROBE PTC_BED \
@@ -25,7 +25,7 @@ opt_enable USE_ZMAX_PLUG REPRAP_DISCOUNT_SMART_CONTROLLER LCD_PROGRESS_BAR LCD_P
2525
NEOPIXEL_LED NEOPIXEL_PIN CASE_LIGHT_ENABLE CASE_LIGHT_USE_NEOPIXEL CASE_LIGHT_MENU \
2626
PID_PARAMS_PER_HOTEND PID_AUTOTUNE_MENU PID_EDIT_MENU PID_EXTRUSION_SCALING LCD_SHOW_E_TOTAL \
2727
PRINTCOUNTER SERVICE_NAME_1 SERVICE_INTERVAL_1 LCD_BED_TRAMMING BED_TRAMMING_INCLUDE_CENTER \
28-
NOZZLE_PARK_FEATURE FILAMENT_RUNOUT_SENSOR FILAMENT_RUNOUT_DISTANCE_MM \
28+
NOZZLE_PARK_FEATURE FILAMENT_RUNOUT_SENSOR FILAMENT_RUNOUT_DISTANCE_MM FILAMENT_MOTION_SENSOR FILAMENT_SWITCH_AND_MOTION \
2929
ADVANCED_PAUSE_FEATURE FILAMENT_LOAD_UNLOAD_GCODES FILAMENT_UNLOAD_ALL_EXTRUDERS \
3030
PASSWORD_FEATURE PASSWORD_ON_STARTUP PASSWORD_ON_SD_PRINT_MENU PASSWORD_AFTER_SD_PRINT_END PASSWORD_AFTER_SD_PRINT_ABORT \
3131
AUTO_BED_LEVELING_BILINEAR Z_MIN_PROBE_REPEATABILITY_TEST DISTINCT_E_FACTORS \

0 commit comments

Comments
 (0)
Please sign in to comment.