Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filament_switch_sensor: runout distance, smart and runout gcode #158

Merged
merged 7 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ If I want my printer to light itself on fire, I should be able to make my printe

- [canbus: custom CAN bus uuid hash for deterministic uuids](https://github.com/DangerKlippers/danger-klipper/pull/156)

- [filament_switch|motion_sensor: runout distance, smart and runout gcode](https://github.com/DangerKlippers/danger-klipper/pull/158)

If you're feeling adventurous, take a peek at the extra features in the bleeding-edge branch [feature documentation](docs/Bleeding_Edge.md)
and [feature configuration reference](docs/Config_Reference_Bleeding_Edge.md):

Expand Down
16 changes: 16 additions & 0 deletions docs/Config_Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -4764,11 +4764,22 @@ more information.
# detected. See docs/Command_Templates.md for G-Code format. If
# pause_on_runout is set to True this G-Code will run after the
# PAUSE is complete. The default is not to run any G-Code commands.
#immediate_runout_gcode:
# A list of G-Code commands to execute immediately after a filament
# runout is detected and runout_distance is greater than 0.
# See docs/Command_Templates.md for G-Code format.
#insert_gcode:
# A list of G-Code commands to execute after a filament insert is
# detected. See docs/Command_Templates.md for G-Code format. The
# default is not to run any G-Code commands, which disables insert
# detection.
#runout_distance: 0.0
# Defines how much filament can still be pulled after the
# switch sensor triggered (e.g. you have a 60cm reverse bowden between your
# extruder and your sensor, you would then set runout_distance to something
# like 590 to leave a small safety margin and now the print will not
# immediately pause when the sensor triggers but rather keep printing until
# the filament is at the extruder). The default is 0 millimeters.
#event_delay: 3.0
# The minimum amount of time in seconds to delay between events.
# Events triggered during this time period will be silently
Expand All @@ -4781,6 +4792,10 @@ more information.
#switch_pin:
# The pin on which the switch is connected. This parameter must be
# provided.
#smart:
# If set to true the sensor will use the virtual_sd_card module to determine
# whether the printer is printing which is more reliable but will not work
# when streaming a print over usb or similar.
```

### [filament_motion_sensor]
Expand All @@ -4807,6 +4822,7 @@ switch_pin:
#insert_gcode:
#event_delay:
#pause_delay:
#smart:
# See the "filament_switch_sensor" section for a description of the
# above parameters.
```
Expand Down
28 changes: 25 additions & 3 deletions docs/G-Codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,31 @@ status of the filament sensor. The data displayed on the terminal will
depend on the sensor type defined in the configuration.

#### SET_FILAMENT_SENSOR
`SET_FILAMENT_SENSOR SENSOR=<sensor_name> ENABLE=[0|1]`: Sets the
filament sensor on/off. If ENABLE is set to 0, the filament sensor
will be disabled, if set to 1 it is enabled.
###### For filament_switch_sensor:
`SET_FILAMENT_SENSOR SENSOR=<sensor_name> [ENABLE=0|1] [RESET=0|1]
[RUNOUT_DISTANCE=<mm>] [SMART=0|1]`: Sets values for the
filament sensor. If all parameters are omitted, the current stats will
be reported. <br>
ENABLE sets the filament sensor on/off. If ENABLE is set to 0, the
filament sensor will be disabled, if set to 1 it is enabled. If the state
of the sensor changes, a reset will be triggered. <br>
RESET removes all pending runout_gcodes and pauses and force a reevaluation
of the sensor state. <br>
RUNOUT_DISTANCE sets the runout_distance. <br>
SMART sets the smart parameter.

###### For filament_motion_sensor:
`SET_FILAMENT_SENSOR SENSOR=<sensor_name> [ENABLE=0|1] [RESET=0|1]
[DETECTION_LENGTH=<mm>] [SMART=0|1]`: Sets values for the
filament sensor. If all parameters are omitted, the current stats will
be reported. <br>
ENABLE sets the filament sensor on/off. If ENABLE is set to 0, the
filament sensor will be disabled, if set to 1 it is enabled. If the sensor
was previously disabled and gets enabled, a reset will be triggered. <br>
RESET resets the state of the sensor and sets it to filament detected. <br>
DETECTION_LENGTH sets the detection_length, if the new detection length is
different from the old one, a reset will be triggered. <br>
SMART sets the smart parameter.

### [firmware_retraction]

Expand Down
50 changes: 49 additions & 1 deletion klippy/extras/filament_motion_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class EncoderSensor:
def __init__(self, config):
# Read config
self.printer = config.get_printer()
self.gcode = self.printer.lookup_object("gcode")
switch_pin = config.get("switch_pin")
self.extruder_name = config.get("extruder")
self.detection_length = config.getfloat(
Expand All @@ -22,7 +23,7 @@ def __init__(self, config):
buttons.register_buttons([switch_pin], self.encoder_event)
# Get printer objects
self.reactor = self.printer.get_reactor()
self.runout_helper = filament_switch_sensor.RunoutHelper(config)
self.runout_helper = filament_switch_sensor.RunoutHelper(config, self)
self.get_status = self.runout_helper.get_status
self.extruder = None
self.estimated_print_time = None
Expand Down Expand Up @@ -88,6 +89,53 @@ def encoder_event(self, eventtime, state):
# Filament is always assumed to be present on an encoder event
self.runout_helper.note_filament_present(True)

def get_sensor_status(self):
return (
"Filament Sensor %s: %s\n"
"Filament Detected: %s\n"
"Detection Length: %.2f\n"
"Smart: %s"
% (
self.runout_helper.name,
(
"enabled"
if self.runout_helper.sensor_enabled > 0
else "disabled"
),
"true" if self.runout_helper.filament_present else "false",
self.detection_length,
"true" if self.runout_helper.smart else "false",
)
)

def sensor_get_status(self, eventtime):
return {"detection_length": float(self.detection_length)}

def get_info(self, gcmd):
detection_length = gcmd.get_float("DETECTION_LENGTH", None, minval=0.0)
rogerlz marked this conversation as resolved.
Show resolved Hide resolved
if detection_length is None:
gcmd.respond_info(self.get_sensor_status())
return True
return False

def reset_needed(self, enable):
if enable and not self.runout_helper.sensor_enabled:
return True
return False

def set_filament_sensor(self, gcmd):
reset_needed = False
detection_length = gcmd.get_float("DETECTION_LENGTH", None, minval=0.0)
if detection_length is not None:
if detection_length != self.detection_length:
reset_needed = True
self.detection_length = detection_length
return reset_needed

def reset(self):
self._update_filament_runout_pos()
self.runout_helper.note_filament_present(True)


def load_config_prefix(config):
return EncoderSensor(config)
Loading
Loading