Skip to content

Commit 9a883df

Browse files
authored
Merge pull request kontakt#12 from fornellas/improvements
Various improvements
2 parents 7be3683 + 3441b77 commit 9a883df

File tree

3 files changed

+66
-65
lines changed

3 files changed

+66
-65
lines changed

README.md

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
# OctoPrint-FilamentReloaded
22

3-
Based on the Octoprint-Filament plugin by MoonshineSG (https://github.com/MoonshineSG/Octoprint-Filament), this modification adds the ability to modify your configuration through OctoPrint settings, as well as adding configurations for both NO and NC switches.
3+
[OctoPrint](http://octoprint.org/) plugin that integrates with a filament sensor hooked up to a Raspberry Pi GPIO pin and allows the filament spool to be changed during a print if the filament runs out.
44

5-
Future developments are planned to include multiple filament sensors, pop-ups, pre-print validation and custom filament run-out scripting.
5+
Future developments are planned to include multiple filament sensors and pop-ups.
66

7-
## Setup
7+
Initial work based on the [Octoprint-Filament](https://github.com/MoonshineSG/Octoprint-Filament) plugin by MoonshineSG.
88

9-
Install via the bundled [Plugin Manager](https://github.com/foosel/OctoPrint/wiki/Plugin:-Plugin-Manager)
10-
or manually using this URL:
11-
12-
https://github.com/kontakt/Octoprint-Filament-Reloaded/archive/master.zip
9+
## Required sensor
1310

1411
Using this plugin requires a filament sensor. The code is set to use the Raspberry Pi's internal Pull-Up resistors, so the switch should be between your detection pin and a ground pin.
1512

1613
This plugin is using the GPIO.BOARD numbering scheme, the pin being used needs to be selected by the physical pin number.
14+
15+
## Features
16+
17+
* Configurable GPIO pin.
18+
* Debounce noisy sensors.
19+
* Support norbally open and normally closed sensors.
20+
* Execution of custom GCODE when out of filament detected.
21+
* Optionally pause print when out of filament.
22+
23+
## Installation
24+
25+
* Install via the bundled [Plugin Manager](https://github.com/foosel/OctoPrint/wiki/Plugin:-Plugin-Manager).
26+
* Manually using this URL: https://github.com/kontakt/Octoprint-Filament-Reloaded/archive/master.zip
27+
28+
## Configuration
29+
30+
After installation, configure the plugin via OctoPrint Settings interface.

octoprint_filamentreload/__init__.py

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
from __future__ import absolute_import
33

44
import octoprint.plugin
5-
from octoprint.events import eventManager, Events
6-
from flask import jsonify, make_response
5+
from octoprint.events import Events
76
import RPi.GPIO as GPIO
87
from time import sleep
98

9+
1010
class FilamentReloadedPlugin(octoprint.plugin.StartupPlugin,
1111
octoprint.plugin.EventHandlerPlugin,
1212
octoprint.plugin.TemplatePlugin,
@@ -16,7 +16,6 @@ def initialize(self):
1616
self._logger.info("Running RPi.GPIO version '{0}'".format(GPIO.VERSION))
1717
if GPIO.VERSION < "0.6": # Need at least 0.6 for edge detection
1818
raise Exception("RPi.GPIO must be greater than 0.6")
19-
GPIO.setmode(GPIO.BOARD) # Use the board numbering scheme
2019
GPIO.setwarnings(False) # Disable GPIO warnings
2120

2221
@property
@@ -36,47 +35,44 @@ def mode(self):
3635
return int(self._settings.get(["mode"]))
3736

3837
@property
39-
def after_pause_gcode(self):
40-
return str(self._settings.get(["after_pause_gcode"])).splitlines()
38+
def no_filament_gcode(self):
39+
return str(self._settings.get(["no_filament_gcode"])).splitlines()
4140

4241
@property
43-
def after_resume_gcode(self):
44-
return str(self._settings.get(["after_resume_gcode"])).splitlines()
42+
def pause_print(self):
43+
return self._settings.get_boolean(["pause_print"])
4544

46-
def on_after_startup(self):
47-
self._logger.info("Filament Sensor Reloaded started")
48-
if self.mode == 0:
49-
GPIO.setmode(GPIO.BOARD)
50-
else:
51-
GPIO.setmode(GPIO.BCM)
45+
def _setup_sensor(self):
5246
if self.sensor_enabled():
47+
self._logger.info("Setting up sensor.")
48+
if self.mode == 0:
49+
self._logger.info("Using Board Mode")
50+
GPIO.setmode(GPIO.BOARD)
51+
else:
52+
self._logger.info("Using BCM Mode")
53+
GPIO.setmode(GPIO.BCM)
5354
self._logger.info("Filament Sensor active on GPIO Pin [%s]"%self.pin)
54-
GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Initialize GPIO as INPUT
55+
GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
5556
else:
5657
self._logger.info("Pin not configured, won't work unless configured!")
5758

59+
def on_after_startup(self):
60+
self._logger.info("Filament Sensor Reloaded started")
61+
self._setup_sensor()
62+
5863
def get_settings_defaults(self):
5964
return dict(
6065
pin = -1, # Default is no pin
6166
bounce = 250, # Debounce 250ms
6267
switch = 0, # Normally Open
63-
mode = 0 # Board Mode
64-
after_pause_gcode = '',
65-
after_resume_gcode = '',
68+
mode = 0, # Board Mode
69+
no_filament_gcode = '',
70+
pause_print = True,
6671
)
6772

6873
def on_settings_save(self, data):
6974
octoprint.plugin.SettingsPlugin.on_settings_save(self, data)
70-
71-
if self.mode == 0:
72-
GPIO.setmode(GPIO.BOARD)
73-
else:
74-
GPIO.setmode(GPIO.BCM)
75-
76-
if self._settings.get(["pin"]) != "-1": # If a pin is defined
77-
self._logger.info("Filament Sensor active on GPIO Pin [%s]"%self.pin)
78-
GPIO.setup(self.pin, GPIO.IN, pull_up_down=GPIO.PUD_UP) # Initialize GPIO as INPUT
79-
75+
self._setup_sensor()
8076

8177
def sensor_enabled(self):
8278
return self.pin != -1
@@ -87,22 +83,12 @@ def no_filament(self):
8783
def get_template_configs(self):
8884
return [dict(type="settings", custom_bindings=False)]
8985

90-
@property
91-
def _filament_change(self):
92-
return self.__dict__.get('_filament_change', False)
93-
9486
def on_event(self, event, payload):
9587
# Early abort in case of out ot filament when start printing, as we
9688
# can't change with a cold nozzle
9789
if event is Events.PRINT_STARTED and self.no_filament():
9890
self._logger.info("Printing aborted: no filament detected!")
9991
self._printer.cancel_print()
100-
# Run after resume gcode
101-
if event is Events.PRINT_RESUMED:
102-
if self._filament_change:
103-
self._logger.info("Sending after resume GCODE!")
104-
self._printer.commands(self.after_resume_gcode)
105-
self._filament_change = False
10692
# Enable sensor
10793
if event in (
10894
Events.PRINT_STARTED,
@@ -111,7 +97,11 @@ def on_event(self, event, payload):
11197
self._logger.info("%s: Enabling filament sensor." % (event))
11298
if self.sensor_enabled():
11399
GPIO.remove_event_detect(self.pin)
114-
GPIO.add_event_detect(self.pin, GPIO.BOTH, callback=self.sensor_callback, bouncetime=self.bounce)
100+
GPIO.add_event_detect(
101+
self.pin, GPIO.BOTH,
102+
callback=self.sensor_callback,
103+
bouncetime=self.bounce
104+
)
115105
# Disable sensor
116106
elif event in (
117107
Events.PRINT_DONE,
@@ -125,17 +115,15 @@ def on_event(self, event, payload):
125115
def sensor_callback(self, _):
126116
sleep(self.bounce/1000)
127117
if self.no_filament():
128-
if self._filament_change:
129-
self._logger.info("Out of filament, waiting for replacement!")
130-
else:
131-
self._logger.info("Out of filament, pausing!")
118+
self._logger.info("Out of filament!")
119+
if self.pause_print:
120+
self._logger.info("Pausing print.")
132121
self._printer.pause_print()
133-
if self.after_pause_gcode:
134-
self._logger.info("Sending after pause GCODE")
135-
self._printer.commands(self.after_pause_gcode)
136-
self._filament_change = True
122+
if self.no_filament_gcode:
123+
self._logger.info("Sending out of filament GCODE")
124+
self._printer.commands(self.no_filament_gcode)
137125
else:
138-
self._logger.info("Filament detected, resume to continue!")
126+
self._logger.info("Filament detected!")
139127

140128
def get_update_information(self):
141129
return dict(
Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
<h4>{{ _('Filament Sensor') }}</h4>
2-
Define the pin used by your sensor and the debounce timing for false positive prevention.
3-
<br>
1+
<h4>{{ _('Filament Sensor Reloaded') }}</h4>
42
<form class="form-horizontal">
53
<div class="control-group">
64
<label class="control-label">{{ _('Pin:') }}</label>
7-
<div class="controls" data-toggle="tooltip" title="{{ _('Which GPIO pin your switch is attached to') }}">
5+
<div class="controls" data-toggle="tooltip" title="{{ _('Which Raspberry Pi GPIO pin your switch is attached to (-1 disables the plugin)') }}">
86
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: settings.plugins.filamentreload.pin">
97
</div>
108
</div>
119
<div class="control-group">
1210
<label class="control-label">{{ _('Debounce Time:') }}</label>
13-
<div class="controls" data-toggle="tooltip" title="{{ _('The amount of time a signal needs to stay constant to be considered valid') }}">
11+
<div class="controls" data-toggle="tooltip" title="{{ _('When a sensor state change happens, wait for this amount of time to wait for the signal to stabilize.') }}">
1412
<input type="number" step="any" min="0" class="input-mini text-right" data-bind="value: settings.plugins.filamentreload.bounce">
1513
<span class="add-on">ms</span>
1614
</div>
1715
</div>
1816
<div class="control-group">
1917
<label class="control-label">{{ _('Switch Type:') }}</label>
20-
<div class="controls" data-toggle="tooltip" title="{{ _('Whether the sensor should trip when the switch goes HIGH or LOW, Normally Open is LOW when filament is present, and Normally Closed is HIGH when filament is present') }}">
18+
<div class="controls" data-toggle="tooltip" title="{{ _('Whether the sensor should trip when the switch goes HIGH or LOW, Normally Open is LOW when filament is present, and Normally Closed is HIGH when filament is present.') }}">
2119
<select class="select-mini" data-bind="value: settings.plugins.filamentreload.switch">
2220
<option value=0>{{ _('Normally Open') }}</option>
2321
<option value=1>{{ _('Normally Closed') }}</option>
@@ -34,15 +32,16 @@ Define the pin used by your sensor and the debounce timing for false positive pr
3432
</div>
3533
</div>
3634
<div class="control-group">
37-
<label class="control-label">{{ _('After pause GCODE:') }}</label>
35+
<label class="control-label">{{ _('Out of filament GCODE:') }}</label>
3836
<div class="controls">
39-
<textarea rows="4" class="block" data-bind="value: settings.plugins.filamentreload.after_pause_gcode"></textarea>
37+
<textarea rows="4" class="block" data-bind="value: settings.plugins.filamentreload.no_filament_gcode"></textarea>
4038
</div>
4139
</div>
4240
<div class="control-group">
43-
<label class="control-label">{{ _('After resume GCODE:') }}</label>
44-
<div class="controls">
45-
<textarea rows="4" class="block" data-bind="value: settings.plugins.filamentreload.after_resume_gcode"></textarea>
41+
<div class="controls" data-toggle="tooltip" title="{{ _('Beware that for some printers, pausing is incompatible with M600 (Filament change pause) as both will move the print head and it can resume at a different position as it paused.') }}">
42+
<label class="checkbox">
43+
<input type="checkbox" data-bind="checked: settings.plugins.filamentreload.pause_print"> {{ _('Pause print when out of filament') }}
44+
</label>
4645
</div>
4746
</div>
4847
</form>

0 commit comments

Comments
 (0)