目前完成了比赛地图的建图,但是还没有实现定位导航

This commit is contained in:
陌欢醉
2026-06-05 22:35:07 +08:00
parent fab5f811cb
commit 5da3d07656
10 changed files with 766 additions and 2 deletions

View File

@@ -13,7 +13,7 @@ find_package(ament_cmake REQUIRED)
# 查找slam_toolbox包
find_package(slam_toolbox REQUIRED)
install(
DIRECTORY launch config params maps
DIRECTORY launch config params maps world
DESTINATION share/${PROJECT_NAME}
)
if(BUILD_TESTING)

View File

@@ -12,7 +12,7 @@ slam_toolbox:
# ============================ ROS 基础参数 =========================================
odom_frame: odom # 里程计坐标系
map_frame: map # 地图坐标系
base_frame: base_link # 机器人基座坐标系
base_frame: base_footprint # 机器人基座坐标系(与 URDF 根帧一致)
scan_topic: /scan # 激光雷达话题
use_map_saver: true # 启用地图保存功能
mode: mapping # mapping = 定位 + 实时建图(地图可更新)

View File

@@ -0,0 +1,196 @@
#!/usr/bin/env python3
# ============================================================================
# gc_slam_mapping.launch.py
# 功能Gazebo 仿真环境 + SLAM 建图启动文件
#
# 适用场景:
# - 在 Gazebo 仿真中遥控机器人建立环境地图
# - 在已有地图基础上增量更新
# - 单独调试 slam_toolbox 建图参数
#
# 架构:
# Gazebo 仿真器 — 加载 zhihui.world智慧楼墙体环境
# spawn_entity — 在 Gazebo 中生成机器人模型
# robot_state_publisher — 发布机器人 TF 树URDF 模型)
# joint_state_publisher — 发布关节状态
# async_slam_toolbox_node — 激光 SLAM 建图mapping 模式)
# 发布 map→odom TF
# 发布 /map 话题(实时地图)
#
# 使用方式:
# ros2 launch gc_navigation2_slamtoolbox gc_slam_mapping.launch.py
#
# 保存地图:
# ros2 service call /slam_toolbox/serialize_map slam_toolbox/srv/SerializePoseGraph \
# "{filename: '/home/guoch/test_ws/src/gc_navigation2_slamtoolbox/maps/my_map'}"
# ros2 run nav2_map_server map_saver_cli -f <map_name>
# ============================================================================
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, ExecuteProcess, SetEnvironmentVariable
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node
from launch_ros.parameter_descriptions import ParameterValue
from launch.substitutions import Command
def generate_launch_description():
"""生成 LaunchDescription启动 Gazebo + slam_toolbox 建图"""
# ============================ 1. 包路径 =========================================
pkg_dir = get_package_share_directory('gc_navigation2_slamtoolbox')
origincar_urdf_dir = get_package_share_directory('origincar_description')
# ============================ 2. 声明启动参数 ====================================
use_sim_time = LaunchConfiguration('use_sim_time', default='true')
# slam_params_file: slam_toolbox 配置文件
slam_params_file = LaunchConfiguration(
'slam_params_file',
default=os.path.join(pkg_dir, 'config', 'slam_toolbox_async.yaml'))
# map_file_name: 可选,加载已有序列化地图继续建图
map_file_name = LaunchConfiguration('map_file_name', default='')
# model: 机器人 URDF 模型路径(用于 xacro 处理)
# origincar.urdf 是纯 URDF不含 xacro 宏xacro 会原样透传
default_model_path = os.path.join(
origincar_urdf_dir, 'urdf', 'origincar.urdf')
model = DeclareLaunchArgument(
name='model', default_value=default_model_path)
# spawn_model: Gazebo 中生成机器人用的 URDF不含 xacro 宏)
spawn_model_path = os.path.join(
origincar_urdf_dir, 'urdf', 'origincar.urdf')
# ============================ 3. 设置 GAZEBO_MODEL_PATH ==========================
# 让 Gazebo 能找到 zhihui 模型,同时保留标准模型路径
gazebo_model_path = os.path.join(pkg_dir, 'world')
existing_gazebo_path = os.environ.get('GAZEBO_MODEL_PATH', '')
if existing_gazebo_path:
full_gazebo_path = f'{gazebo_model_path}:{existing_gazebo_path}'
else:
full_gazebo_path = gazebo_model_path
set_gazebo_model_path = SetEnvironmentVariable(
name='GAZEBO_MODEL_PATH',
value=full_gazebo_path
)
# ============================ 4. 启动 Gazebo 仿真器 ==============================
gazebo_world_path = os.path.join(pkg_dir, 'world', 'zhihui.world')
start_gazebo_cmd = ExecuteProcess(
cmd=['gazebo', '--verbose',
'-s', 'libgazebo_ros_init.so',
'-s', 'libgazebo_ros_factory.so',
gazebo_world_path],
output='screen'
)
# ============================ 5. 机器人初始位姿参数 ==============================
# 通过命令行参数覆盖,例如:
# ros2 launch ... spawn_x:=1.0 spawn_y:=-2.0 spawn_yaw:=1.57
spawn_x = LaunchConfiguration('spawn_x', default='-4.311092')
spawn_y = LaunchConfiguration('spawn_y', default='-4.299756')
spawn_z = LaunchConfiguration('spawn_z', default='0.0')
spawn_yaw = LaunchConfiguration('spawn_yaw', default='0.0')
# 将位姿参数设为环境变量,供 spawn 命令使用
set_spawn_x = SetEnvironmentVariable('SPAWN_X', spawn_x)
set_spawn_y = SetEnvironmentVariable('SPAWN_Y', spawn_y)
set_spawn_z = SetEnvironmentVariable('SPAWN_Z', spawn_z)
set_spawn_yaw = SetEnvironmentVariable('SPAWN_YAW', spawn_yaw)
# ============================ 6. 在 Gazebo 中生成机器人 ==========================
# 通过 bash 读取环境变量,确保位姿参数正确传递
robot_name_in_model = 'mycar'
spawn_entity_cmd = ExecuteProcess(
cmd=['bash', '-c',
'ros2 run gazebo_ros spawn_entity.py '
'-entity mycar '
f'-file {spawn_model_path} '
'-x $SPAWN_X -y $SPAWN_Y -z $SPAWN_Z -Y $SPAWN_YAW'],
output='screen'
)
# ============================ 7. robot_state_publisher ===========================
robot_description = ParameterValue(
Command(['xacro ', LaunchConfiguration('model')]),
value_type=str
)
robot_state_publisher_node = Node(
package='robot_state_publisher',
executable='robot_state_publisher',
name='robot_state_publisher',
output='screen',
parameters=[{
'robot_description': robot_description,
'use_sim_time': True,
'publish_frequency': 30.0,
}],
)
# ============================ 8. joint_state_publisher ===========================
joint_state_publisher_node = Node(
package='joint_state_publisher',
executable='joint_state_publisher',
name='joint_state_publisher',
output='screen',
parameters=[{'use_sim_time': use_sim_time}],
)
# ============================ 9. slam_toolbox 建图节点 ============================
slam_toolbox_node = Node(
package='slam_toolbox',
executable='async_slam_toolbox_node',
name='slam_toolbox',
output='screen',
parameters=[
slam_params_file,
{
'use_sim_time': use_sim_time,
'map_file_name': map_file_name,
},
],
)
# ============================ 10. 组装 LaunchDescription =========================
return LaunchDescription([
DeclareLaunchArgument(
'use_sim_time',
default_value='true',
description='使用仿真时间Gazebo=true, 实车=false)'),
DeclareLaunchArgument(
'slam_params_file',
default_value=os.path.join(pkg_dir, 'config', 'slam_toolbox_async.yaml'),
description='slam_toolbox 参数配置文件路径'),
DeclareLaunchArgument(
'map_file_name',
default_value='',
description='已有序列化地图路径(.posegraph留空则从零开始建图'),
DeclareLaunchArgument(
'spawn_x', default_value='-4.311092',
description='机器人初始 x 坐标m'),
DeclareLaunchArgument(
'spawn_y', default_value='-4.299756',
description='机器人初始 y 坐标m'),
DeclareLaunchArgument(
'spawn_z', default_value='0.0',
description='机器人初始 z 坐标m'),
DeclareLaunchArgument(
'spawn_yaw', default_value='0.0',
description='机器人初始偏航角rad'),
model,
set_gazebo_model_path,
set_spawn_x,
set_spawn_y,
set_spawn_z,
set_spawn_yaw,
start_gazebo_cmd,
spawn_entity_cmd,
robot_state_publisher_node,
joint_state_publisher_node,
slam_toolbox_node,
])

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,7 @@
image: zhihui.pgm
mode: trinary
resolution: 0.05
origin: [-4.98, -4.71, 0]
negate: 0
occupied_thresh: 0.65
free_thresh: 0.25

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" ?>
<model>
<name>zhihui</name>
<version>1.0</version>
<sdf version="1.7">model.sdf</sdf>
<author>
<name></name>
<email></email>
</author>
<description></description>
</model>

