roslaunch# Simulation setup ROS, PX4, Gazebo and mavros (Mavlink) This is to setup simulation environment to simulate drone (PX4) in Gazebo and ROS.
Software and ROS packages needed include
- ROS,
- PX4 as framework of autopilot,
- mavros for communication between PX4 and ROS,
- Controller library (optional).
- Step 1 Install ROS
- Step 2 Build PX4 from source code
- Step 2.5 Enable roslaunch to launch PX4 in Gazebo
- Step 3 Build mavros (Mavlink) from source code
ROS version should be chosen according to OS. Now, ROS LTS (see here) are provided for
- Ubuntu 18.04 (ROS Melodic),
- Ubuntu 20.04 (ROS Noetic).
Installing guide of Noetic for Ubuntu 20.04 can be found here.
- Download PX4 of version 1.12.3
cd Robot_Firmware # somewhere you like
git clone -b v1.12.3 https://github.com/PX4/PX4-Autopilot.git --recursive
Check PX4 version with
git describe
we should get
px4 version- Run the script to install dependencies and tools for nuttx, jMAVSim, Gazebo s
cd PX4-Autopilot
bash ./Tools/setup/ubuntu.sh
- Test PX4 within Gazebo
make px4_sitl gazebo
Following steps of Install PX4 SITL(Only to Simulate) here
cd PX4-Autopilot
DONT_RUN=1 make px4_sitl_default gazebo
In cases of errors, do
cd PX4-Autopilot
make clean
rm -r ~/catkin_ws/devel ~/catkin_ws/build
Modify .bashrc by adding (replace ${address_of_PX4-Autopilot} by where you install PX4-Autopilot int the following commande)
source ${address_of_PX4-Autopilot}/Tools/setup_gazebo.bash ${address_of_PX4-Autopilot} ${address_of_PX4-Autopilot}/build/px4_sitl_default
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:${address_of_PX4-Autopilot}
export ROS_PACKAGE_PATH=$ROS_PACKAGE_PATH:${address_of_PX4-Autopilot}/Tools/sitl_gazebo
- Check if ros can find px4 package
rospack find px4
It should give
px4 found by rosFinally, it should be able to use roslaunch to create px4-gazebo simulation environment by
roslaunch px4 posix_sitl.launch
Common errors:
- gazebo dies
do
ps aux | grep gzserver
kill -2 <pid associated with gzserver>
Mavlink is a protocol for communicating with drones (PX4).
There are two ways of using Mavlink for communication
- mavros wrappers Mavlink into ROS,
- MavSDK provides various programming languages to interface with Mavlink systems.
Our development is conducted in ROS, thus is natural to consider using Mavlink somehow in ROS for the purpose of communicating with drones in ROS environment. Therefore, we take mavros.
PX4 provides guides to install mavros and Mavlink ROS with MAVROS Installation Guide
Here we show do to build MAVROS from source code .
-
Install building and managing tools, catkin_tools and wstool see the install catkin tools and install wstool.
-
Create catkin workspace using catkin tools
cd ~ mkdir catkin_ws cd catkin_ws mkdir src catkin init
Then, add the path of catkin_ws into .bashrc
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
-
Initialize your source space with wstool. The target directory of wstool is src
cd catkin_ws wstool init ./src
-
Install Mavlink (1). note: tee is to write the output of rosinstall_generator into the file mavros.rosinstall
# We use the Kinetic reference for all ROS distros as it's not distro-specific and up to date rosinstall_generator --rosdistro kinetic Mavlink | tee /tmp/mavros.rosinstall
(2). install MAVROS with stable one
rosinstall_generator --upstream mavros | tee -a /tmp/mavros.rosinstall
As a consequence, we can cat the "/temp/mavros.rosinstall" that is shown below. We can see from that, wstool just records the git rep information, such as URL, branch name and version.
```bash - git: local-name: Mavlink uri: https://github.com/Mavlink/Mavlink-gbp-release.git version: release/kinetic/Mavlink/2021.3.3-1 - git: local-name: mavros uri: https://github.com/Mavlink/mavros.git version: 1.10.0 ```
-
Create workspace & deps (explanation of wstool see here)
# run this commande in /catkin_ws which merges config in /tmp/mavros.rosinstall to src/.rosinstall wstool merge -t src /tmp/mavros.rosinstall # This command pulls changes from remote to your local filesystem. # download the source codes from git wstool update -t src -j4 // rosdep install --from-paths src --ignore-src -y
-
Install GeographicLib dataset
./src/mavros/mavros/scripts/install_geographiclib_datasets.sh # if /bin/bash^M: bad interpreter: No such file or directory Try running *dos2unix* on the script: http://dos2unix.sourceforge.net/
-
build source
cd catkin_ws catkin build
-
use setup.bash or setup.zsh from workspace.
cd catkin_ws source ./devel/source.bash
-
check if MAVROS and Mavlink are well installed
rospack find mavros rospack find Mavlink
which gives
mavros and Mavlink are installed- run mavros for communication in simulation
roslaunch mavros px4.launch fcu_url:="udp://:[email protected]:14557"
Note fcu_url:="udp://:[email protected]:14557"
indicates that a drone is simulated by this PC.