|
| 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 | + |
0 commit comments