Skip to content

Commit a544330

Browse files
author
Shashank Date
committed
Added more example code
1 parent 20d8331 commit a544330

9 files changed

+268
-15
lines changed

mrb_tello.rb fiber_tello.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
end
99

1010
# Uses fibers instead of threads for communication with Tello
11-
1211
class Tello
1312

14-
MAX_RETRY = 3 ## Max number of re-tries on fetching the response
15-
FIXED_IP = '192.168.10.1' ## IP if the Tello is in SINGLE mode
13+
FIXED_IP = '192.168.10.1' ## Defined by Tello
1614
COMMAND_PORT = 8889 ## Port to which command is sent - fixed by Tello firmware
1715
RESPOND_PORT = 9000 ## Port on which response is received - can be any other port
1816

@@ -44,7 +42,7 @@ def self.create(*args)
4442
p({tello_ip: tello_ip, port: RESPOND_PORT})
4543
me = self.new(tello_ip)
4644
me.receiver
47-
# sleep(0.1)
45+
4846
if block_given?
4947
me.do('command')
5048
me.do('battery?')
@@ -73,7 +71,6 @@ def receiver
7371

7472
def close
7573
self.do('land')
76-
#@udps.send('land', 0, @sockaddr)
7774
puts '# close Tello'
7875
@udps.close
7976
end

hello_edu.rb

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
## Adapted from Python:
33
# https://raw.githubusercontent.com/dbaldwin/DroneBlocks-TelloEDU-Python/master/swarm-box-mission.py
44
#
5-
## Ruby script to connect to two EDU drones and control them simultaneously
5+
## Ruby script to connect to two TelloEDU drones and control them simultaneously
66

77
require 'socket'
88
require 'time'
99

10-
SEND_PORT = 8889 # Defined by Tello
10+
SEND_PORT = 8889 # Defined by Tello
1111
SEND_IP = '192.168.0.' # Defined by TP-LINK
1212

1313
IP_1 = SEND_IP + (ARGV[0] || '101')
@@ -65,6 +65,8 @@ def receive
6565
end
6666
end
6767

68+
## Capture Video of the flying drones if connected to
69+
## Raspberry Pi with camera
6870
def capture_video(milli_seconds)
6971
return if ($error || (RUBY_PLATFORM != 'arm-linux-gnueabihf'))
7072

@@ -79,7 +81,8 @@ def capture_video(milli_seconds)
7981
end
8082

8183
# Create and start a listening thread that runs in the background
82-
# This utilizes our receive functions and will continuously monitor for incoming messages
84+
# This utilizes our receive functions and will continuously
85+
# monitor for incoming messages
8386
th = Thread.new { receive }
8487

8588
# Put Tello into command mode

hello_tello.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
# Ruby script to connect to 2 EDU drones and control them simultaneously: basic takeoff and land
21

3-
# Adapted from Python: https://raw.githubusercontent.com/dbaldwin/DroneBlocks-TelloEDU-Python/master/swarm-box-mission.py
2+
# Ruby script to connect to a Tello drone: basic takeoff and land
43

54
require 'socket'
65
require 'time'
@@ -11,7 +10,8 @@
1110
BUFF_LEN = 1518
1211

1312
# Create UDP sockets which will recieve the response on any other port
14-
$udps = UDPSocket.new; $udps.bind('0.0.0.0', 9001)
13+
$udps = UDPSocket.new
14+
$udps.bind('0.0.0.0', 9001)
1515

1616
# Send the message to Tello and allow for a delay in seconds
1717
def send(message, delay)
@@ -36,14 +36,13 @@ def send(message, delay)
3636
def receive
3737
# Continuously loop and listen for incoming messages
3838
while true
39-
# Try to receive the message otherwise print the exception
4039
begin
4140
resp, sender = $udps.recvfrom(BUFF_LEN)
4241
puts("Received message: '#{resp.to_s.chomp}' from #{sender}")
4342
rescue Exception => e
4443
# If there's an error close the socket and break out of the loop
4544
$udps.close
46-
# puts("Error receiving message: " + e.to_s)
45+
puts("Error receiving message: " + e.to_s) if $DEBUG
4746
break
4847
end
4948
end
@@ -67,8 +66,8 @@ def receive
6766
send("takeoff", 8)
6867

