feat: AMCL 定位接入、场地定位工具与文档更新
This commit is contained in:
1 parent
0c6b49e276
commit
0e4e4c6e18
45 files changed
+3173
-151
No files matched your search
@@ -0,0 +1,306 @@
|
||||
#!/usr/bin/env python3
|
||||
"""哨兵导航仿真 —— `ros2 launch` 入口(按文件路径启动,不依赖 ament 包注册)。
|
||||
|
||||
用法:
|
||||
|
||||
cd ~/Gazebo_projects/sentry_nav_dev
|
||||
ros2 launch src/sentry_sim/launch/sentry_arena.launch.py # 全部
|
||||
ros2 launch src/sentry_sim/launch/sentry_arena.launch.py headless:=true rviz:=false
|
||||
ros2 launch src/sentry_sim/launch/sentry_arena.launch.py nav2:=false # 只看仿真
|
||||
ros2 launch src/sentry_sim/launch/sentry_arena.launch.py --show-args # 看所有参数
|
||||
|
||||
和 `scripts/sentry_up.sh` 的关系
|
||||
------------------------------
|
||||
两者**等价**,都是完整启动链:
|
||||
|
||||
| | `sentry_up.sh` | 本文件 |
|
||||
|---|---|---|
|
||||
| 入口 | `scripts/sentry_up.sh` | `ros2 launch <路径>` |
|
||||
| 排序 | bash 顺序执行 + 轮询 `/clock` | launch 管进程;桥仍用带等待的脚本 |
|
||||
| 依赖 | 只有 bash | 需要 `ros2 launch` |
|
||||
| 优势 | 少一层、日志直接 | 参数可见(`--show-args`)、进程树可见、退出统一收尾 |
|
||||
|
||||
⚠️ **桥的话题列表只在 `scripts/bridge_after_gz.sh` 里**,本文件只是调用它。
|
||||
(`sentry_up.sh` 把同样的列表内联在自己里面 —— 那是唯一需要手工同步的地方。)
|
||||
|
||||
为什么没有 `package.xml` 也能 `ros2 launch`
|
||||
------------------------------------------
|
||||
`ros2 launch` 接受**文件路径**,不需要包注册。所以:
|
||||
|
||||
* 系统包(`robot_state_publisher` / `nav2_map_server` / `rviz2` …)
|
||||
照常写 `Node(package=..., executable=...)`;
|
||||
* 本工程自己的节点(`self_filter`)用 `ExecuteProcess` 直接指 xmake 产物;
|
||||
* 数据包路径用 `__file__` 往上推,不查 ament。
|
||||
|
||||
坐标系
|
||||
------
|
||||
同时发布两条静态变换:
|
||||
|
||||
map → odom 平移 (0.9, 4.0) 仿真里用里程计当真值,替代 AMCL
|
||||
map → field 平移 (6.0, 4.0) 场地中心 = 零点
|
||||
|
||||
`map` 是官方栅格图坐标系(西南内角 = (0,0)),`field` 是给决策代码用的直觉坐标系。
|
||||
"""
|
||||
|
||||
import os
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from ament_index_python.packages import get_package_share_directory
|
||||
from launch import LaunchDescription
|
||||
from launch.actions import (
|
||||
DeclareLaunchArgument,
|
||||
ExecuteProcess,
|
||||
IncludeLaunchDescription,
|
||||
LogInfo,
|
||||
SetEnvironmentVariable,
|
||||
)
|
||||
from launch.conditions import IfCondition
|
||||
from launch.launch_description_sources import PythonLaunchDescriptionSource
|
||||
from launch.substitutions import (LaunchConfiguration,
|
||||
PathJoinSubstitution, PythonExpression)
|
||||
from launch_ros.actions import Node
|
||||
from launch_ros.parameter_descriptions import ParameterValue
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# 路径推算:launch/ 的上一级是 src/sentry_sim,再上三级是仓库根
|
||||
# ---------------------------------------------------------------------------
|
||||
PKG_SHARE = Path(__file__).resolve().parents[1] # src/sentry_sim
|
||||
WS = Path(__file__).resolve().parents[3] # 仓库根
|
||||
BIN_DIR = WS / 'build' / 'linux' / 'x86_64' / 'release' # xmake 产物
|
||||
SCRIPTS = PKG_SHARE / 'scripts'
|
||||
|
||||
# 场地迁移包:优先用仓库自带的 field/,没有才退回本机旧路径
|
||||
_FIELD_IN_REPO = WS / 'field' / 'RMUL2027_map_transfer_20261016_STEP_R1'
|
||||
_FIELD_FALLBACK = (Path.home() / 'Gazebo_projects' /
|
||||
'RMUL2027_map_transfer_20261016_STEP_R1')
|
||||
DEFAULT_FIELD_ROOT = os.environ.get(
|
||||
'SENTRY_FIELD_ROOT',
|
||||
str(_FIELD_IN_REPO if _FIELD_IN_REPO.is_dir() else _FIELD_FALLBACK))
|
||||
|
||||
DEFAULT_MAP_YAML = os.environ.get(
|
||||
'SENTRY_MAP_YAML',
|
||||
str(Path(DEFAULT_FIELD_ROOT) / 'src' / 'guodong_sentry_sim' / 'maps' /
|
||||
'rmul_2027' / 'rmul_2027.yaml'))
|
||||
|
||||
|
||||
def _robot_description():
|
||||
"""在**构建 launch 描述时**就把 URDF 展开成字符串。
|
||||
|
||||
为什么不放到运行时用 `Command(['xacro ', ...])`:URDF 里的网格路径是
|
||||
`$(env SENTRY_SIM_SHARE)/meshes/*.stl`,而本工程不用 ament。
|
||||
这里显式给 xacro 传入这个环境变量,时序上最稳。
|
||||
"""
|
||||
env = dict(os.environ)
|
||||
env['SENTRY_SIM_SHARE'] = str(PKG_SHARE)
|
||||
return subprocess.run(
|
||||
['xacro', str(PKG_SHARE / 'urdf' / 'sentry.urdf.xacro')],
|
||||
env=env, capture_output=True, text=True, check=True).stdout
|
||||
|
||||
|
||||
def _gz_resource_path():
|
||||
"""GZ_SIM_RESOURCE_PATH:本包的 models + 迁移包的 models(可带用户追加的)。"""
|
||||
# 注意冒号分隔符不能漏 —— 漏了会拼成一条不存在的路径,
|
||||
# gazebo 只会报 "Unable to find uri[model://xxx]",不会说路径写错了
|
||||
parts = [str(PKG_SHARE / 'models'), ':',
|
||||
LaunchConfiguration('field_root'),
|
||||
'/src/guodong_sentry_sim/models']
|
||||
extra = os.environ.get('GZ_SIM_RESOURCE_PATH')
|
||||
if extra:
|
||||
parts += [':', extra]
|
||||
return parts
|
||||
|
||||
|
||||
def generate_launch_description():
|
||||
headless = LaunchConfiguration('headless')
|
||||
nav2 = LaunchConfiguration('nav2')
|
||||
rviz = LaunchConfiguration('rviz')
|
||||
world = LaunchConfiguration('world')
|
||||
map_yaml = LaunchConfiguration('map')
|
||||
|
||||
# 布尔参数统一用 PythonExpression 判,避免 launch 把 "false" 当真值
|
||||
is_headless = PythonExpression(["'", headless, "'.lower() in ('true', '1')"])
|
||||
is_gui = PythonExpression(["'", headless, "'.lower() not in ('true', '1')"])
|
||||
nav2_on = PythonExpression(["'", nav2, "'.lower() in ('true', '1')"])
|
||||
rviz_on = PythonExpression([
|
||||
"'", rviz, "'.lower() in ('true', '1') and '",
|
||||
headless, "'.lower() not in ('true', '1')"])
|
||||
|
||||
robot_description = _robot_description()
|
||||
|
||||
actions = [
|
||||
# ---------------- 参数 ----------------
|
||||
DeclareLaunchArgument(
|
||||
'headless', default_value='false',
|
||||
description='true = 不显示 Gazebo 界面(gz sim -s)'),
|
||||
DeclareLaunchArgument(
|
||||
'nav2', default_value='true', description='false = 不起 Nav2'),
|
||||
DeclareLaunchArgument(
|
||||
'rviz', default_value='true', description='false = 不起 RViz'),
|
||||
DeclareLaunchArgument(
|
||||
'world', default_value='sentry_arena.sdf',
|
||||
description='worlds/ 下的世界文件名'),
|
||||
DeclareLaunchArgument(
|
||||
'map', default_value=DEFAULT_MAP_YAML,
|
||||
description='Nav2 用的栅格图 yaml'),
|
||||
DeclareLaunchArgument(
|
||||
'field_root', default_value=DEFAULT_FIELD_ROOT,
|
||||
description='迁移包(guodong_sentry_sim)所在目录'),
|
||||
|
||||
# ---------------- 环境变量 ----------------
|
||||
SetEnvironmentVariable('GZ_SIM_RESOURCE_PATH', _gz_resource_path()),
|
||||
SetEnvironmentVariable(
|
||||
'GZ_PARTITION', os.environ.get('GZ_PARTITION', 'sentry')),
|
||||
SetEnvironmentVariable(
|
||||
'ROS_DOMAIN_ID', os.environ.get('ROS_DOMAIN_ID', '0')),
|
||||
# URDF 的网格路径靠它解析;tools/check_stack.py 也读它
|
||||
SetEnvironmentVariable('SENTRY_SIM_SHARE', str(PKG_SHARE)),
|
||||
|
||||
# ---------------- 1. Gazebo ----------------
|
||||
# 用 run_gz.sh 包一层:它会清掉 LD_LIBRARY_PATH,
|
||||
# 否则 source 过 ROS 之后连系统版 gz 也会加载 vendor 库(缺 cpu-lidar 插件)
|
||||
# ⚠️ cmd 列表里的每个元素是**独立 argv**,不会自动拼接。
|
||||
# world 是 LaunchConfiguration,所以必须用 PathJoinSubstitution
|
||||
# 合成成一个参数,否则 gz 会收到三个散参数
|
||||
# (报 "The following arguments were not expected: xxx.sdf /")。
|
||||
ExecuteProcess(
|
||||
cmd=[str(SCRIPTS / 'run_gz.sh'), 'sim', '-r',
|
||||
'--force-version', '10',
|
||||
PathJoinSubstitution([str(PKG_SHARE / 'worlds'), world])],
|
||||
name='gazebo',
|
||||
output='screen',
|
||||
condition=IfCondition(is_gui),
|
||||
),
|
||||
# ⚠️ headless 也必须带 -r(启动即运行)。
|
||||
# 少了 -r 的话 gazebo 以**暂停**状态启动:物理引擎不步进 →
|
||||
# MecanumDrive 不发 /odom、传感器不出数据,但 /clock 照常发布,
|
||||
# 表面上一切正常,实际整条数据链是死的。
|
||||
ExecuteProcess(
|
||||
cmd=[str(SCRIPTS / 'run_gz.sh'), 'sim', '-s', '-r',
|
||||
'--force-version', '10',
|
||||
PathJoinSubstitution([str(PKG_SHARE / 'worlds'), world])],
|
||||
name='gazebo',
|
||||
output='screen',
|
||||
condition=IfCondition(is_headless),
|
||||
),
|
||||
|
||||
# ---------------- 2. ros_gz 桥 ----------------
|
||||
# bridge_after_gz.sh 自己轮询 /clock 再起桥:
|
||||
# gz-transport 的发现不回补,桥先于话题存在就订阅会一直收不到数据
|
||||
ExecuteProcess(
|
||||
cmd=[str(SCRIPTS / 'bridge_after_gz.sh')],
|
||||
name='ros_gz_bridge',
|
||||
output='screen',
|
||||
),
|
||||
|
||||
# ---------------- 3. TF 树 ----------------
|
||||
Node(
|
||||
package='robot_state_publisher',
|
||||
executable='robot_state_publisher',
|
||||
name='robot_state_publisher',
|
||||
output='screen',
|
||||
parameters=[{
|
||||
'robot_description': ParameterValue(
|
||||
robot_description, value_type=str),
|
||||
'use_sim_time': True,
|
||||
}],
|
||||
),
|
||||
|
||||
# ---------------- 4. odom -> base_link ----------------
|
||||
# Gazebo 的麦轮插件只发 odometry 消息、不发 TF
|
||||
# SENTRY_ODOM_SCALE != 1.0 时给里程计加比例误差(演示 AMCL 纠正漂移用)。
|
||||
# 这里是构建期读环境变量、直接拼成普通字符串 ——
|
||||
# 如果写成嵌套列表 ['-p', 'odom_scale:=', v],会被拼成 "-podom_scale:=1.15"(没空格),
|
||||
# rclpy 的参数解析会直接报错。
|
||||
ExecuteProcess(
|
||||
cmd=['python3', str(SCRIPTS / 'odom_tf.py'), '--ros-args',
|
||||
'-p', 'use_sim_time:=true',
|
||||
'-p', 'odom_scale:=' + os.environ.get('SENTRY_ODOM_SCALE', '1.0')],
|
||||
name='odom_tf',
|
||||
output='screen',
|
||||
),
|
||||
|
||||
# ---------------- 5. /cmd_vel 改名转发 ----------------
|
||||
# 插件订阅 /model/sentry/cmd_vel,而桥只支持两端同名
|
||||
ExecuteProcess(
|
||||
cmd=['python3', str(SCRIPTS / 'cmd_vel_relay.py'),
|
||||
'--ros-args', '-p', 'use_sim_time:=true'],
|
||||
name='cmd_vel_relay',
|
||||
output='screen',
|
||||
),
|
||||
|
||||
# ---------------- 6. 点云自滤 ----------------
|
||||
# 本工程没有 ament 注册,不能 ros2 run,直接跑 xmake 产物
|
||||
ExecuteProcess(
|
||||
cmd=[str(BIN_DIR / 'self_filter'),
|
||||
'--ros-args', '-p', 'use_sim_time:=true'],
|
||||
name='self_filter',
|
||||
output='screen',
|
||||
),
|
||||
|
||||
# ---------------- 7. 静态变换 ----------------
|
||||
# map -> odom 由 AMCL 发布(use_localization:=True),这里**不能**再发一份,
|
||||
# 否则两个发布者打架。要回到"作弊定位"就把 use_localization 改 False 并放开下面:
|
||||
# Node(package='tf2_ros', executable='static_transform_publisher',
|
||||
# name='map_to_odom',
|
||||
# arguments=['--x','0.9','--y','4.0','--z','0.0',
|
||||
# '--yaw','0.0','--pitch','0.0','--roll','0.0',
|
||||
# '--frame-id','map','--child-frame-id','odom'],
|
||||
# parameters=[{'use_sim_time': True}], output='screen'),
|
||||
# map -> field:场地中心 = 零点。场地 12×8 → field 系 x[-6,6] y[-4,4]
|
||||
Node(
|
||||
package='tf2_ros',
|
||||
executable='static_transform_publisher',
|
||||
name='map_to_field',
|
||||
arguments=['--x', '6.0', '--y', '4.0', '--z', '0.0',
|
||||
'--yaw', '0.0', '--pitch', '0.0', '--roll', '0.0',
|
||||
'--frame-id', 'map', '--child-frame-id', 'field'],
|
||||
parameters=[{'use_sim_time': True}],
|
||||
output='screen',
|
||||
),
|
||||
|
||||
# ---------------- 8. 静态地图服务 ----------------
|
||||
# 由 bringup 提供:serve_static_map 默认跟随 use_localization,
|
||||
# 所以 use_localization:=True 时 bringup 自己起 map_server + amcl。
|
||||
# 这里再起一份会**同名冲突**(lifecycle_manager 崩在 createLifecycleServiceServers)。
|
||||
|
||||
# ---------------- 9. Nav2 ----------------
|
||||
IncludeLaunchDescription(
|
||||
PythonLaunchDescriptionSource(os.path.join(
|
||||
get_package_share_directory('nav2_bringup'),
|
||||
'launch', 'bringup_launch.py')),
|
||||
launch_arguments={
|
||||
'map': map_yaml,
|
||||
'use_sim_time': 'true',
|
||||
'params_file': str(PKG_SHARE / 'config' / 'nav2_params.yaml'),
|
||||
# 组合模式在本机起不来(容器一个组件都不加载),分进程更稳
|
||||
'use_composition': 'False',
|
||||
'use_localization': 'True',
|
||||
# 这两个子系统本工程用不上,少两个 lifecycle 节点少两处启动风险
|
||||
'use_keepout_zones': 'False',
|
||||
'use_speed_zones': 'False',
|
||||
'slam': 'False',
|
||||
}.items(),
|
||||
condition=IfCondition(nav2_on),
|
||||
),
|
||||
|
||||
# ---------------- 10. RViz ----------------
|
||||
Node(
|
||||
package='rviz2',
|
||||
executable='rviz2',
|
||||
name='rviz2',
|
||||
arguments=['-d', str(PKG_SHARE / 'config' / 'sentry.rviz')],
|
||||
parameters=[{'use_sim_time': True}],
|
||||
output='screen',
|
||||
condition=IfCondition(rviz_on),
|
||||
),
|
||||
|
||||
LogInfo(msg=[
|
||||
'\n[sentry_arena] 坐标系:',
|
||||
'\n map 官方栅格图坐标系,西南内角 = (0,0),场地中心 = (6.0, 4.0)',
|
||||
'\n field 场地中心 = (0,0),范围 x[-6,6] y[-4,4],出生点在 (-5.1, 0.0)',
|
||||
'\n[sentry_arena] 体检:python3 tools/check_stack.py',
|
||||
]),
|
||||
]
|
||||
|
||||
return LaunchDescription(actions)
|
||||
Reference in new issue
Block a user