python中用于电机控制和数据采集的并行while循环



假设我有两个函数:

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

在实践中,我希望能够让电机来回振荡(通过有一个循环调用movemotortopposition两次;一次为正数,一次为负数)

当那个"控制"循环迭代时,我想要一个单独的While循环通过调用getMotorPositionnd以某种频率提取数据。然后我将在这个循环中设置一个计时器,让我可以设置采样频率。

在LabView(电机控制器提供一个DLL来挂钩)中,我通过"并行"while循环实现了这一点。我以前从来没有做过并行和python的事情,也不确定哪个是最吸引人的方向。

让你更接近你想要的声音:

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)

相关内容

  • 没有找到相关文章

最新更新