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

Is there a documentetion or example of how to launch gazebo with python launch file?

asked 2022-11-18 04:59:22 -0500

Edvard gravatar image

updated 2022-12-14 06:15:43 -0500

Hi. Sorry for all mistakes, English is not my native language. I am migrating my project to ROS2 Foxy and for now I have a problem with understanding, how to launch gazebo from python launch file. Is there any detailed documentation or example for that case? All that I have found so far is launch file for turtlebot3, and it just don't work for some reason. Appreciate any help.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-11-18 05:27:10 -0500

Ranjit Kathiriya gravatar image

updated 2022-11-18 06:55:59 -0500

Hello,

You can have a look at question #q374976, You can do so by using below code.

import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import ExecuteProcess

def generate_launch_description():
    package_dir =get_package_share_directory('<package_path>') # add package path
    world_files = os.path.join(package_dir,'worlds','<world_path>') # add world file path for *.world file

    ld = LaunchDescription()

    gazeboworld = ExecuteProcess(
        cmd=['gazebo','--verbose',world_files,'-s','libgazebo_ros_factory.so']
    )

    ld.add_action(gazeboworld)
    return ld

And Let's say you have URDF file and you want to add in Gazebo,

def generate_launch_description():

urdf = os.path.join(get_package_share_directory(
        robot_name), 'urdf', '<File>.urdf')

xml = open(urdf, 'r').read()

xml = xml.replace('"', '\\"')

swpan_args = '{name: \"my_robot\", xml: \"' + xml + '\" }'

    return LaunchDescription([
        ExecuteProcess(
            cmd=['gazebo', '--verbose', world,
                 '-s', 'libgazebo_ros_factory.so'],
            output='screen'),
        ExecuteProcess(
            cmd=['ros2', 'service', 'call', '/spawn_entity',
                 'gazebo_msgs/SpawnEntity', swpan_args],
            output='screen'),
    ])
edit flag offensive delete link more

Comments

Thank you very much for your help.

Edvard gravatar image Edvard  ( 2022-11-18 06:54:38 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2022-11-18 04:59:22 -0500

Seen: 351 times

Last updated: Dec 14 '22