76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
from mqtt_client import MQTTClient
|
|
from data_center import DataCenter
|
|
from Manager.mwmotor_manager import MotorManager
|
|
import sys
|
|
import os
|
|
import time
|
|
import signal
|
|
from PySide6.QtCore import *
|
|
|
|
class Application(QCoreApplication):
|
|
def __init__(self, argv):
|
|
super().__init__(argv)
|
|
self.motor_manager = None
|
|
self.mqtt_client = None
|
|
self._signal_received = False
|
|
|
|
# 创建定时器检查信号
|
|
self.timer = QTimer()
|
|
self.timer.timeout.connect(self.check_signals)
|
|
self.timer.start(100) # 每100ms检查一次
|
|
|
|
def check_signals(self):
|
|
if self._signal_received:
|
|
self.timer.stop()
|
|
self.cleanup()
|
|
|
|
def cleanup(self):
|
|
print("\n正在清理资源...")
|
|
# 停止电机
|
|
if self.motor_manager:
|
|
self.motor_manager.emergency_stop(MotorManager.ID_MT1 | MotorManager.ID_MT2)
|
|
time.sleep(0.2)
|
|
self.motor_manager.destroy()
|
|
time.sleep(0.2)
|
|
# 断开MQTT连接
|
|
if self.mqtt_client:
|
|
self.mqtt_client.quit()
|
|
print("资源清理完成,程序退出")
|
|
self.quit()
|
|
|
|
def signal_handler(signum, frame):
|
|
app._signal_received = True
|
|
|
|
def exec_cmd(cmd):
|
|
print("exec:", cmd)
|
|
out = os.popen(cmd)
|
|
print(out.readlines())
|
|
|
|
if __name__ == "__main__":
|
|
app = Application(sys.argv)
|
|
|
|
# 注册信号处理
|
|
signal.signal(signal.SIGINT, signal_handler)
|
|
signal.signal(signal.SIGTERM, signal_handler)
|
|
|
|
exec_cmd("sudo pinctrl set 17 op pn dl")
|
|
|
|
app.motor_manager = MotorManager()
|
|
app.mqtt_client = MQTTClient()
|
|
data_center = DataCenter()
|
|
|
|
app.motor_manager.mqtt_client = app.mqtt_client
|
|
|
|
app.motor_manager.update_online_status.connect(data_center.update_online_status)
|
|
app.motor_manager.update_setting_velocity.connect(data_center.update_motor_setting_velocity)
|
|
app.motor_manager.update_location.connect(data_center.update_motor_location)
|
|
app.motor_manager.update_estimates.connect(data_center.update_motor_estimates)
|
|
app.motor_manager.update_direction.connect(data_center.update_motor_direction)
|
|
app.motor_manager.update_duration.connect(data_center.update_motor_duration)
|
|
app.motor_manager.update_state.connect(data_center.update_motor_state)
|
|
app.motor_manager.update_work_mode.connect(data_center.update_motor_sports_mode)
|
|
data_center.dataReady.connect(app.mqtt_client.send_motor_status)
|
|
app.mqtt_client.write_motor_data.connect(app.motor_manager.set_value)
|
|
app.mqtt_client.write_motor_globaldev_data.connect(app.motor_manager.set_globaldev_value)
|
|
|
|
sys.exit(app.exec()) |