billet_trace_pusher.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. import logging
  2. from utils.statepoint import *
  3. from utils.s7data import *
  4. from models.data_sender import *
  5. class Trace_pusher:
  6. def __init__(self, data_s7: S7data, logger: logging.Logger, sender: Sender, strand_position: list, hostsend_flag=False, hostmove_flag=False):
  7. self.data_s7 = data_s7
  8. self.logger = logger
  9. self.sender = sender
  10. self.strands_cutting = [[], [], [], [], [], [], [], []]
  11. self.strands_buffer = [[], [], [], [], [], [], [], []]
  12. self.locks = [threading.Lock() for i in range(8)]
  13. self.strand_position = strand_position
  14. self.hostsend_flag = hostsend_flag
  15. self.hostmove_flag = hostmove_flag
  16. self.heat_filter_index = 1
  17. self.heat_filter = ["00000000"] + ['' for i in range(9)]
  18. self.old_heatNo = "00000000"
  19. self.current_heatNo = "00000000"
  20. self.old_heatData = {}
  21. self.current_heatData = {}
  22. self.total = 0
  23. self.strand = [0, 0, 0, 0, 0, 0, 0, 0]
  24. self.count_lock = threading.Lock()
  25. self.sizing_count_heatNo = "00000000"
  26. self.sizing_count = {}
  27. self.pusher_left_list = []
  28. self.pusher_right_list = []
  29. self.bed_left = [[], [], []]
  30. self.bed_right = [[], [], []]
  31. self.pusher_left = data_s7.make_point('推钢机激光')
  32. self.pusher_right = data_s7.make_point('推钢机激光')
  33. self.pusher_left.hmd_add(0)
  34. self.pusher_right.hmd_add(0)
  35. self.pusher_left.set_convertor(lambda data: data < min(self.strand_position))
  36. self.pusher_right.set_convertor(lambda data: data > max(self.strand_position))
  37. self.pusher_left.set_excite_action(lambda: self.arrive_cooling_bed('left'))
  38. self.pusher_right.set_excite_action(lambda: self.arrive_cooling_bed('right'))
  39. self.billet_out = [[], [], [], [], [], [], [], []]
  40. self.length_cutting = []
  41. for i in range(8):
  42. self.length_cutting.append(self.data_s7.make_point(f'L{i+1}定尺'))
  43. self.drawing_speed = []
  44. for i in range(8):
  45. self.drawing_speed.append(self.data_s7.make_point(f'L{i+1}拉速'))
  46. # 5号机
  47. if self.hostsend_flag:
  48. # 拉速积分得到长度
  49. self.integration_total = [0] * 8
  50. self.integration_lock = threading.Lock()
  51. self.billet_out_sig = [self.data_s7.make_point(f"L{i+1}拉速", Integration_speed_mpmin) for i in range(8)]
  52. self.billet_position = [
  53. self.data_s7.make_point('L1切割信号[1]'),
  54. self.data_s7.make_point('L2切割信号[1]'),
  55. self.data_s7.make_point('L3切割信号[1]'),
  56. self.data_s7.make_point('L4切割信号[1]'),
  57. self.data_s7.make_point('L5切割信号[1]'),
  58. self.data_s7.make_point('L6切割信号[1]'),
  59. self.data_s7.make_point('L7切割信号[1]'),
  60. self.data_s7.make_point('L8切割信号[1]')
  61. ]
  62. # 6号机
  63. else:
  64. self.billet_position = [
  65. self.data_s7.make_point('L1坯头位置'),
  66. self.data_s7.make_point('L2坯头位置'),
  67. self.data_s7.make_point('L3坯头位置'),
  68. self.data_s7.make_point('L4坯头位置'),
  69. self.data_s7.make_point('L5坯头位置'),
  70. self.data_s7.make_point('L6坯头位置'),
  71. self.data_s7.make_point('L7坯头位置'),
  72. self.data_s7.make_point('L8坯头位置')
  73. ]
  74. self.barrier = [
  75. self.data_s7.make_point('L1挡板'),
  76. self.data_s7.make_point('L2挡板'),
  77. self.data_s7.make_point('L3挡板'),
  78. self.data_s7.make_point('L4挡板'),
  79. self.data_s7.make_point('L5挡板'),
  80. self.data_s7.make_point('L6挡板'),
  81. self.data_s7.make_point('L7挡板'),
  82. self.data_s7.make_point('L8挡板')
  83. ]
  84. self.barrier_checker = [False, False, False, False, False, False, False, False]
  85. if self.hostsend_flag:
  86. self.hostsend_barrier = [
  87. self.data_s7.make_point('热送挡板[0]'),
  88. self.data_s7.make_point('热送挡板[1]'),
  89. self.data_s7.make_point('热送挡板[2]'),
  90. self.data_s7.make_point('热送挡板[3]'),
  91. self.data_s7.make_point('热送挡板[4]'),
  92. self.data_s7.make_point('热送挡板[5]'),
  93. self.data_s7.make_point('热送挡板[6]'),
  94. self.data_s7.make_point('热送挡板[7]')
  95. ]
  96. if self.hostmove_flag:
  97. self.hostmove_sig = data_s7.make_point('推钢机激光')
  98. self.hostmove_sig.hmd_add(0)
  99. self.hostmove_sig.set_convertor(lambda data: data > 19000)
  100. self.hostmove_sig.set_excite_action(lambda: self.billet_to_stack("步进冷床", self.get_billet('right')))
  101. for i in range(8):
  102. self.billet_position[i].allow_update(False)
  103. self.billet_position[i].set_state(False)
  104. # 6号机
  105. if not self.hostsend_flag:
  106. # 正常情况进入跟踪
  107. self.billet_position[i].set_convertor(lambda data: 12000 < data < 20000)
  108. self.billet_position[i].set_excite_action(lambda i=i: self.billet_out_action(i))
  109. self.billet_position[i].set_reset_action(lambda i=i: self.billet_in_buffer_action(i))
  110. # 5号机
  111. else:
  112. # 拉速补充钢坯
  113. self.billet_out_sig[i].set_convertor(lambda data, i=i: round(data*1000) >= self.integration_total[i] + self.length_cutting[i].data)
  114. self.billet_out_sig[i].set_excite_action(lambda i=i: self.billet_out_action(i))
  115. # 正常情况进入跟踪
  116. self.billet_position[i].set_excite_action(lambda i=i: self.billet_in_buffer_action(i))
  117. self.billet_position[i].allow_update()
  118. # 挡板抬起
  119. self.barrier[i].allow_update(False)
  120. self.barrier[i].set_state(False)
  121. self.barrier[i].set_convertor(lambda data: not bool(data))
  122. self.barrier[i].set_excite_action(lambda i=i: self.barrier_up_action(i))
  123. self.barrier[i].set_reset_action(lambda i=i: self.barrier_down_action(i))
  124. self.barrier[i].allow_update()
  125. # 5号机直轧挡板抬起
  126. if self.hostsend_flag:
  127. self.hostsend_barrier[i].allow_update(False)
  128. self.hostsend_barrier[i].set_state(False)
  129. self.hostsend_barrier[i].set_convertor(lambda data: not bool(data))
  130. self.hostsend_barrier[i].set_excite_action(lambda i=i: self.hostsend_barrier_up_action(i))
  131. self.hostsend_barrier[i].set_reset_action(lambda i=i: self.logger.debug(f"{i+1}流热送挡板关闭"))
  132. self.hostsend_barrier[i].allow_update()
  133. def billet_out_action(self, i):
  134. billetNo = self.current_heatNo + '0' + str(i+1) + '99'
  135. sizing = self.length_cutting[i].data
  136. speed = self.drawing_speed[i].data
  137. # 积分的钢坯长度的累计
  138. with self.integration_lock:
  139. if self.strands_cutting[i]:
  140. return None
  141. self.integration_total[i] += sizing
  142. # [坯号, 炉次信息, 定尺, 拉速, 开切时间, 停切时间]
  143. self.billet_out[i] = [billetNo, self.current_heatData, sizing, speed, time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()), '']
  144. # 等待20s,若无开切信号,即为漏坯
  145. start_time = time.time()
  146. while time.time() - start_time < 20:
  147. if self.strands_cutting[i]:
  148. return None
  149. time.sleep(0.5)
  150. self.billet_in_buffer_action(i)
  151. def billet_in_buffer_action(self, i):
  152. with self.locks[i]:
  153. if self.strands_cutting[i]:
  154. time.sleep(15)
  155. self.logger.info(f"[TRACE]{i+1}流新增钢坯存储")
  156. self.strands_buffer[i] = self.strands_cutting[i]
  157. self.strands_cutting[i] = []
  158. elif self.billet_out[i]:
  159. self.logger.info(f"[TRACE]{i+1}流无已经开切的钢坯,算法补入")
  160. self.strands_buffer[i] = self.billet_out[i]
  161. else:
  162. self.logger.info(f"[TRACE]{i+1}流无已经开切的钢坯,但未检测到可补入的钢坯信息")
  163. self.billet_out[i] = []
  164. if self.barrier[i].state and self.barrier_checker[i] == False:
  165. self.barrier_up_action(i)
  166. def barrier_up_action(self, i):
  167. time.sleep(1)
  168. with self.locks[i]:
  169. if self.strands_buffer[i]:
  170. self.barrier_checker[i] = True
  171. time.sleep(12)
  172. billetData = self.strands_buffer[i]
  173. self.strands_buffer[i] = []
  174. if self.strand_position[i] <= self.pusher_left.data:
  175. self.logger.info(f"[TRACE]{i+1}流钢坯通过挡板进入推钢区域,在推钢机左侧")
  176. self.pusher_left_list.append(billetData)
  177. else:
  178. self.logger.info(f"[TRACE]{i+1}流钢坯通过挡板进入推钢区域,在推钢机右侧")
  179. self.pusher_right_list.append(billetData)
  180. if self.hostsend_flag and self.hostsend_barrier[i].state:
  181. self.hostsend_barrier_up_action(i)
  182. def barrier_down_action(self, i):
  183. if self.barrier_checker[i]:
  184. self.logger.debug(f"[TRACE]{i+1}流挡板关闭")
  185. else:
  186. self.logger.error(f"[TRACE]{i+1}流挡板关闭,期间无钢坯流出")
  187. self.barrier_checker[i] = False
  188. def hostsend_barrier_up_action(self, i):
  189. with self.count_lock:
  190. gp_tmp = []
  191. if self.strand_position[i] <= self.pusher_left.data:
  192. index = -1
  193. for j in range(len(self.pusher_left_list)-1, -1, -1):
  194. if self.pusher_left_list[j][0][-3] == str(i+1):
  195. index = j
  196. break
  197. if index == -1:
  198. pass
  199. #self.logger.warning(f"[TRACE]推钢机左侧未找到{i+1}流的热送钢坯")
  200. else:
  201. gp_tmp = self.pusher_left_list[index]
  202. self.pusher_left_list = self.pusher_left_list[:index] + self.pusher_left_list[index+1:]
  203. else:
  204. index = -1
  205. for j in range(len(self.pusher_right_list)-1, -1, -1):
  206. if self.pusher_right_list[j][0][-3] == str(i+1):
  207. index = j
  208. break
  209. if index == -1:
  210. pass
  211. #self.logger.warning(f"[TRACE]推钢机右侧未找到{i+1}流的热送钢坯")
  212. else:
  213. gp_tmp = self.pusher_right_list[index]
  214. self.pusher_right_list = self.pusher_right_list[:index] + self.pusher_right_list[index+1:]
  215. if gp_tmp:
  216. self.logger.info(f"[TRACE]{i+1}流钢坯热送")
  217. if gp_tmp[0][:8] not in self.heat_filter:
  218. self.change_heat(gp_tmp)
  219. if not gp_tmp[0].startswith('0'):
  220. self.hostsend(gp_tmp)
  221. def change_heat(self, data):
  222. # 换炉代码
  223. self.old_heatNo = self.current_heatNo
  224. self.old_heatData = self.current_heatData
  225. self.current_heatNo = data[0][:8]
  226. self.current_heatData = data[1]
  227. self.heat_filter[self.heat_filter_index] = self.current_heatNo
  228. self.heat_filter_index = (self.heat_filter_index + 1) % 10
  229. # 上一炉终止信号在这里发
  230. self.total = 0
  231. self.strand = [0, 0, 0, 0, 0, 0, 0, 0]
  232. def arrive_cooling_bed(self, direc):
  233. with self.count_lock:
  234. if direc == 'left':
  235. self.logger.debug(f"左侧冷床上推入{len(self.pusher_left_list)}根钢坯")
  236. tmp = self.pusher_left_list
  237. self.pusher_left_list = []
  238. elif direc == 'right':
  239. self.logger.debug(f"右侧冷床上推入{len(self.pusher_right_list)}根钢坯")
  240. tmp = self.pusher_right_list
  241. self.pusher_right_list = []
  242. for i in tmp:
  243. if not (i[0].startswith(self.current_heatNo) or i[0].startswith(self.old_heatNo)):
  244. self.change_heat(i)
  245. break
  246. self.billet_to_bed(tmp, direc)
  247. def billet_to_bed(self, billets, direc):
  248. if direc == 'left':
  249. if len(billets):
  250. self.billet_to_bed_impl(billets, self.bed_left)
  251. self.logger.debug(f"左侧冷床目前情况:{len(self.bed_left[0])}根|{len(self.bed_left[1])}根|{len(self.bed_left[2])}根")
  252. elif direc == 'right':
  253. if len(billets):
  254. self.billet_to_bed_impl(billets, self.bed_right)
  255. self.logger.debug(f"右侧冷床目前情况:{len(self.bed_right[2])}根|{len(self.bed_right[1])}根|{len(self.bed_right[0])}根")
  256. def billet_to_bed_impl(self, billets: list, dst: list):
  257. i = 0
  258. count = 0
  259. while i < len(dst) and dst[i]:
  260. count += 1
  261. i += 1
  262. if count == 0:
  263. dst[0].extend(billets)
  264. elif count == 3 and (len(billets) >= 4 or len(dst[count-1]) >= 4 or len(dst[count-1]) + len(billets) > 4):
  265. self.logger.error(f"组坯异常!")
  266. dst.remove(dst[0])
  267. dst.append(billets)
  268. count -= 1
  269. elif len(billets) >= 4 or len(dst[count-1]) >= 4 or len(dst[count-1]) + len(billets) > 4:
  270. dst[count].extend(billets)
  271. else:
  272. dst[count-1].extend(billets)
  273. count -= 1
  274. if len(dst[count]) >= 4:
  275. self.billet_union(dst[count])
  276. def billet_union(self, billets):
  277. if self.sizing_count_heatNo != self.current_heatNo:
  278. self.sizing_count_heatNo = self.current_heatNo
  279. self.sizing_count = {}
  280. if billets[0][2] not in self.sizing_count:
  281. self.sizing_count[billets[0][2]] = 0
  282. self.sizing_count[billets[0][2]] += 1
  283. billet_unionNo = self.current_heatNo + '{:0>5}'.format(int(billets[0][2])) + '{:0>2}'.format(self.sizing_count[billets[0][2]])
  284. billetsNo = []
  285. ccmNo = self.current_heatData['ccmNo'] if self.current_heatData else '0'
  286. for i in billets:
  287. strandNo = i[0][9]
  288. self.total += 1
  289. self.strand[int(strandNo)-1] += 1
  290. billetNo = self.current_heatNo + ccmNo + strandNo + '{:0>2}'.format(self.strand[int(strandNo)-1])
  291. billetsNo.append(billetNo)
  292. i[0] = billetNo
  293. i[1] = self.current_heatData
  294. if self.current_heatData:
  295. self.sender.billet_upload(self.current_heatData, billetNo, self.total, i[2], i[3], i[4], i[5], billet_unionNo)
  296. if self.current_heatData:
  297. self.sender.billet_union(self.current_heatData, billet_unionNo, billetsNo, int(billets[0][2]))
  298. self.logger.info(f"{self.current_heatNo}炉组号{billet_unionNo}钢坯{len(billets)}根:\n {'、'.join(billetsNo)}")
  299. def data_from_casting(self, i, data, extend=False):
  300. with self.locks[i]:
  301. if extend:
  302. if self.strands_cutting[i] and self.strands_cutting[i][0] == data[0]:
  303. self.logger.info(f"{i+1}流补充了钢坯停切时间")
  304. self.strands_cutting[i] = data
  305. elif self.strands_buffer[i] and self.strands_buffer[i][0] == data[0]:
  306. self.logger.info(f"{i+1}流补充了钢坯停切时间")
  307. self.strands_buffer[i] = data
  308. else:
  309. self.logger.warning(f"{i+1}流对已经离开的钢坯补充停切时间,无效")
  310. else:
  311. with self.integration_lock:
  312. self.billet_out_sig[i].data = 0
  313. self.integration_total[i] = 0
  314. if self.strands_cutting[i]:
  315. self.logger.warning(f"{i+1}流有钢坯开切冲突")
  316. self.strands_cutting[i] = data
  317. self.strands_cutting[i].append('')
  318. else:
  319. self.logger.info(f"{i+1}流钢坯开切")
  320. self.strands_cutting[i] = data
  321. self.strands_cutting[i].append('')
  322. def hostsend(self, i):
  323. ccmNo = self.current_heatData['ccmNo']
  324. strandNo = i[0][9]
  325. self.total += 1
  326. self.strand[int(strandNo)-1] += 1
  327. billetNo = self.current_heatNo + ccmNo + strandNo + '{:0>2}'.format(self.strand[int(strandNo)-1])
  328. i[0] = billetNo
  329. i[1] = self.current_heatData
  330. self.sender.billet_upload(self.current_heatData, billetNo, self.total, i[2], i[3], i[4], i[5], '')
  331. self.logger.info(f"{self.current_heatNo}炉钢坯热送:{billetNo}")
  332. self.sender.host_send(ccmNo, billetNo, "棒一")
  333. def clean_status(self):
  334. self.logger.debug(f"[TRACE]小冷床状态清空")
  335. with self.count_lock:
  336. self.pusher_left_list = []
  337. self.pusher_right_list = []
  338. self.bed_left = [[], [], []]
  339. self.bed_right = [[], [], []]
  340. def get_billet(self, direc):
  341. with self.count_lock:
  342. if direc == "left":
  343. return self.get_billet_action(self.bed_left)
  344. if direc == "right":
  345. return self.get_billet_action(self.bed_right)
  346. def get_billet_action(self, src: list):
  347. for i in range(len(src)-1, -1, -1):
  348. if len(src[i]) >= 4:
  349. tmp = [j[0] for j in src.pop(i)]
  350. src.append([])
  351. if tmp[0].startswith('0'):
  352. return []
  353. return tmp
  354. return []
  355. def billet_to_stack(self, stackNo, billets):
  356. self.logger.info(f"有钢坯放入{stackNo}堆垛")
  357. # [坯号, 炉次信息, 定尺, 拉速, 开切时间, 停切时间]