Nav2导航 + 初始化位置功能
- nav2_navigator.py: 通过 ros2 action 与 Nav2 通信,修复 YAML 格式(pose 嵌套) - mission_executor.py: 改用 Nav2 navigate_to_pose action 导航 - app.py: navigate/to 等 API 改用 Nav2Navigator;新增 /api/mission/init_pose API,通过子进程调用 rclpy 发布 /initialpose 到 (0,0,0) - setting.html/js: 任务配置Tab加「初始化位置」按钮 注意:ROS2 daemon (domain=1) 与 Flask 在不同 domain, Flask 进程内调用 rclpy 会破坏 daemon 状态,需通过子进程调用
This commit is contained in:
@@ -641,6 +641,33 @@ def api_mission_position():
|
||||
return jsonify({"ok": True, "position": pos, "battery": battery})
|
||||
|
||||
|
||||
@app.route("/api/mission/init_pose", methods=["POST"])
|
||||
def api_mission_init_pose():
|
||||
"""将 AMCL 初始位置设为 (0,0,0),无需 RViz"""
|
||||
try:
|
||||
script = "/tmp/ros2_init_pose.sh"
|
||||
lines = [
|
||||
"#!/bin/bash",
|
||||
"export ROS_DOMAIN_ID=1",
|
||||
"source /opt/ros/humble/setup.bash",
|
||||
"source ~/agv_pro_ros2/install/setup.bash",
|
||||
"python3 /tmp/publish_init_pose.py",
|
||||
]
|
||||
with open(script, "w") as f:
|
||||
f.write("\n".join(lines) + "\n")
|
||||
os.chmod(script, 0o755)
|
||||
result = subprocess.run(
|
||||
[script],
|
||||
capture_output=True, text=True, timeout=12,
|
||||
env={**os.environ, "ROS_DOMAIN_ID": "1"}
|
||||
)
|
||||
logger.info(f"init_pose: rc={result.returncode}")
|
||||
return jsonify({"ok": True, "message": "初始位置已设为 (0,0,0)"})
|
||||
except Exception as e:
|
||||
logger.error(f"初始化位置失败: {e}")
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
|
||||
@app.route("/api/mission/config", methods=["GET"])
|
||||
def api_mission_config_get():
|
||||
"""获取任务配置(网格尺寸和空位矩阵)"""
|
||||
|
||||
Reference in New Issue
Block a user