调试更改+挡板电机添加线程和配置文件
This commit is contained in:
@ -33,6 +33,7 @@ custom_config = {
|
||||
'acc': ACC,
|
||||
}
|
||||
|
||||
|
||||
class ServoInterface:
|
||||
def __init__(self, config=None):
|
||||
self.servo_controller = ServoController(config)
|
||||
@ -46,9 +47,11 @@ class ServoInterface:
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"舵机初始化失败:{str(e)}") from e
|
||||
|
||||
|
||||
# 全局变量
|
||||
_Servo = ServoInterface(custom_config)
|
||||
|
||||
|
||||
def _degree_to_raw(degree: int) -> int:
|
||||
"""
|
||||
角度转原始值
|
||||
@ -61,6 +64,7 @@ def _degree_to_raw(degree: int) -> int:
|
||||
|
||||
return max(0, min(raw_value, 4095))
|
||||
|
||||
|
||||
def _raw_to_degree(raw: int) -> float:
|
||||
"""
|
||||
原始值转角度
|
||||
@ -69,6 +73,7 @@ def _raw_to_degree(raw: int) -> float:
|
||||
"""
|
||||
return round(raw * RAW_TO_DEGREE_RATIO, 2)
|
||||
|
||||
|
||||
def set_servo_speed(value: int):
|
||||
"""
|
||||
设置舵机速度
|
||||
@ -82,6 +87,7 @@ def set_servo_speed(value: int):
|
||||
else:
|
||||
raise ValueError("速度值超出限定范围")
|
||||
|
||||
|
||||
def set_servo_acceleration(value: int):
|
||||
"""
|
||||
设置舵机加速度
|
||||
@ -94,6 +100,7 @@ def set_servo_acceleration(value: int):
|
||||
else:
|
||||
raise ValueError("加速度值超出限定范围")
|
||||
|
||||
|
||||
def set_servo_ori_position(ori_position: int):
|
||||
"""
|
||||
设置舵机原点位
|
||||
@ -103,6 +110,7 @@ def set_servo_ori_position(ori_position: int):
|
||||
POS_START = _degree_to_raw(ori_position)
|
||||
print(f"舵机原点位置已设置为:{ori_position}度(对应原始值:{POS_START})")
|
||||
|
||||
|
||||
def set_servo_rot_position(rot_position: int):
|
||||
"""
|
||||
设置舵机翻转位置
|
||||
@ -112,6 +120,7 @@ def set_servo_rot_position(rot_position: int):
|
||||
POS_END = _degree_to_raw(rot_position)
|
||||
print(f"舵机翻转位置已设置为:{rot_position}度(对应原始值:{POS_END})")
|
||||
|
||||
|
||||
def move_to_rot_position():
|
||||
"""舵机旋转到翻转位置"""
|
||||
try:
|
||||
@ -123,6 +132,7 @@ def move_to_rot_position():
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"舵机移动到翻转位置失败:{str(e)}") from e
|
||||
|
||||
|
||||
def move_to_ori_position():
|
||||
"""舵机旋转到原点"""
|
||||
try:
|
||||
@ -134,6 +144,7 @@ def move_to_ori_position():
|
||||
except Exception as e:
|
||||
raise RuntimeError(f"舵机移动到原点位置失败:{str(e)}") from e
|
||||
|
||||
|
||||
# ----------调试接口----------
|
||||
if __name__ == '__main__':
|
||||
set_servo_speed(1500)
|
||||
@ -143,9 +154,9 @@ if __name__ == '__main__':
|
||||
|
||||
move_to_rot_position() # 旋转180度
|
||||
time.sleep(1)
|
||||
move_to_ori_position() # 旋转0度
|
||||
time.sleep(1)
|
||||
# move_to_ori_position() # 旋转0度
|
||||
# time.sleep(1)
|
||||
|
||||
while(True):
|
||||
while True:
|
||||
time.sleep(1)
|
||||
|
||||
|
||||
@ -11,7 +11,7 @@ import logging
|
||||
|
||||
# -------------参数配置--------------
|
||||
BAUDRATE = 115200 # 舵机的波特率
|
||||
PORT = 'COM4'
|
||||
PORT = '/dev/ttyACM0 '
|
||||
SERVO_IDS = [1, 2, 3, 4, 5] # 舵机们的 ID 号
|
||||
POS_START = 2047
|
||||
POS_END = 0
|
||||
@ -20,14 +20,15 @@ ACC = 0
|
||||
TIME_INTERVAL1 = ((2047-0) / 1500) + 2 # 翻转回来的时间
|
||||
TIME_INTERVAL2 = 10 # 不用翻转运行的时间
|
||||
|
||||
|
||||
class ServoController:
|
||||
def __init__(self, config=None):
|
||||
"""
|
||||
初始化舵机控制器
|
||||
"""
|
||||
self.config = config
|
||||
self.time_interval1 = self.config['time_interval1']
|
||||
self.time_interval2 = self.config['time_interval2']
|
||||
# self.time_interval1 = self.config['time_interval1']
|
||||
# self.time_interval2 = self.config['time_interval2']
|
||||
|
||||
# 初始化串口和舵机处理器
|
||||
self.port_handler = PortHandler(self.config['port'])
|
||||
@ -130,14 +131,14 @@ class ServoController:
|
||||
self.config['speed'],
|
||||
self.config['acc'])
|
||||
print("运动到180度")
|
||||
time.sleep(self.time_interval1)
|
||||
time.sleep(2)
|
||||
|
||||
# 运动到结束位置(0度)
|
||||
self.write_position(self.config['pos_end'],
|
||||
self.config['speed'],
|
||||
self.config['acc'])
|
||||
print("运动到0度")
|
||||
time.sleep(self.time_interval2)
|
||||
time.sleep(2)
|
||||
|
||||
def run(self):
|
||||
"""循环运行:复位位置之后,先转到180度,延时TIME_INTERVAL1一段时间,再转到0度"""
|
||||
|
||||
Reference in New Issue
Block a user