ControlPanelCore.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import logging
  2. from tkinter import messagebox
  3. import PitchControl
  4. import MotorControl
  5. class ControlPanelCore:
  6. def __init__(self):
  7. self.motor = MotorControl.MotorClient()
  8. self.initialize_variables()
  9. def initialize_variables(self):
  10. self.motor_position = self.motor_velocity = self.motor_mode = self.motor_angle = 0
  11. self.pitch_motor_angle = round(12, 1)
  12. self.motor_is_running = self.pitch_motor_is_running = False
  13. def update_motor_status(self):
  14. """更新电机状态信息,包括运行状态、速度、位置和模式。"""
  15. self.motor_is_running = self.is_motor_running()
  16. self.motor_velocity = self.calculate_motor_velocity()
  17. self.motor_position = self.motor.get_current_position()
  18. self.motor_angle = self.get_motor_angle()
  19. self.motor_mode = self.motor.get_mode()
  20. def is_motor_running(self):
  21. """判断电机是否在运行。"""
  22. return self.motor.get_status() == "0x1"
  23. def calculate_motor_velocity(self):
  24. """计算电机的当前速度。"""
  25. velocity = int(self.motor.get_velocity()) * 360 / self.motor.resolution
  26. return round(velocity, 2)
  27. def start_motor(self, velocity, offset_pixels):
  28. """启动电机"""
  29. self.motor.set_velocity_mode()
  30. self.calibrate_velocity(velocity, offset_pixels)
  31. values = [6, 7, 15] # 状态切换
  32. for value in values:
  33. self.motor.write(61443, value)
  34. self.motor.read(61443, 1)
  35. self.motor.read(61444, 1)
  36. def calibrate_velocity(self, velocity, offset_pixels):
  37. velocity = int(self.motor.resolution * velocity / 60) # 将将速度单位转换为 P/s
  38. # 精确调整转台的速度值来适应快门速度的变化
  39. pixels_per_round = 2160 * 100 - offset_pixels*2 # 每100帧实际的像素数
  40. target_pixels_per_round = 2160 * 100 # 每100帧目标像素数
  41. partial = pixels_per_round / target_pixels_per_round # 目标像素数与实际像素数的比例
  42. velocity = int(velocity / partial) # 根据该比例调整速度
  43. self.motor.set_velocity(velocity) # 用调整后的速度给转台发指令
  44. def get_motor_angle(self):
  45. position = self.motor_position if self.motor_position >= 0 else self.motor_position + 2147483648
  46. angle = float((position % self.motor.resolution) / self.motor.resolution * 360)
  47. return round(angle, 2)
  48. def stop_motor(self):
  49. self.motor.shutdown()
  50. def motor_home(self):
  51. # 开始回零
  52. self.motor.homing()
  53. # 成功的对话框
  54. messagebox.showinfo("通知", "回零成功!")
  55. def pitch_motor_home(self):
  56. self.pitch_motor.find_zero_position()
  57. def increase_pitch(self):
  58. try:
  59. self.pitch_motor.decrease_angle()
  60. except ValueError:
  61. messagebox.showerror(title='错误', message='角度超出调整范围!')
  62. def decrease_pitch(self):
  63. try:
  64. self.pitch_motor.increase_angle()
  65. except ValueError:
  66. messagebox.showerror(title='错误', message='角度超出调整范围!')
  67. def zero_pitch(self):
  68. self.pitch_motor.move_to_angle(0)
  69. def move_relative(self, angle):
  70. """相对移动指定角度"""
  71. relative_position = int(angle * self.motor.resolution / 360) # 计算目标位置
  72. self.motor.set_target_position(relative_position) # 设置目标位置
  73. self.motor.enable_operation() # 启动电机操作
  74. self.motor.set_relative_position() # 执行相对移动
  75. def download_segment(self ,position, velocity, acceleration, wait_time,target_value,loop=True,segment_number=0,mode=1,search_mode=1):
  76. """
  77. 下载运动段参数到电机控制器。
  78. 参数:
  79. - position: 目标位置。
  80. - velocity: 运动速度。
  81. - acceleration: 加速度。
  82. - wait_time: 等待时间。
  83. - target_value: 目标值。
  84. - loop: 是否循环执行该段,默认为 True。
  85. - segment_number: 段号,默认为 0。
  86. - mode: 模式,默认为 1。
  87. - search_mode: 搜索模式,默认为 1。
  88. 返回:
  89. - None
  90. """
  91. # 设置段号和循环模式
  92. self.motor.set_segment(segment_number,loop)
  93. # 设置段的参数,包括位置、速度、加速度、等待时间、目标值、模式和搜索模式
  94. self.motor.set_segment_parameters(segment_number, position, velocity, acceleration, wait_time, target_value, mode, search_mode)
  95. pass
  96. def start_segment(self):
  97. self.motor.set_start_segment()
  98. pass
  99. if __name__ == "__main__":
  100. Core = ControlPanelCore()
  101. velocity= 50
  102. acceleration = 200
  103. wait_time = 200
  104. target_value = 60
  105. target_value = int(Core.motor.resolution * target_value / 360)
  106. loop = True
  107. Core.download_segment(position=target_value,velocity=velocity,acceleration=acceleration,wait_time=wait_time,target_value=target_value,loop=loop)
  108. Core.start_segment()
  109. while True:
  110. print(Core.motor.read(1030, 1, 'UNS16'))
  111. # if __name__ == "__main__":
  112. # Core = ControlPanelCore()
  113. # Core.stop_motor()