#Phyton Script with klipper macros

117 messages · Page 1 of 1 (latest)

grand parcel
#

How can i run a phyton script with a macro? Or if possible control a servo connected to the rpi with macros and without a phyton script.

grand parcel
#

So i basically have this script (example not final one)

import time

servoPIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)

p = GPIO.PWM(servoPIN, 50) # GPIO 17 als PWM mit 50Hz
p.start(2.5) # Initialisierung
try:
  while True:
    p.ChangeDutyCycle(5)
    time.sleep(0.5)
    p.ChangeDutyCycle(7.5)
    time.sleep(0.5)
    p.ChangeDutyCycle(10)
    time.sleep(0.5)
    p.ChangeDutyCycle(12.5)
    time.sleep(0.5)
    p.ChangeDutyCycle(10)
    time.sleep(0.5)
    p.ChangeDutyCycle(7.5)
    time.sleep(0.5)
    p.ChangeDutyCycle(5)
    time.sleep(0.5)
    p.ChangeDutyCycle(2.5)
    time.sleep(0.5)
except KeyboardInterrupt:
  p.stop()
  GPIO.cleanup()```
and my question is where to save it and how to execute it (and what to adjust cause this is not written for klipper)
magic canyon
vale thorn
#

Why not simple setup a Linux mcu and control the pin?

grand parcel
#

and that data pin needs to send a pulse with a specific lenght

vale thorn
#

Never tried it on a Linux mcu but should be easy to try out for you

grand parcel
#

I configured the servo in the printer config but i get this error

#
pin: gpiochip0/gpio26
minimum_pulse_width: 0.0005
maximum_pulse_width: 0.0024```
#

camera is just the name

#

and then pulse width is from the datasheet

vale thorn
#

You missed the mcu name

#

Have you setup the Linux mcu first

grand parcel
#

yes

#

witht the makemenu

#

and then make flash

vale thorn
#

And did you add it also in the printer.cfg

grand parcel
#

i dont think so let me look into that

grand parcel
#
serial:  /dev/serial/by-path/platform-3f980000.usb-usb-0:1.3:1.0-port0
baud: 115200
restart_method: command

[mcu host]
serial: /tmp/klipper_host_mcu```
like this?
vale thorn
#

Yes

grand parcel
#

Okay starts with no errors

#

ill go try if the servo works

#

ignore this SET_SERVO SERVO=my_servo ANGLE=180

#

nothing happens no idea why

#
pin: host:gpiochip0/gpio26
minimum_pulse_width: 0.0005
maximum_pulse_width: 0.0024```
#

command i sent SET_SERVO SERVO=camera ANGLE=100

#

no errors

vale thorn
#

Maybe Klipper can not do it t the pi

#

Or your 5V is not strong enough

grand parcel
#

thats probably it

vale thorn
#

Or you simple used the wrong io

grand parcel
#

a good psu is already ordered because i get the undervolted warning a lot

vale thorn
#

I would use a scope to measure it but you might not have one

grand parcel
#

i dont

#

i guess thats the reason

#

Datasheet says this Operating voltage: 4.8 V (~5V)

#

ill wait for the power supply and then test

grand parcel
#

I tried to use my arduino for power and it still didnt work and no errors

#

The data pin is a pwm0 but my rpi 3b doesnt have a normal pwm pin

#

Maybe thats the issue idk?

#

Ill try to debug tomorrow maybe i made a mistake when setting up the mcu or smth

grand parcel
#

just a basic led also doesnt work

vale thorn
#

Than you most likly use the wrong IO

grand parcel
#

Ill just try with a different pin later

#

I only have pwm0 is that fine or do i have to do smth different

#

Or pwm1

grand parcel
#

not even an led works

#
white_pin: host:gpiochip0/gpio25```
#

line 25: "GPIO25" unused input active-high (gpioinfo)

#

and the cpu usage on the mcu and host is the same so its set up correctly (i guess)

vale thorn
#

Pins used by Klipper are seen

grand parcel
#

but gpio 25 is unused

#

but set as input

vale thorn
#
pin: rpi: gpio16
value:0```
#

Change rpi with host

grand parcel
#

in printer.cfg?

vale thorn
#

You will not get an output as long gpioinfo does not show it

#

Use my example, change the pin to host: gpio16

#

Restart Klipper and verify gpioinfo

grand parcel
#

and gpio25?

#

the led is in 25

vale thorn
#

Guy I provide you with an example that I know it is working

grand parcel
#

should i put it in printer.cfg or somewhere else

vale thorn
#

If that not working you might have missed the step with the group permission

#

Sorry but what is so hard to understand?
Put

[output_pin lock_M106]
pin: host: gpio16
value:0```
In your printer.cfg. Do a firmware restart. SSH in your pi and run gpioinfo to check if that IO is now marked as Klipper
grand parcel
#

set to klipper and output

#

and a switch shows up on the dashboard

#

and if i plug in a led its lights up

#

but not very bright (probably because of the psu)

vale thorn
#

What did gpioinfo tells you

grand parcel
#

line 16: "GPIO16" "klipper" output active-high [used]

vale thorn
#

So now test your led example remove the gpiochip

#

That should work also

grand parcel
#

should i remove the output_pin part?

vale thorn
#

You can use each pin only once

#

We know it is working in general so remove my example completely

grand parcel
#

the led is still not changing

#

its on all the time

#
white_pin: host: gpio16```
vale thorn
#

Is the pin shown in gpioinfo

grand parcel
#

line 16: "GPIO16" "klipper" output active-high [used]

#

yes

vale thorn
#

Good the rest is than your external hw

grand parcel
#

Ill just wait for the power supply maybe thats causing it

grand parcel
#

Alright the new power supply arrived and its still the same

#

No matter what i change the led is on but not very bright

#

And i cant turn it off

vale thorn
#

What kind of led do you use

grand parcel
#

These

vale thorn
#

lol how should that help me. I need a part number

grand parcel
#

Thats the datasheet

grand parcel
#

Bump

vale thorn
#

Have you placed a serial resistor as a current limit?

grand parcel
#

white one is gpio16 and black is ground

vale thorn
#

What value?

grand parcel
#

Id say 10k ohms

#

Yeah its 10k

vale thorn
#

No wonder why it is only glimmimg 3.3V/10k = 0.3mA

#

The data sheet expects a forward current of 20mA

#

But be aware you should not draw more that 16mA from a PI IO

#

So 3.3V/16mA=> ~205 Ohm so something around 220 would be ok. A 0.25W type is ok as it will draw ~0.05W (U*U/R)

#

And that is basic electrical knowledge and has nothing to do with 3d printing

grand parcel
#

But its on even when i turn the pin off

#

And its still not very bright with a 200 ohm resistor

#

I only have 200 and 330 ohm resistors and bigger/smaller ones but 200 is the closest i have

vale thorn
#

Honestly I have no idea what you did setup, you talk about a wrong PS and so one, I proof to you that the functionality of a klipper mcu controlling an GPIO is working.
The easist way to proof that is a multimeter that should show ~0V if the GPIO is off and around 3.3V if the IO is on. Also important that you did not enable the weak pullup of the IO