-
This commit is contained in:
+23
-14
@@ -61,14 +61,18 @@ class ArmClient:
|
||||
def get_angles(self) -> Tuple[bool, List[float]]:
|
||||
"""获取所有关节角度"""
|
||||
ok, resp = self.send_command("get_angles()")
|
||||
if ok and resp.startswith("get_angles:["):
|
||||
if ok:
|
||||
try:
|
||||
# get_angles:[0.174, 0.520, ...] → list
|
||||
nums = resp.split("[")[1].split("]")[0]
|
||||
angles = [float(x) for x in nums.split(",")]
|
||||
return True, angles
|
||||
# 兼容 "get_angles:[-260.2,...]" 和 "[-260.2,...]" 两种格式
|
||||
text = resp.split(":", 1)[-1] if ":" in resp else resp
|
||||
text = text.strip()
|
||||
if text.startswith("[") and text.endswith("]"):
|
||||
nums = text[1:-1].split(",")
|
||||
angles = [float(x) for x in nums]
|
||||
if len(angles) == 6:
|
||||
return True, angles
|
||||
except:
|
||||
return False, []
|
||||
pass
|
||||
return False, []
|
||||
|
||||
def set_angles(self, angles: List[float], speed: int = 500) -> bool:
|
||||
@@ -94,13 +98,17 @@ class ArmClient:
|
||||
def get_coords(self) -> Tuple[bool, List[float]]:
|
||||
"""获取当前坐标和姿态 [x, y, z, rx, ry, rz]"""
|
||||
ok, resp = self.send_command("get_coords()")
|
||||
if ok and "get_coords:" in resp:
|
||||
if ok:
|
||||
try:
|
||||
nums = resp.split("[")[1].split("]")[0]
|
||||
coords = [float(x) for x in nums.split(",")]
|
||||
return True, coords
|
||||
text = resp.split(":", 1)[-1] if ":" in resp else resp
|
||||
text = text.strip()
|
||||
if text.startswith("[") and text.endswith("]"):
|
||||
nums = text[1:-1].split(",")
|
||||
coords = [float(x) for x in nums]
|
||||
if len(coords) == 6:
|
||||
return True, coords
|
||||
except:
|
||||
return False, []
|
||||
pass
|
||||
return False, []
|
||||
|
||||
def set_coords(self, coords: List[float], speed: int = 500) -> bool:
|
||||
@@ -132,19 +140,20 @@ class ArmClient:
|
||||
def state_check(self) -> bool:
|
||||
"""检查机械臂状态是否正常"""
|
||||
ok, resp = self.send_command("state_check()")
|
||||
return ok and resp == "state_check:1"
|
||||
# 兼容 "state_check:1" 和 "1" 两种格式
|
||||
return ok and resp.strip().lstrip("state_check:") == "1"
|
||||
|
||||
def check_running(self) -> bool:
|
||||
"""检查机械臂是否在运行"""
|
||||
ok, resp = self.send_command("check_running()")
|
||||
return ok and resp == "check_running:1"
|
||||
return ok and resp.strip().lstrip("check_running:") == "1"
|
||||
|
||||
def wait_done(self, timeout: float = 30) -> bool:
|
||||
"""等待上一条命令执行完成"""
|
||||
start = time.time()
|
||||
while time.time() - start < timeout:
|
||||
ok, resp = self.send_command("check_running()")
|
||||
if ok and resp == "check_running:0":
|
||||
if ok and resp.strip().lstrip("check_running:") == "0":
|
||||
return True
|
||||
time.sleep(0.5)
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user