$\color{white}\colorbox{FE9227}{Approaching a comprehensive grasp of the subject!}$

▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓░░░░░░░░░░░ 60%


Now that we have a working tf2 tree, it’s time to open our simulation environment and spawn in our robot. The simulation environment for robots is Gazebo, and once Gazebo is launched our robot will start interacting and experience simulated physics. This is important because the plugins used to control robots operate in the gazebo environment. Let’s start by launching the Gazebo simulation environment.

Table of Contents

Adding Gazebo Model and Plugin Paths

Arc p10.mp4

Before you’re able to spawn a robot into Gazebo, you must first add the model paths of your package directory to that environment. This allows the Gazebo visualization engine to parse your meshes and xacro files and use them to give a visual representation of your environment. To add these model and plugin paths, open your simulation.launch.py file and add the following lines:

from ament_index_python.packages import get_package_share_directory, get_package_prefix
import os
		[ ... ]

		install_dir = get_package_prefix('hunter_se_description')

		# Installing Gazebo Model and Plugin Paths
    if 'GAZEBO_MODEL_PATH' in os.environ:
        os.environ['GAZEBO_MODEL_PATH'] = os.environ['GAZEBO_MODEL_PATH'] + \\
            ':' + install_dir + '/share'
    else:
        os.environ['GAZEBO_MODEL_PATH'] = install_dir + "/share"
    if 'GAZEBO_PLUGIN_PATH' in os.environ:
        os.environ['GAZEBO_PLUGIN_PATH'] = os.environ['GAZEBO_PLUGIN_PATH'] + \\
            ':' + install_dir + '/lib'
    else:
        os.environ['GAZEBO_PLUGIN_PATH'] = install_dir + '/lib'

Launching the Gazebo Client and Gazebo Server

In this tutorial, we are going to be launching two distinct Gazebo nodes. The gzclient.launch.py and gzserver.launch.py handle all the plugins and kinematics of the robot. These are launch files in the gazebo_ros package and must be launched.

Create a new file called simulation.launch.py. Open yoursimulation.launch.py file and add the following lines:

from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import PathJoinSubstitution, TextSubstitution
from launch_ros.substitutions import FindPackageShare

[...]	

		gzserver_launch = IncludeLaunchDescription(
        PythonLaunchDescriptionSource([
            PathJoinSubstitution([
                FindPackageShare('gazebo_ros'),
                'launch', 'gzserver.launch.py',
            ])
        ]),
        launch_arguments={
            'verbose': 'true',
        }.items()
    )

    gzclient_launch = IncludeLaunchDescription(
        PythonLaunchDescriptionSource([
            PathJoinSubstitution([
                FindPackageShare('gazebo_ros'),
                'launch', 'gzclient.launch.py'
            ])
        ]),
        launch_arguments={
            'verbose': 'false',
        }.items()
    )
		
		ld.add_action(gzserver_launch)
		ld.add_action(gzclient_launch)

Examine the code

The first lines add the necessary ROS imports to call on a launch file from another package. Then, using the IncludeLaunchDescription function, the gzserver.launch.py file is called.

gzserver_launch = IncludeLaunchDescription(
        PythonLaunchDescriptionSource([
            PathJoinSubstitution([
                FindPackageShare('gazebo_ros'),
                'launch', 'gzserver.launch.py',
            ])
        ]),
        launch_arguments={
            'verbose': 'true',
            'world': TextSubstitution(text=str(gazebo_world))
        }.items()
    )

Setting the verbose launch argument to true allows for easier debugging of the simulation. This will print out all relevant information to the simulation into the terminal.

Next, the gzclient.launch.py file is called to begin the client-side simulation.

gzclient_launch = IncludeLaunchDescription(
        PythonLaunchDescriptionSource([
            PathJoinSubstitution([
                FindPackageShare('gazebo_ros'),
                'launch', 'gzclient.launch.py'
            ])
        ]),
        launch_arguments={
            'verbose': 'false',
        }.items()
    )

Both these actions are then added to the LaunchDescription. Next, we need to spawn our robot into the Gazebo environment.