Last active
May 29, 2023 22:50
-
-
Save boulama/aae1e9e9ad095a2c97ff2e3afba632b9 to your computer and use it in GitHub Desktop.
This code controls a robot's movement by taking user input and controlling the GPIO pins of a Raspberry Pi to drive the motors in different directions.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import RPi.GPIO as GPIO | |
from time import sleep | |
import curses | |
# Define GPIO pin numbers | |
RIGHT_FORWARD_PIN = 17 | |
RIGHT_BACKWARD_PIN = 18 | |
RIGHT_ENABLE_PIN = 4 | |
LEFT_FORWARD_PIN = 22 | |
LEFT_BACKWARD_PIN = 23 | |
LEFT_ENABLE_PIN = 27 | |
BACK_RIGHT_FORWARD_PIN = 6 | |
BACK_RIGHT_BACKWARD_PIN = 12 | |
BACK_RIGHT_ENABLE_PIN = 16 | |
BACK_LEFT_FORWARD_PIN = 24 | |
BACK_LEFT_BACKWARD_PIN = 25 | |
BACK_LEFT_ENABLE_PIN = 26 | |
# Set up GPIO | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BCM) | |
# Set up motor pins as outputs | |
GPIO.setup([RIGHT_FORWARD_PIN, RIGHT_BACKWARD_PIN, RIGHT_ENABLE_PIN], GPIO.OUT) | |
GPIO.setup([LEFT_FORWARD_PIN, LEFT_BACKWARD_PIN, LEFT_ENABLE_PIN], GPIO.OUT) | |
GPIO.setup([BACK_RIGHT_FORWARD_PIN, BACK_RIGHT_BACKWARD_PIN, BACK_RIGHT_ENABLE_PIN], GPIO.OUT) | |
GPIO.setup([BACK_LEFT_FORWARD_PIN, BACK_LEFT_BACKWARD_PIN, BACK_LEFT_ENABLE_PIN], GPIO.OUT) | |
# Initialize PWM objects | |
right_motor_pwm = GPIO.PWM(RIGHT_ENABLE_PIN, 100) | |
left_motor_pwm = GPIO.PWM(LEFT_ENABLE_PIN, 100) | |
back_right_motor_pwm = GPIO.PWM(BACK_RIGHT_ENABLE_PIN, 100) | |
back_left_motor_pwm = GPIO.PWM(BACK_LEFT_ENABLE_PIN, 100) | |
# Start PWM output | |
right_motor_pwm.start(75) | |
left_motor_pwm.start(75) | |
back_right_motor_pwm.start(75) | |
back_left_motor_pwm.start(75) | |
# Function to move forward | |
def move_forward(): | |
GPIO.output(RIGHT_FORWARD_PIN, GPIO.HIGH) | |
GPIO.output(RIGHT_BACKWARD_PIN, GPIO.LOW) | |
GPIO.output(LEFT_FORWARD_PIN, GPIO.HIGH) | |
GPIO.output(LEFT_BACKWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_RIGHT_FORWARD_PIN, GPIO.HIGH) | |
GPIO.output(BACK_RIGHT_BACKWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_LEFT_FORWARD_PIN, GPIO.HIGH) | |
GPIO.output(BACK_LEFT_BACKWARD_PIN, GPIO.LOW) | |
print("Moving forward") | |
# Function to move backward | |
def move_backward(): | |
GPIO.output(RIGHT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(RIGHT_BACKWARD_PIN, GPIO.HIGH) | |
GPIO.output(LEFT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(LEFT_BACKWARD_PIN, GPIO.HIGH) | |
GPIO.output(BACK_RIGHT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_RIGHT_BACKWARD_PIN, GPIO.HIGH) | |
GPIO.output(BACK_LEFT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_LEFT_BACKWARD_PIN, GPIO.HIGH) | |
print("Moving backward") | |
# Function to turn right | |
def turn_right(): | |
GPIO.output(RIGHT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(RIGHT_BACKWARD_PIN, GPIO.HIGH) | |
GPIO.output(LEFT_FORWARD_PIN, GPIO.HIGH) | |
GPIO.output(LEFT_BACKWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_RIGHT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_RIGHT_BACKWARD_PIN, GPIO.HIGH) | |
GPIO.output(BACK_LEFT_FORWARD_PIN, GPIO.HIGH) | |
GPIO.output(BACK_LEFT_BACKWARD_PIN, GPIO.LOW) | |
print("Turning right") | |
# Function to turn left | |
def turn_left(): | |
GPIO.output(RIGHT_FORWARD_PIN, GPIO.HIGH) | |
GPIO.output(RIGHT_BACKWARD_PIN, GPIO.LOW) | |
GPIO.output(LEFT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(LEFT_BACKWARD_PIN, GPIO.HIGH) | |
GPIO.output(BACK_RIGHT_FORWARD_PIN, GPIO.HIGH) | |
GPIO.output(BACK_RIGHT_BACKWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_LEFT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_LEFT_BACKWARD_PIN, GPIO.HIGH) | |
print("Turning left") | |
# Function to stop the robot | |
def stop_robot(): | |
GPIO.output(RIGHT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(RIGHT_BACKWARD_PIN, GPIO.LOW) | |
GPIO.output(LEFT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(LEFT_BACKWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_RIGHT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_RIGHT_BACKWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_LEFT_FORWARD_PIN, GPIO.LOW) | |
GPIO.output(BACK_LEFT_BACKWARD_PIN, GPIO.LOW) | |
print("Stopping") | |
actions = { | |
curses.KEY_UP: move_forward, | |
curses.KEY_DOWN: move_backward, | |
curses.KEY_LEFT: turn_left, | |
curses.KEY_RIGHT: turn_right, | |
} | |
def main(window): | |
next_key = None | |
while True: | |
curses.halfdelay(1) | |
if next_key is None: | |
key = window.getch() | |
else: | |
key = next_key | |
next_key = None | |
if key != -1: | |
# KEY PRESSED | |
curses.halfdelay(3) | |
action = actions.get(key) | |
if action is not None: | |
action() | |
next_key = key | |
while next_key == key: | |
next_key = window.getch() | |
# KEY RELEASED | |
stop_robot() | |
curses.wrapper(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment