2017-10-10 09:35:20 +02:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 15:00:57 +01:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2017-10-10 09:35:20 +02:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 06:57:50 +02:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2017-10-10 09:35:20 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2018-10-16 14:28:52 +02:00
|
|
|
#pragma once
|
2017-10-10 09:35:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* feature/runout.h - Runout sensor support
|
|
|
|
*/
|
|
|
|
|
2018-02-18 09:42:09 +01:00
|
|
|
#include "../sd/cardreader.h"
|
|
|
|
#include "../module/printcounter.h"
|
2019-09-14 10:05:10 +02:00
|
|
|
#include "../module/planner.h"
|
|
|
|
#include "../module/stepper.h" // for block_t
|
2018-02-18 09:42:09 +01:00
|
|
|
#include "../gcode/queue.h"
|
2017-10-10 09:35:20 +02:00
|
|
|
|
2018-02-18 09:42:09 +01:00
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
2018-10-08 22:44:05 +02:00
|
|
|
#if ENABLED(EXTENSIBLE_UI)
|
2020-03-13 22:29:29 +01:00
|
|
|
#include "../lcd/extui/ui_api.h"
|
2018-10-08 22:44:05 +02:00
|
|
|
#endif
|
|
|
|
|
2019-02-12 22:55:47 +01:00
|
|
|
#if ENABLED(ADVANCED_PAUSE_FEATURE)
|
|
|
|
#include "pause.h"
|
|
|
|
#endif
|
|
|
|
|
2018-10-16 14:28:52 +02:00
|
|
|
//#define FILAMENT_RUNOUT_SENSOR_DEBUG
|
2019-05-07 20:58:10 +02:00
|
|
|
#ifndef FILAMENT_RUNOUT_THRESHOLD
|
|
|
|
#define FILAMENT_RUNOUT_THRESHOLD 5
|
|
|
|
#endif
|
2018-03-05 08:49:30 +01:00
|
|
|
|
2019-10-03 01:51:25 +02:00
|
|
|
void event_filament_runout();
|
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
class FilamentMonitorBase {
|
2018-02-22 19:26:14 +01:00
|
|
|
public:
|
2019-02-12 22:55:47 +01:00
|
|
|
static bool enabled, filament_ran_out;
|
|
|
|
|
|
|
|
#if ENABLED(HOST_ACTION_COMMANDS)
|
|
|
|
static bool host_handling;
|
|
|
|
#else
|
2019-04-04 10:06:19 +02:00
|
|
|
static constexpr bool host_handling = false;
|
2019-02-12 22:55:47 +01:00
|
|
|
#endif
|
2018-10-16 14:28:52 +02:00
|
|
|
};
|
2018-02-18 09:42:09 +01:00
|
|
|
|
2018-10-16 14:28:52 +02:00
|
|
|
template<class RESPONSE_T, class SENSOR_T>
|
2018-11-14 18:45:57 +01:00
|
|
|
class TFilamentMonitor : public FilamentMonitorBase {
|
2018-10-16 14:28:52 +02:00
|
|
|
private:
|
|
|
|
typedef RESPONSE_T response_t;
|
|
|
|
typedef SENSOR_T sensor_t;
|
2018-11-14 18:45:57 +01:00
|
|
|
static response_t response;
|
|
|
|
static sensor_t sensor;
|
2018-02-18 09:42:09 +01:00
|
|
|
|
2018-10-16 14:28:52 +02:00
|
|
|
public:
|
2018-11-14 18:45:57 +01:00
|
|
|
static inline void setup() {
|
2018-10-16 14:28:52 +02:00
|
|
|
sensor.setup();
|
2018-11-14 18:45:57 +01:00
|
|
|
reset();
|
2018-10-16 14:28:52 +02:00
|
|
|
}
|
2018-02-18 09:42:09 +01:00
|
|
|
|
2018-11-02 05:14:20 +01:00
|
|
|
static inline void reset() {
|
2018-10-16 14:28:52 +02:00
|
|
|
filament_ran_out = false;
|
|
|
|
response.reset();
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
// Call this method when filament is present,
|
|
|
|
// so the response can reset its counter.
|
2018-11-02 05:14:20 +01:00
|
|
|
static inline void filament_present(const uint8_t extruder) {
|
2018-10-16 14:28:52 +02:00
|
|
|
response.filament_present(extruder);
|
|
|
|
}
|
|
|
|
|
2019-05-04 06:53:15 +02:00
|
|
|
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
|
|
|
|
static inline float& runout_distance() { return response.runout_distance_mm; }
|
|
|
|
static inline void set_runout_distance(const float &mm) { response.runout_distance_mm = mm; }
|
|
|
|
#endif
|
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
// Handle a block completion. RunoutResponseDelayed uses this to
|
|
|
|
// add up the length of filament moved while the filament is out.
|
|
|
|
static inline void block_completed(const block_t* const b) {
|
|
|
|
if (enabled) {
|
|
|
|
response.block_completed(b);
|
|
|
|
sensor.block_completed(b);
|
|
|
|
}
|
2018-10-16 14:28:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
// Give the response a chance to update its counter.
|
|
|
|
static inline void run() {
|
2019-10-03 01:54:20 +02:00
|
|
|
if (enabled && !filament_ran_out && (printingIsActive()
|
2019-09-20 07:50:11 +02:00
|
|
|
#if ENABLED(ADVANCED_PAUSE_FEATURE)
|
|
|
|
|| did_pause_print
|
|
|
|
#endif
|
|
|
|
)) {
|
2019-05-04 06:53:15 +02:00
|
|
|
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
|
2018-11-14 18:45:57 +01:00
|
|
|
cli(); // Prevent RunoutResponseDelayed::block_completed from accumulating here
|
|
|
|
#endif
|
2018-10-16 14:28:52 +02:00
|
|
|
response.run();
|
|
|
|
sensor.run();
|
2018-11-14 18:45:57 +01:00
|
|
|
const bool ran_out = response.has_run_out();
|
2019-05-04 06:53:15 +02:00
|
|
|
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
|
2018-11-14 18:45:57 +01:00
|
|
|
sei();
|
|
|
|
#endif
|
|
|
|
if (ran_out) {
|
2018-10-16 14:28:52 +02:00
|
|
|
filament_ran_out = true;
|
2019-02-12 22:55:47 +01:00
|
|
|
event_filament_runout();
|
2018-10-16 14:28:52 +02:00
|
|
|
planner.synchronize();
|
|
|
|
}
|
2018-02-22 19:26:14 +01:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 14:28:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*************************** FILAMENT PRESENCE SENSORS ***************************/
|
2018-02-18 09:42:09 +01:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
class FilamentSensorBase {
|
2018-10-16 14:28:52 +02:00
|
|
|
protected:
|
|
|
|
static void filament_present(const uint8_t extruder);
|
|
|
|
|
|
|
|
public:
|
2018-11-28 03:13:24 +01:00
|
|
|
static inline void setup() {
|
2018-10-16 14:28:52 +02:00
|
|
|
#if ENABLED(FIL_RUNOUT_PULLUP)
|
|
|
|
#define INIT_RUNOUT_PIN(P) SET_INPUT_PULLUP(P)
|
|
|
|
#elif ENABLED(FIL_RUNOUT_PULLDOWN)
|
|
|
|
#define INIT_RUNOUT_PIN(P) SET_INPUT_PULLDOWN(P)
|
|
|
|
#else
|
|
|
|
#define INIT_RUNOUT_PIN(P) SET_INPUT(P)
|
|
|
|
#endif
|
|
|
|
|
2020-02-02 04:00:53 +01:00
|
|
|
#define _INIT_RUNOUT(N) INIT_RUNOUT_PIN(FIL_RUNOUT##N##_PIN);
|
|
|
|
REPEAT_S(1, INCREMENT(NUM_RUNOUT_SENSORS), _INIT_RUNOUT)
|
|
|
|
#undef _INIT_RUNOUT
|
2018-10-16 14:28:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-28 03:13:24 +01:00
|
|
|
// Return a bitmask of runout pin states
|
|
|
|
static inline uint8_t poll_runout_pins() {
|
2020-02-02 04:00:53 +01:00
|
|
|
#define _OR_RUNOUT(N) | (READ(FIL_RUNOUT##N##_PIN) ? _BV((N) - 1) : 0)
|
|
|
|
return (0 REPEAT_S(1, INCREMENT(NUM_RUNOUT_SENSORS), _OR_RUNOUT));
|
|
|
|
#undef _OR_RUNOUT
|
2018-11-28 03:13:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return a bitmask of runout flag states (1 bits always indicates runout)
|
|
|
|
static inline uint8_t poll_runout_states() {
|
2020-02-02 04:00:53 +01:00
|
|
|
return (poll_runout_pins()
|
2018-11-28 03:13:24 +01:00
|
|
|
#if DISABLED(FIL_RUNOUT_INVERTING)
|
2020-02-02 04:00:53 +01:00
|
|
|
^ uint8_t(_BV(NUM_RUNOUT_SENSORS) - 1)
|
2018-11-28 03:13:24 +01:00
|
|
|
#endif
|
|
|
|
);
|
2018-10-16 14:28:52 +02:00
|
|
|
}
|
2020-02-02 04:00:53 +01:00
|
|
|
|
|
|
|
#undef INIT_RUNOUT_PIN
|
2018-10-16 14:28:52 +02:00
|
|
|
};
|
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
#if ENABLED(FILAMENT_MOTION_SENSOR)
|
2018-10-16 14:28:52 +02:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
/**
|
|
|
|
* This sensor uses a magnetic encoder disc and a Hall effect
|
|
|
|
* sensor (or a slotted disc and optical sensor). The state
|
|
|
|
* will toggle between 0 and 1 on filament movement. It can detect
|
|
|
|
* filament runout and stripouts or jams.
|
|
|
|
*/
|
|
|
|
class FilamentSensorEncoder : public FilamentSensorBase {
|
|
|
|
private:
|
|
|
|
static uint8_t motion_detected;
|
|
|
|
|
|
|
|
static inline void poll_motion_sensor() {
|
|
|
|
static uint8_t old_state;
|
|
|
|
const uint8_t new_state = poll_runout_pins(),
|
|
|
|
change = old_state ^ new_state;
|
|
|
|
old_state = new_state;
|
|
|
|
|
|
|
|
#ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
|
2018-11-27 23:41:26 +01:00
|
|
|
if (change) {
|
2018-11-29 23:58:58 +01:00
|
|
|
SERIAL_ECHOPGM("Motion detected:");
|
2018-11-27 23:41:26 +01:00
|
|
|
for (uint8_t e = 0; e < NUM_RUNOUT_SENSORS; e++)
|
2020-01-09 01:31:57 +01:00
|
|
|
if (TEST(change, e)) SERIAL_CHAR(' ', '0' + e);
|
2018-11-27 23:41:26 +01:00
|
|
|
SERIAL_EOL();
|
|
|
|
}
|
2018-09-17 08:06:22 +02:00
|
|
|
#endif
|
2018-10-16 14:28:52 +02:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
motion_detected |= change;
|
|
|
|
}
|
2018-10-16 14:28:52 +02:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
public:
|
|
|
|
static inline void block_completed(const block_t* const b) {
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// Clear motion triggers for next block
|
|
|
|
motion_detected = 0;
|
|
|
|
}
|
2018-02-18 09:42:09 +01:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
static inline void run() { poll_motion_sensor(); }
|
|
|
|
};
|
2018-10-16 14:28:52 +02:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
#else
|
2018-10-16 14:28:52 +02:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
/**
|
|
|
|
* This is a simple endstop switch in the path of the filament.
|
|
|
|
* It can detect filament runout, but not stripouts or jams.
|
|
|
|
*/
|
|
|
|
class FilamentSensorSwitch : public FilamentSensorBase {
|
|
|
|
private:
|
2018-11-28 03:13:24 +01:00
|
|
|
static inline bool poll_runout_state(const uint8_t extruder) {
|
|
|
|
const uint8_t runout_states = poll_runout_states();
|
2019-03-13 06:42:50 +01:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
#if NUM_RUNOUT_SENSORS == 1
|
2018-11-24 04:07:24 +01:00
|
|
|
UNUSED(extruder);
|
2019-03-13 06:42:50 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (true
|
|
|
|
#if NUM_RUNOUT_SENSORS > 1
|
|
|
|
#if ENABLED(DUAL_X_CARRIAGE)
|
2019-03-16 04:46:27 +01:00
|
|
|
&& (dual_x_carriage_mode == DXC_DUPLICATION_MODE || dual_x_carriage_mode == DXC_MIRRORED_MODE)
|
2019-03-13 06:42:50 +01:00
|
|
|
#elif ENABLED(MULTI_NOZZLE_DUPLICATION)
|
|
|
|
&& extruder_duplication_enabled
|
2019-08-19 04:11:33 +02:00
|
|
|
#else
|
|
|
|
&& false
|
2019-03-13 06:42:50 +01:00
|
|
|
#endif
|
2018-11-14 18:45:57 +01:00
|
|
|
#endif
|
2019-03-13 06:42:50 +01:00
|
|
|
) return runout_states; // Any extruder
|
|
|
|
|
|
|
|
#if NUM_RUNOUT_SENSORS > 1
|
|
|
|
return TEST(runout_states, extruder); // Specific extruder
|
2018-11-14 18:45:57 +01:00
|
|
|
#endif
|
|
|
|
}
|
2018-10-16 14:28:52 +02:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
public:
|
2019-10-01 04:44:07 +02:00
|
|
|
static inline void block_completed(const block_t* const) {}
|
2018-10-16 14:28:52 +02:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
static inline void run() {
|
2018-11-28 03:13:24 +01:00
|
|
|
const bool out = poll_runout_state(active_extruder);
|
2018-11-14 18:45:57 +01:00
|
|
|
if (!out) filament_present(active_extruder);
|
|
|
|
#ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
|
|
|
|
static bool was_out = false;
|
|
|
|
if (out != was_out) {
|
|
|
|
was_out = out;
|
2018-11-29 23:58:58 +01:00
|
|
|
SERIAL_ECHOPGM("Filament ");
|
2018-11-14 18:45:57 +01:00
|
|
|
serialprintPGM(out ? PSTR("OUT\n") : PSTR("IN\n"));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
2018-10-16 14:28:52 +02:00
|
|
|
|
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
#endif // !FILAMENT_MOTION_SENSOR
|
2018-10-16 14:28:52 +02:00
|
|
|
|
|
|
|
/********************************* RESPONSE TYPE *********************************/
|
|
|
|
|
2019-05-04 06:53:15 +02:00
|
|
|
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
|
2018-10-16 14:28:52 +02:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
// RunoutResponseDelayed triggers a runout event only if the length
|
|
|
|
// of filament specified by FILAMENT_RUNOUT_DISTANCE_MM has been fed
|
|
|
|
// during a runout condition.
|
2018-10-16 14:28:52 +02:00
|
|
|
class RunoutResponseDelayed {
|
|
|
|
private:
|
2018-11-14 18:45:57 +01:00
|
|
|
static volatile float runout_mm_countdown[EXTRUDERS];
|
2018-10-16 14:28:52 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
static float runout_distance_mm;
|
|
|
|
|
2018-11-28 03:13:24 +01:00
|
|
|
static inline void reset() {
|
2018-11-14 18:45:57 +01:00
|
|
|
LOOP_L_N(i, EXTRUDERS) filament_present(i);
|
2018-10-16 14:28:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void run() {
|
|
|
|
#ifdef FILAMENT_RUNOUT_SENSOR_DEBUG
|
2018-11-14 18:45:57 +01:00
|
|
|
static millis_t t = 0;
|
|
|
|
const millis_t ms = millis();
|
|
|
|
if (ELAPSED(ms, t)) {
|
|
|
|
t = millis() + 1000UL;
|
|
|
|
LOOP_L_N(i, EXTRUDERS) {
|
|
|
|
serialprintPGM(i ? PSTR(", ") : PSTR("Remaining mm: "));
|
2018-11-29 23:58:58 +01:00
|
|
|
SERIAL_ECHO(runout_mm_countdown[i]);
|
2018-10-16 14:28:52 +02:00
|
|
|
}
|
|
|
|
SERIAL_EOL();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
static inline bool has_run_out() {
|
|
|
|
return runout_mm_countdown[active_extruder] < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void filament_present(const uint8_t extruder) {
|
|
|
|
runout_mm_countdown[extruder] = runout_distance_mm;
|
2018-10-16 14:28:52 +02:00
|
|
|
}
|
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
static inline void block_completed(const block_t* const b) {
|
2019-09-29 11:25:39 +02:00
|
|
|
if (b->steps.x || b->steps.y || b->steps.z
|
2019-02-12 22:55:47 +01:00
|
|
|
#if ENABLED(ADVANCED_PAUSE_FEATURE)
|
|
|
|
|| did_pause_print // Allow pause purge move to re-trigger runout state
|
|
|
|
#endif
|
|
|
|
) {
|
2018-11-27 20:45:25 +01:00
|
|
|
// Only trigger on extrusion with XYZ movement to allow filament change and retract/recover.
|
|
|
|
const uint8_t e = b->extruder;
|
2019-09-29 11:25:39 +02:00
|
|
|
const int32_t steps = b->steps.e;
|
2018-11-27 20:45:25 +01:00
|
|
|
runout_mm_countdown[e] -= (TEST(b->direction_bits, E_AXIS) ? -steps : steps) * planner.steps_to_mm[E_AXIS_N(e)];
|
|
|
|
}
|
2018-10-16 14:28:52 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#else // !FILAMENT_RUNOUT_DISTANCE_MM
|
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
// RunoutResponseDebounced triggers a runout event after a runout
|
|
|
|
// condition has been detected runout_threshold times in a row.
|
2017-10-10 09:35:20 +02:00
|
|
|
|
2018-10-16 14:28:52 +02:00
|
|
|
class RunoutResponseDebounced {
|
|
|
|
private:
|
2019-05-07 20:58:10 +02:00
|
|
|
static constexpr int8_t runout_threshold = FILAMENT_RUNOUT_THRESHOLD;
|
2018-11-14 18:45:57 +01:00
|
|
|
static int8_t runout_count;
|
2018-10-16 14:28:52 +02:00
|
|
|
public:
|
2018-11-14 18:45:57 +01:00
|
|
|
static inline void reset() { runout_count = runout_threshold; }
|
2019-05-07 20:58:10 +02:00
|
|
|
static inline void run() { if (runout_count >= 0) runout_count--; }
|
2018-11-14 18:45:57 +01:00
|
|
|
static inline bool has_run_out() { return runout_count < 0; }
|
2019-10-01 04:44:07 +02:00
|
|
|
static inline void block_completed(const block_t* const) { }
|
|
|
|
static inline void filament_present(const uint8_t) { runout_count = runout_threshold; }
|
2018-10-16 14:28:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // !FILAMENT_RUNOUT_DISTANCE_MM
|
|
|
|
|
|
|
|
/********************************* TEMPLATE SPECIALIZATION *********************************/
|
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
typedef TFilamentMonitor<
|
2019-05-04 06:53:15 +02:00
|
|
|
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
|
|
|
|
RunoutResponseDelayed,
|
2018-10-16 14:28:52 +02:00
|
|
|
#if ENABLED(FILAMENT_MOTION_SENSOR)
|
2019-05-04 06:53:15 +02:00
|
|
|
FilamentSensorEncoder
|
2018-10-16 14:28:52 +02:00
|
|
|
#else
|
2019-05-04 06:53:15 +02:00
|
|
|
FilamentSensorSwitch
|
2018-10-16 14:28:52 +02:00
|
|
|
#endif
|
|
|
|
#else
|
2018-11-14 18:45:57 +01:00
|
|
|
RunoutResponseDebounced, FilamentSensorSwitch
|
2018-10-16 14:28:52 +02:00
|
|
|
#endif
|
2018-11-14 18:45:57 +01:00
|
|
|
> FilamentMonitor;
|
2018-10-16 14:28:52 +02:00
|
|
|
|
2018-11-14 18:45:57 +01:00
|
|
|
extern FilamentMonitor runout;
|