McuDeviationSet.py 12 KB

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