WaitFor
Wait for motor(s) to stop (busy waiting)
Contents
Syntax
OBJ.WaitFor TIMEDOUT = OBJ.WaitFor(TIMEOUT)
OBJ.WaitFor(HANDLE) TIMEDOUT = OBJ.WaitFor(TIMEOUT, HANDLE)
Description
OBJ.WaitFor waits for motor specified by OBJ to stop. We do this by reading the motor state from the NXT brick repeatedly until controlled movement is finished. If the motor is set to run infinitely, the method returns immediately and displays a warning.
TIMEDOUT = OBJ.WaitFor(TIMEOUT) does the same as described above but has an additional timeout TIMEOUT (given in seconds). After this time the function stops waiting and returns true. Otherwise it returns false. This functionality is useful to avoid that your robot (and your program) get stuck in case the motor should somehow get stalled (e.g.by driving against a wall).
Use HANDLE (optional) to identify the connection to use for this command.
Note:
If you specify TIMEOUT and the motor is not able to finish its current movement command in time (maybe because the motor is blocked?), waiting will be aborted. The motor is probably still busy in this case, so you have to make sure it is ready to accept commands before using it again (i.e. by calling .Stop()).
Examples:
% If a |SendToNXT| command is immediately followed by a |Stop| % command without using |WaitFor| MATLAB does not wait to send % the stop command until the motor has finished its rotation. % Thus, the motor does not rotate at all, since the stop command % reaches the NXT before the motor starts its rotation due to % its mechanical inertia. motorA = NXTMotor('A', 'Power', 60, 'TachoLimit', 1000); motorA.SendToNXT(); motorA.Stop('off'); % motor A barely moved at all... % To avoid this issue, WaitFor has to be used! motorC = NXTMotor('C', 'Power', -20, 'TachoLimit', 120); motorC.SendToNXT(); motorC.WaitFor(); data = motorC.ReadFromNXT();
% Instantiate motor A and run it m = NXTMotor('A', 'Power', 50, 'TachoLimit', 1000); m.SendToNXT(); % Wait for the motor, try waiting for max. 10 seconds timedOut = WaitFor(m, 10); if timedOut disp('Motor timed out, is it stalled?') m.Stop('off'); % this needed to "unlock" the motor end%if % now we can send new motor commands again...
See also
NXTMotor, ReadFromNXT, SendToNXT, Stop, StopMotor,
Signature
- Author: Aulis Telle, Linus Atorf (see AUTHORS)
- Date: 2009/07/20
- Copyright: 2007-2010, RWTH Aachen University