0

私が2つの機能を持っているとしましょう:

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 を2回呼び出すループを持つことにより、正の位置で1回、負の位置で1回)。

その「制御」ループが反復している間、getMotorPositionnd を呼び出して、別の while ループが特定の頻度でデータをプルするようにします。次に、このループにタイマーを設定して、サンプリング周波数を設定できるようにします。

LabView (モーター コントローラーはフックする DLL を提供します) では、これを「並列」while ループで実現します。私は以前に並列と python で何もしたことがなく、どちらが最も説得力のある方向性なのか正確にはわかりません。

4

1 に答える 1

2

あなたが望んでいるように聞こえるものに少し近づくために:

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)
于 2013-01-18T00:58:33.283 に答える