123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import logging
- from tkinter import messagebox
- import PitchControl
- import MotorControl
- class ControlPanelCore:
- def __init__(self):
- self.motor = MotorControl.MotorClient()
- self.initialize_variables()
- def initialize_variables(self):
- self.motor_position = self.motor_velocity = self.motor_mode = self.motor_angle = 0
- self.pitch_motor_angle = round(12, 1)
- self.motor_is_running = self.pitch_motor_is_running = False
- def update_motor_status(self):
- """更新电机状态信息,包括运行状态、速度、位置和模式。"""
- self.motor_is_running = self.is_motor_running()
- self.motor_velocity = self.calculate_motor_velocity()
- self.motor_position = self.motor.get_current_position()
- self.motor_angle = self.get_motor_angle()
- self.motor_mode = self.motor.get_mode()
- def is_motor_running(self):
- """判断电机是否在运行。"""
- return self.motor.get_status() == "0x1"
- def calculate_motor_velocity(self):
- """计算电机的当前速度。"""
- velocity = int(self.motor.get_velocity()) * 360 / self.motor.resolution
- return round(velocity, 2)
- def start_motor(self, velocity, offset_pixels):
- """启动电机"""
- self.motor.set_velocity_mode()
- self.calibrate_velocity(velocity, offset_pixels)
- values = [6, 7, 15] # 状态切换
- for value in values:
- self.motor.write(61443, value)
- self.motor.read(61443, 1)
- self.motor.read(61444, 1)
- def calibrate_velocity(self, velocity, offset_pixels):
- velocity = int(self.motor.resolution * velocity / 60) # 将将速度单位转换为 P/s
- # 精确调整转台的速度值来适应快门速度的变化
- pixels_per_round = 2160 * 100 - offset_pixels*2 # 每100帧实际的像素数
- target_pixels_per_round = 2160 * 100 # 每100帧目标像素数
- partial = pixels_per_round / target_pixels_per_round # 目标像素数与实际像素数的比例
- velocity = int(velocity / partial) # 根据该比例调整速度
- self.motor.set_velocity(velocity) # 用调整后的速度给转台发指令
- def get_motor_angle(self):
- position = self.motor_position if self.motor_position >= 0 else self.motor_position + 2147483648
- angle = float((position % self.motor.resolution) / self.motor.resolution * 360)
- return round(angle, 2)
- def stop_motor(self):
- self.motor.shutdown()
- def motor_home(self):
- # 开始回零
- self.motor.homing()
- # 成功的对话框
- messagebox.showinfo("通知", "回零成功!")
- def pitch_motor_home(self):
- self.pitch_motor.find_zero_position()
- def increase_pitch(self):
- try:
- self.pitch_motor.decrease_angle()
- except ValueError:
- messagebox.showerror(title='错误', message='角度超出调整范围!')
- def decrease_pitch(self):
- try:
- self.pitch_motor.increase_angle()
- except ValueError:
- messagebox.showerror(title='错误', message='角度超出调整范围!')
- def zero_pitch(self):
- self.pitch_motor.move_to_angle(0)
- def move_relative(self, angle):
- """相对移动指定角度"""
- relative_position = int(angle * self.motor.resolution / 360) # 计算目标位置
- self.motor.set_target_position(relative_position) # 设置目标位置
- self.motor.enable_operation() # 启动电机操作
- self.motor.set_relative_position() # 执行相对移动
- def download_segment(self ,position, velocity, acceleration, wait_time,target_value,loop=True,segment_number=0,mode=1,search_mode=1):
- """
- 下载运动段参数到电机控制器。
- 参数:
- - position: 目标位置。
- - velocity: 运动速度。
- - acceleration: 加速度。
- - wait_time: 等待时间。
- - target_value: 目标值。
- - loop: 是否循环执行该段,默认为 True。
- - segment_number: 段号,默认为 0。
- - mode: 模式,默认为 1。
- - search_mode: 搜索模式,默认为 1。
- 返回:
- - None
- """
- # 设置段号和循环模式
- self.motor.set_segment(segment_number,loop)
- # 设置段的参数,包括位置、速度、加速度、等待时间、目标值、模式和搜索模式
- self.motor.set_segment_parameters(segment_number, position, velocity, acceleration, wait_time, target_value, mode, search_mode)
- pass
- def start_segment(self):
- self.motor.set_start_segment()
- pass
- if __name__ == "__main__":
- Core = ControlPanelCore()
- velocity= 50
- acceleration = 200
- wait_time = 200
- target_value = 60
- target_value = int(Core.motor.resolution * target_value / 360)
- loop = True
- Core.download_segment(position=target_value,velocity=velocity,acceleration=acceleration,wait_time=wait_time,target_value=target_value,loop=loop)
- Core.start_segment()
- while True:
- print(Core.motor.read(1030, 1, 'UNS16'))
- # if __name__ == "__main__":
- # Core = ControlPanelCore()
- # Core.stop_motor()
|