root/branches/atorf/RWTHMindstormsNXT-Lejos-Combined/@NXTmotor/wait.m @ 267

Revision 267, 1.5 KB (checked in by atorf, 5 years ago)

Added new toolbox branch "LeJOS-combined" (copied from /branches/telle/RWTHMindstormsNXT-Lejos) to merge COM files for LeJOS-USB-Support…

Line 
1function [ 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
31if nargout > 0
32    timedOut = 0;
33end
34
35if ~exist('timeout','var')
36    timeout = 0;
37end
38
39starttime = now;
40endtime = starttime + datenum(0,0,0,0,0,timeout);
41
42data = read(obj);
43if data.TachoLimit == 0 && ~ strcmp(data.RunState,'idle')
44    warning('RWTHMINDSTORMS:MotorWait',...
45        'Motor running infinitely. Not waiting for this motor!');
46    return;
47end
48
49while ~ 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);
56end
Note: See TracBrowser for help on using the browser.