Files
fluent_widgets_pyside6/app/controller/mi_an/main_controller.py

117 lines
4.0 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from ...view.mi_an_main_window import Window as MainWindow
from ...view.cood_forms_interface import CoordinateFormsWidget
from ...view.cood_forms_interface import CoordinateTableWidget
from typing import TypedDict
from ...service.robot_service import RobotService
# 法奥机器人
from fairino import Robot
# 子界面管理
class SubViewsDict(TypedDict):
position: CoordinateFormsWidget
class MainController:
def __init__(self):
# 主界面
self.main_window = MainWindow()
# 初始化子界面
self._initSubViews()
# 初始化子控制器
self._initSubControllers()
# 机器人实例
self.robot_client = Robot.RPC("192.168.58.2")
self.robot_service = RobotService(self.robot_client)
self.__connectSignals()
def _initSubViews(self):
self.sub_views: SubViewsDict = {
"system": self.main_window.system, # 系统设置
"product": self.main_window.product, # 生产界面
"robot": self.main_window.robot, # 机器臂基础设置
"io": self.main_window.io, # io面板
"position": self.main_window.position.formsWidget, # 点位设置
"basic": self.main_window.basic, # 基础设置
"point": self.main_window.point, # 点位调试
"other": self.main_window.other, # 其他设置
"data": self.main_window.data, # 数据采集
}
def _initSubControllers(self):
# self.sub_controllers = {
# "system": SystemController(self.sub_views["system"]),
# "produce": ProduceController(self.sub_views["produce"]),
# }
pass
def showMainWindow(self):
self.main_window.show()
def __connectSignals(self):
self.sub_views["position"].form_move_signal.connect(self.handleMove)
self.sub_views["position"].form_update_signal.connect(self.handleGetPosition)
self.sub_views["position"].form_machine_stop_signal.connect(self.handleMachineStop)
# 机器臂停止移动
def handleMachineStop(self):
self.robot_service.stop_moves()
# 设置点位
def handleGetPosition(self, form_obj: CoordinateTableWidget):
result = self.robot_client.GetActualTCPPose() # 获取 工具坐标
if isinstance(result, tuple) and len(result) == 2:
# 成功:解包错误码和位姿
error, tcp_pos = result
# print(f"成功,位姿:{tcp_pos}")
# 保留三位小数
tcp_pos_rounded = [round(pos, 3) for pos in tcp_pos]
# print(f"保留三位小数后:{tcp_pos_rounded}")
else:
# 失败result 即为错误码
print(f"设置位置失败,错误码:{result}")
return
form_obj.update_table_data(tcp_pos_rounded)
# 机器臂移动
# pos_list 中一定是合法坐标
def handleMove(self, pos_list: list, name_list: list):
# print("handleMove 移动 pos_list: ", pos_list)
# print("handleMove 移动 pos_list: ", name_list)
# 后续需要根据状态来设置
tool_id = 1 # 当前 1为吸盘
vel = 20.0
# self.robot_service.start_moves(pos_list, name_list, tool_id=tool_id, vel=vel)
try:
self.robot_service.start_moves(
pos_list=pos_list,
name_list=name_list,
tool_id=tool_id,
vel=vel
)
except ValueError as e:
# 处理“坐标列表与名称列表长度不一致”的错误
print(f"❌ 移动任务提交失败(参数错误):{str(e)}")
# for desc_pos in pos_list:
# print("handleMove 移动 desc_pos: ", pos_list)
# error_code = self.robot_client.MoveCart(desc_pos=desc_pos, tool=tool_id, user=0,vel=vel)
# if error_code == 0:
# print("运动成功")
# else:
# print(f"运动失败,错误码: {error_code}")