From 5461a09eeedf7b1acbfb12a433877f97137b2ce5 Mon Sep 17 00:00:00 2001 From: LiangShiyun <15723182159@163.com> Date: Thu, 12 Jun 2025 21:36:36 +0800 Subject: [PATCH] jiaru recv --- Massage/MassageControl/MassageRobot_nova5.py | 6 +++- Massage/MassageControl/hardware/dobot_api.py | 34 ++++++++++++++------ 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/Massage/MassageControl/MassageRobot_nova5.py b/Massage/MassageControl/MassageRobot_nova5.py index ad680d9..e569438 100644 --- a/Massage/MassageControl/MassageRobot_nova5.py +++ b/Massage/MassageControl/MassageRobot_nova5.py @@ -545,6 +545,7 @@ class MassageRobot: def arm_command_loop(self): self.logger.log_info("机械臂控制线程启动") self.arm.filter_matirx = np.zeros((1,7)) # 位姿伺服用 + recv_time = 0 # self.arm.filter_matrix = np.zeros((1,6)) # 角度伺服用 while (not self.arm.is_exit) and (not self.exit_event.is_set()): try: @@ -581,7 +582,7 @@ class MassageRobot: ).encode() run_time = time.time() - process_start_time - sleep_duration = self.control_rate.to_sec() - run_time + sleep_duration = self.control_rate.to_sec() - run_time - recv_time b2 =time.time() if sleep_duration > 0: time.sleep(sleep_duration) @@ -594,6 +595,9 @@ class MassageRobot: break try: self.arm.dashboard.socket_dobot.sendall(tcp_command) + recv_start_time = time.time() + self.arm.dashboard.socket_dobot.recv(1024) + recv_time = time.time() - recv_start_time break except Exception as e: print(f"发送指令错误:{e}") diff --git a/Massage/MassageControl/hardware/dobot_api.py b/Massage/MassageControl/hardware/dobot_api.py index 0b9d060..90d2494 100644 --- a/Massage/MassageControl/hardware/dobot_api.py +++ b/Massage/MassageControl/hardware/dobot_api.py @@ -178,21 +178,33 @@ class DobotApi: # self.log(f'Receive from {self.ip}:{self.port}: {data_str}') return data_str + # def flush_recv_buffer(self): + # """非阻塞清空接收缓冲区""" + # self.socket_dobot.setblocking(0) + # try: + # while True: + # ready = select.select([self.socket_dobot], [], [], 0.01) + # if ready[0]: + # try: + # self.socket_dobot.recv(1024) + # except: + # break + # else: + # break + # finally: + # self.socket_dobot.setblocking(1) + def flush_recv_buffer(self): - """非阻塞清空接收缓冲区""" - self.socket_dobot.setblocking(0) + """极速清空接收缓冲区""" + self.socket_dobot.setblocking(False) try: while True: - ready = select.select([self.socket_dobot], [], [], 0.01) - if ready[0]: - try: - self.socket_dobot.recv(1024) - except: - break - else: + try: + self.socket_dobot.recv(4096) # 一次最多清64KB + except BlockingIOError: break finally: - self.socket_dobot.setblocking(1) + self.socket_dobot.setblocking(True) def close(self): """ @@ -1947,7 +1959,9 @@ class DobotApiDashboard(DobotApi): for ii in params: string = string + ','+ii string = string + ')' + start_time = time.time() self.flush_recv_buffer() + print(f"+++++++{time.time()-start_time}s++++++") return self.sendRecvMsg(string) def MovL(self, a1, b1, c1, d1, e1, f1, coordinateMode, user=-1, tool=-1, a=-1, v=-1, speed=-1, cp=-1, r=-1):