View File

@@ -0,0 +1,471 @@
<?xml version='1.0'?>
<sdf version='1.7'>
<model name='zhihui'>
<pose>-2.11635 -1.73906 0 0 -0 0</pose>
<link name='Wall_12'>
<collision name='Wall_12_Collision'>
<geometry>
<box>
<size>1.75 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_12_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>1.75 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>-1.61999 -0.744014 0 0 -0 0</pose>
</link>
<link name='Wall_15'>
<collision name='Wall_15_Collision'>
<geometry>
<box>
<size>2.75 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_15_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>2.75 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>1.62754 -0.744014 0 0 -0 0</pose>
</link>
<link name='Wall_17'>
<collision name='Wall_17_Collision'>
<geometry>
<box>
<size>1 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_17_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>1 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>-0.827543 -0.3078 0 0 -0 1.5708</pose>
</link>
<link name='Wall_19'>
<collision name='Wall_19_Collision'>
<geometry>
<box>
<size>1 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_19_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>1 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>0.327543 -0.319014 0 0 -0 1.5708</pose>
</link>
<link name='Wall_2'>
<collision name='Wall_2_Collision'>
<geometry>
<box>
<size>5 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_2_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>5 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>0.062177 -2.89677 0 0 -0 0</pose>
</link>
<link name='Wall_29'>
<collision name='Wall_29_Collision'>
<geometry>
<box>
<size>3.75 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_29_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>3.75 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>-0.305988 0.758719 0 0 -0 0</pose>
</link>
<link name='Wall_31'>
<collision name='Wall_31_Collision'>
<geometry>
<box>
<size>3.75 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_31_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>3.75 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>-0.300519 2.09188 0 0 -0 0</pose>
</link>
<link name='Wall_33'>
<collision name='Wall_33_Collision'>
<geometry>
<box>
<size>1.5 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_33_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>1.5 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>1.65085 1.41824 0 0 -0 -1.5708</pose>
</link>
<link name='Wall_36'>
<collision name='Wall_36_Collision'>
<geometry>
<box>
<size>2.75 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_36_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>2.75 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>2.3125 1.40828 0 0 -0 1.5708</pose>
</link>
<link name='Wall_38'>
<collision name='Wall_38_Collision'>
<geometry>
<box>
<size>5.25 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_38_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>5.25 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>-0.19189 2.74413 0 0 -0 3.14159</pose>
</link>
<link name='Wall_4'>
<collision name='Wall_4_Collision'>
<geometry>
<box>
<size>2 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_4_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>2 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>2.6265 -1.8264 0 0 -0 1.5708</pose>
</link>
<link name='Wall_40'>
<collision name='Wall_40_Collision'>
<geometry>
<box>
<size>1.5 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_40_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>1.5 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>-2.25486 1.43754 0 0 -0 -1.5708</pose>
</link>
<link name='Wall_44'>
<collision name='Wall_44_Collision'>
<geometry>
<box>
<size>3 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_44_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>3 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>-2.89932 1.47176 0 0 -0 -1.5708</pose>
</link>
<link name='Wall_46'>
<collision name='Wall_46_Collision'>
<geometry>
<box>
<size>2.25 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_46_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>2.25 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>-1.87754 0.1172 0 0 -0 3.14159</pose>
</link>
<link name='Wall_48'>
<collision name='Wall_48_Collision'>
<geometry>
<box>
<size>2.13496 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_48_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>2.13496 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>1.32002 0.107134 0 0 -0 0.001157</pose>
</link>
<link name='Wall_6'>
<collision name='Wall_6_Collision'>
<geometry>
<box>
<size>2 0.15 2.5</size>
</box>
</geometry>
<pose>0 0 1.25 0 -0 0</pose>
</collision>
<visual name='Wall_6_Visual'>
<pose>0 0 1.25 0 -0 0</pose>
<geometry>
<box>
<size>2 0.15 2.5</size>
</box>
</geometry>
<material>
<script>
<uri>file://media/materials/scripts/gazebo.material</uri>
<name>Gazebo/Grey</name>
</script>
<ambient>1 1 1 1</ambient>
</material>
<meta>
<layer>0</layer>
</meta>
</visual>
<pose>-2.50971 -1.81518 0 0 -0 1.5708</pose>
</link>
<static>1</static>
</model>
</sdf>