added gamepad input

This commit is contained in:
Lucca Pirovano
2026-06-11 14:33:08 -04:00
parent b4919bcbf6
commit 24ce3cfd9a
+60 -37
View File
@@ -1,64 +1,87 @@
from codrone_edu.drone import * from codrone_edu.drone import *
import pygame import pygame
import time import time
import sys
# Initialize pygame # ====================== SETTINGS ======================
POWER = 22 # Safe & slow speed (15-30)
DEADZONE = 0.15
# =====================================================
pygame.init() pygame.init()
screen = pygame.display.set_mode((400, 300)) # Small window (focus here) screen = pygame.display.set_mode((500, 300))
pygame.display.set_caption("CoDrone Arrow Key Controller") pygame.display.set_caption("CoDrone Controller")
# Gamepad setup
pygame.joystick.init()
joysticks = [pygame.joystick.Joystick(i) for i in range(pygame.joystick.get_count())]
for joy in joysticks:
joy.init()
print(f"🎮 Detected: {joy.get_name()}")
drone = Drone() drone = Drone()
drone.connect() # or drone.pair() drone.connect()
print("Connecting...")
time.sleep(3) time.sleep(3)
try: try:
drone.takeoff() drone.takeoff()
print("✅ Drone ready!") print("✅ Drone flying! Controls: Arrows / Left Stick | W/S | A/D")
print("Use Arrow Keys:") print("ESC or Start button to land")
print("↑ Forward ↓ Backward ← Left → Right")
print("W = Up S = Down")
print("Press ESC or close window to land")
power = 20 # Very safe & slow (15-25 recommended)
clock = pygame.time.Clock() clock = pygame.time.Clock()
running = True running = True
while running: while running:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE): if event.type == pygame.QUIT or \
(event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE) or \
(event.type == pygame.JOYBUTTONDOWN and event.button in (7, 6)): # Start/Options
running = False running = False
# Get current key states
keys = pygame.key.get_pressed() keys = pygame.key.get_pressed()
roll = pitch = throttle = yaw = 0
# Reset all movement # ==================== KEYBOARD ====================
drone.set_roll(0) if keys[pygame.K_UP]: pitch = POWER
drone.set_pitch(0) if keys[pygame.K_DOWN]: pitch = -POWER
drone.set_yaw(0) if keys[pygame.K_LEFT]: roll = -POWER
drone.set_throttle(0) if keys[pygame.K_RIGHT]: roll = POWER
if keys[pygame.K_w]: throttle = POWER
if keys[pygame.K_s]: throttle = -POWER
if keys[pygame.K_a]: yaw = -POWER
if keys[pygame.K_d]: yaw = POWER
# Arrow keys for horizontal movement # ==================== GAMEPAD ====================
if keys[pygame.K_UP]: if joysticks:
drone.set_pitch(power) # Forward joy = joysticks[0]
if keys[pygame.K_DOWN]:
drone.set_pitch(-power) # Backward
if keys[pygame.K_LEFT]:
drone.set_roll(-power) # Left
if keys[pygame.K_RIGHT]:
drone.set_roll(power) # Right
# W/S for altitude # Left stick
if keys[pygame.K_w]: roll += int(joy.get_axis(0) * POWER)
drone.set_throttle(power) # Up pitch += int(-joy.get_axis(1) * POWER)
if keys[pygame.K_s]:
drone.set_throttle(-power) # Down
drone.move(0.1) # Send command # Right stick / Triggers
clock.tick(30) # ~30 updates per second (smooth but not too fast) if joy.get_numaxes() > 3:
throttle += int(-joy.get_axis(3) * POWER)
yaw += int(joy.get_axis(2) * POWER)
# Common trigger mapping (Xbox/PS)
if joy.get_numaxes() > 5:
yaw += int(joy.get_axis(5) * POWER) # Right trigger
yaw += int(-joy.get_axis(4) * POWER) # Left trigger
# Clamp values
roll = max(-100, min(100, roll))
pitch = max(-100, min(100, pitch))
throttle = max(-100, min(100, throttle))
yaw = max(-100, min(100, yaw))
# Apply movement
drone.set_roll(roll)
drone.set_pitch(pitch)
drone.set_throttle(throttle)
drone.set_yaw(yaw)
drone.move(0.1)
clock.tick(30)
except Exception as e: except Exception as e:
print("Error:", e) print("Error:", e)