import RPi.GPIO as GPIO
import time
import sys
# --- Распиновка (BCM) ---
IN1 = 20 # мотор A
IN2 = 16 # мотор A
IN3 = 26 # мотор B
IN4 = 19 # мотор B
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
for pin in (IN1, IN2, IN3, IN4):
GPIO.setup(pin, GPIO.OUT)
GPIO.output(pin, GPIO.LOW)
def motor_a(direction):
"""direction: 1 = вперёд, -1 = назад, 0 = стоп"""
if direction > 0:
GPIO.output(IN1, GPIO.HIGH)
GPIO.output(IN2, GPIO.LOW)
elif direction < 0:
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.HIGH)
else:
GPIO.output(IN1, GPIO.LOW)
GPIO.output(IN2, GPIO.LOW)
def motor_b(direction):
if direction > 0:
GPIO.output(IN3, GPIO.HIGH)
GPIO.output(IN4, GPIO.LOW)
elif direction < 0:
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.HIGH)
else:
GPIO.output(IN3, GPIO.LOW)
GPIO.output(IN4, GPIO.LOW)
def stop():
motor_a(0)
motor_b(0)
def forward():
motor_a(1); motor_b(1)
def backward():
motor_a(-1); motor_b(-1)
def turn_left():
motor_a(-1); motor_b(1)
def turn_right():
motor_a(1); motor_b(-1)
def cleanup():
stop()
GPIO.cleanup()