0

I am currently trying to connect a laser to my 3D printer and give a TTL signal manipulated by an M106 command.

The "official" way didn't work out for me so right now I am trying to control it by my Raspberry Pi 3B+ with Octoprint on it. I am using a plugin called Fan Speed Mirror to transform the Fan Speed (M106) to a PWM signal on a GPIO pin.

Therefore I have copied this bash script, which is calling the python script and giving it the M106 number (at least I think that's what it does, please note that I know only basic programming stuff and near to nothing about python or bash).

fan.sh script:

#!/bin/bash
killall fan.py
echo 1ドル 2ドル 3ドル >>/home/pi/fan.txt
python /home/pi/fan.py 1ドル
exit $?

fan.py script:

#!/usr/bin/python3
import sys
import time
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
s = int(sys.argv[1])
dc = s*100/255
p = GPIO.PWM(11,1000) # channel=11 frequency=1kHz
p.start(dc)
time.sleep(100000000000000000000)

As you can see I am trying to run the PWM signal as long as no new M106 command is entered.

But that's not really working out.

Instead it runs the first M106 command and then stops the gcode.

I already asked the developer of the plugin here: https://github.com/b-morgan/OctoPrint-FanSpeedMirror/issues/3

But I was hoping to get some input here as well. I hope that what I am trying to achieve is even possible.

joan
71.9k5 gold badges76 silver badges108 bronze badges
asked Dec 28, 2021 at 11:54
3
  • 1
    what is your specific question? Commented Dec 28, 2021 at 16:25
  • What is a M106 command? Commented Dec 28, 2021 at 21:12
  • Why use a bash script to run a python script. Python is a scripting language? Commented Dec 28, 2021 at 21:14

2 Answers 2

0

If I was going to do this I would use hardware PWM which continues to run after the program exits.

Hardware PWM is available in my Pi.GPIO (which is a superset of RPi.GPIO). It is also available in pigpio

The following modification to your program should work (I have not tested it) and avoid the strange sleep and need to kill processes, although it is rather inelegant.

Call directly - DO NOT use any scripts.

#!/usr/bin/python3
import sys
from signal import pause
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
GPIO.setwarnings(False)
s = int(sys.argv[1])
dc = s*100/255
p = GPIO.PWM(11,1000) # channel=11 frequency=1kHz
p.start(dc)
pause()

PS You should not get into the bad habit of using GPIO.BOARD - use BCM numbers like practically every other program.

NOTE RPi.GPIO PWM is software timed and imprecise - it is more suitable for dimming LEDs, whether this matters depends on your hardware.

answered Dec 28, 2021 at 23:18
1
  • I copied your script and tried to call it directly but that didn't work somehow. But then I called it by using the batch script with the added '&' as joan suggested and now it works perfectly as intended. Thank you both very much!! Commented Dec 29, 2021 at 20:23
0

If I guess properly at the question it may be solved by changing

python /home/pi/fan.py 1ドル

to

python /home/pi/fan.py 1ドル &

answered Dec 28, 2021 at 16:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.