switched control.py to use pygame, works perfectly
This commit is contained in:
Executable → Regular
+41
-25
@@ -1,55 +1,71 @@
|
||||
from codrone_edu.drone import *
|
||||
import keyboard
|
||||
import pygame
|
||||
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.connect() # or drone.pair() depending on your setup
|
||||
drone.connect() # or drone.pair()
|
||||
|
||||
print("Connecting to drone...")
|
||||
time.sleep(3) # Give it time to connect
|
||||
print("Connecting...")
|
||||
time.sleep(3)
|
||||
|
||||
try:
|
||||
drone.takeoff()
|
||||
print("Drone took off! Use Arrow Keys to fly.")
|
||||
print("↑ Forward | ↓ Backward | ← Left | → Right")
|
||||
print("Hold keys for continuous movement. Press 'q' to land and quit.")
|
||||
print("✅ Drone ready!")
|
||||
print("Use Arrow Keys:")
|
||||
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:
|
||||
# Reset all movement every loop
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
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_pitch(0)
|
||||
drone.set_yaw(0)
|
||||
drone.set_throttle(0)
|
||||
|
||||
# Arrow key controls (slow & safe)
|
||||
if keyboard.is_pressed('up'):
|
||||
# Arrow keys for horizontal movement
|
||||
if keys[pygame.K_UP]:
|
||||
drone.set_pitch(power) # Forward
|
||||
if keyboard.is_pressed('down'):
|
||||
if keys[pygame.K_DOWN]:
|
||||
drone.set_pitch(-power) # Backward
|
||||
if keyboard.is_pressed('left'):
|
||||
if keys[pygame.K_LEFT]:
|
||||
drone.set_roll(-power) # Left
|
||||
if keyboard.is_pressed('right'):
|
||||
if keys[pygame.K_RIGHT]:
|
||||
drone.set_roll(power) # Right
|
||||
|
||||
# Optional: Add throttle control with other keys
|
||||
if keyboard.is_pressed('w'):
|
||||
# W/S for altitude
|
||||
if keys[pygame.K_w]:
|
||||
drone.set_throttle(power) # Up
|
||||
if keyboard.is_pressed('s'):
|
||||
if keys[pygame.K_s]:
|
||||
drone.set_throttle(-power) # Down
|
||||
|
||||
if keyboard.is_pressed('q'):
|
||||
print("Landing...")
|
||||
break
|
||||
|
||||
drone.move(0.1) # Send command every 0.1 seconds (smooth control)
|
||||
time.sleep(0.05) # Fast loop for responsive feel
|
||||
drone.move(0.1) # Send command
|
||||
clock.tick(30) # ~30 updates per second (smooth but not too fast)
|
||||
|
||||
except Exception as e:
|
||||
print("Error:", e)
|
||||
|
||||
finally:
|
||||
print("Landing...")
|
||||
drone.land()
|
||||
drone.disconnect()
|
||||
print("Drone landed and disconnected.")
|
||||
pygame.quit()
|
||||
print("Disconnected.")
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
codrone_edu
|
||||
keyboard
|
||||
pygame
|
||||
|
||||
Reference in New Issue
Block a user