-
Notifications
You must be signed in to change notification settings - Fork 2
DIY GlamBot Recipe
This is a recipe to create your own GlamBot at home!
If you do not know what the GlamBot is, here an example:
It is a crane with a camera that moves fast so it can record cool slow-motion videos. With this project, I tried to recreate this with a Tello drone.
To recreate this project you only need a Tello drone and a laptop.
As setup, I used a virtual environment in anaconda and installed all the necessary libraries there. For this, I installed the following packages:
- Python 3.9.20
- OpenCv (cv2) 4.8.0.74
- djitellopy 2.5.0. Unsure if these specific versions are necessary but it at least works in this constellation.
Firstly import the necessary libraries:
import cv2 from threading import Thread, Event from djitellopy import Tello import os from time import sleep from datetime import datetime
Initialize the drone. It also displays the battery percentage which is practical.
tello = Tello() tello.connect() tello.query_battery()
Set up the directory to save your videos. And add an event so that you can stop the video recording automatically `record_dir = "C:/Users/HP-Note/Documents/uni/SPC/drones/videos" if not os.path.exists(record_dir): os.makedirs(record_dir)
mission_complete = Event()`
I then defined my two functions the video recording and the flight pattern.
Firstly, the video recording. The VideoWriter needs the path (I used the timestamp to make sure new videos do not get overwritten), the codec (in this case mp4), the framerate (10 for slow motion, you could play around with that, 5 ist super slow), and the image shape which normally you would grab from an example image but this didn’t work in my case so I just hardcoded it to 960,720 which is the correct shape.
Additionally, we turn on the tello stream.
def videoRec(): out = cv2.VideoWriter( f'{record_dir}/test_flight_{datetime.now().strftime("%Y%m%d_%H%M%S")}.mp4', cv2.VideoWriter_fourcc(*'mp4v'), 10, (960, 720) ) tello.streamon()
Then we try to record the video stream by grabbing every frame and “writing” it. Before this, the pictures are set to the correct colorspace. We also display it but that is not necessary as the saved result is more important. To interrupt the recording you could press the ‘q’ key. If something in the video recording doesn’t work it will from an exception which is very practical as this happens rather often if everything is not set up correctly. After the recording, we turn everything off and “clean up” ` try: while True: # Capture a frame from the Tello video stream frame_read = tello.get_frame_read() frame = frame_read.frame if frame is not None: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Convert color space out.write(frame) # Write the frame to the video file
# Display the captured frame in a window
cv2.imshow("Frame", frame)
# Check for the 'q' key press or mission completion to exit the loop
if cv2.waitKey(24) & 0xFF == ord('q') or mission_complete.is_set():
break
except Exception as e:
print(f'An exception occurred in recording Tello video stream: {e}')
finally:
# Close the window and release the video writer
cv2.destroyAllWindows()
out.release()
tello.streamoff()
print("Video recording stopped.")
The flight function was rather complicated and is still not perfect. Firstly we start the motors and get the drone up in the air at about eye level. Then I start the video recording thread so the ascend is not recorded but you could place this code block anywhere where you would like. The drone still hovers a bit so the video stream has time to boot up. def mission_B():
tello.send_rc_control(0, 0, 0, 0)
sleep(0.1)
# Turns motors on:
tello.send_rc_control(-100, -100, -100, 100)
sleep(2)
tello.send_rc_control(0, 10, 70, 0)
sleep(3)
# Start recording after this point
receive_video_thread = Thread(target=videoRec)
receive_video_thread.daemon = True
receive_video_thread.start()
print("Started recording.")
tello.send_rc_control(0, 0, 0, 0)
sleep(4)`
Then the actual flight starts. My plan was a circle but it is more kind of a spiral. To fly a circle the function send_rc_control(left_right_velocity, forward_backward_velocity, up_down_velocity, yaw_velocity) is used which is rather math-y and a little complicated to wrap one head around. But through trial and error, I got a result I am happy with. The drone doesn’t quite end where it started and the subject of the video has to adjust their position a bit to stay in frame but it can be started in a medium-big room and keep a comfortable distance from the person.
So in 4 parts, the drone moves around the person in a quasi-circle.
v_up = 0 for _ in range(4): tello.send_rc_control(28, -4, v_up, -40) sleep(4) tello.send_rc_control(0, 0, 0, 0) sleep(0.5)
Some other settings also kind of worked. All the settings could be adjusted more, as well as how often and how long parts were. Even AI's solutions were not quite the right fit but at least it is fun to play around with.
#tello.send_rc_control(40,-5,0,-35) # original starting point (range(4), sleep 4) best circle but rather large and slow #tello.send_rc_control(25, -3, 0, -45) # Tighter circle (sleep 3) #tello.send_rc_control(30, -4, 0, -40) #Larger nearly circle #tello.send_rc_control(27, -4, 0, -40) #good one
After that, we stop the recording and land the drone. ` mission_complete.set() print("Stopping recording.") receive_video_thread.join() # Wait for the recording thread to finish
tello.land()
tello.reboot()
print("Mission complete. Drone has landed.")`
Outside the function definitions again, we start the flight mission thread which starts the video thread in itself. And that’s it. `mission_thread = Thread(target=mission_B) mission_thread.daemon = True mission_thread.start()
mission_thread.join()`
In future work, I would want to add an object detector that only starts the video recording after a person is detected or other flight patterns could be fun to explore.
Running the project is relatively simple, you just have to connect your Tello drone and start the script. Positioning the drone about 80 centimeters in front of you or the person to be recorded, with the camera facing the person. The drone needs about 1.5 m of space to the right, left, and front to fly safely. And then you can perform in the middle and get a fun recording in slow-motion. Just like this
Just nearly as fancy!