Testing speed

This commit is contained in:
2025-09-23 22:16:51 +02:00
parent 12bbb35dc5
commit 39bca502a5

View File

@@ -9,13 +9,13 @@ SERVO1_PIN = 19
SERVO2_PIN = 18 SERVO2_PIN = 18
# Buttons # Buttons
BUTTON1_FWD = 5 # Motor1 forward BUTTON1_FWD = 5 # Motor1 increase speed
BUTTON1_BWD = 6 # Motor1 backward BUTTON1_BWD = 6 # Motor1 decrease speed
BUTTON2_FWD = 17 # Motor2 forward BUTTON2_FWD = 17 # Motor2 increase speed
BUTTON2_BWD = 27 # Motor2 backward BUTTON2_BWD = 27 # Motor2 decrease speed
SHUTDOWN_BTN = 26 # Shutdown button SHUTDOWN_BTN = 26 # Shutdown button
# Setup buttons (pulled down, pressed = HIGH) # Setup buttons
for button in [BUTTON1_FWD, BUTTON1_BWD, BUTTON2_FWD, BUTTON2_BWD, SHUTDOWN_BTN]: 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(button, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
@@ -35,14 +35,18 @@ def pulse_to_duty(pulse_us):
return (pulse_us / 20000.0) * 100.0 # 20ms period → duty % return (pulse_us / 20000.0) * 100.0 # 20ms period → duty %
# Pulse limits # Pulse limits
STOP_PULSE = 1500 MIN_PULSE = 1000
MIN_PULSE = 1000 MAX_PULSE = 1500
MAX_PULSE = 1500
# Initialize at 1200 µs
INIT_PULSE = 1200 INIT_PULSE = 1200
servo1.ChangeDutyCycle(pulse_to_duty(INIT_PULSE)) STEP = 10 # how much to change per press (µs)
servo2.ChangeDutyCycle(pulse_to_duty(INIT_PULSE))
# Speed variables
pulse1 = INIT_PULSE
pulse2 = INIT_PULSE
# Initialize servos
servo1.ChangeDutyCycle(pulse_to_duty(pulse1))
servo2.ChangeDutyCycle(pulse_to_duty(pulse2))
# ---------------------------- # ----------------------------
# Main loop # Main loop
@@ -54,19 +58,17 @@ try:
# Motor 1 # Motor 1
if GPIO.input(BUTTON1_FWD) == GPIO.HIGH: if GPIO.input(BUTTON1_FWD) == GPIO.HIGH:
servo1.ChangeDutyCycle(pulse_to_duty(MAX_PULSE)) pulse1 = min(MAX_PULSE, pulse1 + STEP)
elif GPIO.input(BUTTON1_BWD) == GPIO.HIGH: elif GPIO.input(BUTTON1_BWD) == GPIO.HIGH:
servo1.ChangeDutyCycle(pulse_to_duty(MIN_PULSE)) pulse1 = max(MIN_PULSE, pulse1 - STEP)
else: servo1.ChangeDutyCycle(pulse_to_duty(pulse1))
servo1.ChangeDutyCycle(pulse_to_duty(STOP_PULSE))
# Motor 2 # Motor 2
if GPIO.input(BUTTON2_FWD) == GPIO.HIGH: if GPIO.input(BUTTON2_FWD) == GPIO.HIGH:
servo2.ChangeDutyCycle(pulse_to_duty(MAX_PULSE)) pulse2 = min(MAX_PULSE, pulse2 + STEP)
elif GPIO.input(BUTTON2_BWD) == GPIO.HIGH: elif GPIO.input(BUTTON2_BWD) == GPIO.HIGH:
servo2.ChangeDutyCycle(pulse_to_duty(MIN_PULSE)) pulse2 = max(MIN_PULSE, pulse2 - STEP)
else: servo2.ChangeDutyCycle(pulse_to_duty(pulse2))
servo2.ChangeDutyCycle(pulse_to_duty(STOP_PULSE))
time.sleep(0.05) time.sleep(0.05)