ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

When things don't seem to make sense, it's better to stop and review those seemingly unimportant things that you don't doubt you're doing right, because you're probably making a silly mistake. And that is precisely what was happening to me. Both the use of SetEnvironmentVariable and os.environ work correctly when I use them inside the launch. The problem was that to save typing the same thing many times in the console, I was using the option to reuse previous commands and I didn't realize that I was executing another launch file that had the same name except for one character. That's why no matter what I modified in the code it never worked.

The following code works perfectly:

import os
import xacro

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription 
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch_ros.actions import Node
from launch.actions import SetEnvironmentVariable

def generate_launch_description():

    pkg_install_path = get_package_share_directory('mars_robot')

    if 'GAZEBO_MODEL_PATH' in os.environ:
        model_path =  os.environ['GAZEBO_MODEL_PATH'] + ':' + pkg_install_path
    else:
        model_path =  pkg_install_path

    gazebo = IncludeLaunchDescription(
                PythonLaunchDescriptionSource([os.path.join(
                    get_package_share_directory('gazebo_ros'), 'launch'), '/gazebo.launch.py']),
             )

    xacro_robot_pkg_path = os.path.join(
        get_package_share_directory('mars_robot'))

    xacro_file = os.path.join(xacro_robot_pkg_path,
                              'urdf',
                              'mars_robot.xacro')

    doc = xacro.parse(open(xacro_file))
    xacro.process_doc(doc)
    params = {'robot_description': doc.toxml()}

    node_robot_state_publisher = Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        output='screen',
        parameters=[params]
    )

    spawn_entity = Node(package='gazebo_ros', executable='spawn_entity.py',
                        arguments=['-topic', 'robot_description',
                                   '-entity', 'mars_robot'],
                        output='screen')

    return LaunchDescription([
        SetEnvironmentVariable(name='GAZEBO_MODEL_PATH', value=model_path),
        gazebo,
        node_robot_state_publisher,
        spawn_entity,
    ])

Thanks to @ljaniec and @Fetullah Atas for their help, they both gave me a correct solution and allowed me to learn a bit more.

When things don't seem to make sense, it's better to stop and review those seemingly unimportant things that you don't doubt you're doing right, because you're probably making a silly mistake. And that is precisely what was happening to me. Both the use of SetEnvironmentVariable and os.environ work correctly when I use them inside the launch. launch file. The problem was that to save typing the same thing many times in the console, I was using the option to reuse previous commands and I didn't realize that I was executing another launch file that had the same name except for one character. That's why no matter what I modified in the code it never worked.

The following code works perfectly:

import os
import xacro

from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription 
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch_ros.actions import Node
from launch.actions import SetEnvironmentVariable

def generate_launch_description():

    pkg_install_path = get_package_share_directory('mars_robot')

    if 'GAZEBO_MODEL_PATH' in os.environ:
        model_path =  os.environ['GAZEBO_MODEL_PATH'] + ':' + pkg_install_path
    else:
        model_path =  pkg_install_path

    gazebo = IncludeLaunchDescription(
                PythonLaunchDescriptionSource([os.path.join(
                    get_package_share_directory('gazebo_ros'), 'launch'), '/gazebo.launch.py']),
             )

    xacro_robot_pkg_path = os.path.join(
        get_package_share_directory('mars_robot'))

    xacro_file = os.path.join(xacro_robot_pkg_path,
                              'urdf',
                              'mars_robot.xacro')

    doc = xacro.parse(open(xacro_file))
    xacro.process_doc(doc)
    params = {'robot_description': doc.toxml()}

    node_robot_state_publisher = Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        output='screen',
        parameters=[params]
    )

    spawn_entity = Node(package='gazebo_ros', executable='spawn_entity.py',
                        arguments=['-topic', 'robot_description',
                                   '-entity', 'mars_robot'],
                        output='screen')

    return LaunchDescription([
        SetEnvironmentVariable(name='GAZEBO_MODEL_PATH', value=model_path),
        gazebo,
        node_robot_state_publisher,
        spawn_entity,
    ])

Thanks to @ljaniec and @Fetullah Atas for their help, they both gave me a correct solution and allowed me to learn a bit more. more.