Autonomous Drone Software E01: OFFBOARD Control and Gazebo Simulation
OFFBOARD mode (Guided mode in the case of Ardupilot ) is a powerful function that allows you to control your drone with companion computers, and we will be testing it in Gazebo simulation environment.
We suggest developers to install GAAS mirror to use the project:
GAAS v0.7 Release Mirror - x64Join our slack
Your drone can be controlled in OFFBOARD mode by a companion computer primarily through a set of MAVROS Commands (don't worry, we will dive into that later) which is a higher level wrapper of MAVLink API that saves us a great deal of efforts when controlling your drone. With the help of MAVROS, we can easily realize many functions such as Takeoff, Land, Position Target, Yaw control etc.
In this tutorial, we will begin with a simple yet powerful function that runs on your companion computer and controls your drone in Python! To do this, let's start with setting up your environment.
This tutorial is tested on Ubuntu 16.04 LTS with ROS-Kinetic, you can also choose to use Ubuntu 18.04 LTS with ROS-Melodic
Environment Setup
You can choose to set up the environment by building each package from source or you can directly use a Docker to run everything. For better performance, we recommend using the first method.
1. Setup from Source
General Dependencies
To use all provided utilities, there are some packages we need to install first:
ROS-Kinetic
ROS (Robot Operating System) is a powerful framework that contains many libraries and tools that can help you with developing robots, and we will be using ROS throughout our tutorial.
To install ROS Kinetic, please follow the instructions below:
Visit http://wiki.ros.org/kinetic/Installation/Ubuntu for more information.
If you would like to use Ubuntu 18.04 LTS with ROS-Melodic , just replace ros-kinetic
withros-melodic
. For example , replace:
sudo apt-get install ros-kinetic-desktop-full
as:
sudo apt-get install ros-melodic-desktop-full
Once you have installed ROS, you can test it by opening a terminal and type:
If you have installed it properly, you will see something like:
Generate your catkin workspace. This is the place where you will be storing ROS projects such as MAVROS etc.
MAVROS
MAVROS is a communication node based on MAVLink for ROS that is specially designed for communication between the drone and the companion computer. To install it, follow the following instructions:
PX4 Firmware
We will be using PX4 v1.8.0 throughout our tutorial.
Now you should see a window pop out and a drone is centered in the middle of the environment, but let's close the window for now.
Modifying your 'bashrc' so that your environment remains the same every time you open a new terminal:
Open a new terminal and type the following command:
In another terminal:
Or you can simply use:
A window should pop out which looks like:
In a new terminal, if you type:
You should see:
If you see the above results and 'connected: True', it means your simulation environment is properly set up and MAVROS connection is successful.
While running Gazebo, if everything works properly with the exception of the above Err message, the Err message itself DOES NOT affect the performance of Gazebo.
If you happen to know how to resolve this Err message, please submit a PR on GitHub to help us make it go away.
2. Using Docker
If you'd like to skip all the processes mentioned in the last part, you can quickly jump to the next part by using provided Docker. Docker is easy to use but resource-consuming, meaning the frame rates might be slow, but VNC clients are widely available on multiple platforms such as MacOS, Windows and Android etc.
It is recommended to set up your environment using the first procedure.
Using Virtual Network Computing (VNC) and Pulling Docker Image
In order to use GUI applications within Docker environment, you will need to install VNC-viewer in your local environment.
For Linux systems, you can directly download the following binary file to your local environment by:
You can checkout this website for more information:
Then:
The installation is quite simple and straight forward, and we will skip this process for other systems. After installing VNC-viewer, we need to pull the Docker Image to your local environment by:
Now, to start a Container, open a terminal and type:
In another terminal:
You should see a window which looks like:
Enter the following address:
Hit enter, an interactive window will pop out and you can use it just like any other Ubuntu system:
Everything you need for this tutorial can be found in the following folder:
At last, you will need to manually conduct the following commands before running the simulation every time you use a Docker:
Now, you can jump to the last section and start doing practical!
OFFBOARD Control
You can skip this part and jump to Control Your Drone if you are using Docker.
Before diving into the python scripts, let's give it a go. Firstly, clone the scripts to your local environment. It contains custom drone models, world models as well as many other things that will help you build your drone, for now we will only be looking at python MAVROS examples, but feel free to test other things if you are interested.
You will see four folders:
demo: everything we need for this tutorial can be found in this folder;
hardware: it contains relevant information on how to build the hardware;
simulator: it contains drone simulation models, world models etc. ;
software: it contains other packages such as ObstacleMap, SLAM, Local and Global path planner etc.
You will find more information in each folder's README.MD.
Next, add customized model path to bashrc by:
Copy models and config files to corresponding PX4 folders:
Control Your Drone
If you are using Docker, remember to conduct the following command every time you try to run the simulation:
A Gazebo simulation environment should pop out but let's close the window for now.
Next, before executing any of the following scripts, remember to launch the simulation first:
And don't forget to check MAVROS connection status by:
If MAVROS connection is successful, in a new terminal, change directory to the place where you put GAAS and go to DEMO by:
You will see a drone gradually takes off to 3 meters high and hovers at this height. In another terminal:
You will see the drone moves in the following order:
moves 1 meter to the right;
turns 90 degrees counter-clockwise;
land.
Or you can open a new terminal and control the drone using provided API like:
You will see the drone moves in a square, goes up and then land. You have probably found that the drone was moving relative to its "current" position and the movement frame is defined as BODY_OFFSET_ENU, or FLU (Forward, Left and Up, see reading materials) which means each movement command will control the drone to move relative to its body frame. To illustrate, the first parameter in function move(x, y, z) means going Forward, the second means going to the left and the third means going up. The movement frame is set to BODY_OFFSET_ENU by Default. If you wish to use LOCAL_ENU which means the movement is relative to its takeoff position, you can add a fourth parameter like:
Now you can checkout other APIs and test them in the simulation environment.
In the next tutorial, we will talk about how to build a 3D model using OFFBOARD mode and popular SfM algorithms in simulation.
If you find any bug or need more help, please let us know either by opening an issue or through our facebook group.
Give us a Star 🌟 on Github if you find this tutorial useful.
Help us to understand what you want to learn in the upcoming tutorials by fill out this three-question survey.
Reading Materials
We find the following materials very useful during developing our work, so if you are interested, you can have a look.
General ROS introduction: http://wiki.ros.org/
MAVROS: http://wiki.ros.org/mavros
MAVLINK: https://mavlink.io/
Gazebo: http://gazebosim.org/
Last updated