init: ROS2 learning suite
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
"""
|
||||
robot_tf2_launch.py —— 启动 3 关节机械臂的 TF2 演示。
|
||||
|
||||
启动顺序:
|
||||
joint_state_publisher (本包) → /joint_states
|
||||
↓
|
||||
robot_state_publisher (系统包 ros-humble-robot-state-publisher)
|
||||
读取 URDF + JointState → 算 base_link -> link1 -> link2 -> gripper 的 TF → /tf
|
||||
↓
|
||||
tf2_listener (本包) 查 base_link -> gripper,周期打印末端位姿。
|
||||
|
||||
URDF 通过 CommandLineAction 用 xacro/直接读 XML,
|
||||
这里用 'command' 直接 cat 文件,launch 时读出来给 robot_state_publisher。
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from launch import LaunchDescription
|
||||
from launch.actions import DeclareLaunchArgument
|
||||
from launch.substitutions import LaunchConfiguration
|
||||
from launch_ros.actions import Node
|
||||
|
||||
from ament_index_python.packages import get_package_share_directory
|
||||
|
||||
|
||||
def generate_launch_description():
|
||||
pkg_share = get_package_share_directory('cpp_robot_tf2')
|
||||
urdf_path = os.path.join(pkg_share, 'urdf', 'simple_arm.urdf')
|
||||
|
||||
# robot_description 参数:把 URDF 文件内容作为字符串直接传入。
|
||||
# 注意:不在 launch 时执行 shell 命令(launch 的 Command 列表拼接有时会
|
||||
# 丢空格,产生 'cat/root/...' 之类错误);改为 launch 启动前直接读文件。
|
||||
with open(urdf_path, 'r', encoding='utf-8') as f:
|
||||
robot_description = f.read()
|
||||
|
||||
use_sim_time = LaunchConfiguration('use_sim_time')
|
||||
|
||||
return LaunchDescription([
|
||||
DeclareLaunchArgument(
|
||||
'use_sim_time', default_value='false',
|
||||
description='Use simulation (Gazebo) clock if true'),
|
||||
|
||||
# 1) JointState 发布者:本包 C++ 节点,模拟关节运动。
|
||||
Node(
|
||||
package='cpp_robot_tf2',
|
||||
executable='joint_state_publisher',
|
||||
name='joint_state_publisher_cpp',
|
||||
output='screen',
|
||||
parameters=[{'use_sim_time': use_sim_time}],
|
||||
),
|
||||
|
||||
# 2) robot_state_publisher:ROS2 系统包,URDF + JointState -> /tf。
|
||||
Node(
|
||||
package='robot_state_publisher',
|
||||
executable='robot_state_publisher',
|
||||
name='robot_state_publisher',
|
||||
output='screen',
|
||||
parameters=[{
|
||||
'robot_description': robot_description,
|
||||
'use_sim_time': use_sim_time,
|
||||
}],
|
||||
),
|
||||
|
||||
# 3) tf2_listener:本包 C++ 节点,周期打印 gripper 位姿。
|
||||
Node(
|
||||
package='cpp_robot_tf2',
|
||||
executable='tf2_listener',
|
||||
name='tf2_listener_cpp',
|
||||
output='screen',
|
||||
parameters=[{'use_sim_time': use_sim_time}],
|
||||
),
|
||||
])
|
||||
Reference in New Issue
Block a user