Final
This commit is contained in:
48
motor.py
48
motor.py
@@ -2,55 +2,47 @@ import RPi.GPIO as GPIO
|
|||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
# Use the fat numberings
|
||||||
GPIO.setmode(GPIO.BCM)
|
GPIO.setmode(GPIO.BCM)
|
||||||
|
|
||||||
# Servo pins
|
# Constants
|
||||||
SERVO1_PIN = 19
|
SERVO1_PIN = 18
|
||||||
SERVO2_PIN = 18
|
SERVO2_PIN = 19
|
||||||
|
BUTTON1_FWD = 5 # Motor1
|
||||||
|
BUTTON1_BWD = 6
|
||||||
|
BUTTON2_FWD = 17 # Motor2
|
||||||
|
BUTTON2_BWD = 27
|
||||||
|
SHUTDOWN_BTN = 26 # Shutdown rpi
|
||||||
|
|
||||||
# Buttons
|
# Setup pins
|
||||||
BUTTON1_FWD = 5 # Motor1 increase speed
|
|
||||||
BUTTON1_BWD = 6 # Motor1 decrease speed
|
|
||||||
BUTTON2_FWD = 17 # Motor2 increase speed
|
|
||||||
BUTTON2_BWD = 27 # Motor2 decrease speed
|
|
||||||
SHUTDOWN_BTN = 26 # Shutdown button
|
|
||||||
|
|
||||||
# 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)
|
||||||
|
|
||||||
# Setup servo pins
|
|
||||||
GPIO.setup(SERVO1_PIN, GPIO.OUT)
|
GPIO.setup(SERVO1_PIN, GPIO.OUT)
|
||||||
GPIO.setup(SERVO2_PIN, GPIO.OUT)
|
GPIO.setup(SERVO2_PIN, GPIO.OUT)
|
||||||
|
|
||||||
servo1 = GPIO.PWM(SERVO1_PIN, 50) # 50Hz
|
servo1 = GPIO.PWM(SERVO1_PIN, 50)
|
||||||
servo2 = GPIO.PWM(SERVO2_PIN, 50)
|
servo2 = GPIO.PWM(SERVO2_PIN, 50)
|
||||||
servo1.start(0)
|
servo1.start(0)
|
||||||
servo2.start(0)
|
servo2.start(0)
|
||||||
|
|
||||||
# ----------------------------
|
# Adjust this for maximum range
|
||||||
# Helpers
|
|
||||||
# ----------------------------
|
|
||||||
def pulse_to_duty(pulse_us):
|
def pulse_to_duty(pulse_us):
|
||||||
return (pulse_us / 20000.0) * 100.0 # 20ms period → duty %
|
return (pulse_us / 20000.0) * 100.0
|
||||||
|
|
||||||
# Pulse limits
|
# Pulse limits
|
||||||
MIN_PULSE = 1000
|
MIN_PULSE = 1000
|
||||||
MAX_PULSE = 1500
|
MAX_PULSE = 2000
|
||||||
INIT_PULSE = 1200
|
INIT_PULSE = 1500
|
||||||
STEP = 10 # how much to change per press (µs)
|
STEP = 10 # Adjust smoothness
|
||||||
|
SLEEP = 0.005
|
||||||
# Speed variables
|
|
||||||
pulse1 = INIT_PULSE
|
|
||||||
pulse2 = INIT_PULSE
|
|
||||||
|
|
||||||
# Initialize servos
|
# Initialize servos
|
||||||
|
pulse1 = INIT_PULSE
|
||||||
|
pulse2 = INIT_PULSE
|
||||||
servo1.ChangeDutyCycle(pulse_to_duty(pulse1))
|
servo1.ChangeDutyCycle(pulse_to_duty(pulse1))
|
||||||
servo2.ChangeDutyCycle(pulse_to_duty(pulse2))
|
servo2.ChangeDutyCycle(pulse_to_duty(pulse2))
|
||||||
|
|
||||||
# ----------------------------
|
|
||||||
# Main loop
|
# Main loop
|
||||||
# ----------------------------
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
if GPIO.input(SHUTDOWN_BTN) == GPIO.HIGH:
|
if GPIO.input(SHUTDOWN_BTN) == GPIO.HIGH:
|
||||||
@@ -70,7 +62,7 @@ try:
|
|||||||
pulse2 = max(MIN_PULSE, pulse2 - STEP)
|
pulse2 = max(MIN_PULSE, pulse2 - STEP)
|
||||||
servo2.ChangeDutyCycle(pulse_to_duty(pulse2))
|
servo2.ChangeDutyCycle(pulse_to_duty(pulse2))
|
||||||
|
|
||||||
time.sleep(0.05)
|
time.sleep(SLEEP)
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user