Let's say I've got two functions:
def moveMotorToPosition(position,velocity)
#moves motor to a particular position
#does not terminate until motor is at that position
and
def getMotorPosition()
#retrieves the motor position at any point in time
In practice what I want to be able to have the motor oscillating back and forth (by having a loop that calls moveMotorToPosition twice; once with a positive position and one with a negative position)
While that 'control' loop is iterating, I want a separate while loop to be pulling data at some frequency by calling getMotorPositionnd. I would then set a timer on this loop that would let me set the sampling frequency.
In LabView (the motor controller supplies a DLL to hook into) I achieve this with 'parallel' while loops. I've never done anything with parallel and python before, and not exactly sure which is the most compelling direction to head.
To point you a little closer to what it sounds like you're wanting:
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)