Created
November 30, 2024 06:19
-
-
Save Suave101/ecbfe86314127ff157800c9ebe145314 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 math | |
| import os | |
| import time | |
| import socket | |
| import threading | |
| import pygame | |
| pygame.init() | |
| pygame.joystick.init() | |
| joystick = pygame.joystick.Joystick(0) | |
| def send(message): | |
| try: | |
| sock.sendto(message.encode(), tello_address) | |
| print("Sending message: " + message) | |
| except Exception as e: | |
| print("Error sending: " + str(e)) | |
| def receive(): | |
| while True: | |
| try: | |
| response, _ = sock.recvfrom(128) | |
| message = response.decode(encoding='utf-8') | |
| print("Received message: " + message) | |
| except Exception as e: | |
| sock.close() | |
| print("Error receiving: " + str(e)) | |
| break | |
| tello_address = ('192.168.10.1', 8889) | |
| sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
| sock.bind(('', 8889)) | |
| receiveThread = threading.Thread(target=receive) | |
| receiveThread.daemon = True | |
| receiveThread.start() | |
| send("command") | |
| time.sleep(3) | |
| send("takeoff") | |
| time.sleep(3) | |
| while True: | |
| pygame.event.get() | |
| # print(f"LR: {joystick.get_axis(0)}") # Left: Left and Right (+) | |
| # print(f"UD: {-joystick.get_axis(1)}") # Left: Up and Down (-) | |
| # print(f"Axis 3: {joystick.get_axis(2)}") # Right: Left and Right (+) | |
| # print(f"Axis 4: {-joystick.get_axis(3)}") # Right: Up and Down (-) | |
| send(f"rc {math.floor(joystick.get_axis(0) * 100)} {-1 * math.floor(joystick.get_axis(1) * 100)} {math.floor(joystick.get_axis(2) * 100)} {-1 * math.floor(joystick.get_axis(3) * 100)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment