伺服器产生额外的循环

时间:2018-07-03 13:30:34

标签: python-3.x arduino

我有两个Python脚本可以更改两个舵机的位置。第二个脚本可以按需运行,但是当我运行第一个脚本时,第一个伺服器会产生额外的循环或旋转,这是一个问题。你知道为什么会这样吗? (这只是添加文本以通过SO字符长度检查)

第一个脚本:

from pyfirmata import ArduinoMega, SERVO
from time import sleep

port = '/dev/ttyACM0'
board = ArduinoMega(port)
sleep(5)

board.digital[13].mode = SERVO
board.digital[12].mode = SERVO

def set_first_servo(angle):
    board.digital[13].write(angle)
    sleep(0.015)

def set_second_servo(angle):
    board.digital[12].write(angle)
    sleep(0.015)

a, b = True, True
while a:
    for position in range(135, 0, -1):
        set_first_servo(position)

    a = False

while b:
    for position in range(0, 135):
        set_second_servo(position)

    b = False

board.exit()

第二个脚本(这个脚本没问题):

from pyfirmata import ArduinoMega, SERVO
from time import sleep

port = '/dev/ttyACM0'
board = ArduinoMega(port)
sleep(5)

board.digital[13].mode = SERVO
board.digital[12].mode = SERVO

def set_first_servo(angle):
    board.digital[13].write(angle)
    sleep(0.015)

def set_second_servo(angle):
    board.digital[12].write(angle)
    sleep(0.015)

c, d = True, True

while c:
    for position in range(0, b):
        set_first_servo(position)
    c = False

while d:
    for position in range(e, 0, -1):
        set_second_servo(position)

    d = False

board.exit()

1 个答案:

答案 0 :(得分:0)

也许您想翻转(135,0,-1)和(0,135)。

from pyfirmata import ArduinoMega, SERVO
from time import sleep

port = '/dev/ttyACM0'
board = ArduinoMega(port)
sleep(5)

board.digital[13].mode = SERVO
board.digital[12].mode = SERVO

def set_first_servo(angle):
    board.digital[13].write(angle)
    sleep(0.015)

def set_second_servo(angle):
    board.digital[12].write(angle)
    sleep(0.015)

a, b = True, True
while a:
    for position in range(0, 135):
        set_first_servo(position)

    a = False

while b:
    for position in range(135, 0, -1):
        set_second_servo(position)

    b = False

board.exit()

假设您的第二本作品是我看到的唯一区别。