billet_stacks.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. from utils.s7data import S7data
  2. import time
  3. class Billet_stack:
  4. def __init__(self, name, ccmNo, data_s7: S7data, height_init_offset = 0):
  5. self.name = name
  6. self.ccmNo = ccmNo
  7. self.height_init_offset = height_init_offset
  8. self.height = data_s7.make_point(f"{name}堆垛高度")
  9. self.toptotal = data_s7.make_point(f"{name}堆垛组数")
  10. self.fangx = data_s7.make_point(f"{name}堆垛方向")
  11. self.last_toptotal = self.toptotal.data
  12. self.changed_time = time.time()
  13. self.changed_count = 0
  14. self.jinwei = 0
  15. # self.groups = []
  16. # self.vertical = data_s7.make_point(f"{name}堆垛摆放方式")
  17. # self.crane = data_s7.make_point(f"{name}堆垛行车")
  18. # self.update_sig = data_s7.make_point(f"{name}堆垛更新信号")
  19. # for i in range(12):
  20. # self.groups.append(data_s7.make_point(f"{name}堆垛第{i+1}组"))
  21. self.height.set_convertor(self.layerNum_cal)
  22. self.toptotal.set_convertor(self.do_update)
  23. self.fangx.set_convertor(self.height.allow_update)
  24. def layerNum_cal(self, data):
  25. res = (data + self.height_init_offset) // 170
  26. if self.fangx.data == 1:
  27. if res % 2 == 0:
  28. res += 1
  29. elif self.fangx.data == 2:
  30. if res % 2:
  31. res += 1
  32. return res
  33. def do_update(self, data):
  34. if data != self.last_toptotal:
  35. self.jinwei = 0
  36. self.changed_count = data - self.last_toptotal
  37. self.last_toptotal = data
  38. self.changed_time = time.time()
  39. if self.changed_count > 2:
  40. self.changed_count = -1
  41. self.jinwei = -1
  42. elif self.changed_count < -2:
  43. self.changed_count = 1
  44. self.jinwei = 1
  45. def get_changed(self):
  46. if time.time() - self.changed_time <= 20:
  47. return self.changed_count
  48. return 0
  49. class Stack_manager:
  50. def __init__(self):
  51. self.stacks = {}
  52. def add_stack(self, name, ccmNo, data_s7: S7data, height_init_offset = 0):
  53. self.stacks[name] = Billet_stack(name, ccmNo, data_s7, height_init_offset)
  54. def wait_signal(self, name):
  55. stack = self.stacks[name]
  56. count = 4
  57. while count:
  58. time.sleep(5)
  59. count -= 1
  60. changed = stack.get_changed()
  61. if changed > 0:
  62. return [changed, stack.height.state, stack.toptotal.data]
  63. elif changed < 0:
  64. return [changed, stack.height.state-stack.jinwei, 1 if stack.jinwei else stack.toptotal.data+1]
  65. else:
  66. continue
  67. return [0, 0, 0]