Fix jittering and cleanup

This commit is contained in:
2025-09-23 22:58:21 +02:00
parent acb3116c69
commit 7448c9aeb7
3 changed files with 54 additions and 50 deletions

View File

@@ -1,2 +1,3 @@
https://pinout.xyz/ Full pinout for the rpi3 https://pinout.xyz/ Full pinout for the rpi3
https://components101.com/sites/default/files/component_datasheet/SG90%20Servo%20Motor%20Datasheet.pdf Datasheet for the servomotor used https://components101.com/sites/default/files/component_datasheet/SG90%20Servo%20Motor%20Datasheet.pdf Datasheet for the servomotor used
https://ben.akrin.com/raspberry-pi-servo-jitter/ Blog post how to fix jittering

View File

@@ -1,73 +1,79 @@
import pigpio
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import time import time
import os import os
# Use the fat numberings
GPIO.setmode(GPIO.BCM)
# Constants # Constants
SERVO1_PIN = 18 SERVO1_PIN = 18
SERVO2_PIN = 19 SERVO2_PIN = 19
BUTTON1_FWD = 5 # Motor1
BUTTON1_FWD = 5
BUTTON1_BWD = 6 BUTTON1_BWD = 6
BUTTON2_FWD = 17 # Motor2 BUTTON2_FWD = 17
BUTTON2_BWD = 27 BUTTON2_BWD = 27
SHUTDOWN_BTN = 26 # Shutdown rpi SHUTDOWN_BTN = 26
# Setup pins MIN_PULSE = 1000 # In ms
for button in [BUTTON1_FWD, BUTTON1_BWD, BUTTON2_FWD, BUTTON2_BWD, SHUTDOWN_BTN]:
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(SERVO1_PIN, GPIO.OUT)
GPIO.setup(SERVO2_PIN, GPIO.OUT)
servo1 = GPIO.PWM(SERVO1_PIN, 50)
servo2 = GPIO.PWM(SERVO2_PIN, 50)
servo1.start(0)
servo2.start(0)
# Adjust this for maximum range
def pulse_to_duty(pulse_us):
return (pulse_us / 20000.0) * 100.0
# Pulse limits
MIN_PULSE = 1000
MAX_PULSE = 2000 MAX_PULSE = 2000
INIT_PULSE = 1500 INIT_PULSE = 1500
STEP = 10 # Adjust smoothness STEP = 10
SLEEP = 0.005 LOOP_DELAY = 0.01 # In seconds
# Setup
GPIO.setmode(GPIO.BCM)
for btn in [BUTTON1_FWD, BUTTON1_BWD, BUTTON2_FWD, BUTTON2_BWD, SHUTDOWN_BTN]:
GPIO.setup(btn, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
pi = pigpio.pi()
if not pi.connected:
print("Cannot connect to pigpio daemon!")
exit()
# Initialize servos
pulse1 = INIT_PULSE pulse1 = INIT_PULSE
pulse2 = INIT_PULSE pulse2 = INIT_PULSE
servo1.ChangeDutyCycle(pulse_to_duty(pulse1)) pi.set_servo_pulsewidth(SERVO1_PIN, pulse1)
servo2.ChangeDutyCycle(pulse_to_duty(pulse2)) pi.set_servo_pulsewidth(SERVO2_PIN, pulse2)
# Main loop # Helpers
def move_servo(current, target, step=STEP):
if current < target:
current = min(current + step, target)
elif current > target:
current = max(current - step, target)
return current
# Main
try: try:
while True: while True:
# Shutdown
if GPIO.input(SHUTDOWN_BTN) == GPIO.HIGH: if GPIO.input(SHUTDOWN_BTN) == GPIO.HIGH:
os.system("sudo shutdown now") os.system("sudo shutdown now")
# Motor 1 # Motor 1
if GPIO.input(BUTTON1_FWD) == GPIO.HIGH: target1 = pulse1
pulse1 = min(MAX_PULSE, pulse1 + STEP) if GPIO.input(BUTTON1_FWD):
elif GPIO.input(BUTTON1_BWD) == GPIO.HIGH: target1 = min(MAX_PULSE, pulse1 + STEP)
pulse1 = max(MIN_PULSE, pulse1 - STEP) elif GPIO.input(BUTTON1_BWD):
servo1.ChangeDutyCycle(pulse_to_duty(pulse1)) target1 = max(MIN_PULSE, pulse1 - STEP)
pulse1 = move_servo(pulse1, target1)
pi.set_servo_pulsewidth(SERVO1_PIN, pulse1)
# Motor 2 # Motor 2
if GPIO.input(BUTTON2_FWD) == GPIO.HIGH: target2 = pulse2
pulse2 = min(MAX_PULSE, pulse2 + STEP) if GPIO.input(BUTTON2_FWD):
elif GPIO.input(BUTTON2_BWD) == GPIO.HIGH: target2 = min(MAX_PULSE, pulse2 + STEP)
pulse2 = max(MIN_PULSE, pulse2 - STEP) elif GPIO.input(BUTTON2_BWD):
servo2.ChangeDutyCycle(pulse_to_duty(pulse2)) target2 = max(MIN_PULSE, pulse2 - STEP)
pulse2 = move_servo(pulse2, target2)
pi.set_servo_pulsewidth(SERVO2_PIN, pulse2)
time.sleep(SLEEP) time.sleep(LOOP_DELAY)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass
finally: finally:
servo1.stop() pi.set_servo_pulsewidth(SERVO1_PIN, 0)
servo2.stop() pi.set_servo_pulsewidth(SERVO2_PIN, 0)
pi.stop()
GPIO.cleanup() GPIO.cleanup()

View File

@@ -1,11 +1,9 @@
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import time import time
# ----------------------------
# Setup # Setup
# ---------------------------- GPIO.setmode(GPIO.BCM)
GPIO.setmode(GPIO.BCM) # Use BCM numbering PIN = 26
PIN = 26 # The pin you want to monitor (BCM26)
# Setup pin as input with pull-up # Setup pin as input with pull-up
GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
@@ -14,15 +12,14 @@ print(f"Monitoring GPIO{PIN}. Press Ctrl+C to exit.")
try: try:
while True: while True:
if GPIO.input(PIN): # Pin is HIGH (1) if GPIO.input(PIN):
print(f"GPIO{PIN} is HIGH") print(f"GPIO{PIN} is HIGH")
else: # Pin is LOW (0) else:
print(f"GPIO{PIN} is LOW") print(f"GPIO{PIN} is LOW")
time.sleep(0.1) # Check every 100 ms time.sleep(0.5)
except KeyboardInterrupt: except KeyboardInterrupt:
print("Exiting...") print("Exiting...")
finally: finally:
GPIO.cleanup() GPIO.cleanup()