Rejigger Filament Runout class (#12428)

This commit is contained in:
Scott Lahteine 2018-11-14 11:45:57 -06:00 committed by GitHub
parent f4c128ecaa
commit edfd106bc5
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 153 additions and 118 deletions

View file

@ -30,23 +30,29 @@
#include "runout.h" #include "runout.h"
FilamentRunoutSensor runout; FilamentMonitor runout;
bool FilamentSensorBase::enabled = true, bool FilamentMonitorBase::enabled = true,
FilamentSensorBase::filament_ran_out; // = false FilamentMonitorBase::filament_ran_out; // = false
void FilamentSensorTypeBase::filament_present(const uint8_t extruder) { /**
runout.filament_present(extruder); * Called by FilamentSensorSwitch::run when filament is detected.
* Called by FilamentSensorEncoder::block_completed when motion is detected.
*/
void FilamentSensorBase::filament_present(const uint8_t extruder) {
runout.filament_present(extruder); // calls response.filament_present(extruder)
} }
uint8_t FilamentSensorTypeEncoder::motion_detected, #if ENABLED(FILAMENT_MOTION_SENSOR)
FilamentSensorTypeEncoder::old_state; // = 0 uint8_t FilamentSensorEncoder::motion_detected,
FilamentSensorEncoder::old_state; // = 0
#endif
#if FILAMENT_RUNOUT_DISTANCE_MM > 0 #if FILAMENT_RUNOUT_DISTANCE_MM > 0
float RunoutResponseDelayed::runout_distance_mm = FILAMENT_RUNOUT_DISTANCE_MM; float RunoutResponseDelayed::runout_distance_mm = FILAMENT_RUNOUT_DISTANCE_MM;
int32_t RunoutResponseDelayed::steps_since_detection[EXTRUDERS]; volatile float RunoutResponseDelayed::runout_mm_countdown[EXTRUDERS];
#else #else
uint8_t RunoutResponseDebounced::runout_count; // = 0 int8_t RunoutResponseDebounced::runout_count; // = 0
#endif #endif
#endif // FILAMENT_RUNOUT_SENSOR #endif // FILAMENT_RUNOUT_SENSOR

View file

@ -38,7 +38,7 @@
//#define FILAMENT_RUNOUT_SENSOR_DEBUG //#define FILAMENT_RUNOUT_SENSOR_DEBUG
class FilamentSensorBase { class FilamentMonitorBase {
public: public:
static bool enabled; static bool enabled;
@ -47,16 +47,17 @@ class FilamentSensorBase {
}; };
template<class RESPONSE_T, class SENSOR_T> template<class RESPONSE_T, class SENSOR_T>
class TFilamentSensor : public FilamentSensorBase { class TFilamentMonitor : public FilamentMonitorBase {
private: private:
typedef RESPONSE_T response_t; typedef RESPONSE_T response_t;
typedef SENSOR_T sensor_t; typedef SENSOR_T sensor_t;
static response_t response; static response_t response;
static sensor_t sensor; static sensor_t sensor;
public: public:
static void setup() { static inline void setup() {
sensor.setup(); sensor.setup();
reset();
} }
static inline void reset() { static inline void reset() {
@ -64,21 +65,34 @@ class TFilamentSensor : public FilamentSensorBase {
response.reset(); response.reset();
} }
// The sensor calls this method when filament is present // Call this method when filament is present,
// so the response can reset its counter.
static inline void filament_present(const uint8_t extruder) { static inline void filament_present(const uint8_t extruder) {
response.filament_present(extruder); response.filament_present(extruder);
} }
static inline void block_complete(const block_t *b) { // Handle a block completion. RunoutResponseDelayed uses this to
response.block_complete(b); // add up the length of filament moved while the filament is out.
sensor.block_complete(b); static inline void block_completed(const block_t* const b) {
if (enabled) {
response.block_completed(b);
sensor.block_completed(b);
}
} }
static void run() { // Give the response a chance to update its counter.
static inline void run() {
if (enabled && !filament_ran_out && (IS_SD_PRINTING() || print_job_timer.isRunning())) { if (enabled && !filament_ran_out && (IS_SD_PRINTING() || print_job_timer.isRunning())) {
#if FILAMENT_RUNOUT_DISTANCE_MM > 0
cli(); // Prevent RunoutResponseDelayed::block_completed from accumulating here
#endif
response.run(); response.run();
sensor.run(); sensor.run();
if (response.has_runout()) { const bool ran_out = response.has_run_out();
#if FILAMENT_RUNOUT_DISTANCE_MM > 0
sei();
#endif
if (ran_out) {
filament_ran_out = true; filament_ran_out = true;
#if ENABLED(EXTENSIBLE_UI) #if ENABLED(EXTENSIBLE_UI)
UI::onFilamentRunout(); UI::onFilamentRunout();
@ -92,7 +106,7 @@ class TFilamentSensor : public FilamentSensorBase {
/*************************** FILAMENT PRESENCE SENSORS ***************************/ /*************************** FILAMENT PRESENCE SENSORS ***************************/
class FilamentSensorTypeBase { class FilamentSensorBase {
protected: protected:
static void filament_present(const uint8_t extruder); static void filament_present(const uint8_t extruder);
@ -153,155 +167,170 @@ class FilamentSensorTypeBase {
} }
}; };
/** #if ENABLED(FILAMENT_MOTION_SENSOR)
* This sensor is a simple endstop
* switch in the path of the filament. It detects
* filament runout, but not stripouts or jams.
*/
class FilamentSensorTypeSwitch : public FilamentSensorTypeBase { /**
private: * This sensor uses a magnetic encoder disc and a Hall effect
static bool poll_runout_pin(const uint8_t extruder) { * sensor (or a slotted disc and optical sensor). The state
const uint8_t runout_bits = poll_runout_pins(); * will toggle between 0 and 1 on filament movement. It can detect
#if NUM_RUNOUT_SENSORS == 1 * filament runout and stripouts or jams.
return runout_bits; // A single sensor applying to all extruders */
#else class FilamentSensorEncoder : public FilamentSensorBase {
#if ENABLED(DUAL_X_CARRIAGE) private:
if (dual_x_carriage_mode == DXC_DUPLICATION_MODE || dual_x_carriage_mode == DXC_SCALED_DUPLICATION_MODE) static uint8_t motion_detected;
return runout_bits; // Any extruder
else static inline void poll_motion_sensor() {
#elif ENABLED(DUAL_NOZZLE_DUPLICATION_MODE) static uint8_t old_state;
if (extruder_duplication_enabled) const uint8_t new_state = poll_runout_pins(),
return runout_bits; // Any extruder change = old_state ^ new_state;
else old_state = new_state;
#ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
if (change) SERIAL_PROTOCOLLNPAIR("Motion detected: ", int(change));
#endif #endif
return TEST(runout_bits, extruder); // Specific extruder
#endif
}
public: motion_detected |= change;
static inline void block_complete(const block_t *b) {} }
static inline void run() { public:
if (!poll_runout_pin(active_extruder)) static inline void block_completed(const block_t* const b) {
filament_present(active_extruder); // 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);
// This filament sensor uses a magnetic encoder disc and a hall // Clear motion triggers for next block
// effect sensor (or a slitted disc and an optical sensor). The state motion_detected = 0;
// will toggle between 0 and 1 with filament movement. It can detect }
// filament runout and stripouts or jams.
class FilamentSensorTypeEncoder : public FilamentSensorTypeBase { static inline void run() { poll_motion_sensor(); }
private: };
static uint8_t motion_detected, old_state;
static void poll_motion_sensor() { #else
const uint8_t new_state = poll_runout_pins(),
change = old_state ^ new_state;
old_state = new_state;
#ifdef FILAMENT_RUNOUT_SENSOR_DEBUG /**
if (change) SERIAL_PROTOCOLLNPAIR("motion detected: ", change); * This is a simple endstop switch in the path of the filament.
#endif * It can detect filament runout, but not stripouts or jams.
*/
class FilamentSensorSwitch : public FilamentSensorBase {
private:
static bool poll_runout_pin(const uint8_t extruder) {
const uint8_t runout_bits = poll_runout_pins();
#if NUM_RUNOUT_SENSORS == 1
return runout_bits; // A single sensor applying to all extruders
#else
#if ENABLED(DUAL_X_CARRIAGE)
if (dual_x_carriage_mode == DXC_DUPLICATION_MODE || dual_x_carriage_mode == DXC_SCALED_DUPLICATION_MODE)
return runout_bits; // Any extruder
else
#elif ENABLED(DUAL_NOZZLE_DUPLICATION_MODE)
if (extruder_duplication_enabled)
return runout_bits; // Any extruder
else
#endif
return TEST(runout_bits, extruder); // Specific extruder
#endif
}
motion_detected |= change; public:
} static inline void block_completed(const block_t* const b) {}
public: static inline void run() {
static void block_complete(const block_t *b) { const bool out = poll_runout_pin(active_extruder);
// If the just-executed block caused the sensor wheel if (!out) filament_present(active_extruder);
// to turn, reset the runout counter for that extruder. #ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
if (TEST(motion_detected, b->extruder)) static bool was_out = false;
filament_present(b->extruder); if (out != was_out) {
was_out = out;
SERIAL_PROTOCOL("Filament ");
serialprintPGM(out ? PSTR("OUT\n") : PSTR("IN\n"));
}
#endif
}
};
// Clear motion triggers for next block
motion_detected = 0;
}
static inline void run() { poll_motion_sensor(); } #endif // !FILAMENT_MOTION_SENSOR
};
/********************************* RESPONSE TYPE *********************************/ /********************************* RESPONSE TYPE *********************************/
#if FILAMENT_RUNOUT_DISTANCE_MM > 0 #if FILAMENT_RUNOUT_DISTANCE_MM > 0
// The RunoutResponseDelayed will trigger an runout event only after // RunoutResponseDelayed triggers a runout event only if the length
// RUNOUT_DISTANCE_MM of filament have been fed after a runout condition. // of filament specified by FILAMENT_RUNOUT_DISTANCE_MM has been fed
// during a runout condition.
class RunoutResponseDelayed { class RunoutResponseDelayed {
private: private:
static int32_t steps_since_detection[EXTRUDERS]; static volatile float runout_mm_countdown[EXTRUDERS];
static float get_mm_since_runout(const uint8_t extruder) {
return (steps_since_detection[extruder] / planner.settings.axis_steps_per_mm[E_AXIS_N(extruder)]);
}
public: public:
static float runout_distance_mm; static float runout_distance_mm;
static inline bool has_runout() { static void reset() {
return get_mm_since_runout(active_extruder) > runout_distance_mm; LOOP_L_N(i, EXTRUDERS) filament_present(i);
}
static inline void filament_present(const uint8_t extruder) {
steps_since_detection[extruder] = 0;
} }
static inline void run() { static inline void run() {
#ifdef FILAMENT_RUNOUT_SENSOR_DEBUG #ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
static uint16_t r = 0; static millis_t t = 0;
if ((r++ % 24000) == 0) { const millis_t ms = millis();
SERIAL_PROTOCOLPGM("mm since filament detection: "); if (ELAPSED(ms, t)) {
LOOP_L_N(i, NUM_RUNOUT_SENSORS) { t = millis() + 1000UL;
if (i > 0) SERIAL_PROTOCOLPGM(", "); LOOP_L_N(i, EXTRUDERS) {
SERIAL_PROTOCOL(get_mm_since_runout(i)); serialprintPGM(i ? PSTR(", ") : PSTR("Remaining mm: "));
SERIAL_PROTOCOL(runout_mm_countdown[i]);
} }
SERIAL_EOL(); SERIAL_EOL();
} }
#endif #endif
} }
static void reset() { static inline bool has_run_out() {
LOOP_L_N(i, NUM_RUNOUT_SENSORS) steps_since_detection[i] = 0; return runout_mm_countdown[active_extruder] < 0;
} }
static inline void block_complete(const block_t *b) { static inline void filament_present(const uint8_t extruder) {
steps_since_detection[b->extruder] += TEST(b->direction_bits, E_AXIS) ? -b->steps[E_AXIS] : b->steps[E_AXIS]; runout_mm_countdown[extruder] = runout_distance_mm;
}
static inline void block_completed(const block_t* const b) {
const uint8_t e = b->extruder;
const int32_t steps = b->steps[E_AXIS];
runout_mm_countdown[e] -= (TEST(b->direction_bits, E_AXIS) ? -steps : steps) * planner.steps_to_mm[E_AXIS_N(e)];
} }
}; };
#else // !FILAMENT_RUNOUT_DISTANCE_MM #else // !FILAMENT_RUNOUT_DISTANCE_MM
// The RunoutResponseDebounced will trigger an runout event after // RunoutResponseDebounced triggers a runout event after a runout
// a runout condition is detected FIL_RUNOUT_THRESHOLD times in a row. // condition has been detected runout_threshold times in a row.
class RunoutResponseDebounced { class RunoutResponseDebounced {
private: private:
static constexpr uint8_t FIL_RUNOUT_THRESHOLD = 5; static constexpr int8_t runout_threshold = 5;
static uint8_t runout_count; static int8_t runout_count;
public: public:
static inline bool has_runout() { return runout_count > FIL_RUNOUT_THRESHOLD; } static inline void reset() { runout_count = runout_threshold; }
static inline void block_complete(const block_t *b) {} static inline void run() { runout_count--; }
static inline void filament_present(const uint8_t extruder) { runout_count = 0; UNUSED(extruder); } static inline bool has_run_out() { return runout_count < 0; }
static inline void run() { runout_count++; } static inline void block_completed(const block_t* const b) {}
static inline void reset() { runout_count = 0; } static inline void filament_present(const uint8_t extruder) { runout_count = runout_threshold; UNUSED(extruder); }
}; };
#endif // !FILAMENT_RUNOUT_DISTANCE_MM #endif // !FILAMENT_RUNOUT_DISTANCE_MM
/********************************* TEMPLATE SPECIALIZATION *********************************/ /********************************* TEMPLATE SPECIALIZATION *********************************/
typedef TFilamentSensor< typedef TFilamentMonitor<
#if FILAMENT_RUNOUT_DISTANCE_MM > 0 #if FILAMENT_RUNOUT_DISTANCE_MM > 0
#if ENABLED(FILAMENT_MOTION_SENSOR) #if ENABLED(FILAMENT_MOTION_SENSOR)
RunoutResponseDelayed, FilamentSensorTypeEncoder RunoutResponseDelayed, FilamentSensorEncoder
#else #else
RunoutResponseDelayed, FilamentSensorTypeSwitch RunoutResponseDelayed, FilamentSensorSwitch
#endif #endif
#else #else
RunoutResponseDebounced, FilamentSensorTypeSwitch RunoutResponseDebounced, FilamentSensorSwitch
#endif #endif
> FilamentRunoutSensor; > FilamentMonitor;
extern FilamentRunoutSensor runout; extern FilamentMonitor runout;

View file

@ -1492,7 +1492,7 @@ uint32_t Stepper::stepper_block_phase_isr() {
// If current block is finished, reset pointer // If current block is finished, reset pointer
if (step_events_completed >= step_event_count) { if (step_events_completed >= step_event_count) {
#if FILAMENT_RUNOUT_DISTANCE_MM > 0 #if FILAMENT_RUNOUT_DISTANCE_MM > 0
runout.block_complete(current_block); runout.block_completed(current_block);
#endif #endif
axis_did_move = 0; axis_did_move = 0;
current_block = NULL; current_block = NULL;

View file

@ -149,7 +149,7 @@
#elif MB(RAMPS_ENDER_4) #elif MB(RAMPS_ENDER_4)
#include "pins_RAMPS_ENDER_4.h" // ATmega2560 env:megaatmega2560 #include "pins_RAMPS_ENDER_4.h" // ATmega2560 env:megaatmega2560
#elif MB(RAMPS_CREALITY) #elif MB(RAMPS_CREALITY)
#include "pins_RAMPS_CREALITY.h" // ATmega2560 env:megaatmega2560 #include "pins_RAMPS_CREALITY.h" // ATmega2560 env:megaatmega2560
#elif MB(FYSETC_F6_13) #elif MB(FYSETC_F6_13)
#include "pins_FYSETC_F6_13.h" // ATmega2560 env:megaatmega2560 #include "pins_FYSETC_F6_13.h" // ATmega2560 env:megaatmega2560