Fix up structure and README

This commit is contained in:
Andrew Johnson
2025-04-03 20:31:10 +02:00
parent 1480a75a61
commit 3b5ab2f7cd
19 changed files with 7 additions and 1 deletions

34
examples/10_throw_takeoff.py Executable file
View File

@@ -0,0 +1,34 @@
from tello_sim.tello_sim_client import TelloSimClient
import time
# Create a Tello instance
tello = TelloSimClient()
# Connect to Tello
tello.connect()
print("Get ready to throw in ...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
print("Throw the drone within 5 seconds!")
tello.initiate_throw_takeoff()
for i in range(5, 0, -1):
print(i)
time.sleep(1)
print("Waiting for...")
for i in range(10, 0, -1):
print(i)
time.sleep(1)
print("Landing")
# Land
tello.land()
# End the connection
tello.end()

33
examples/11_emergency_stop.py Executable file
View File

@@ -0,0 +1,33 @@
from tello_sim.tello_sim_client import TelloSimClient
import time
from tello_sim.tello_sim_client import TelloSimClient
# Create a Tello instance
tello = TelloSimClient()
# Connect to Tello
tello.connect()
print("Starting flying in ...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
# Takeoff
print("Take off")
tello.takeoff()
for i in range(10, 0, -1):
print("Get Ready to catch drone!")
print("Emergency stop in", i)
time.sleep(1)
print("Emergency stop now!")
# Land
tello.emergency()
# End the connection
tello.end()

59
examples/12_flips.py Executable file
View File

@@ -0,0 +1,59 @@
from tello_sim.tello_sim_client import TelloSimClient
import time
ROTATION_DEGREES = 90
HIGHT_CM = 100
# Create a Tello instance
tello = TelloSimClient()
# Connect to Tello
tello.connect()
print("Starting flying in ...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
def pause():
print("Hovering for...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
def warn_of_flip(direction: str):
print("WARNING!")
print(f"Flipping {direction} in ...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
flip_func = getattr(tello, f"flip_{direction}")
flip_func()
# Takeoff
print("Take off")
tello.takeoff()
tello.move_up(HIGHT_CM)
warn_of_flip("left")
warn_of_flip("right")
warn_of_flip("forward")
warn_of_flip("back")
pause()
print("Landing")
# Land
tello.land()
# End the connection
tello.end()

158
examples/13_rc_movement.py Normal file
View File

@@ -0,0 +1,158 @@
from typing import Callable
from tello_sim.tello_sim_client import TelloSimClient
import time
SPEED_SETTING_CM_S = 30
MOVEMENT_MAGNITUDE = 30
TIME_PER_ACTION_SECS = 3
# Create a Tello instance
tello = TelloSimClient()
# Connect to Tello
tello.connect()
print("Starting flying in ...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
# Takeoff
print("Take off")
tello.takeoff()
print("Setting speed to", SPEED_SETTING_CM_S)
tello.set_speed(SPEED_SETTING_CM_S)
def do_action_for_time(label: str, action: Callable, time_in_seconds: int):
print(label)
action()
time.sleep(time_in_seconds)
left_right_velocity = 0
forward_backward_velocity = 0
up_down_velocity = 0
yaw_velocity = 0
do_action_for_time(
"Staying still",
lambda: tello.send_rc_control(
left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity
),
TIME_PER_ACTION_SECS,
)
# Move forward
left_right_velocity = 0
forward_backward_velocity = MOVEMENT_MAGNITUDE
up_down_velocity = 0
yaw_velocity = 0
do_action_for_time(
"Moving Forward",
lambda: tello.send_rc_control(
left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity
),
TIME_PER_ACTION_SECS,
)
# Move Backwards
left_right_velocity = 0
forward_backward_velocity = -MOVEMENT_MAGNITUDE
up_down_velocity = 0
yaw_velocity = 0
do_action_for_time(
"Moving backwards",
lambda: tello.send_rc_control(
left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity
),
TIME_PER_ACTION_SECS,
)
# Move Left
left_right_velocity = MOVEMENT_MAGNITUDE
forward_backward_velocity = 0
up_down_velocity = 0
yaw_velocity = 0
do_action_for_time(
"Moving left",
lambda: tello.send_rc_control(
left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity
),
TIME_PER_ACTION_SECS,
)
# Move Right
left_right_velocity = -MOVEMENT_MAGNITUDE
forward_backward_velocity = 0
up_down_velocity = 0
yaw_velocity = 0
do_action_for_time(
"Moving right",
lambda: tello.send_rc_control(
left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity
),
TIME_PER_ACTION_SECS,
)
# Move Up
left_right_velocity = 0
forward_backward_velocity = 0
up_down_velocity = MOVEMENT_MAGNITUDE
yaw_velocity = 0
do_action_for_time(
"Moving up",
lambda: tello.send_rc_control(
left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity
),
TIME_PER_ACTION_SECS,
)
# Move down
left_right_velocity = 0
forward_backward_velocity = 0
up_down_velocity = -MOVEMENT_MAGNITUDE
yaw_velocity = 0
do_action_for_time(
"Moving down",
lambda: tello.send_rc_control(
left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity
),
TIME_PER_ACTION_SECS,
)
# Turn Left
left_right_velocity = 0
forward_backward_velocity = 0
up_down_velocity = 0
yaw_velocity = MOVEMENT_MAGNITUDE
do_action_for_time(
"Turning left",
lambda: tello.send_rc_control(
left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity
),
TIME_PER_ACTION_SECS,
)
# Turn Right
left_right_velocity = 0
forward_backward_velocity = 0
up_down_velocity = 0
yaw_velocity = -MOVEMENT_MAGNITUDE
do_action_for_time(
"Turning right",
lambda: tello.send_rc_control(
left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity
),
TIME_PER_ACTION_SECS,
)
print("Landing")
# Land
tello.land()
# End the connection
tello.end()

42
examples/14_speed.py Normal file
View File

@@ -0,0 +1,42 @@
from tello_sim.tello_sim_client import TelloSimClient
import time
TEST_DISTANCE = 500
# Create a Tello instance
tello = TelloSimClient()
# Connect to Tello
tello.connect()
def pause(next_action: str):
print(f"Hovering before {next_action}")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
print("Starting flying in ...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
# Takeoff
print("Take off")
tello.takeoff()
pause("ascending")
tello.move_up(100)
for speed in range(10, 101, 10):
pause(f"speed change to {speed} cm/s")
print("Setting speed to", speed, "cm/s")
tello.set_speed(speed)
print(f"Testing forward and back at {speed} cm/s")
tello.move_forward(1000)
tello.move_back(TEST_DISTANCE)
time.sleep(1)
pause("landing")
print("Finished Landing")
tello.land()

6
examples/1_check_import.py Executable file
View File

@@ -0,0 +1,6 @@
from tello_sim.tello_sim_client import TelloSimClient
print(TelloSimClient)
print("Tello import works fine")

View File

@@ -0,0 +1,34 @@
import time
from tello_sim.tello_sim_client import TelloSimClient
# Create a Tello instance
tello = TelloSimClient()
print("Attempting to connect to drone ...")
# Connect to Tello
tello.connect()
print("Starting flying in ...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
# Takeoff
print("Take off")
tello.takeoff()
print("Hovering for...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
print("Landing")
# Land
tello.land()
# End the connection
tello.end()

52
examples/3_drone_information.py Executable file
View File

@@ -0,0 +1,52 @@
import time
from tello_sim.tello_sim_client import TelloSimClient
# Create a Tello instance
tello = TelloSimClient()
# Connect to Tello
tello.connect()
print("Get some information from the drone")
# Print battery capacity
battery_capacity = tello.get_battery()
print("Battery Capacity:", battery_capacity, "%")
# Print distance from time-of-flight sensor
print("Distance (TOF):", tello.get_distance_tof(), "cm")
# Print height
print("Height:", tello.get_height(), "cm")
# Print flight time
print("Flight Time:", tello.get_flight_time(), "seconds")
# Print speed in the x, y, z directions
print("Speed X:", tello.get_speed_x(), "cm/s")
print("Speed Y:", tello.get_speed_y(), "cm/s")
print("Speed Z:", tello.get_speed_z(), "cm/s")
# Print acceleration in the x, y, z directions
print("Acceleration X:", tello.get_acceleration_x(), "cm/s²")
print("Acceleration Y:", tello.get_acceleration_y(), "cm/s²")
print("Acceleration Z:", tello.get_acceleration_z(), "cm/s²")
print("Pitch", tello.get_pitch(), "degrees")
print("Roll", tello.get_roll(), "degrees")
print("Yaw", tello.get_yaw(), "degrees")
print("IMU data", tello.query_attitude())
print("State", tello.get_current_state())

View File

@@ -0,0 +1,32 @@
from tello_sim.tello_sim_client import TelloSimClient
import cv2
# Create a Tello instance
tello = TelloSimClient()
# Desired window size
WINDOW_WIDTH = 640
WINDOW_HEIGHT = 360
# Connect to Tello
tello.connect()
tello.streamon()
# Create a normal, resizable window
cv2.namedWindow("frame", cv2.WINDOW_NORMAL)
cv2.resizeWindow("frame", WINDOW_WIDTH, WINDOW_HEIGHT)
try:
while True:
img = tello.get_frame_read().frame
if img is not None:
img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
# Resize the image to fit the window size
img_resized = cv2.resize(img_bgr, (WINDOW_WIDTH, WINDOW_HEIGHT), interpolation=cv2.INTER_AREA)
cv2.imshow("frame", img_resized)
if cv2.waitKey(1) == ord('q'):
break
except KeyboardInterrupt:
pass
finally:
print("fin")
cv2.destroyAllWindows()

31
examples/5_take_a_picture.py Executable file
View File

@@ -0,0 +1,31 @@
import os
import cv2
import time
import numpy as np
from tello_sim.tello_sim_client import TelloSimClient
# Initialize and connect
tello = TelloSimClient()
tello.connect()
tello.streamon()
# Takeoff
tello.takeoff()
time.sleep(5) # Let drone stabilize after takeoff
# Get frame object
frame_read = tello.get_frame_read()
tello.streamoff()
# Prepare directory to save
script_dir = os.path.dirname(__file__)
artifact_folder_path = os.path.join(script_dir, "../../artifacts/images")
os.makedirs(artifact_folder_path, exist_ok=True)
# Save the frame
save_path = os.path.join(artifact_folder_path, "picture.png")
cv2.imwrite(save_path, np.array(frame_read.frame))
# Land
tello.land()

82
examples/6_record_video.py Executable file
View File

@@ -0,0 +1,82 @@
import os
from threading import Thread
import time
from typing import cast
from tello_sim.tello_sim_client import TelloSimClient
import cv2
# Configuration
FPS = 30
# Initialize Tello connector
tello = TelloSimClient()
tello.connect()
tello.streamon()
time.sleep(2)
# Define paths to save inside tello_recording
recording_folder = os.path.join(os.getcwd(), "output", "tello_recording")
images_folder_path = os.path.join(recording_folder, "frames")
video_file_path = os.path.join(recording_folder, "video.mp4")
os.makedirs(images_folder_path, exist_ok=True)
# Recording control
keep_recording = True
def capture_images():
frame_count = 0
while keep_recording:
frame = tello.get_frame_read().frame
if frame is not None:
image_file_path = os.path.join(images_folder_path, f"frame_{frame_count:04d}.jpg")
cv2.imwrite(image_file_path, cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
frame_count += 1
print(f"[Capture] Saved frame {frame_count}")
else:
print("[Capture] No frame received.")
time.sleep(1 / FPS)
print("[Capture] Finished capturing frames.")
# Start recording thread
recorder_thread = Thread(target=capture_images)
recorder_thread.start()
# Drone mission
tello.takeoff()
time.sleep(5)
tello.move_up(50)
time.sleep(5)
tello.rotate_counter_clockwise(360)
time.sleep(5)
tello.land()
# Stop recording
keep_recording = False
recorder_thread.join()
# Create video from frames
frame_files = sorted(os.listdir(images_folder_path))
if frame_files:
first_frame = cv2.imread(os.path.join(images_folder_path, frame_files[0]))
height, width, _ = first_frame.shape
video_writer = cv2.VideoWriter(
video_file_path, cv2.VideoWriter_fourcc(*"mp4v"), FPS, (width, height) # type: ignore
)
for frame_file in frame_files:
frame_path = os.path.join(images_folder_path, frame_file)
frame = cv2.imread(frame_path)
video_writer.write(frame)
video_writer.release()
print(f"[Video] Video saved at {video_file_path}")
else:
print("[Video] No frames captured. Video not created.")
print("Finished creating video")
tello.streamoff()

60
examples/7_movement.py Executable file
View File

@@ -0,0 +1,60 @@
from tello_sim.tello_sim_client import TelloSimClient
import time
MOVEMENT_DISTANCE_CM = 70
# Create a Tello instance
tello = TelloSimClient()
# Connect to Tello
tello.connect()
print("Starting flying in ...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
# Takeoff
print("Take off")
tello.takeoff()
def pause(next_action: str):
print(f"Hovering before {next_action}")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
pause("forward")
print(f"Moving forward {MOVEMENT_DISTANCE_CM}cm")
tello.move_forward(MOVEMENT_DISTANCE_CM)
pause("backwards")
print(f"Moving backwards {MOVEMENT_DISTANCE_CM}cm")
tello.move_back(MOVEMENT_DISTANCE_CM)
pause("left")
print(f"Moving left {MOVEMENT_DISTANCE_CM}cm")
tello.move_left(MOVEMENT_DISTANCE_CM)
pause("right")
print(f"Moving right {MOVEMENT_DISTANCE_CM}cm")
tello.move_right(MOVEMENT_DISTANCE_CM)
pause("up")
print(f"Moving up {MOVEMENT_DISTANCE_CM}cm")
tello.move_up(MOVEMENT_DISTANCE_CM)
pause("down")
print(f"Moving down {MOVEMENT_DISTANCE_CM}cm")
tello.move_down(MOVEMENT_DISTANCE_CM)
pause("landing")
print("Landing")
# Land
tello.land()
# End the connection
tello.end()

47
examples/8_rotate.py Executable file
View File

@@ -0,0 +1,47 @@
from tello_sim.tello_sim_client import TelloSimClient
import time
ROTATION_DEGREES = 180
# Create a Tello instance
tello = TelloSimClient()
# Connect to Tello
tello.connect()
print("Starting flying in ...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
def pause():
print("Hovering for...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
# Takeoff
print("Take off")
tello.takeoff()
pause()
print(f"Rotating clockwise {ROTATION_DEGREES} degrees")
tello.rotate_clockwise(ROTATION_DEGREES)
pause()
print(f"Rotating counter clockwise {ROTATION_DEGREES} degrees")
tello.rotate_counter_clockwise(ROTATION_DEGREES)
pause()
print("Landing")
# Land
tello.land()
# End the connection
tello.end()

61
examples/9_advanced_movement.py Executable file
View File

@@ -0,0 +1,61 @@
# Not verified working on normal Tello!
from tello_sim.tello_sim_client import TelloSimClient
import time
ROTATION_DEGREES = 90
# Create a Tello instance
tello = TelloSimClient()
# Connect to Tello
tello.connect()
print("Starting flying in ...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
def pause():
print("Hovering for...")
for i in range(3, 0, -1):
print(i)
time.sleep(1)
# Takeoff
print("Take off")
tello.takeoff()
pause()
tello.curve_xyz_speed(100, 0, 0, 200, 200, 0, 20)
tello.wait_until_motion_complete()# Use this instead of pause for longer duration curve_xyz_speed/go_xyz_speed,to prevent break in animation.
#tello.go_xyz_speed(100, 0, 0, 10)
#tello.wait_until_motion_complete()
# Should go backwards 1m at 10 cm/s
#tello.go_xyz_speed(-100, 0, 0, 10)
# Should go forward and up at 10 cm/s
# tello.go_xyz_speed(10, 10, 10, 10)
# Should go backward left and up at 10 cm/s
# tello.go_xyz_speed(-80, 10, 10, 20)
#pause()
# Should go forward to the right and up at with rotations 10 cm/s
# tello.curve_xyz_speed(10, 10, 10, 20, 20, 120, 10)
#tello.curve_xyz_speed(40, 10, 140, -200, 80, 40, 20)
print("Landing")
# Land
tello.land()
# End the connection
tello.end()

9
examples/README.md Normal file
View File

@@ -0,0 +1,9 @@
# Example Exercises
This folder contains a collection of sample scripts and exercises designed to help you get started with and understand the functionality of the DJI Tello playground. These examples demonstrate how to use various components of the project—from basic drone commands to more advanced features like computer vision integrations.
Make sure to always execute the scripts from the `src` directory to ensure proper module imports and file references.
The numbered exercises are ordered from basic to advanced, with each script building upon the concepts introduced in the previous ones. Feel free to explore and modify the scripts to suit your needs or experiment with new ideas.
The other scripts are more advaned and demonstrate additional capabilities of the Tello drone, such as face tracking, object detection, and autonomous flight.

38
examples/navigate_route.py Executable file
View File

@@ -0,0 +1,38 @@
"Here try to create a script that navigates the drone through a course."
from tello_sim.tello_sim_client import TelloSimClient
import time
# Create a Tello instance
tello = TelloSimClient()
# Connect to Tello
tello.connect()
print("Starting flying in ...")
for i in range(10, 0, -1):
print(i)
time.sleep(1)
# Takeoff
tello.takeoff()
# Here change the commands to navigate though the course
# # Go forward 100 cm
tello.move_forward(100)
# # Turn right
# tello.rotate_clockwise(90)
# # Go forward 150 cm
# tello.move_forward(150)
#####
# Land
tello.land()
# End the connection
tello.end()