McuDeviationSet.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. from collections import defaultdict
  2. from .McuDebug import McuDebug
  3. from functools import partial
  4. import time, asyncio
  5. import settings
  6. class McuDeviationSet:
  7. # 调试处理
  8. # data_sign = Signal(dict)
  9. # info_sign = Signal(str)
  10. # is_get_offset = Signal(dict)
  11. def __init__(self, mcu):
  12. super().__init__()
  13. self.mcu = mcu
  14. # self.setupUi(self)
  15. # self.init()
  16. # self.set_enable_by_mcu()
  17. # self.show()
  18. self.last_value = defaultdict(float)
  19. is_debug = True if settings.IS_DEBUG == "true" else False
  20. self.mcu_debug = McuDebug(mcu, is_debug=is_debug, is_deviation=False)
  21. # loop = asyncio.get_event_loop()
  22. self.get_mcu_deviation()
  23. # # 运动到设定位
  24. # QTimer.singleShot(2500, self.init_pos)
  25. # def set_enable_by_mcu(self):
  26. # if self.mcu.state_camera_motor != 2:
  27. # self.doubleSpinBox.setEnabled(False)
  28. # self.pushButton_3.setEnabled(False)
  29. # if self.mcu.state_camera_steering != 2:
  30. # self.doubleSpinBox_2.setEnabled(False)
  31. # self.pushButton_16.setEnabled(False)
  32. # if self.mcu.state_turntable_steering != 2:
  33. # self.doubleSpinBox_3.setEnabled(False)
  34. # self.pushButton_22.setEnabled(False)
  35. # self.doubleSpinBox_7.setEnabled(False)
  36. def init(self):
  37. self.is_get_offset.connect(self.init_pos)
  38. self.windows.data_info.connect(self.get_mcu_deviation_info)
  39. # 相机电机
  40. self.doubleSpinBox.setSingleStep(1)
  41. self.doubleSpinBox.setMinimum(0)
  42. self.doubleSpinBox.setMaximum(400)
  43. self.doubleSpinBox.valueChanged.connect(
  44. lambda x: self.change_value(x, "相机电机")
  45. )
  46. # 相机舵机
  47. self.doubleSpinBox_2.setSingleStep(0.1)
  48. self.doubleSpinBox_2.setMinimum(-40)
  49. self.doubleSpinBox_2.setMaximum(40)
  50. self.doubleSpinBox_2.valueChanged.connect(
  51. lambda x: self.change_value(x, "相机舵机")
  52. )
  53. # 转盘电机
  54. self.doubleSpinBox_3.setSingleStep(1)
  55. self.doubleSpinBox_3.setMinimum(-720)
  56. self.doubleSpinBox_3.setMaximum(720)
  57. self.doubleSpinBox_3.valueChanged.connect(
  58. lambda x: self.change_value(x, "转盘舵机")
  59. )
  60. # 转盘前后电机
  61. self.doubleSpinBox_7.setSingleStep(1)
  62. self.doubleSpinBox_7.setMinimum(0)
  63. self.doubleSpinBox_7.setMaximum(950)
  64. self.doubleSpinBox_7.setValue(300)
  65. self.doubleSpinBox_7.valueChanged.connect(
  66. lambda x: self.change_value(x, "转盘前后电机")
  67. )
  68. # 翻板舵机中位
  69. self.doubleSpinBox_4.setSingleStep(0.5)
  70. self.doubleSpinBox_4.setValue(90)
  71. self.doubleSpinBox_4.setMinimum(0)
  72. self.doubleSpinBox_4.setMaximum(180)
  73. self.doubleSpinBox_4.valueChanged.connect(
  74. lambda x: self.change_value(x, "翻板舵机中位")
  75. )
  76. # 翻板舵机高位
  77. self.doubleSpinBox_5.setSingleStep(0.5)
  78. self.doubleSpinBox_5.setValue(10)
  79. self.doubleSpinBox_5.setMinimum(0)
  80. self.doubleSpinBox_5.setMaximum(180)
  81. self.doubleSpinBox_5.valueChanged.connect(
  82. lambda x: self.change_value(x, "翻板舵机高位")
  83. )
  84. # 翻板舵机上升速度
  85. self.doubleSpinBox_6.setSingleStep(1)
  86. self.doubleSpinBox_6.setValue(5)
  87. self.doubleSpinBox_6.setMinimum(1)
  88. self.doubleSpinBox_6.setMaximum(10)
  89. self.doubleSpinBox_6.valueChanged.connect(
  90. lambda x: self.change_value(x, "翻板舵机上升速度")
  91. )
  92. # 翻板舵机下降速度
  93. self.doubleSpinBox_8.setSingleStep(1)
  94. self.doubleSpinBox_8.setValue(5)
  95. self.doubleSpinBox_8.setMinimum(1)
  96. self.doubleSpinBox_8.setMaximum(10)
  97. self.doubleSpinBox_8.valueChanged.connect(
  98. lambda x: self.change_value(x, "翻板舵机下降速度")
  99. )
  100. # 获取偏移量
  101. self.pushButton_2.clicked.connect(self.get_mcu_deviation)
  102. # 设定相机高度偏移量
  103. self.pushButton_3.clicked.connect(lambda *args: self.set_deviation("相机电机"))
  104. # 设定相机舵机偏移量
  105. self.pushButton_16.clicked.connect(lambda *args: self.set_deviation("相机舵机"))
  106. # 设定转盘偏移量
  107. self.pushButton_22.clicked.connect(lambda *args: self.set_deviation("转盘舵机"))
  108. # 设定翻版舵机偏移量
  109. self.pushButton_8.clicked.connect(
  110. lambda *args: self.set_deviation("翻板舵机中位")
  111. )
  112. # 设定翻版舵机偏移量
  113. self.pushButton_9.clicked.connect(
  114. lambda *args: self.set_deviation("翻板舵机高位")
  115. )
  116. self.pushButton_10.clicked.connect(
  117. lambda *args: self.set_deviation("翻板舵机上升速度")
  118. )
  119. self.pushButton_12.clicked.connect(
  120. lambda *args: self.set_deviation("翻板舵机下降速度")
  121. )
  122. # 转盘重新获取转速比
  123. # self.pushButton_7.clicked.connect(self.set_turntable_to_ratio_init)
  124. # 转盘重新定位到原点
  125. self.pushButton_11.clicked.connect(self._to_init_all)
  126. def init_pos(self, data):
  127. time.sleep(0.6)
  128. func = partial(self._move_equipment, data=data)
  129. self.do_thread_run(func, call_back=None, time_out=30)
  130. def _move_equipment(self, data):
  131. self.mcu_debug.move_equipment(data=data)
  132. # MCU运动是否有停止检查,设定超时时间
  133. time.sleep(1.5)
  134. if self.check_mcu_move_is_stop() is False:
  135. return
  136. def check_mcu_move_is_stop(self, out_time=15):
  137. _s = time.time()
  138. while 1:
  139. time.sleep(0.1)
  140. if time.time() - _s > out_time:
  141. return True
  142. if self.mcu.mcu_move_state == 2:
  143. return True
  144. if settings.IS_LIN_SHI_TEST:
  145. time.sleep(3)
  146. return True
  147. def change_value(self, value, name):
  148. value = round(float(value), 2)
  149. print("change_value===>", value, name)
  150. if name == "相机电机":
  151. self.mcu_debug.camera_high_motor(value=value)
  152. if name == "相机舵机":
  153. self.mcu_debug.camera_steering(value=value)
  154. if name == "转盘舵机":
  155. self.mcu_debug.turntable_steering(value=value)
  156. if name == "转盘前后电机":
  157. self.mcu_debug.turntable_position_motor(value=value)
  158. if name == "翻板舵机中位":
  159. self.mcu_debug.overturn_steering(value=value)
  160. if name == "翻板舵机高位":
  161. self.mcu_debug.overturn_steering(value=value)
  162. if name == "翻板舵机上升速度":
  163. pass
  164. print(value, name)
  165. self.last_value[name] = value
  166. def set_deviation(self, name):
  167. if name == "相机电机":
  168. # 设定相机高度偏移量 单位mm
  169. camera_high_motor_deviation = int(self.last_value[name])
  170. device_name = "camera_high_motor"
  171. self.mcu.set_deviation(
  172. device_name=device_name, _type=0, deviation=camera_high_motor_deviation
  173. )
  174. if name == "相机舵机":
  175. # 设定相机舵机偏移量
  176. camera_steering_deviation = self.last_value[name]
  177. device_name = "camera_steering"
  178. self.mcu.set_deviation(
  179. device_name=device_name, _type=0, deviation=camera_steering_deviation
  180. )
  181. # self.doubleSpinBox_2.setValue(0.0)
  182. if name == "转盘舵机":
  183. # 设定转盘舵机偏移角度 单位 度
  184. turntable_steering_deviation = self.last_value[name]
  185. device_name = "turntable_steering"
  186. self.mcu.set_deviation(
  187. device_name=device_name, _type=1, deviation=turntable_steering_deviation
  188. )
  189. if name == "翻板舵机中位":
  190. # 设定翻版舵机偏移量
  191. value = self.last_value[name]
  192. device_name = "overturn_steering"
  193. self.mcu.set_deviation(device_name=device_name, _type=0, deviation=value)
  194. if name == "翻板舵机高位":
  195. # 设定翻版舵机偏移量
  196. value = self.last_value[name]
  197. device_name = "overturn_steering"
  198. self.mcu.set_deviation(device_name=device_name, _type=1, deviation=value)
  199. if name == "翻板舵机上升速度":
  200. # 设定翻版舵机偏移量
  201. value = self.last_value[name]
  202. device_name = "overturn_steering"
  203. self.mcu.set_deviation(device_name=device_name, _type=2, deviation=value)
  204. if name == "翻板舵机下降速度":
  205. # 设定翻版舵机偏移量
  206. value = self.last_value[name]
  207. device_name = "overturn_steering"
  208. self.mcu.set_deviation(device_name=device_name, _type=3, deviation=value)
  209. def _to_init_all(self, *args):
  210. self.mcu.to_init_device_origin_point(device_name="mcu", is_force=True)
  211. def get_mcu_deviation(self):
  212. # await asyncio.sleep(0.01)
  213. # await self.mcu.getDeviationInfo()
  214. # loop = asyncio.get_event_loop()
  215. # loop.create_task(self.mcu.getDeviationInfo(), name="get_mcu_deviation")
  216. asyncio.run(self.mcu.getDeviationInfo())
  217. def get_mcu_deviation_info(self, data):
  218. if "_type" not in data:
  219. return
  220. get_deviation_data = {}
  221. if data["_type"] == "get_deviation_data":
  222. data = data["data"]
  223. print("偏移量信息")
  224. # overturn_steering_up_speed overturn_steering_down_speed
  225. # 相机电机 单位mm
  226. if "camera_high_motor_deviation" in data:
  227. # self.doubleSpinBox.setValue(float(data["camera_high_motor_deviation"]))
  228. get_deviation_data["camera_high_motor_deviation"] = float(
  229. data["camera_high_motor_deviation"]
  230. )
  231. # 相机舵机
  232. if "camera_steering_deviation" in data:
  233. # self.doubleSpinBox_2.setValue(float(data["camera_steering_deviation"]))
  234. get_deviation_data["camera_steering_deviation"] = float(
  235. data["camera_steering_deviation"]
  236. )
  237. # 转盘偏移位
  238. if "turntable_steering_deviation" in data:
  239. # self.doubleSpinBox_3.setValue(
  240. # float(data["turntable_steering_deviation"])
  241. # )
  242. get_deviation_data["turntable_steering_deviation"] = float(
  243. data["turntable_steering_deviation"]
  244. )
  245. # 翻板舵机中位
  246. if "overturn_steering_middle" in data:
  247. # self.doubleSpinBox_4.setValue(float(data["overturn_steering_middle"]))
  248. get_deviation_data["overturn_steering_middle"] = float(
  249. data["overturn_steering_middle"]
  250. )
  251. # 翻板舵机高位
  252. if "overturn_steering_high" in data:
  253. # self.doubleSpinBox_5.setValue(float(data["overturn_steering_high"]))
  254. get_deviation_data["overturn_steering_high"] = float(
  255. data["overturn_steering_high"]
  256. )
  257. # 翻板舵机
  258. if "overturn_steering_up_speed" in data:
  259. # self.doubleSpinBox_6.setValue(float(data["overturn_steering_up_speed"]))
  260. get_deviation_data["overturn_steering_up_speed"] = float(
  261. data["overturn_steering_up_speed"]
  262. )
  263. if "overturn_steering_down_speed" in data:
  264. # self.doubleSpinBox_8.setValue(
  265. # float(data["overturn_steering_down_speed"])
  266. # )
  267. get_deviation_data["overturn_steering_down_speed"] = float(
  268. data["overturn_steering_down_speed"]
  269. )
  270. # 初始化位置
  271. # data = {
  272. # "相机电机": self.doubleSpinBox.value(),
  273. # "相机舵机": self.doubleSpinBox_2.value(),
  274. # "转盘舵机": self.doubleSpinBox_3.value(),
  275. # "转盘前后电机": self.doubleSpinBox_7.value(),
  276. # }
  277. # self.is_get_offset.emit(data)
  278. print("获取初始化位置 [get_deviation_data] ", get_deviation_data)
  279. def __del__(self):
  280. self.state = 2
  281. self.t = None