Last active
September 9, 2023 09:00
-
-
Save rfzeg/63d824f0c6ed44da639e9630a76fbc6c to your computer and use it in GitHub Desktop.
ROS2 launch Gazebo and spawn URDF robot using Execute Process
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Launch Gazebo server and client with command line arguments.""" | |
"""Spawn robot from URDF file.""" | |
import os | |
from ament_index_python.packages import get_package_share_directory | |
from launch import LaunchDescription | |
from launch.actions import ExecuteProcess | |
from launch.substitutions import LaunchConfiguration | |
def generate_launch_description(): | |
use_sim_time = LaunchConfiguration('use_sim_time', default='true') | |
robot_name = 'rrbot_description' | |
world_file_name = 'empty.world' | |
world = os.path.join(get_package_share_directory( | |
robot_name), 'worlds', world_file_name) | |
urdf = os.path.join(get_package_share_directory( | |
robot_name), 'urdf', 'my_robot.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', 'param', 'set', '/gazebo', | |
'use_sim_time', use_sim_time], | |
output='screen'), | |
ExecuteProcess( | |
cmd=['ros2', 'service', 'call', '/spawn_entity', | |
'gazebo_msgs/SpawnEntity', swpan_args], | |
output='screen'), | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I launch but gazebo simulaor crash everytime saying "Gazebo" is not responding