Flying DJI Tello drone using Golang

Flying DJI Tello drone using Golang

Gobot is a framework for controlling robots, drones, and the Internet of Thing devices using the Golang programming language.

In this article, you will build a simple application to control your DJI Tello drone using your Keyboard in Golang.

Prerequisites

Before you begin this guide, you'll need the following:

  • You will need a DJI Tello drone to follow along with this article
  • A valid Golang installation

Information about the drone

The DJI Tello is a great starter drone that is fully programmable and costs about 80-100€.  Here are some pieces of information about the drone:

  • Weight: Approximately 80g
  • Built-In Functions: Range Finder, 5MP Camera with 720P Live View  Vision System, WIFI 802.11n 2.4G,
  • Max Flight Distance: 100m
  • Speed: 8m/s
  • Flight Time: Up to 15min per battery
  • Detachable Battery: 1.1Ah/3.8V
  • Port: Micro USB Charging Port

Setting up the project

The first step is setting up the project file structure, which is pretty easy because it only requires a single file.

touch main.go

After that, you also need to install the necessary dependencies for using the gobot library.

gobot.io/x/gobot

Connecting to the Tello Drone

After completing the project setup, you can continue by connecting to your drone using the following code.

package main

import (
	"time"

	"gobot.io/x/gobot"
	"gobot.io/x/gobot/platforms/dji/tello"
)

func main() {
	drone := tello.NewDriver("8888")

	work := func() {
		drone.TakeOff()

		gobot.After(5*time.Second, func() {
			drone.Land()
		})
	}

	robot := gobot.NewRobot("tello",
		[]gobot.Connection{},
		[]gobot.Device{drone},
		work,
	)

	robot.Start()
}

As you can see, the tello.NewDriver() function is used to create a new drone driver, which can then be passed to the gobot robot as a device. We also define a function called work where we define the action the drone should perform.

In this example, the drone takes off at the start of the script using the drone.TakeOff() function. The example also makes use of the gobot.After() helper function that allows us to include a time delay for a command.

Adding controls for flying

Now that you are connected to the drone, the next step is to execute the commands when pressing the keyboard instead of using a time delay.

For that, we use the keyboard module of the gobot library. You first need to register a new keyboard driver and add the driver to your gobot devices. After that, you can use the keys.On() function in your work() function.

package main

import (
	"fmt"

	"gobot.io/x/gobot"
	"gobot.io/x/gobot/platforms/dji/tello"
	"gobot.io/x/gobot/platforms/keyboard"
)

var (
	intensity int = 20
)

func main() {
	keys := keyboard.NewDriver()
	drone := tello.NewDriver("8888")

	work := func() {
		keys.On(keyboard.Key, handleKeyboardInput(drone))
	}

	robot := gobot.NewRobot("tello",
		[]gobot.Connection{},
		[]gobot.Device{keys, drone},
		work,
	)

	robot.Start()

}

The keys.On() function takes two arguments:

  1. The key that it should react to
  2. The function that should handle the keyboard event

Let's continue by implementing the function to handle the keyboard input.

func handleKeyboardInput(drone *tello.Driver) func(interface{}) {
	return func(data interface{}) {
		key := data.(keyboard.KeyEvent).Key

		switch key {
		case keyboard.A:
			fmt.Println("Going left.")
			drone.Left(intensity)
		case keyboard.D:
			fmt.Println("Going right.")
			drone.Right(intensity)
		case keyboard.W:
			fmt.Println("Going up.")
			drone.Up(intensity)
		case keyboard.S:
			fmt.Println("Going down.")
			drone.Down(intensity)
		case keyboard.U:
			fmt.Println("Going forward.")
			drone.Forward(intensity)
		case keyboard.J:
			fmt.Println("Going backward.")
			drone.Backward(intensity)
		case keyboard.K:
			fmt.Println("Rotating counter-clockwise.")
			drone.CounterClockwise(intensity)
		case keyboard.H:
			fmt.Println("Rotating clockwise.")
			drone.Clockwise(intensity)
		case keyboard.L:
			fmt.Println("Landing drone")
			drone.Land()
		case keyboard.T:
			fmt.Println("Take off drone")
			drone.TakeOff()
		default:
			resetDronePostion(drone)
		}
	}
}

As you can see, we extract the key from the event and react to it by executing the right drone action.

If the key is none of our defined options we reset the drone position instead.

func resetDronePostion(drone *tello.Driver) {
	drone.Forward(0)
	drone.Backward(0)
	drone.Up(0)
	drone.Down(0)
	drone.Left(0)
	drone.Right(0)
	drone.Clockwise(0)
}

Testing the application

Excellent, now that we have finished the application, you can run it using the following command.

go run main.go

You should now be connected to the drone and be able to take off and fly using the keys you defined earlier.

Conclusion

You made it all the way until the end! I hope that this article helped you understand Gobot and how you can use it to control your DJI Tello drone.

If you have found this useful, please consider recommending and sharing it with other fellow developers and subscribing to my newsletter. If you have any questions or feedback, let me know using my contact form or contact me on twitter.