假设我有两个功能:
def moveMotorToPosition(position,velocity)
#moves motor to a particular position
#does not terminate until motor is at that position
和
def getMotorPosition()
#retrieves the motor position at any point in time
在实践中,我希望能够让电机来回摆动(通过一个调用moveMotorToPosition两次的循环;一次带有正位置,一个带负位置)
虽然'control'循环是迭代的,但是我希望一个单独的while循环通过调用getMotorPositionnd以某种频率提取数据。然后我会在这个循环上设置一个定时器,让我设置采样频率。
在LabView中(电机控制器提供一个DLL挂钩)我通过'并行'while循环来实现这一点。我之前从未做过平行和python的任何事情,也不确定哪个是最引人注目的方向。
答案 0 :(得分:1)
让你更接近你想要的声音:
import threading
def poll_position(fobj, seconds=0.5):
"""Call once to repeatedly get statistics every N seconds."""
position = getMotorPosition()
# Do something with the position.
# Could store it in a (global) variable or log it to file.
print position
fobj.write(position + '\n')
# Set a timer to run this function again.
t = threading.Timer(seconds, poll_position, args=[fobj, seconds])
t.daemon = True
t.start()
def control_loop(positions, velocity):
"""Repeatedly moves the motor through a list of positions at a given velocity."""
while True:
for position in positions:
moveMotorToPosition(position, velocity)
if __name__ == '__main__':
# Start the position gathering thread.
poll_position()
# Define `position` and `velocity` as it relates to `moveMotorToPosition()`.
control_loop([first_position, second_position], velocity)