2024-10-08 21:36:42 +08:00
|
|
|
import os
|
|
|
|
|
|
|
|
import xacro
|
|
|
|
from ament_index_python.packages import get_package_share_directory
|
|
|
|
from launch import LaunchDescription
|
|
|
|
from launch.actions import DeclareLaunchArgument, OpaqueFunction, IncludeLaunchDescription, RegisterEventHandler
|
|
|
|
from launch.event_handlers import OnProcessExit
|
|
|
|
from launch.substitutions import PathJoinSubstitution
|
|
|
|
from launch_ros.actions import Node
|
|
|
|
from launch_ros.substitutions import FindPackageShare
|
2024-10-21 21:47:25 +08:00
|
|
|
from launch.launch_description_sources import PythonLaunchDescriptionSource
|
2024-10-08 21:36:42 +08:00
|
|
|
|
|
|
|
|
2024-10-21 21:47:25 +08:00
|
|
|
def launch_setup(context, *args, **kwargs):
|
|
|
|
package_description = context.launch_configurations['pkg_description']
|
|
|
|
init_height = context.launch_configurations['height']
|
2024-10-08 21:36:42 +08:00
|
|
|
pkg_path = os.path.join(get_package_share_directory(package_description))
|
|
|
|
|
2024-10-21 21:47:25 +08:00
|
|
|
xacro_file = os.path.join(pkg_path, 'xacro', 'robot.xacro')
|
|
|
|
robot_description = xacro.process_file(xacro_file, mappings={'GAZEBO': 'true'}).toxml()
|
2024-10-08 21:36:42 +08:00
|
|
|
|
|
|
|
rviz_config_file = os.path.join(get_package_share_directory(package_description), "config", "visualize_urdf.rviz")
|
|
|
|
|
2024-10-21 21:47:25 +08:00
|
|
|
rviz = Node(
|
|
|
|
package='rviz2',
|
|
|
|
executable='rviz2',
|
|
|
|
name='rviz_ocs2',
|
|
|
|
output='screen',
|
|
|
|
arguments=["-d", rviz_config_file]
|
|
|
|
)
|
2024-10-08 21:36:42 +08:00
|
|
|
|
|
|
|
gz_spawn_entity = Node(
|
|
|
|
package='ros_gz_sim',
|
|
|
|
executable='create',
|
|
|
|
output='screen',
|
|
|
|
arguments=['-topic', 'robot_description', '-name',
|
2024-10-21 21:47:25 +08:00
|
|
|
'robot', '-allow_renaming', 'true', '-z', init_height]
|
2024-10-08 21:36:42 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
robot_state_publisher = Node(
|
|
|
|
package='robot_state_publisher',
|
|
|
|
executable='robot_state_publisher',
|
|
|
|
name='robot_state_publisher',
|
|
|
|
parameters=[
|
|
|
|
{
|
|
|
|
'publish_frequency': 20.0,
|
|
|
|
'use_tf_static': True,
|
|
|
|
'robot_description': robot_description,
|
|
|
|
'ignore_timestamp': True
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
joint_state_publisher = Node(
|
|
|
|
package="controller_manager",
|
|
|
|
executable="spawner",
|
|
|
|
arguments=["joint_state_broadcaster",
|
|
|
|
"--controller-manager", "/controller_manager"],
|
|
|
|
)
|
|
|
|
|
|
|
|
imu_sensor_broadcaster = Node(
|
|
|
|
package="controller_manager",
|
|
|
|
executable="spawner",
|
|
|
|
arguments=["imu_sensor_broadcaster",
|
|
|
|
"--controller-manager", "/controller_manager"],
|
|
|
|
)
|
|
|
|
|
|
|
|
leg_pd_controller = Node(
|
|
|
|
package="controller_manager",
|
|
|
|
executable="spawner",
|
|
|
|
arguments=["leg_pd_controller",
|
|
|
|
"--controller-manager", "/controller_manager"],
|
|
|
|
)
|
|
|
|
|
|
|
|
controller = Node(
|
|
|
|
package="controller_manager",
|
|
|
|
executable="spawner",
|
2024-10-21 21:47:25 +08:00
|
|
|
arguments=["rl_quadruped_controller", "--controller-manager", "/controller_manager"]
|
2024-10-08 21:36:42 +08:00
|
|
|
)
|
|
|
|
|
2024-10-21 21:47:25 +08:00
|
|
|
|
|
|
|
return [
|
|
|
|
rviz,
|
2024-10-08 21:36:42 +08:00
|
|
|
Node(
|
|
|
|
package='ros_gz_bridge',
|
|
|
|
executable='parameter_bridge',
|
|
|
|
arguments=['/clock@rosgraph_msgs/msg/Clock[gz.msgs.Clock'],
|
|
|
|
output='screen'
|
|
|
|
),
|
|
|
|
IncludeLaunchDescription(
|
|
|
|
PythonLaunchDescriptionSource(
|
|
|
|
[PathJoinSubstitution([FindPackageShare('ros_gz_sim'),
|
|
|
|
'launch',
|
|
|
|
'gz_sim.launch.py'])]),
|
|
|
|
launch_arguments=[('gz_args', [' -r -v 4 empty.sdf'])]),
|
|
|
|
robot_state_publisher,
|
|
|
|
gz_spawn_entity,
|
|
|
|
leg_pd_controller,
|
|
|
|
RegisterEventHandler(
|
|
|
|
event_handler=OnProcessExit(
|
|
|
|
target_action=leg_pd_controller,
|
2024-10-21 21:47:25 +08:00
|
|
|
on_exit=[imu_sensor_broadcaster, joint_state_publisher, controller],
|
2024-10-08 21:36:42 +08:00
|
|
|
)
|
|
|
|
),
|
2024-10-21 21:47:25 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def generate_launch_description():
|
|
|
|
pkg_description = DeclareLaunchArgument(
|
|
|
|
'pkg_description',
|
|
|
|
default_value='go2_description',
|
|
|
|
description='package for robot description'
|
|
|
|
)
|
|
|
|
|
|
|
|
height = DeclareLaunchArgument(
|
|
|
|
'height',
|
|
|
|
default_value='0.5',
|
|
|
|
description='Init height in simulation'
|
|
|
|
)
|
|
|
|
|
|
|
|
return LaunchDescription([
|
|
|
|
pkg_description,
|
|
|
|
height,
|
|
|
|
OpaqueFunction(function=launch_setup),
|
2024-10-08 21:36:42 +08:00
|
|
|
])
|