-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReturnToParkingAction.py
More file actions
92 lines (72 loc) · 2.91 KB
/
Copy pathReturnToParkingAction.py
File metadata and controls
92 lines (72 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
ReturnToParkingAction — 自主返回待命点
功能:返回地图中 PARKING 类型的 POI 点,支持多机排队(需 Lora 模块)
依赖:需要在地图中预先配置 PARKING 类型 POI;固件版本 ≥ 4.5.5
"""
import time
import requests
# ── 配置 ──────────────────────────────────────────────
ROBOT_IP = "10.160.129.252"
PORT = 1448
BASE_URL = f"http://{ROBOT_IP}:{PORT}/api/core/motion/v1/actions"
# 绕过系统代理(避免 127.0.0.1:7890 超时)
SESSION = __import__("requests").Session()
SESSION.trust_env = False
# 目标待命点 POI 的 ID,留空则自动选择最近的 PARKING 点
PARKING_POI_ID = ""
# 目标停车位被占用时是否等待
WAIT_FOR_PARKING = True
# 等待超时时间(毫秒)
MAX_WAIT_TIME = 60000
SPEED_RATIO = 0.8
POLL_INTERVAL_S = 1.0
# ──────────────────────────────────────────────────────
def build_payload():
options = {
"wait_for_parking": WAIT_FOR_PARKING,
"max_wait_time": MAX_WAIT_TIME,
"move_options": {
"mode": 0,
"speed_ratio": SPEED_RATIO,
},
}
if PARKING_POI_ID:
options["parking_poi_id"] = PARKING_POI_ID
return {
"action_name": "agent.actions.ReturnToParkingAction",
"options": options,
}
def poll_until_done(action_id):
url = f"{BASE_URL}/{action_id}"
while True:
resp = SESSION.get(url, timeout=5)
data = resp.json()
status = data["state"]["status"]
result = data["state"]["result"]
reason = data["state"].get("reason", "")
if status == 4:
if result == 0:
print(f"[完成] action_id={action_id} 已到达待命点")
else:
print(f"[失败] action_id={action_id} result={result} reason={reason}")
if "POI_NOT_FOUND" in reason:
print(" 提示:请确认地图中已配置 PARKING 类型的 POI")
return result == 0
status_desc = {0: "初始化", 1: "前往待命点"}.get(status, str(status))
print(f"[{status_desc}] action_id={action_id} ...")
time.sleep(POLL_INTERVAL_S)
def main():
poi_desc = PARKING_POI_ID if PARKING_POI_ID else "自动选择最近 PARKING 点"
print(f"[ReturnToParkingAction] 目标={poi_desc} 等待={WAIT_FOR_PARKING}")
payload = build_payload()
resp = SESSION.post(BASE_URL, json=payload, timeout=5)
if resp.status_code != 200:
print(f"[错误] HTTP {resp.status_code}: {resp.text}")
return
data = resp.json()
action_id = data["action_id"]
stage = data.get("stage", "")
print(f"[已创建] action_id={action_id} stage={stage}")
poll_until_done(action_id)
if __name__ == "__main__":
main()