GAAS
Search…
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:
Join 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:
1
sudo apt install -y \
2
ninja-build \
3
exiftool \
4
python-argparse \
5
python-empy \
6
python-toml \
7
python-numpy \
8
python-yaml \
9
python-dev \
10
python-pip \
11
ninja-build \
12
protobuf-compiler \
13
libeigen3-dev \
14
genromfs
Copied!
1
pip install \
2
pandas \
3
jinja2 \
4
pyserial \
5
cerberus \
6
pyulog \
7
numpy \
8
toml \
9
pyquaternion
Copied!

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:
1
sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
2
sudo apt-key adv --keyserver 'hkp://keyserver.ubuntu.com:80' --recv-key C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654
3
sudo apt-get update
4
sudo apt-get install ros-kinetic-desktop-full
5
sudo rosdep init
6
rosdep update
7
echo "source /opt/ros/kinetic/setup.bash" >> ~/.bashrc
8
source ~/.bashrc
9
sudo apt install python-rosinstall python-rosinstall-generator python-catkin-tools python-wstool build-essential
10
11
# install ros-gazebo plugins
12
sudo apt install ros-kinetic-gazebo-* \
Copied!
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:
1
roscore
Copied!
If you have installed it properly, you will see something like:
1
... logging to /home/.ros/log/6a1b2330-2eb3-11e9-a39c-9cb6d0e498fb/roslaunch-gishr-XPS-15-9560-4452.log
2
Checking log directory for disk usage. This may take awhile.
3
Press Ctrl-C to interrupt
4
Done checking log file disk usage. Usage is <1GB.
5
6
started roslaunch server http://XPS-15:44361/
7
ros_comm version 1.12.14
8
9
10
SUMMARY
11
========
12
13
PARAMETERS
14
* /rosdistro: kinetic
15
* /rosversion: 1.12.14
16
17
NODES
18
19
auto-starting new master
20
process[master]: started with pid [4463]
21
ROS_MASTER_URI=http://XPS-15:11311/
22
23
setting /run_id to 6a1b2330-2eb3-11e9-a39c-*********
24
process[rosout-1]: started with pid [4476]
25
started core service [/rosout
Copied!
Generate your catkin workspace. This is the place where you will be storing ROS projects such as MAVROS etc.
1
mkdir -p ~/catkin_ws/src
Copied!

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:
1
# you can either choose to use apt or build from source
2
3
# method 1
4
sudo apt-get install ros-kinetic-mavros ros-kinetic-mavros-extras
5
# and instal geographic lib by :
6
wget https://raw.githubusercontent.com/mavlink/mavros/master/mavros/scripts/install_geographiclib_datasets.sh
7
sudo ./install_geographiclib_datasets.sh
8
9
10
# method 2
11
cd ~/catkin_ws
12
catkin init && wstool init src
13
14
rosinstall_generator --rosdistro kinetic mavlink | tee /tmp/mavros.rosinstall
15
rosinstall_generator --upstream mavros | tee -a /tmp/mavros.rosinstall
16
17
wstool merge -t src /tmp/mavros.rosinstall
18
wstool update -t src -j4
19
rosdep install --from-paths src --ignore-src -y
20
21
sudo ./src/mavros/mavros/scripts/install_geographiclib_datasets.sh
22
sudo apt install ros-kinetic-catkin python-catkin-tools
23
24
catkin build
Copied!
1
visit the following web for more information:
2
https://github.com/mavlink/mavros/blob/master/mavros/README.md#installation
Copied!

PX4 Firmware

We will be using PX4 v1.8.0 throughout our tutorial.
1
cd ~/catkin_ws/src
2
#it could take a while
3
git clone https://github.com/PX4/Firmware.git
4
cd Firmware
5
git checkout v1.8.0
6
make posix_sitl_default gazebo
Copied!
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:
1
# Use your favorite editor, we will be using gedit
2
# NOTE: you will need to use ROOT to edit bashrc
3
sudo gedit ~/.bashrc
4
5
# in the new terminal, scroll down to the bottom and add the following lines to the bottom
6
# if you used apt to install MAVROS (method 1), there would be no setup.bash under ~/catkin_ws/devel/
7
# and you can safely neglect the first step.
8
source ~/catkin_ws/devel/setup.bash
9
source ~/catkin_ws/src/Firmware/Tools/setup_gazebo.bash ~/catkin_ws/src/Firmware/ ~/catkin_ws/src/Firmware/build/posix_sitl_default
10
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/catkin_ws/src/Firmware
11
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:~/catkin_ws/src/Firmware/Tools/sitl_gazebo
Copied!
Open a new terminal and type the following command:
1
# This will launch Gazebo simulation
2
roslaunch px4 posix_sitl.launch
Copied!
In another terminal:
1
# This will launch MAVROS
2
roslaunch mavros px4.launch fcu_url:="udp://:[email protected]:14557"
Copied!
Or you can simply use:
1
# This will launch MAVROS and Gazebo simulation at the same time
2
roslaunch px4 mavros_posix_sitl.launch
Copied!
A window should pop out which looks like:
Figure 1, Gazebo and PX4 simulation environment in an empty world
In a new terminal, if you type:
1
rostopic echo /mavros/state
Copied!
You should see:
1
header:
2
seq: 1
3
stamp:
4
secs: 730
5
nsecs: 280000000
6
frame_id: ''
7
connected: True
8
armed: False
9
guided: False
10
mode: "MANUAL"
11
system_status: 3
12
---
Copied!
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:
1
wget https://www.realvnc.com/download/file/viewer.files/VNC-Viewer-6.19.107-Linux-x64
Copied!
You can checkout this website for more information:
1
https://www.realvnc.com/en/connect/download/viewer/
Copied!
Then:
1
# NOTE the exact file name might differ
2
chmod +x VNC-Viewer-6.19.107-Linux-x64
3
./VNC-Viewer-6.19.107-Linux-x64
Copied!
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:
1
# NOTE you probably need ROOT privilege
2
docker pull gaas/mavros-gazebo7-px4
Copied!
Now, to start a Container, open a terminal and type:
1
# It might take a while depending on your hardware setup
2
docker run -p 6080:80 -p 5900:5900 gaas/mavros-gazebo7-px4:latest
Copied!
In another terminal:
1
./VNC-Viewer-6.19.107-Linux-x64
Copied!
You should see a window which looks like:
Figure 2, VNC viewer window
Enter the following address:
1
127.0.0.1:5900
Copied!
Hit enter, an interactive window will pop out and you can use it just like any other Ubuntu system:
Figure 3, Docker Image
Everything you need for this tutorial can be found in the following folder:
1
/root/gi/GAAS/demo/tutorial_1
Copied!
At last, you will need to manually conduct the following commands before running the simulation every time you use a Docker:
1
cd /root/gi/px4/Firmware && make posix_sitl_default gazebo
Copied!
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.
1
git clone [email protected]:generalized-intelligence/GAAS.git
Copied!
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:
1
echo "export GAZEBO_MODEL_PATH=${GAZEBO_MODEL_PATH}:(GAAS_PATH)/simulator/models" >> ~/.bashrc
Copied!
Copy models and config files to corresponding PX4 folders:
1
# if you cloned GAAS to a different place
2
cp -r (GAAS_PATH)/simulator/models/* ~/catkin_ws/src/Firmware/Tools/sitl_gazebo/models/
3
cp -r (GAAS_PATH)/GAAS/simulator/worlds/* ~/catkin_ws/src/Firmware/Tools/sitl_gazebo/worlds/
4
cp -r (GAAS_PATH)/GAAS/simulator/posix-config/* ~/catkin_ws/src/Firmware/posix-configs/SITL/init/ekf2/
Copied!

Control Your Drone

If you are using Docker, remember to conduct the following command every time you try to run the simulation:
1
cd /root/gi/px4/Firmware && make posix_sitl_default gazebo
Copied!
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:
1
roslaunch px4 mavros_posix_sitl.launch
Copied!
And don't forget to check MAVROS connection status by:
1
rostopic echo /mavros/state
Copied!
If MAVROS connection is successful, in a new terminal, change directory to the place where you put GAAS and go to DEMO by:
1
# if you are using docker
2
cd /root/gi/GAAS/demo/tutorial_1/1_px4_mavros_offboard_controller
3
python px4_mavros_run.py
4
5
# if you installed everything from apt
6
cd (GAAS_PATH)/demo/tutorial_1/1_px4_mavros_offboard_controller
7
python px4_mavros_run.py
Copied!
You will see a drone gradually takes off to 3 meters high and hovers at this height. In another terminal:
1
python commander.py
Copied!
You will see the drone moves in the following order:
  1. 1.
    moves 1 meter to the right;
  2. 2.
    turns 90 degrees counter-clockwise;
  3. 3.
    land.
Or you can open a new terminal and control the drone using provided API like:
1
# in folder '1_px4_mavros_offboard_controller'
2
python
3
4
# import packages
5
from commander import Commander
6
import time
7
8
# create Commander instance
9
con = Commander()
10
11
# control the drone to move 1 meter to the right
12
con.move(1,0,0)
13
# wait for 2 seconds
14
time.sleep(2)
15
16
# control the drone to move 1 meter to the front
17
con.move(0,1,0)
18
# wait for 2 seconds
19
time.sleep(2)
20
21
# control the drone to move 1 meter to the left
22
con.move(-1,0,0)
23
# wait for 2 seconds
24
time.sleep(2)
25
26
# control the drone to move 1 meter to the back
27
con.move(0,-1,0)
28
# wait for 2 seconds
29
time.sleep(2)
30
31
# control the drone to move 1 meter above
32
con.move(0,0,1)
33
# wait for 2 seconds
34
time.sleep(2)
35
36
# land
37
con.land()
Copied!
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:
1
con = Commander()
2
3
# for BODY_OFF_SET_ENU or FLU frame
4
con.move(x,y,z)
5
6
# for LOCAL_ENU frame
7
con.move(x,y,z,BODY_OFF_SET_ENU=False)
Copied!
Figure 4, FLU body frame
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.
Last modified 1yr ago