GPS can provide the drone with accurate position estimate, but there are cases when GPS signal are not applicable or stable, such as under the bridge, inside a room or in a densely populated city. To enable the drone to fly in these environments we will need to provide other sources of position estimation such as SLAM. SLAM uses camera as its primary sensor, which could be monocular camera, stereo camera or RGBD camera. These cameras have their corresponding advantages and disadvantages; Monocular camera is cheap and small in size but it is not capable of estimating the depth of an environment without the help of other sensors; Stereo Camera is able to estimate the depth of an environment but it is computational intensive and requires good calibration; RGBD camera is capable of estimating the depth but it has narrow range and noises and it is prone to errors caused by direct sunlight.
This tutorial has been validated on a real drone but the performance is not stable and there is plenty of room for improvement. Therefore, I would recommend you to proceed with extreme caution and do not test it on a real drone before you are ready. In this tutorial we will test it in Gazebo and the setup procedure is pretty much the same as a real drone.
This tutorial can be organized into the following part:
Environment update and setup;
Building SLAM and its usage;
Test it in Gazebo.
If you have finished previous tutorials, you have set up ROS, MAVROS, Gazebo as well as PX4 firmware. Now, change directory to where you put GAAS or clone a clean copy by :
git clone https://github.com/generalized-intelligence/GAAS
Or update GAAS by:
cd （GAAS_PATH）git pull origin master
Copy ROS launch files to PX4 firmware so you can launch the simulation environment in a new terminal:
cp -r (GAAS_PATH)/simulator/launch/* (PX4_FIRMWARE_PATH)/Firmware/launch
We have updated Gazebo simulation models for drones, and you will need to remove old ones correspondingly from "(PX4_FIRMWARE_PATH)/Firmware/Tools/sitl_gazebo/models" and recopy them by:
cp -r (GAAS_PATH)/simulator/models/* ~/catkin_ws/src/Firmware/Tools/sitl_gazebo/models/
SLAM used by GAAS is based on the following project：
Firstly, install the following dependencies：
Mavros msgs: sudo apt install ros-kinetic-mavros-msgsPangolin (for visualization): https://github.com/stevenlovegrove/PangolinEigen3: sudo apt-get install libeigen3-devg2o: sudo apt-get install libcxsparse-dev libqt4-dev libcholmod3.0.6 libsuitesparse-dev qt4-qmakeglog (for logging): sudo apt-get install libgoogle-glog-devROS Octomap: sudo apt-get install ros-kinetic-octomap-*
next, install PCL, DBoW3, g2o and opencv by：
# PCLhttps://github.com/PointCloudLibrary/pcl# DBoW3https://github.com/rmsalinas/DBow3# g2ohttps://github.com/RainerKuemmerle/g2o# opencv, version higher than 3.4.5 recommended, you'll need opencv contribhttps://github.com/opencv/opencv
Then, change directory to SLAM/ygz_slam_ros and build it by:
cd （GAAS_PATH)/software/SLAM/ygz_slam_ros#it might take a whilesh generate.sh
We will be using QGroundControl to set up and visualize Drone parameters and states, to download it, vistit:
Select your OS platform and configure it according to the description stated in the link.
If you have successfully built ygz_slam_ros and downloaded QGroundControl, you can continue to the next step.
In ygz_slam_ros/examples, you will find a config file that will be used by SLAM, which is called simulationCamera.yaml. The config file includes image left and right topic names, camera intrinstics as well as other parameters that will be used. To give you a glimpse of it:
%YAML:1.0#--------------------------------------------------------------------------------------------# Camera Parameters. Adjust them!#--------------------------------------------------------------------------------------------# camera and imu topicsLeft: /gi/simulation/left/image_rawRight: /gi/simulation/right/image_raw# not used for nowImu: /mavros/imu/data# Camera calibration and distortion parameters (OpenCV)# if running in pure stereo vision modePureVisionMode: true# do we need visualization?UseViewer: false# display mappoints?displayMapPoints: falseCamera.fx: 376.0Camera.fy: 376.0Camera.cx: 376.0Camera.cy: 240.0Camera.k1: 0.0Camera.k2: 0.0Camera.p1: 0.0Camera.p2: 0.0
Feel free to modify camera topic names if you'd like to test it on your own device, note that IMU is not used for now and it is still a working-in-progress project.
Now, let's cut to the chase and start to test it up in Gazebo.
Firstly, let's launch a simulation environment by:
roslaunch px4 slam.launch
A Gazebo window will pop out and the drone will be centered in an empty world. As I mentioned in previous tutorials, remember to check out MAVROS connection status by:
rostopic echo /mavros/state
Make sure "connected: True".
Before continuing, if you take a look at slam.launch you will find the following argument:
<arg name="vehicle" default="iris_stereo_gray_no_gps"/>
You can see I am using "iris_stereo_gray_no_gps" drone model for the simulation and in this model I have disabled GPS plugin, so there would be no GPS signal in the simulation. If you have downloaded QGroundControl, you can start it in a terminal:
./QGroundControl.AppImage # if you downloaded AppImage
QGroundControl will automatically connect to Gazebo simulation, and in the window you will see "No GPS Lock for Vehicle", which means GPS signal is deactivated and we will only be using SLAM as the only external position estimation.
Now let's start SLAM. In a terminal, start SLAM by:
A window will appear and you are able to visualize left camera image in real time. Note there are dots in different colors on the image and it means SLAM is initialized, otherwise you'll need to update Gazebo drone models as mentioned in the first part.
To enable vision as the source of external position estimation, select the top left GEAR button and select "Parameters", input "vision" in the search and tick "vision position fusion" only. Click "save" to save the modification.
We will be using SLAM for the estimation of horizontal plane position estimation only, neglecting SLAM yaw and using barometer for the drone height estimation.
Next, hit "Clear" , select "Tools" and choose "reboot vehicle" to make modifications take effect.
In a terminal, type:
rostopic echo /mavros/vision_pose/pose
You will find the current SLAM position estimation looks like:
---header:seq: 1489stamp:secs: 3453nsecs: 368000000frame_id: ''pose:position:x: 0.000944685346145y: -0.00012923774903z: 0.000286279467091orientation:x: 0.0y: 0.0z: 0.0w: 0.0---
Now, change directory to (GAAS_PATH)/demo/tutorial_3 and takeoff the drone:
In another terminal, command the drone to fly a square of size 3x3 meters:
You will find the drone achieves good position estimation and the drone is able to successfully use SLAM to achieve complex task.
After completing all the tasks, the drone will land on the ground. You can visualize the flight path in the above window.
Although the procedures mentioned above have been validated on a real drone, the performance is not stable and it requires a fair amount of parameter tuning and SLAM improvements before we can achieve good performance. Some problems we have found while testing:
There are SLAM segmentation faults, so the drone would change to ALTITUDE mode while flying;
There are cases when the drone will gradually drift away even though SLAM is on and relative parameters are properly set;
Poor performance when large and sudden movements.
If you'd like to test it on a real drone, typical steps are:
start MAVROS and make sure it is connected to the Drone;
set relative parameters with QGroundControl;
check /mavros/local_position/pose is publishing meaningful position information, this topic fuses position output from /mavros/vision_pose/pose, so if the output of it, for example, is 1e-20, it means SLAM output is not successfully provided and you have to check the states of MAVROS, corresponding parameters as well as SLAM.
takeoff the drone with either your controller and set the drone to "POSITION" mode or use provided python scripts to takeoff the drone.
To sum up, in this tutorial we have illustrated how to build SLAM, how to use QGroundControl with Gazebo simulation and how to conduct a task without GPS information.