#Coding4Fun – How to control your #drone with 20 lines of code! (3/N)

Buy Me A Coffee

Hi!

Today I’ll write the equivalent of a Hello World in the drone ecosystem. This is a very complex app which:

  • take off the drone
  • wait a couple of seconds
  • lands the drone

I’ve followed the Python code sample from the SDK and the final code is very complex (see below). And it deserves some remarks

  • Line 11. The function recv() run in a separated thread to receive messages from the drone. The thread is started on line 44
  • Line 19. The function sendMessage() sends messages to the drone. Important, the messages must be UTF-8 encoded. This took me some time, until I figure it out. This time implements a timeout with 5 second until the response is processed in a separated thread
  • Lines 31-41. Connection information and sockets to communicate with the drone
  • Line 48. Main code for the app. Start the SDK mode, Wait 5 seconds, send the take off message, and then send the land message. A very simple exception catch is implemented here.

Important: When using the SDK, the 1st command should be “command”, as I did in line 49.

Here is the code:

# Bruno Capuano
# Simple drone demo
# connect to drone
# send take off, sleep and land
import threading
import socket
import sys
import time
def recv():
global response
while True:
try:
response, _ = clientSocket.recvfrom(1024)
except:
break
def sendMessage(command):
global response
timestamp = int(time.time() * 1000)
clientSocket.sendto(command.encode('utf-8'), address)
while response is None:
if (time.time() * 1000) timestamp > 5 * 1000:
return False
return response
# connection info
UDP_IP = '192.168.10.1'
UDP_PORT = 8889
last_received_command = time.time()
STATE_UDP_PORT = 8890
address = (UDP_IP, UDP_PORT)
response = None
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSocket.bind(('', UDP_PORT))
# start threads
recThread = threading.Thread(target=recv)
recThread.daemon = True
recThread.start()
try:
msg = "command"
sendMessage(msg) # start SDK mode
time.sleep(5)
msg = "takeoff"
sendMessage(msg) # takeoff
time.sleep(3)
msg = "land"
sendMessage(msg) # land
except Exception as e:
print (f'\nError processing the message:{msg}\n{e}')
finally:
print ('\n And now Stop\n')
sock.close()

Here is the app running at 3X speed so you don’t spend all day watching boring drones videos:

Drone Hello World, take off and land

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


2 comments

  1. I’m learning pseudocode and was wondering if you happen to have this code written out in pseudocode so I can look at that and learn how it’s written. Thanks

    Like

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: