switched control.py to use pygame, works perfectly

This commit is contained in:
Lucca Pirovano
2026-06-11 14:23:15 -04:00
parent bdb09909e7
commit b4919bcbf6
2 changed files with 42 additions and 26 deletions
Executable → Regular
+41 -25
View File
@@ -1,55 +1,71 @@
from codrone_edu.drone import * from codrone_edu.drone import *
import keyboard import pygame
import time import time
import sys
# Initialize pygame
pygame.init()
screen = pygame.display.set_mode((400, 300)) # Small window (focus here)
pygame.display.set_caption("CoDrone Arrow Key Controller")
drone = Drone() drone = Drone()
drone.connect() # or drone.pair() depending on your setup drone.connect() # or drone.pair()
print("Connecting to drone...") print("Connecting...")
time.sleep(3) # Give it time to connect time.sleep(3)
try: try:
drone.takeoff() drone.takeoff()
print("Drone took off! Use Arrow Keys to fly.") print("Drone ready!")
print("↑ Forward | ↓ Backward | ← Left | → Right") print("Use Arrow Keys:")
print("Hold keys for continuous movement. Press 'q' to land and quit.") print("↑ Forward ↓ Backward ← Left → Right")
print("W = Up S = Down")
print("Press ESC or close window to land")
power = 25 # ← Keep this LOW for safety (15-30 recommended) power = 20 # Very safe & slow (15-25 recommended)
while True: clock = pygame.time.Clock()
# Reset all movement every loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE):
running = False
# Get current key states
keys = pygame.key.get_pressed()
# Reset all movement
drone.set_roll(0) drone.set_roll(0)
drone.set_pitch(0) drone.set_pitch(0)
drone.set_yaw(0) drone.set_yaw(0)
drone.set_throttle(0) drone.set_throttle(0)
# Arrow key controls (slow & safe) # Arrow keys for horizontal movement
if keyboard.is_pressed('up'): if keys[pygame.K_UP]:
drone.set_pitch(power) # Forward drone.set_pitch(power) # Forward
if keyboard.is_pressed('down'): if keys[pygame.K_DOWN]:
drone.set_pitch(-power) # Backward drone.set_pitch(-power) # Backward
if keyboard.is_pressed('left'): if keys[pygame.K_LEFT]:
drone.set_roll(-power) # Left drone.set_roll(-power) # Left
if keyboard.is_pressed('right'): if keys[pygame.K_RIGHT]:
drone.set_roll(power) # Right drone.set_roll(power) # Right
# Optional: Add throttle control with other keys # W/S for altitude
if keyboard.is_pressed('w'): if keys[pygame.K_w]:
drone.set_throttle(power) # Up drone.set_throttle(power) # Up
if keyboard.is_pressed('s'): if keys[pygame.K_s]:
drone.set_throttle(-power) # Down drone.set_throttle(-power) # Down
if keyboard.is_pressed('q'): drone.move(0.1) # Send command
print("Landing...") clock.tick(30) # ~30 updates per second (smooth but not too fast)
break
drone.move(0.1) # Send command every 0.1 seconds (smooth control)
time.sleep(0.05) # Fast loop for responsive feel
except Exception as e: except Exception as e:
print("Error:", e) print("Error:", e)
finally: finally:
print("Landing...")
drone.land() drone.land()
drone.disconnect() drone.disconnect()
print("Drone landed and disconnected.") pygame.quit()
print("Disconnected.")
+1 -1
View File
@@ -1,2 +1,2 @@
codrone_edu codrone_edu
keyboard pygame