6968
# Flip
70-
send("flip l", 8)
71-
send("flip r", 8)
69+
# send("flip l", 8)
70+
# send("flip r", 8)
7271

7372
# Land
7473
send("land", 4)

ractor_tello.rb

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# https://github.com/katoy/dron-tello/blob/master/tello.rb
2+
3+
## Ruby 3.0 script to control Tello drones using Ractors
4+
5+
require 'socket'
6+
7+
class Tello
8+
def initialize(local)
9+
@sockaddr = Socket.pack_sockaddr_in(8889, (local ? '127.0.0.1' : '192.168.10.1'))
10+
11+
@udps = UDPSocket.open
12+
@udps.bind('0.0.0.0', 9000)
13+
14+
@ractor = nil
15+
end
16+
17+
def self.create(local=nil)
18+
me = self.new(local)
19+
me.receiver
20+
sleep(0.1)
21+
if block_given?
22+
me.do('command')
23+
me.do('takeoff')
24+
yield(me)
25+
me.close
26+
end
27+
me
28+
end
29+
30+
def receiver
31+
@ractor ||= Ractor.new(@udps) do |udps|
32+
loop do
33+
begin
34+
retries ||= 0
35+
while(retries < 2) do
36+
sleep(0.1)
37+
resp = udps.recv(1518)
38+
puts "# #{retries+1}. response #{resp}"
39+
break if (resp.to_s.upcase == 'OK')
40+
retries += 1
41+
end
42+
rescue StandardError => e
43+
puts "# #{retries+1}. error #{e.message}"
44+
retry if (retries += 1) < 2
45+
end
46+
end
47+
end
48+
end
49+
50+
def close
51+
puts '# close Tello'
52+
self.do('battery?')
53+
@udps.send('land', 0, @sockaddr)
54+
@udps.close
55+
end
56+
57+
def do(message)
58+
puts "# send #{message}"
59+
@udps.send(message, 0, @sockaddr)
60+
sleep_well(message)
61+
end
62+
63+
private
64+
65+
def sleep_well(cmd)
66+
verb = cmd.to_s.downcase.split(' ').first
67+
well = case verb
68+
when 'command'
69+
1
70+
when 'land'
71+
2
72+
when 'cw', 'ccw'
73+
3
74+
when 'up', 'down', 'left', 'right'
75+
4
76+
when 'takeoff'
77+
5
78+
else
79+
6
80+
end
81+
82+
puts "# sleeping for #{well} seconds"
83+
sleep(well)
84+
return well
85+
end
86+
end
87+
88+
if __FILE__ == $0
89+
Tello.create(ARGV[0].to_s.downcase == 'local') do |tello|
90+
tello.do('flip r')
91+
end
92+
end

