如何让舵机慢慢到达想要的角度?
How to make servo reach the desired angle slowly?
我找到了一个让舵机一步一步达到想要的角度的解决方案,但这取决于舵机当前的角度。
这是代码:
if servo_current angle > servo_desired_angle :
while(x=true) :
servo_current_angle -=1
duty_cycle = (((12.5-2.5)/(180-0) * servo_current_angle) +2.5
p.ChangeDutyCycle (duty_cycle)
time.sleep(0.01)
if servo_current angle = servo_desired_angle :
x = false
elif servo_current_angle < servo_desired_angle :
while(x=true) :
servo_current_angle +=1
duty_cycle = (((12.5-2.5)/(180-0) * servo_current_angle) +2.5
p.ChangeDutyCycle (duty_cycle)
time.sleep(0.01)
if servo_current angle = servo_desired_angle :
x = false
问题是如何知道 servo_current_angle?
What about tracking the current servo position during runtime and
update it along the way?
You can use the PWM module in
RPi.GPIO and in the beginning move
the servo into its initial position to start from.
伺服电机的位置由一个脉冲的长度决定。伺服系统预计至少每 20 毫秒接收一次脉冲。如果该脉冲高电平持续 1 毫秒,伺服角度将为零;如果是1.5毫秒,它会在它的中心位置;如果是2毫秒,就是180度。
示例程序将PWM频率设置为100Hz,每10毫秒向舵机发送一个脉冲。角度转换为 0 到 100 之间的占空比。这实际上产生的脉冲比预期的最小值短 1 毫秒,最大值长于 2 毫秒。
来源here
对于典型的开环爱好伺服(没有反馈编码器的伺服),根本无法知道位置。您所能做的就是如上所述提供一个脉冲,并相信伺服的负载不会阻止它移动到指令位置。其他选项:
- 您可以添加自己的编码器,例如数字编码器或光学编码器。
- 您可以改用像 Dynamixel 系列一样内置编码器的闭环伺服系统(贵得多)。
为了解决您的逐步移动更平滑的问题,您需要命令它在当前位置和所需位置之间的中间位置。将其视为采取 20(在下面的示例中)小步骤而不是一大步骤。为此,请使用循环来更新占空比。这是一个例子:
current duty_cycle = 7.5
desired_duty_cycle = 5.0
steps = 20.0
duty_cycle_delta = (desired_duty_cycle - current_duty_cycle) / steps
while (current_duty_cycle != desired_duty_cycle):
if (current_duty_cycle > desired_duty_cycle):
duty_cycle_delta = -duty_cycle_delta
current_duty_cycle = current_duty_cycle + duty_cycle_delta
p.ChangeDutyCycle (current_duty_cycle)
这将通过命令中间位置接近所需位置,直到到达所需位置。改变 steps
变量以改变它接近所需位置的速度。
我找到了一个让舵机一步一步达到想要的角度的解决方案,但这取决于舵机当前的角度。 这是代码:
if servo_current angle > servo_desired_angle :
while(x=true) :
servo_current_angle -=1
duty_cycle = (((12.5-2.5)/(180-0) * servo_current_angle) +2.5
p.ChangeDutyCycle (duty_cycle)
time.sleep(0.01)
if servo_current angle = servo_desired_angle :
x = false
elif servo_current_angle < servo_desired_angle :
while(x=true) :
servo_current_angle +=1
duty_cycle = (((12.5-2.5)/(180-0) * servo_current_angle) +2.5
p.ChangeDutyCycle (duty_cycle)
time.sleep(0.01)
if servo_current angle = servo_desired_angle :
x = false
问题是如何知道 servo_current_angle?
What about tracking the current servo position during runtime and update it along the way?
You can use the PWM module in RPi.GPIO and in the beginning move the servo into its initial position to start from.
伺服电机的位置由一个脉冲的长度决定。伺服系统预计至少每 20 毫秒接收一次脉冲。如果该脉冲高电平持续 1 毫秒,伺服角度将为零;如果是1.5毫秒,它会在它的中心位置;如果是2毫秒,就是180度。
示例程序将PWM频率设置为100Hz,每10毫秒向舵机发送一个脉冲。角度转换为 0 到 100 之间的占空比。这实际上产生的脉冲比预期的最小值短 1 毫秒,最大值长于 2 毫秒。
来源here
对于典型的开环爱好伺服(没有反馈编码器的伺服),根本无法知道位置。您所能做的就是如上所述提供一个脉冲,并相信伺服的负载不会阻止它移动到指令位置。其他选项:
- 您可以添加自己的编码器,例如数字编码器或光学编码器。
- 您可以改用像 Dynamixel 系列一样内置编码器的闭环伺服系统(贵得多)。
为了解决您的逐步移动更平滑的问题,您需要命令它在当前位置和所需位置之间的中间位置。将其视为采取 20(在下面的示例中)小步骤而不是一大步骤。为此,请使用循环来更新占空比。这是一个例子:
current duty_cycle = 7.5
desired_duty_cycle = 5.0
steps = 20.0
duty_cycle_delta = (desired_duty_cycle - current_duty_cycle) / steps
while (current_duty_cycle != desired_duty_cycle):
if (current_duty_cycle > desired_duty_cycle):
duty_cycle_delta = -duty_cycle_delta
current_duty_cycle = current_duty_cycle + duty_cycle_delta
p.ChangeDutyCycle (current_duty_cycle)
这将通过命令中间位置接近所需位置,直到到达所需位置。改变 steps
变量以改变它接近所需位置的速度。