Compare commits

...

3 Commits

2 changed files with 48 additions and 14 deletions
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/python
# This test assumes you are trying to control a motor through a DRV8833 motor
# controller chip. This general approach should work for other motors as well.
import lgpio
import time
# pin assignments
gpio_pwm = 18 #MOTA1
gpio_reverse = 16 #MOTA2
gpio_notsleep = 24 #MSLEEP
# parameters
pwm_freq = 1000
h = lgpio.gpiochip_open(0)
try:
lgpio.gpio_claim_output(h, gpio_reverse)
lgpio.gpio_claim_output(h, gpio_notsleep)
lgpio.gpio_write(h, gpio_reverse, 0) # low on reverse pin to turn off h-bridge MOTA2
lgpio.gpio_write(h, gpio_notsleep, 1) # high on /sleep to enable both h-bridges
while True:
for pwm_level in range(0,100):
lgpio.tx_pwm(h, gpio_pwm, pwm_freq, pwm_level)
pwm_level += 10
time.sleep(0.05)
for pwm_level in range(100,0,-1):
lgpio.tx_pwm(h, gpio_pwm, pwm_freq, pwm_level)
pwm_level += 10
time.sleep(0.05)
finally:
lgpio.gpio_write(h, gpio_notsleep, 1) # high on /sleep to disable
lgpio.gpiochip_close(h) # Always close!
+13 -13
View File
@@ -5,21 +5,21 @@ import time
# Open GPIO chip (0 = /dev/gpiochip0, on Pi 5 gpiochip4 also possible) # Open GPIO chip (0 = /dev/gpiochip0, on Pi 5 gpiochip4 also possible)
h = lgpio.gpiochip_open(0) h = lgpio.gpiochip_open(0)
try:
lgpio.gpio_claim_output(h, 16) # GPIO16 as output (MOTA2)
lgpio.gpio_write(h, 16, 0) # LOW
# Hardware PWM on GPIO18 (pin 12) must be PWM-capable # Hardware PWM on GPIO18 (pin 12) must be PWM-capable
# Frequency: 1000 Hz, duty cycle: 50% pwm_pin = 18
lgpio.tx_pwm(h, 18, 1000, 50) pwm_freq = 1000
time.sleep(2) try:
while True:
for pwm_level in range(0,100):
lgpio.tx_pwm(h, pwm_pin, pwm_freq, pwm_level)
pwm_level += 10
time.sleep(0.05)
for pwm_level in range(100,0,-1):
lgpio.tx_pwm(h, pwm_pin, pwm_freq, pwm_level)
pwm_level += 10
time.sleep(0.05)
# Reduce duty cycle to 20%
lgpio.tx_pwm(h, 18, 1000, 20)
time.sleep(2)
lgpio.tx_pwm(h, 18, 0, 0) # Stop PWM
finally: finally:
lgpio.tx_pwm(h, pwm_pin, 0, 0) # Stop PWM
lgpio.gpiochip_close(h) # Always close! lgpio.gpiochip_close(h) # Always close!