thread_tello.rb

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# https://github.com/katoy/dron-tello/blob/master/tello.rb
2+
require 'socket'
3+
4+
## Ruby 2.x script to control Tello drones using Threads
5+
6+
class Tello
7+
def initialize(local)
8+
@udps = UDPSocket.open
9+
@sockaddr = Socket.pack_sockaddr_in(8889, (local ? '127.0.0.1' : '192.168.10.1'))
10+
@udps.bind('0.0.0.0', 9000)
11+
@th = nil
12+
end
13+
14+
def self.create(local=nil)
15+
me = self.new(local)
16+
me.receiver
17+
sleep(0.1)
18+
if block_given?
19+
me.do('command')
20+
me.do('takeoff')
21+
yield(me)
22+
me.close
23+
end
24+
me
25+
end
26+
27+
def receiver
28+
@th ||= Thread.start(@udps) do |udps|
29+
loop do
30+
begin
31+
retries ||= 0
32+
while(retries < 2) do
33+
sleep(0.1)
34+
resp = udps.recv(1518)
35+
puts "# #{retries+1}. response #{resp}"
36+
break if (resp.to_s.upcase == 'OK')
37+
retries += 1
38+
end
39+
rescue StandardError => e
40+
puts "# #{retries+1}. error #{e.message}"
41+
retry if (retries += 1) < 2
42+
end
43+
end
44+
end
45+
end
46+
47+
def close
48+
puts '# close Tello'
49+
@udps.send('land', 0, @sockaddr)
50+
@th.kill
51+
@udps.close
52+
end
53+
54+
def do(message)
55+
puts "# send #{message}"
56+
@udps.send(message, 0, @sockaddr)
57+
sleep_well(message)
58+
end
59+
60+
private
61+
62+
def sleep_well(cmd)
63+
verb = cmd.to_s.downcase.split(' ').first
64+
well = case verb
65+
when 'command'
66+
0.5
67+
when 'land'
68+
1.5
69+
when 'cw', 'ccw'
70+
2.5
71+
when 'up', 'down', 'left', 'right'
72+
3.5
73+
when 'takeoff'
74+
4.5
75+
when 'flip'
76+
5.5
77+
else
78+
6.5
79+
end
80+
81+
puts "# sleeping for #{well} seconds"
82+
sleep(well)
83+
return well
84+
end
85+
end
86+
87+
if __FILE__ == $0
88+
Tello.create(ARGV[0].to_s.downcase == 'local') do |tello|
89+
tello.do('flip l')
90+
end
91+
end

