Skip to content

Latest commit

 

History

History
executable file
·
230 lines (181 loc) · 6.98 KB

2_1_Simulation_ROS_PX4.md

File metadata and controls

executable file
·
230 lines (181 loc) · 6.98 KB

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).

Table of contents

Step 1 Install ROS

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.

Step 2 Build PX4 from source code

  1. 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
  1. Run the script to install dependencies and tools for nuttx, jMAVSim, Gazebo s
    cd PX4-Autopilot
    bash ./Tools/setup/ubuntu.sh
  1. Test PX4 within Gazebo
    make px4_sitl gazebo

Step 2.5 Enable roslaunch to launch PX4 in 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
  1. Check if ros can find px4 package
rospack find px4

It should give

px4 found by ros

Finally, it should be able to use roslaunch to create px4-gazebo simulation environment by

roslaunch px4 posix_sitl.launch

px4-gazebo simulation environment

Common errors:

  1. gazebo dies

gazebo errors

do

    ps aux | grep gzserver
    kill -2 <pid associated with gzserver>

Step 3 Build mavros (Mavlink) from source code

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 .

  1. Install building and managing tools, catkin_tools and wstool see the install catkin tools and install wstool.

  2. 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
  3. Initialize your source space with wstool. The target directory of wstool is src

    cd catkin_ws
    wstool init ./src
  4. 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
     ```
    
  5. 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
  6. 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/
  7. build source

    cd catkin_ws
    catkin build
  8. use setup.bash or setup.zsh from workspace.

    cd catkin_ws
    source ./devel/source.bash
  9. check if MAVROS and Mavlink are well installed

    rospack find mavros
    
    rospack find Mavlink

which gives

mavros and Mavlink are installed
  1. 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.