codex整理结果
This commit is contained in:
+28
-48
@@ -1208,35 +1208,16 @@ def api_agv_stop():
|
||||
|
||||
@app.route("/api/agv/reset", methods=["POST"])
|
||||
def api_agv_reset():
|
||||
"""撞物体后复位 - 停止运动并尝试重新上电"""
|
||||
"""撞物体后复位 - 停止运动并重新检查 ROS2 连接"""
|
||||
import time
|
||||
if not gs.agv_controller:
|
||||
return jsonify({"ok": False, "error": "AGV 控制器未初始化"}), 400
|
||||
try:
|
||||
# 1. 先停止运动
|
||||
gs.agv_controller.stop()
|
||||
time.sleep(0.5)
|
||||
|
||||
# 2. 检查 AGV 对象是否存在
|
||||
agv = gs.agv_controller._agv
|
||||
if not agv:
|
||||
return jsonify({"ok": False, "error": "AGV 未连接,请重新连接"}), 400
|
||||
|
||||
# 3. 检查电源状态
|
||||
power_on = agv.is_power_on()
|
||||
if not power_on:
|
||||
# 撞物体后可能自动断电保护,尝试重新上电
|
||||
agv.power_on()
|
||||
time.sleep(2)
|
||||
power_on = agv.is_power_on()
|
||||
if power_on:
|
||||
gs.agv_controller._connected = True
|
||||
return jsonify({"ok": True, "message": "复位成功,已重新上电"})
|
||||
else:
|
||||
return jsonify({"ok": False, "error": "上电失败,请检查 AGV 状态"}), 500
|
||||
else:
|
||||
# 电源正常,只需要停止
|
||||
return jsonify({"ok": True, "message": "复位成功,AGV 已停止"})
|
||||
if gs.agv_controller.connect():
|
||||
return jsonify({"ok": True, "message": "复位成功,AGV 已停止并重新连接"})
|
||||
return jsonify({"ok": False, "error": "AGV 已停止,但 ROS2 连接检查失败"}), 500
|
||||
except Exception as e:
|
||||
return jsonify({"ok": False, "error": str(e)}), 500
|
||||
|
||||
@@ -1259,13 +1240,10 @@ def api_mission_start():
|
||||
}
|
||||
print(f"[Mission] options: {options}")
|
||||
|
||||
# 清除可能存在的旧实例,确保可以启动
|
||||
if hasattr(MissionExecutorV3, "_instance"):
|
||||
MissionExecutorV3._instance = None
|
||||
gs.state = State.IDLE
|
||||
|
||||
if hasattr(MissionExecutorV3, "_instance") and MissionExecutorV3._instance:
|
||||
existing = getattr(MissionExecutorV3, "_instance", None)
|
||||
if existing and existing.report.get("status") not in ("idle", "completed"):
|
||||
return jsonify({"ok": False, "error": "任务已在运行中"}), 400
|
||||
MissionExecutorV3._instance = None
|
||||
|
||||
def run(single_step):
|
||||
from config import AGV_CONFIG
|
||||
@@ -1276,28 +1254,30 @@ def api_mission_start():
|
||||
}
|
||||
executor = MissionExecutorV3(config)
|
||||
|
||||
conn = executor.connect_all()
|
||||
if not conn.get("agv") or not conn.get("arm"):
|
||||
gs.mission_report = {"error": "连接失败", "details": conn}
|
||||
gs.state = State.IDLE
|
||||
return
|
||||
try:
|
||||
conn = executor.connect_all()
|
||||
if not conn.get("agv") or not conn.get("arm"):
|
||||
gs.mission_report = {"error": "连接失败", "details": conn}
|
||||
gs.state = State.IDLE
|
||||
return
|
||||
|
||||
gs.state = State.RUNNING
|
||||
gs.state = State.RUNNING
|
||||
|
||||
machines_list = gs.machines_config if isinstance(gs.machines_config, list) else gs.machines_config.get("machines", [])
|
||||
models_list = gs.models_config if isinstance(gs.models_config, list) else gs.models_config.get("models", [])
|
||||
machines_list = gs.machines_config if isinstance(gs.machines_config, list) else gs.machines_config.get("machines", [])
|
||||
models_list = gs.models_config if isinstance(gs.models_config, list) else gs.models_config.get("models", [])
|
||||
|
||||
report = executor.execute_mission(
|
||||
mission_config=gs.mission_config,
|
||||
machines=machines_list,
|
||||
qr_configs=gs.qr_config,
|
||||
models=models_list,
|
||||
single_step=single_step,
|
||||
options=options,
|
||||
)
|
||||
gs.mission_report = report
|
||||
executor.disconnect_all()
|
||||
gs.state = State.IDLE if report.get("error") is None else State.PAUSED
|
||||
report = executor.execute_mission(
|
||||
mission_config=gs.mission_config,
|
||||
machines=machines_list,
|
||||
qr_configs=gs.qr_config,
|
||||
models=models_list,
|
||||
single_step=single_step,
|
||||
options=options,
|
||||
)
|
||||
gs.mission_report = report
|
||||
gs.state = State.IDLE if report.get("error") is None else State.PAUSED
|
||||
finally:
|
||||
executor.disconnect_all()
|
||||
|
||||
thread = threading.Thread(target=run, args=(single_step,), daemon=True)
|
||||
thread.start()
|
||||
|
||||
Reference in New Issue
Block a user