udp/udp_example.c

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <stdint.h>
2+
#ifdef __cplusplus
3+
extern
4+
#endif
5+
const uint8_t udp_example_symbol[] = {
6+
0x52,0x49,0x54,0x45,0x30,0x32,0x30,0x30,0x00,0x00,0x01,0x9e,0x4d,0x41,0x54,0x5a,
7+
0x30,0x30,0x30,0x30,0x49,0x52,0x45,0x50,0x00,0x00,0x01,0x50,0x30,0x33,0x30,0x30,
8+
0x00,0x00,0x01,0x44,0x00,0x06,0x00,0x0c,0x00,0x00,0x00,0x00,0x00,0x71,0x1d,0x06,
9+
0x00,0x52,0x07,0x00,0x43,0x06,0x27,0x06,0x00,0x0c,0x12,0x06,0x52,0x07,0x01,0x2f,
10+
0x06,0x01,0x01,0x25,0x00,0x09,0x12,0x06,0x52,0x07,0x02,0x2f,0x06,0x02,0x01,0x52,
11+
0x01,0x03,0x0e,0x02,0x1e,0x61,0x52,0x03,0x04,0x1d,0x06,0x03,0x2f,0x06,0x04,0x00,
12+
0x01,0x04,0x06,0x01,0x06,0x04,0x01,0x07,0x01,0x01,0x08,0x02,0x2f,0x06,0x05,0x02,
13+
0x01,0x06,0x04,0x01,0x07,0x03,0x06,0x08,0x01,0x09,0x01,0x01,0x0a,0x02,0x2f,0x06,
14+
0x06,0x04,0x01,0x06,0x04,0x01,0x07,0x03,0x2f,0x07,0x07,0x00,0x2f,0x06,0x08,0x01,
15+
0x01,0x05,0x06,0x12,0x06,0x01,0x07,0x05,0x2f,0x06,0x09,0x01,0x39,0x06,0x6a,0x00,
16+
0x05,0x00,0x00,0x03,0x33,0x2e,0x30,0x00,0x00,0x00,0x36,0x55,0x73,0x69,0x6e,0x67,
17+
0x20,0x6d,0x72,0x75,0x62,0x79,0x20,0x33,0x2e,0x30,0x20,0x63,0x6f,0x6d,0x70,0x69,
18+
0x6c,0x65,0x64,0x20,0x75,0x73,0x69,0x6e,0x67,0x20,0x22,0x6d,0x72,0x75,0x62,0x79,
19+
0x2d,0x73,0x6f,0x63,0x6b,0x65,0x74,0x22,0x20,0x63,0x6f,0x72,0x65,0x20,0x67,0x65,
20+
0x6d,0x00,0x00,0x00,0x06,0x73,0x6f,0x63,0x6b,0x65,0x74,0x00,0x00,0x00,0x09,0x31,
21+
0x32,0x37,0x2e,0x30,0x2e,0x30,0x2e,0x31,0x00,0x00,0x00,0x0f,0x6d,0x65,0x73,0x73,
22+
0x61,0x67,0x65,0x20,0x74,0x6f,0x20,0x73,0x65,0x6c,0x66,0x00,0x00,0x0a,0x00,0x0c,
23+
0x52,0x55,0x42,0x59,0x5f,0x56,0x45,0x52,0x53,0x49,0x4f,0x4e,0x00,0x00,0x04,0x70,
24+
0x75,0x74,0x73,0x00,0x00,0x07,0x72,0x65,0x71,0x75,0x69,0x72,0x65,0x00,0x00,0x09,
25+
0x55,0x44,0x50,0x53,0x6f,0x63,0x6b,0x65,0x74,0x00,0x00,0x03,0x6e,0x65,0x77,0x00,
26+
0x00,0x04,0x62,0x69,0x6e,0x64,0x00,0x00,0x04,0x73,0x65,0x6e,0x64,0x00,0x00,0x04,
27+
0x73,0x69,0x7a,0x65,0x00,0x00,0x08,0x72,0x65,0x63,0x76,0x66,0x72,0x6f,0x6d,0x00,
28+
0x00,0x01,0x70,0x00,0x4c,0x56,0x41,0x52,0x00,0x00,0x00,0x32,0x00,0x00,0x00,0x05,
29+
0x00,0x04,0x68,0x6f,0x73,0x74,0x00,0x04,0x70,0x6f,0x72,0x74,0x00,0x04,0x6d,0x65,
30+
0x73,0x67,0x00,0x02,0x75,0x31,0x00,0x04,0x64,0x61,0x74,0x61,0x00,0x00,0x00,0x01,
31+
0x00,0x02,0x00,0x03,0x00,0x04,0x45,0x4e,0x44,0x00,0x00,0x00,0x00,0x08,
32+
};

udp/udp_example.mrb

414 Bytes
Binary file not shown.

udp/udp_example.rb

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
if RUBY_VERSION == '3.0'
2+
puts 'Using mruby 3.0 compiled using "mruby-socket" core gem'
3+
else
4+
require 'socket'
5+
end
6+
7+
host = '127.0.0.1'
8+
port = 7777
9+
mesg = 'message to self'
10+
u1 = UDPSocket.new
11+
u1.bind(host, port)
12+
u1.send(mesg, 0, host, port)
13+
data = u1.recvfrom(mesg.size)
14+
p data
15+

udp/udp_example_stub.c

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
/*
3+
** Compile this file as follows:
4+
** $ gcc -std=c99 -I$HOME/mruby/include udp_example_stub.c -o udp_example $HOME/mruby/build/host/lib/libmruby.a -lm
5+
*/
6+
#include <mruby.h>
7+
#include <mruby/irep.h>
8+
9+
#include "udp_example.c"
10+
11+
int
12+
main(void)
13+
{
14+
mrb_state *mrb = mrb_open();
15+
16+
if (!mrb) {
17+
/* handle error */
18+
printf("%s", "Error calling mrb_open()\n");
19+
}
20+
21+
mrb_load_irep(mrb, udp_example_symbol);
22+
mrb_close(mrb);
23+
return 0;
24+
}

0 commit comments

Comments
 (0)