''' This demo show the communication interface of MR813 motion control board based on Lcm. Dependency: - robot_control_cmd_lcmt.py - robot_control_response_lcmt.py ''' import lcm import sys import os import time from threading import Thread, Lock import sys from utils.robot_control_cmd_lcmt import robot_control_cmd_lcmt from utils.robot_control_response_lcmt import robot_control_response_lcmt from utils.localization_lcmt import localization_lcmt from utils.image_raw import ImageProcessor from task_1.task_1 import run_task_1 from task_5.task_5 import run_task_5 def main(): Ctrl = Robot_Ctrl() Ctrl.run() msg = robot_control_cmd_lcmt() image_processor = ImageProcessor() image_processor.run() try: print("Recovery stand") msg.mode = 12 # Recovery stand msg.gait_id = 0 msg.life_count += 1 Ctrl.Send_cmd(msg) Ctrl.Wait_finish(12, 0) # time.sleep(1) # run_task_1(Ctrl, msg, image_processor) run_task_5(Ctrl, msg) # time.sleep(100) except KeyboardInterrupt: print("\n程序被用户中断") except Exception as e: print(f"发生错误: {e}") finally: print("正在清理资源...") Ctrl.quit() image_processor.destroy() print("程序已退出") sys.exit() class Robot_Ctrl(object): def __init__(self): self.rec_thread = Thread(target=self.rec_responce) self.send_thread = Thread(target=self.send_publish) self.odo_thread = Thread(target=self.rec_responce_o) self.lc_r = lcm.LCM("udpm://239.255.76.67:7670?ttl=255") self.lc_s = lcm.LCM("udpm://239.255.76.67:7671?ttl=255") self.lc_o = lcm.LCM("udpm://239.255.76.67:7667?ttl=255") self.cmd_msg = robot_control_cmd_lcmt() self.rec_msg = robot_control_response_lcmt() self.odo_msg = localization_lcmt() self.send_lock = Lock() self.delay_cnt = 0 self.mode_ok = 0 self.gait_ok = 0 self.runing = 1 def run(self): self.lc_r.subscribe("robot_control_response", self.msg_handler) self.lc_o.subscribe("global_to_robot", self.msg_handler_o) self.send_thread.start() self.rec_thread.start() self.odo_thread.start() def msg_handler(self, channel, data): self.rec_msg = robot_control_response_lcmt().decode(data) if self.rec_msg.order_process_bar >= 95: self.mode_ok = self.rec_msg.mode else: self.mode_ok = 0 def msg_handler_o(self, channel, data): self.odo_msg = localization_lcmt().decode(data) if self.odo_msg.timestamp % 100 == 0: print(self.odo_msg.rpy) def rec_responce(self): while self.runing: self.lc_r.handle() time.sleep(0.002) def rec_responce_o(self): while self.runing: self.lc_o.handle() time.sleep(0.002) def Wait_finish(self, mode, gait_id): count = 0 while self.runing and count < 2000: # 10s if self.mode_ok == mode and self.gait_ok == gait_id: return True else: time.sleep(0.005) count += 1 print("Wait_finish timeout.") return False def send_publish(self): while self.runing: self.send_lock.acquire() if self.delay_cnt > 20: # Heartbeat signal 10HZ self.lc_s.publish("robot_control_cmd", self.cmd_msg.encode()) self.delay_cnt = 0 self.delay_cnt += 1 self.send_lock.release() time.sleep(0.005) def Send_cmd(self, msg): self.send_lock.acquire() self.delay_cnt = 50 self.cmd_msg = msg self.send_lock.release() def quit(self): self.runing = 0 self.rec_thread.join() self.send_thread.join() # Main function if __name__ == '__main__': main()