Reputation: 1
I am trying to make a filament movement sensor and integrate it with Octoprint running on Raspberry Pi 3 model B+.
My goal is to make a device which would pause the print when it senses that the filament is not moving for a given time, let's say 10 seconds.
I've already whipped up a prototype from trash lying around and a toy car stolen from my brother and connected it to Raspberry.
It utilizes an optocoupler encoder module with LM393 and other stuff built in. It's digital output is connected to Raspberry's GPIO pin. Filament goes in between rubber wheels turning them and on the upper wheel's axle is mounted encoder wheel which changes sensor's state.
Now here comes my problem. I am not quite familiar with Python, especially the interrupts.
I know how to read sensor's state in a loop:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
try:
while True:
if (GPIO.input(26) == 1)
do magic
elif (GPIO.input(26) == 0)
do other magic
I've also seen that it's better to actually use interrupts for this:
GPIO.setup(pin, GPIO.IN)
GPIO.add_event_detect(pin, GPIO.BOTH, callback = function)
but I have no idea how to set up a countdown which would count from 10 to 0 after changing sensor's state AND which would stop and reset on the state change and count again.
When the countdown actually will get to 0, it would set off another GPIO pin.
After solving that I would utilize OctoPrint's plugin
to handle the jam indication signal and pause the print.
I would also happily make better design for sensor and share it over thingiverse or something.
And yes, I am aware that a commercial all-in-one jam sensor already exists, but there is no fun in just buying everything and not using trash (and not stealing car toys). I also believe that my idea is a little bit cheaper.
Upvotes: 0
Views: 259
Reputation: 116
You'd use a timer.
import threading def hello(): print "hello, world" t = Timer(30.0, hello) t.start() # after 30 seconds, "hello, world" will be printed
from https://docs.python.org/3.8/library/threading.html#timer-objects
The code is similar once you move to a OctoPrint plugin.
def hello(): print("Hello World!") t = RepeatedTimer(1.0, hello) t.start() # prints "Hello World!" every second
Upvotes: 0