From 9a24dad17f311ee88b2fba35e88b276576ff9ac0 Mon Sep 17 00:00:00 2001 From: Liu-Xiaoyan97 Date: Fri, 17 Nov 2023 15:01:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=97=A5=E5=BF=97=E8=B0=83?= =?UTF-8?q?=E8=AF=95=E6=A8=A1=E5=BC=8F=EF=BC=8C=E5=B9=B6=E7=B2=BE=E7=AE=80?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZKDogApi/zkmeta.py | 100 ++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 51 deletions(-) diff --git a/ZKDogApi/zkmeta.py b/ZKDogApi/zkmeta.py index c9ff65f..a32d18a 100644 --- a/ZKDogApi/zkmeta.py +++ b/ZKDogApi/zkmeta.py @@ -5,24 +5,25 @@ LastEditors: WZX 17839623189@168.com LastEditTime: 2023-12-05 17:01:44 FilePath: /wzx/zkmetaapi/wukong/ZKMetaUnit/ZKMeta.py ''' -import os, sys +import os +import sys import time import math +import logging from typing import List -from .utils import MQTT, parse_robot_state, calcu_vecloty, calcu_distance +from utils import MQTT, parse_robot_state, calcu_vecloty, calcu_distance sys.path.append("/home/unitree/alg/prejects/ZK-AI/zkdog/ZKDogApi/src") import robot_interface as sdk HIGHLEVEL = 0xee LOWLEVEL = 0xff -# cmake -DPYTHON_LIBRARY=/home/unitree/python3.8/lib \ -# -DPYTHON_INCLUDE_DIR=/home/unitree/python3.8/include/python3.8 \ -# -DPYTHON_EXECUTABLE=/home/unitree/python3.8/bin/python3.8 \ -# .. +logger = logging.getLogger(__name__) +logging.basicConfig(format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) class ZKDOG: - def __init__(self) -> None: + def __init__(self, mode=True) -> None: + self.mode = mode # When mode is equal to false, it is debugging mode, no action is executed, and only logs are printed. self.udp = sdk.UDP(HIGHLEVEL, 8080, "192.168.123.161", 8082) self.MQTT = MQTT() self.cmd = sdk.HighCmd() @@ -247,64 +248,61 @@ class ZKDOG: def getcurrenrstate(self): return self.state.mode - + + def handle_exception(callback): + def decorator(func): + def wrapper(self, *args, **kwargs): + try: + # print(args, kwargs, func) + if self.mode: + res = func(self, *args, **kwargs) + logging.info(f"mode ==> {self.mode} && action {func} ==> {kwargs}") + return callback(res) + else: + time.sleep(1) + logging.info(f"mode ==> {self.mode} && action {func} ==> {kwargs}") + return {"type": True} + except Exception as e: + return callback({"type": False, "msg": str(e)}) + return wrapper + return decorator + + @handle_exception(lambda res: {"type": res}) def turn_left(self, tag=90): - try: - res = self.rolling(tag=tag) - return {"type": res} - except Exception as e: - return {"type": False, "msg": e} + return self.rolling(tag=tag) + @handle_exception(lambda res: {"type": res}) def turn_right(self, tag=90): - try: - res = self.rolling(tag=tag, yawSpeed = -1) - return {"type": res} - except Exception as e: - return {"type": False, "msg": e} - + return self.rolling(tag=tag, yawSpeed=-1) + + @handle_exception(lambda res: {"type": res}) def up(self, tag=1): - try: - res = self.forward(distance=tag) - return {"type": res} - except Exception as e: - return {"type": False, "msg": e} + return self.forward(distance=tag) + @handle_exception(lambda res: {"type": res}) def down(self, tag=1): - try: - res = self.forward(distance=tag, velocaity=[-1, 0]) - return {"type": res} - except Exception as e: - return {"type": False, "msg": e} + return self.forward(distance=tag, velocaity=[-1, 0]) + @handle_exception(lambda res: {"type": True}) def bark(self, tag="bark"): - try: - tag="bark" - if not os.system(f"ssh unitree@192.168.123.13 -t 'aplay -D plughw:2,0 /home/unitree/tmp/{tag}'.wav"): - return {"type": True} - except Exception as e: - return {"type": False, "msg": e} - - def sitdown(self, tag=1): - try: - res = self.__sitdown() - return {"type": res} - except Exception as e: - return {"type": False, "msg": e} + if not os.system(f"ssh unitree@192.168.123.13 -t \'aplay -D plughw:2,0 /home/unitree/tmp/{tag}\'.wav"): + return True + @handle_exception(lambda res: {"type": res}) + def sitdown(self, tag=1): + return self.__sitdown() + + @handle_exception(lambda res: {"type": res}) def standup(self, tag=1): - try: - res = self.__standup() - return {"type": res} - except Exception as e: - return {"type": False, "msg": e} + return self.__standup() if __name__=="__main__": - zd = ZKDOG() + zd = ZKDOG(mode=False) - print(zd.down(tag=1)) - print(zd.turn_right(tag=180)) + # print(zd.down(tag=1)) + # print(zd.turn_right(tag=180)) print(zd.up(tag=1)) print(zd.down(tag=1)) # zd.sitdown() # zd.standup() - # zd.bark() \ No newline at end of file + zd.bark() \ No newline at end of file