Skip to content

Commit ffaea78

Browse files
author
Sd Raspi
committed
First commit
0 parents  commit ffaea78

File tree

5 files changed

+203
-0
lines changed

5 files changed

+203
-0
lines changed

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Tello-Python/
2+
tello_video/
3+
tello_gem/
4+
/log/*
5+
/tmp/*
6+
*.pdf
7+
/videos/*.mp4

capture_vdo.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash +x
2+
3+
now=`date +%T`
4+
raspivid -t $1 -o "videos/tello-$now.mp4" &
5+
6+
echo "capturing video for $1 milliseconds at $now"

hello_edu.rb

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
2+
## Adapted from Python:
3+
# https://raw.githubusercontent.com/dbaldwin/DroneBlocks-TelloEDU-Python/master/swarm-box-mission.py
4+
#
5+
## Ruby script to connect to two EDU drones and control them simultaneously
6+
7+
require 'socket'
8+
require 'time'
9+
10+
SEND_PORT = 8889 # Defined by Tello
11+
SEND_IP = '192.168.0.' # Defined by TP-LINK
12+
13+
IP_1 = SEND_IP + (ARGV[0] || '101')
14+
IP_2 = SEND_IP + (ARGV[1] || '102')
15+
16+
# Create a Sockets that we'll send the command to
17+
$sock_1 = Socket.pack_sockaddr_in(SEND_PORT, IP_1)
18+
$sock_2 = Socket.pack_sockaddr_in(SEND_PORT, IP_2)
19+
20+
# Create UDP sockets which will recieve the response
21+
$udps_1 = UDPSocket.new; $udps_1.bind('0.0.0.0', 9001)
22+
$udps_2 = UDPSocket.new; $udps_2.bind('0.0.0.0', 9003)
23+
24+
# set error
25+
$error = false
26+
27+
# Send the message to Tello and allow for a delay in seconds
28+
def send(commands, delay)
29+
begin
30+
if commands.size == 2
31+
cmd_1, cmd_2 = commands
32+
else
33+
cmd_1 = cmd_2 = commands.first
34+
end
35+
$udps_1.send(cmd_1, 0, $sock_1)
36+
$udps_2.send(cmd_2, 0, $sock_2)
37+
puts("Sent commands: #{[cmd_1, cmd_2]} at #{Time.now.to_s}")
38+
rescue Exception => e
39+
puts("Error sending commands #{[cmd_1, cmd_2]}: #{e.to_s}")
40+
end
41+
42+
# Delay for a user-defined period
43+
sleep(delay)
44+
end
45+
46+
# Receive the message from Tello
47+
def receive
48+
# Continuously loop and listen for incoming messages
49+
while true
50+
begin
51+
resp_1 = $udps_1.recv(512).to_s ## Buffer length must be longer than the message
52+
resp_2 = $udps_2.recv(512).to_s
53+
54+
puts("Received message from Tello EDU #1: #{resp_1}")
55+
puts("Received message from Tello EDU #2: #{resp_2}")
56+
$error = ((resp_1 =~ /error/i) || (resp_2 =~ /error/i)) if ($error == false)
57+
rescue Exception => e
58+
# If there's an error close the socket and break out of the loop
59+
$udps_1.close
60+
$udps_2.close
61+
puts("Error receiving: #{e.to_s}") if $DEBUG
62+
$error = true
63+
break
64+
end
65+
end
66+
end
67+
68+
def capture_video
69+
return if ($error || (RUBY_PLATFORM != 'arm-linux-gnueabihf'))
70+
71+
get_camera_status = `vcgencmd get_camera`.strip
72+
return if (get_camera_status != 'supported=1 detected=1')
73+
74+
Thread.new do
75+
cmd = 'capture_vdo.sh 25000'
76+
sleep(0.1) ## so that the main thread gets scheduled
77+
puts `#{cmd}`
78+
end
79+
end
80+
81+
# Create and start a listening thread that runs in the background
82+
# This utilizes our receive functions and will continuously monitor for incoming messages
83+
th = Thread.new { receive }
84+
85+
# Put Tello into command mode
86+
send(['command'], 2)
87+
88+
# Get battery levels
89+
send(['battery?'], 2)
90+
91+
# start video capture if possible
92+
capture_video
93+
94+
# Send the takeoff command
95+
send(['takeoff'], 7)
96+
97+
# Flip
98+
send(['cw 90','ccw 90'], 7)
99+
100+
# Flip again
101+
send(['ccw 90','cw 90'], 7)
102+
103+
# Land
104+
send(['land'], 2)
105+
106+
puts("Mission completed with #{$error ? 'error' : 'success'}!")
107+
108+
# Close the sockets
109+
$udps_1.close
110+
$udps_2.close
111+

hello_tello.rb

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Ruby script to connect to 2 EDU drones and control them simultaneously: basic takeoff and land
2+
3+
# Adapted from Python: https://raw.githubusercontent.com/dbaldwin/DroneBlocks-TelloEDU-Python/master/swarm-box-mission.py
4+
5+
require 'socket'
6+
require 'time'
7+
8+
## Constants defined by Tello
9+
HOST = '192.168.10.1'
10+
PORT = 8889
11+
BUFF_LEN = 1518
12+
13+
# Create UDP sockets which will recieve the response on any other port
14+
$udps = UDPSocket.new; $udps.bind('0.0.0.0', 9001)
15+
16+
# Send the message to Tello and allow for a delay in seconds
17+
def send(message, delay)
18+
19+
bytes = 0
20+
21+
# Try to send the message otherwise print the exception
22+
begin
23+
bytes = $udps.send(message, 0, HOST, PORT)
24+
puts("Sent message: '#{message}' of #{bytes} bytes")
25+
rescue Exception => e
26+
puts("Error sending message '#{message}': " + e.to_s)
27+
end
28+
29+
# Delay for a user-defined period
30+
sleep(delay)
31+
32+
bytes
33+
end
34+
35+
# Receive the message from Tello
36+
def receive
37+
# Continuously loop and listen for incoming messages
38+
while true
39+
# Try to receive the message otherwise print the exception
40+
begin
41+
resp, sender = $udps.recvfrom(BUFF_LEN)
42+
puts("Received message: '#{resp.to_s.chomp}' from #{sender}")
43+
rescue Exception => e
44+
# If there's an error close the socket and break out of the loop
45+
$udps.close
46+
# puts("Error receiving message: " + e.to_s)
47+
break
48+
end
49+
end
50+
end
51+
52+
# Create and start a listening thread that runs in the background
53+
# This utilizes our receive functions and will continuously monitor
54+
# for incoming messages
55+
th = Thread.new { receive }
56+
57+
# Put Tello into command mode
58+
send("command", 2)
59+
60+
# Check battery status
61+
send("battery?", 2)
62+
63+
# Check battery status
64+
send("time?", 2)
65+
66+
# Takeoff
67+
send("takeoff", 8)
68+
69+
# Flip
70+
send("flip l", 8)
71+
send("flip r", 8)
72+
73+
# Land
74+
send("land", 4)
75+
76+
# Close the socket
77+
#$udps.close
78+
79+
puts 'Mission Accomplished'

videos/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)