0

我正在用 python 编写一个程序,其中在特定时间移动伺服系统是项目的关键部分。为了实现这一点,我正在使用 pyfirmata 库。

我尝试了两种延迟方法,但似乎都不起作用。当我运行代码时,伺服器第一次转动,但在延迟之后,它没有转动,程序只是停止,而不是将伺服器移动到 0 度然后停止。

这是在 pyfirmata 库中内置 board.pass_time() 的那个:

from pyfirmata import Arduino, util
import time
board = Arduino('COM3')
servo = board.get_pin('d:9:s')

servo.write(180)        #This works and turns the servo
time.sleep(1)
servo.write(0)          #This time the servo does not turn, then the program ends

这是带有 time.sleep() 的那个:

from pyfirmata import Arduino, util
board = Arduino('COM3')
servo = board.get_pin('d:9:s')

servo.write(180)        #This works and turns the servo
board.pass_time(1)
servo.write(0)          #This time the servo does not turn, then the program ends

如果有人可以提供帮助,我将不胜感激。

谢谢你,大卫

4

2 回答 2

0

您的代码是正确的,但您需要再次延迟以将其转回 0 度。所以你的代码应该看起来像这样。

servo.write(180)
time.sleep(2)
servo.write(0)
time.sleep(2)

每次更改伺服电机时都需要延迟才能进行这些更改

于 2020-05-16T07:15:12.470 回答
0

我找到了答案!可能有更好的答案,但如果我写成board.get_pin('d:9:s')这样board.get_pin('d:9:'),我需要用 0 到 5 而不是 0 到 360 的值来控制它,但是延迟有效

于 2019-02-28T16:21:18.213 回答