Keyboard Commands

OVERVIEW


This lesson is one of our favorites because it lets you tap into all of the commands available in the Tello SDK. We'll be using a Python script to accept commands directly from our keyboard. This means we'll be able to send commands while Tello is in the air. This is a great way to become familiar with how the numerous SDK commands behave. Remember to review the current Tello command list in our GitHub repository:

https://github.com/dbaldwin/droneblocks-tello-python

CODING


The Python code for this lesson is very basic and builds upon what we've already learned. You can grab it from the link below and be sure to paste it into a new Jupyter Notebook.

https://raw.githubusercontent.com/dbaldwin/DroneBlocks-Tello-Python/master/lesson6-keyboard-commands/TelloKeyboardCommands.py

In this script we do the usual UDP send/receive function set up. What's different about this script is we're using an infinite loop to listen for keyboard input.

# Loop infinitely waiting for commands or until the user types quit or ctrl-c
while True:
  
  try:
    # Read keybord input from the user
    if (sys.version_info > (3, 0)):
      # Python 3 compatibility
      message = input('')
    else:
      # Python 2 compatibility
      message = raw_input('')
    
    # If user types quit then lets exit and close the socket
    if 'quit' in message:
      print("Program exited sucessfully")
      sock.close()
      break
    
    # Send the command to Tello
    send(message)
    
  # Handle ctrl-c case to quit and close the socket
  except KeyboardInterrupt as e:
    sock.close()
    break

As we discussed previously, the while True: statement will loop indefinitely until an exception or logic condition is encountered. In our case we will listen for keyboard input with the input or raw_input function (depending on what version of Python is installed). Our code will listen for keyboard input unless it encounters the word quit, which will cause the program to terminate.

Assuming the user has not typed quit into the command line the script will try to execute whatever command is typed and send it to Tello using the send(message) function.

Click Run in your Jupyter Notebook and you'll see a text field where you can enter SDK commands.

Remember that before we send any SDK commands to Tello we need to put Tello into command mode. Type the word command and press enter.

The message will be sent to Tello and if everything goes as planned we'll receive an OK message back from Tello. Now we're ready to send Tello into the air! Let's issue a takeoff message and see what happens. Make sure you're in an open area before you start sending commands.

Once again we'll see an OK message from Tello saying that it processed the takeoff command. In the example above we're sending a flip f command, which will instruct Tello do flip forward. We encourage you to experiment with any and all SDK commands to get familiar with all the capabilities of this powerful little drone. You can find the latest commands listed at the link below:

http://www.github.com/dbaldwin/droneblocks-tello-python

When you're done experimenting don't forget to issue the land command to bring Tello back to the ground.

CONCLUSION


You've successfully learned how to create an interactive command environment to control Tello. Think about extending upon this knowledge and try to navigate Tello through an obstacle course with keyboard commands. This is a fun challenge where students can compete for the fastest time. With Python there are infinite possibilities to learn programming as well as what can be done with drones in the real world.