McuDeviationSet.py 12 KB

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