FILAMENT_SWITCH_AND_MOTION (#25548)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
This commit is contained in:
BIGTREETECH 2023-03-22 15:46:03 +08:00 committed by GitHub
parent 8b1830c487
commit 2ecd4cca1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 243 additions and 69 deletions

View file

@ -1903,6 +1903,47 @@
// as the filament moves. (Be sure to set FILAMENT_RUNOUT_DISTANCE_MM
// large enough to avoid false positives.)
//#define FILAMENT_MOTION_SENSOR
#if ENABLED(FILAMENT_MOTION_SENSOR)
//#define FILAMENT_SWITCH_AND_MOTION
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
#define NUM_MOTION_SENSORS 1 // Number of sensors, up to one per extruder. Define a FIL_MOTION#_PIN for each.
//#define FIL_MOTION1_PIN -1
// Override individually if the motion sensors vary
//#define FIL_MOTION1_STATE LOW
//#define FIL_MOTION1_PULLUP
//#define FIL_MOTION1_PULLDOWN
//#define FIL_MOTION2_STATE LOW
//#define FIL_MOTION2_PULLUP
//#define FIL_MOTION2_PULLDOWN
//#define FIL_MOTION3_STATE LOW
//#define FIL_MOTION3_PULLUP
//#define FIL_MOTION3_PULLDOWN
//#define FIL_MOTION4_STATE LOW
//#define FIL_MOTION4_PULLUP
//#define FIL_MOTION4_PULLDOWN
//#define FIL_MOTION5_STATE LOW
//#define FIL_MOTION5_PULLUP
//#define FIL_MOTION5_PULLDOWN
//#define FIL_MOTION6_STATE LOW
//#define FIL_MOTION6_PULLUP
//#define FIL_MOTION6_PULLDOWN
//#define FIL_MOTION7_STATE LOW
//#define FIL_MOTION7_PULLUP
//#define FIL_MOTION7_PULLDOWN
//#define FIL_MOTION8_STATE LOW
//#define FIL_MOTION8_PULLUP
//#define FIL_MOTION8_PULLDOWN
#endif
#endif
#endif
#endif

View file

@ -47,7 +47,7 @@ bool FilamentMonitorBase::enabled = true,
#if HAS_FILAMENT_RUNOUT_DISTANCE
float RunoutResponseDelayed::runout_distance_mm = FILAMENT_RUNOUT_DISTANCE_MM;
volatile float RunoutResponseDelayed::runout_mm_countdown[NUM_RUNOUT_SENSORS];
volatile countdown_t RunoutResponseDelayed::mm_countdown;
#if ENABLED(FILAMENT_MOTION_SENSOR)
uint8_t FilamentSensorEncoder::motion_detected;
#endif

View file

@ -43,12 +43,18 @@
#define FILAMENT_RUNOUT_THRESHOLD 5
#endif
#if ENABLED(FILAMENT_MOTION_SENSOR)
#define HAS_FILAMENT_MOTION 1
#endif
#if DISABLED(FILAMENT_MOTION_SENSOR) || ENABLED(FILAMENT_SWITCH_AND_MOTION)
#define HAS_FILAMENT_SWITCH 1
#endif
void event_filament_runout(const uint8_t extruder);
template<class RESPONSE_T, class SENSOR_T>
class TFilamentMonitor;
class FilamentSensorEncoder;
class FilamentSensorSwitch;
class FilamentSensor;
class RunoutResponseDelayed;
class RunoutResponseDebounced;
@ -56,7 +62,7 @@ class RunoutResponseDebounced;
typedef TFilamentMonitor<
TERN(HAS_FILAMENT_RUNOUT_DISTANCE, RunoutResponseDelayed, RunoutResponseDebounced),
TERN(FILAMENT_MOTION_SENSOR, FilamentSensorEncoder, FilamentSensorSwitch)
FilamentSensor
> FilamentMonitor;
extern FilamentMonitor runout;
@ -98,6 +104,11 @@ class TFilamentMonitor : public FilamentMonitorBase {
static void filament_present(const uint8_t extruder) {
response.filament_present(extruder);
}
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
static void filament_motion_present(const uint8_t extruder) {
response.filament_motion_present(extruder);
}
#endif
#if HAS_FILAMENT_RUNOUT_DISTANCE
static float& runout_distance() { return response.runout_distance_mm; }
@ -171,37 +182,25 @@ class FilamentSensorBase {
static void filament_present(const uint8_t extruder) {
runout.filament_present(extruder); // ...which calls response.filament_present(extruder)
}
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
static void filament_motion_present(const uint8_t extruder) {
runout.filament_motion_present(extruder); // ...which calls response.filament_motion_present(extruder)
}
#endif
public:
static void setup() {
#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)
#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);
#define INIT_RUNOUT_PIN(N) _INIT_RUNOUT_PIN(FIL_RUNOUT##N##_PIN, FIL_RUNOUT##N##_STATE, FIL_RUNOUT##N##_PULLUP, FIL_RUNOUT##N##_PULLDOWN)
#if NUM_RUNOUT_SENSORS >= 1
INIT_RUNOUT_PIN(1);
#endif
#if NUM_RUNOUT_SENSORS >= 2
INIT_RUNOUT_PIN(2);
#endif
#if NUM_RUNOUT_SENSORS >= 3
INIT_RUNOUT_PIN(3);
#endif
#if NUM_RUNOUT_SENSORS >= 4
INIT_RUNOUT_PIN(4);
#endif
#if NUM_RUNOUT_SENSORS >= 5
INIT_RUNOUT_PIN(5);
#endif
#if NUM_RUNOUT_SENSORS >= 6
INIT_RUNOUT_PIN(6);
#endif
#if NUM_RUNOUT_SENSORS >= 7
INIT_RUNOUT_PIN(7);
#endif
#if NUM_RUNOUT_SENSORS >= 8
INIT_RUNOUT_PIN(8);
REPEAT_1(NUM_RUNOUT_SENSORS, INIT_RUNOUT_PIN);
#undef INIT_RUNOUT_PIN
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
#define INIT_MOTION_PIN(N) _INIT_RUNOUT_PIN(FIL_MOTION##N##_PIN, FIL_MOTION##N##_STATE, FIL_MOTION##N##_PULLUP, FIL_MOTION##N##_PULLDOWN)
REPEAT_1(NUM_MOTION_SENSORS, INIT_MOTION_PIN);
#undef INIT_MOTION_PIN
#endif
#undef _INIT_RUNOUT_PIN
#undef INIT_RUNOUT_PIN
}
// Return a bitmask of runout pin states
@ -213,36 +212,29 @@ class FilamentSensorBase {
// Return a bitmask of runout flag states (1 bits always indicates runout)
static uint8_t poll_runout_states() {
return poll_runout_pins() ^ uint8_t(0
#if NUM_RUNOUT_SENSORS >= 1
| (FIL_RUNOUT1_STATE ? 0 : _BV(1 - 1))
#endif
#if NUM_RUNOUT_SENSORS >= 2
| (FIL_RUNOUT2_STATE ? 0 : _BV(2 - 1))
#endif
#if NUM_RUNOUT_SENSORS >= 3
| (FIL_RUNOUT3_STATE ? 0 : _BV(3 - 1))
#endif
#if NUM_RUNOUT_SENSORS >= 4
| (FIL_RUNOUT4_STATE ? 0 : _BV(4 - 1))
#endif
#if NUM_RUNOUT_SENSORS >= 5
| (FIL_RUNOUT5_STATE ? 0 : _BV(5 - 1))
#endif
#if NUM_RUNOUT_SENSORS >= 6
| (FIL_RUNOUT6_STATE ? 0 : _BV(6 - 1))
#endif
#if NUM_RUNOUT_SENSORS >= 7
| (FIL_RUNOUT7_STATE ? 0 : _BV(7 - 1))
#endif
#if NUM_RUNOUT_SENSORS >= 8
| (FIL_RUNOUT8_STATE ? 0 : _BV(8 - 1))
#endif
);
#define _OR_RUNOUT(N) | (FIL_RUNOUT##N##_STATE ? 0 : _BV(N - 1))
return poll_runout_pins() ^ uint8_t(0 REPEAT_1(NUM_RUNOUT_SENSORS, _OR_RUNOUT));
#undef _OR_RUNOUT
}
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
// Return a bitmask of motion pin states
static uint8_t poll_motion_pins() {
#define _OR_MOTION(N) | (READ(FIL_MOTION##N##_PIN) ? _BV((N) - 1) : 0)
return (0 REPEAT_1(NUM_MOTION_SENSORS, _OR_MOTION));
#undef _OR_MOTION
}
// Return a bitmask of motion flag states (1 bits always indicates runout)
static uint8_t poll_motion_states() {
#define _OR_MOTION(N) | (FIL_MOTION##N##_STATE ? 0 : _BV(N - 1))
return poll_motion_pins() ^ uint8_t(0 REPEAT_1(NUM_MOTION_SENSORS, _OR_MOTION));
#undef _OR_MOTION
}
#endif
};
#if ENABLED(FILAMENT_MOTION_SENSOR)
#if HAS_FILAMENT_MOTION
/**
* This sensor uses a magnetic encoder disc and a Hall effect
@ -256,14 +248,14 @@ class FilamentSensorBase {
static void poll_motion_sensor() {
static uint8_t old_state;
const uint8_t new_state = poll_runout_pins(),
const uint8_t new_state = TERN(FILAMENT_SWITCH_AND_MOTION, poll_motion_pins, poll_runout_pins)(),
change = old_state ^ new_state;
old_state = new_state;
#if ENABLED(FILAMENT_RUNOUT_SENSOR_DEBUG)
if (change) {
SERIAL_ECHOPGM("Motion detected:");
LOOP_L_N(e, NUM_RUNOUT_SENSORS)
LOOP_L_N(e, TERN(FILAMENT_SWITCH_AND_MOTION, NUM_MOTION_SENSORS, NUM_RUNOUT_SENSORS))
if (TEST(change, e)) SERIAL_CHAR(' ', '0' + e);
SERIAL_EOL();
}
@ -277,7 +269,7 @@ class FilamentSensorBase {
// If the sensor wheel has moved since the last call to
// this method reset the runout counter for the extruder.
if (TEST(motion_detected, b->extruder))
filament_present(b->extruder);
TERN(FILAMENT_SWITCH_AND_MOTION, filament_motion_present, filament_present)(b->extruder);
// Clear motion triggers for next block
motion_detected = 0;
@ -286,7 +278,9 @@ class FilamentSensorBase {
static void run() { poll_motion_sensor(); }
};
#else
#endif // HAS_FILAMENT_MOTION
#if HAS_FILAMENT_SWITCH
/**
* This is a simple endstop switch in the path of the filament.
@ -324,25 +318,56 @@ class FilamentSensorBase {
}
};
#endif // HAS_FILAMENT_SWITCH
/**
* This is a simple endstop switch in the path of the filament.
* It can detect filament runout, but not stripouts or jams.
*/
class FilamentSensor : public FilamentSensorBase {
private:
TERN_(HAS_FILAMENT_MOTION, static FilamentSensorEncoder encoder_sensor);
TERN_(HAS_FILAMENT_SWITCH, static FilamentSensorSwitch switch_sensor);
public:
static void block_completed(const block_t * const b) {
TERN_(HAS_FILAMENT_MOTION, encoder_sensor.block_completed(b));
TERN_(HAS_FILAMENT_SWITCH, switch_sensor.block_completed(b));
}
static void run() {
TERN_(HAS_FILAMENT_MOTION, encoder_sensor.run());
TERN_(HAS_FILAMENT_SWITCH, switch_sensor.run());
}
};
#endif // !FILAMENT_MOTION_SENSOR
/********************************* RESPONSE TYPE *********************************/
#if HAS_FILAMENT_RUNOUT_DISTANCE
typedef struct {
float runout[NUM_RUNOUT_SENSORS];
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
float motion[NUM_MOTION_SENSORS];
#endif
} countdown_t;
// RunoutResponseDelayed triggers a runout event only if the length
// of filament specified by FILAMENT_RUNOUT_DISTANCE_MM has been fed
// during a runout condition.
class RunoutResponseDelayed {
private:
static volatile float runout_mm_countdown[NUM_RUNOUT_SENSORS];
static volatile countdown_t mm_countdown;
public:
static float runout_distance_mm;
static void reset() {
LOOP_L_N(i, NUM_RUNOUT_SENSORS) filament_present(i);
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
LOOP_L_N(i, NUM_MOTION_SENSORS) filament_motion_present(i);
#endif
}
static void run() {
@ -352,7 +377,11 @@ class FilamentSensorBase {
if (ELAPSED(ms, t)) {
t = millis() + 1000UL;
LOOP_L_N(i, NUM_RUNOUT_SENSORS)
SERIAL_ECHOF(i ? F(", ") : F("Remaining mm: "), runout_mm_countdown[i]);
SERIAL_ECHOF(i ? F(", ") : F("Runout remaining mm: "), mm_countdown.runout[i]);
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
LOOP_L_N(i, NUM_MOTION_SENSORS)
SERIAL_ECHOF(i ? F(", ") : F("Motion remaining mm: "), mm_countdown.motion[i]);
#endif
SERIAL_EOL();
}
#endif
@ -360,20 +389,33 @@ class FilamentSensorBase {
static uint8_t has_run_out() {
uint8_t runout_flags = 0;
LOOP_L_N(i, NUM_RUNOUT_SENSORS) if (runout_mm_countdown[i] < 0) SBI(runout_flags, i);
LOOP_L_N(i, NUM_RUNOUT_SENSORS) if (mm_countdown.runout[i] < 0) SBI(runout_flags, i);
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
LOOP_L_N(i, NUM_MOTION_SENSORS) if (mm_countdown.motion[i] < 0) SBI(runout_flags, i);
#endif
return runout_flags;
}
static void filament_present(const uint8_t extruder) {
runout_mm_countdown[extruder] = runout_distance_mm;
mm_countdown.runout[extruder] = runout_distance_mm;
}
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
static void filament_motion_present(const uint8_t extruder) {
mm_countdown.motion[extruder] = runout_distance_mm;
}
#endif
static void block_completed(const block_t * const b) {
if (b->steps.x || b->steps.y || b->steps.z || did_pause_print) { // Allow pause purge move to re-trigger runout state
// Only trigger on extrusion with XYZ movement to allow filament change and retract/recover.
const uint8_t e = b->extruder;
const int32_t steps = b->steps.e;
runout_mm_countdown[e] -= (TEST(b->direction_bits, E_AXIS) ? -steps : steps) * planner.mm_per_step[E_AXIS_N(e)];
const float mm = (TEST(b->direction_bits, E_AXIS) ? -steps : steps) * planner.mm_per_step[E_AXIS_N(e)];
if (e < NUM_RUNOUT_SENSORS) mm_countdown.runout[e] -= mm;
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
if (e < NUM_MOTION_SENSORS) mm_countdown.motion[e] -= mm;
#endif
}
}
};

View file

@ -1212,6 +1212,97 @@
#endif
#endif // FILAMENT_RUNOUT_SENSOR
#if ENABLED(FILAMENT_SWITCH_AND_MOTION)
#if NUM_MOTION_SENSORS >= 1
#ifndef FIL_MOTION1_STATE
#define FIL_MOTION1_STATE FIL_RUNOUT_STATE
#endif
#ifndef FIL_MOTION1_PULLUP
#define FIL_MOTION1_PULLUP FIL_RUNOUT_PULLUP
#endif
#ifndef FIL_MOTION1_PULLDOWN
#define FIL_MOTION1_PULLDOWN FIL_RUNOUT_PULLDOWN
#endif
#endif
#if NUM_MOTION_SENSORS >= 2
#ifndef FIL_MOTION2_STATE
#define FIL_MOTION2_STATE FIL_RUNOUT_STATE
#endif
#ifndef FIL_MOTION2_PULLUP
#define FIL_MOTION2_PULLUP FIL_RUNOUT_PULLUP
#endif
#ifndef FIL_MOTION2_PULLDOWN
#define FIL_MOTION2_PULLDOWN FIL_RUNOUT_PULLDOWN
#endif
#endif
#if NUM_MOTION_SENSORS >= 3
#ifndef FIL_MOTION3_STATE
#define FIL_MOTION3_STATE FIL_RUNOUT_STATE
#endif
#ifndef FIL_MOTION3_PULLUP
#define FIL_MOTION3_PULLUP FIL_RUNOUT_PULLUP
#endif
#ifndef FIL_MOTION3_PULLDOWN
#define FIL_MOTION3_PULLDOWN FIL_RUNOUT_PULLDOWN
#endif
#endif
#if NUM_MOTION_SENSORS >= 4
#ifndef FIL_MOTION4_STATE
#define FIL_MOTION4_STATE FIL_RUNOUT_STATE
#endif
#ifndef FIL_MOTION4_PULLUP
#define FIL_MOTION4_PULLUP FIL_RUNOUT_PULLUP
#endif
#ifndef FIL_MOTION4_PULLDOWN
#define FIL_MOTION4_PULLDOWN FIL_RUNOUT_PULLDOWN
#endif
#endif
#if NUM_MOTION_SENSORS >= 5
#ifndef FIL_MOTION5_STATE
#define FIL_MOTION5_STATE FIL_RUNOUT_STATE
#endif
#ifndef FIL_MOTION5_PULLUP
#define FIL_MOTION5_PULLUP FIL_RUNOUT_PULLUP
#endif
#ifndef FIL_MOTION5_PULLDOWN
#define FIL_MOTION5_PULLDOWN FIL_RUNOUT_PULLDOWN
#endif
#endif
#if NUM_MOTION_SENSORS >= 6
#ifndef FIL_MOTION6_STATE
#define FIL_MOTION6_STATE FIL_RUNOUT_STATE
#endif
#ifndef FIL_MOTION6_PULLUP
#define FIL_MOTION6_PULLUP FIL_RUNOUT_PULLUP
#endif
#ifndef FIL_MOTION6_PULLDOWN
#define FIL_MOTION6_PULLDOWN FIL_RUNOUT_PULLDOWN
#endif
#endif
#if NUM_MOTION_SENSORS >= 7
#ifndef FIL_MOTION7_STATE
#define FIL_MOTION7_STATE FIL_RUNOUT_STATE
#endif
#ifndef FIL_MOTION7_PULLUP
#define FIL_MOTION7_PULLUP FIL_RUNOUT_PULLUP
#endif
#ifndef FIL_MOTION7_PULLDOWN
#define FIL_MOTION7_PULLDOWN FIL_RUNOUT_PULLDOWN
#endif
#endif
#if NUM_MOTION_SENSORS >= 8
#ifndef FIL_MOTION8_STATE
#define FIL_MOTION8_STATE FIL_RUNOUT_STATE
#endif
#ifndef FIL_MOTION8_PULLUP
#define FIL_MOTION8_PULLUP FIL_RUNOUT_PULLUP
#endif
#ifndef FIL_MOTION8_PULLDOWN
#define FILMOTION8_PULLDOWN FIL_RUNOUT_PULLDOWN
#endif
#endif
#endif // FILAMENT_SWITCH_AND_MOTION
// Homing to Min or Max
#if X_HOME_DIR > 0
#define X_HOME_TO_MAX 1

View file

@ -2664,7 +2664,7 @@
#endif
/**
* Up to 3 PWM fans
* Up to 8 PWM fans
*/
#ifndef FAN_INVERTING
#define FAN_INVERTING false

View file

@ -15,7 +15,7 @@ opt_set MOTHERBOARD BOARD_RAMBO \
TEMP_SENSOR_PROBE 1 TEMP_PROBE_PIN 12 \
TEMP_SENSOR_CHAMBER 3 TEMP_CHAMBER_PIN 3 HEATER_CHAMBER_PIN 45 \
GRID_MAX_POINTS_X 16 AUTO_POWER_E_TEMP 80 \
FANMUX0_PIN 53
FANMUX0_PIN 53 FIL_MOTION1_PIN 45
opt_disable Z_MIN_PROBE_USES_Z_MIN_ENDSTOP_PIN USE_WATCHDOG
opt_enable USE_ZMAX_PLUG REPRAP_DISCOUNT_SMART_CONTROLLER LCD_PROGRESS_BAR LCD_PROGRESS_BAR_TEST \
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
NEOPIXEL_LED NEOPIXEL_PIN CASE_LIGHT_ENABLE CASE_LIGHT_USE_NEOPIXEL CASE_LIGHT_MENU \
PID_PARAMS_PER_HOTEND PID_AUTOTUNE_MENU PID_EDIT_MENU PID_EXTRUSION_SCALING LCD_SHOW_E_TOTAL \
PRINTCOUNTER SERVICE_NAME_1 SERVICE_INTERVAL_1 LCD_BED_TRAMMING BED_TRAMMING_INCLUDE_CENTER \
NOZZLE_PARK_FEATURE FILAMENT_RUNOUT_SENSOR FILAMENT_RUNOUT_DISTANCE_MM \
NOZZLE_PARK_FEATURE FILAMENT_RUNOUT_SENSOR FILAMENT_RUNOUT_DISTANCE_MM FILAMENT_MOTION_SENSOR FILAMENT_SWITCH_AND_MOTION \
ADVANCED_PAUSE_FEATURE FILAMENT_LOAD_UNLOAD_GCODES FILAMENT_UNLOAD_ALL_EXTRUDERS \
PASSWORD_FEATURE PASSWORD_ON_STARTUP PASSWORD_ON_SD_PRINT_MENU PASSWORD_AFTER_SD_PRINT_END PASSWORD_AFTER_SD_PRINT_ABORT \
AUTO_BED_LEVELING_BILINEAR Z_MIN_PROBE_REPEATABILITY_TEST DISTINCT_E_FACTORS \