| 1 | function [ timedOut ] = wait( obj , timeout) |
|---|
| 2 | % WAIT Wait for motor to stop (busy waiting) |
|---|
| 3 | % |
|---|
| 4 | % WAIT(OBJ) waits for motor specified by OBJ to stop. |
|---|
| 5 | % We do this by reading the motor settings from the NXT |
|---|
| 6 | % brick repeatedly until the RunState goes to 'idle'. If the motor |
|---|
| 7 | % is set to run infinitely, return immediately and display a |
|---|
| 8 | % warning. |
|---|
| 9 | % |
|---|
| 10 | % TIMEDOUT = WAIT(OBJ, TIMEOUT) does the same as described |
|---|
| 11 | % above but has an additional timeout TIMEOUT (given in |
|---|
| 12 | % seconds). After this time the function stops waiting and |
|---|
| 13 | % returns |
|---|
| 14 | % |
|---|
| 15 | % Example: |
|---|
| 16 | % % Instantiate Motor and run it |
|---|
| 17 | % m = NXTmotor('A', 'Power', 50, 'MotorOn', 1, ... |
|---|
| 18 | % 'RunState', 'running', 'TachoLimit', 1000); |
|---|
| 19 | % send(m); |
|---|
| 20 | % % Wait for the motor |
|---|
| 21 | % t = wait(m, 0.5); |
|---|
| 22 | % if t ~= 0 |
|---|
| 23 | % disp('Timed out'); |
|---|
| 24 | % end |
|---|
| 25 | % |
|---|
| 26 | % See also NXTmotor/set, NXTmotor/get. |
|---|
| 27 | % |
|---|
| 28 | |
|---|
| 29 | % Author: Aulis Telle, IND, RWTH Aachen |
|---|
| 30 | |
|---|
| 31 | if nargout > 0 |
|---|
| 32 | timedOut = 0; |
|---|
| 33 | end |
|---|
| 34 | |
|---|
| 35 | if ~exist('timeout','var') |
|---|
| 36 | timeout = 0; |
|---|
| 37 | end |
|---|
| 38 | |
|---|
| 39 | starttime = now; |
|---|
| 40 | endtime = starttime + datenum(0,0,0,0,0,timeout); |
|---|
| 41 | |
|---|
| 42 | data = read(obj); |
|---|
| 43 | if data.TachoLimit == 0 && ~ strcmp(data.RunState,'idle') |
|---|
| 44 | warning('RWTHMINDSTORMS:MotorWait',... |
|---|
| 45 | 'Motor running infinitely. Not waiting for this motor!'); |
|---|
| 46 | return; |
|---|
| 47 | end |
|---|
| 48 | |
|---|
| 49 | while ~ strcmp(data.RunState,'idle') |
|---|
| 50 | if exist('timedOut', 'var') && timeout > 0 && now >= endtime |
|---|
| 51 | timedOut = 1; |
|---|
| 52 | break; |
|---|
| 53 | end |
|---|
| 54 | pause(0.002); |
|---|
| 55 | data = read(obj); |
|---|
| 56 | end |
|---|