| 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 | % Signature |
|---|
| 30 | % Author: Aulis Telle (see AUTHORS) |
|---|
| 31 | % Date: 2008/08/15 |
|---|
| 32 | % Copyright: 2007-2008, RWTH Aachen University |
|---|
| 33 | % |
|---|
| 34 | % |
|---|
| 35 | % *********************************************************************************************** |
|---|
| 36 | % * This file is part of the RWTH - Mindstorms NXT Toolbox. * |
|---|
| 37 | % * * |
|---|
| 38 | % * The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify * |
|---|
| 39 | % * it under the terms of the GNU General Public License as published by the Free Software * |
|---|
| 40 | % * Foundation, either version 3 of the License, or (at your option) any later version. * |
|---|
| 41 | % * * |
|---|
| 42 | % * The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful, * |
|---|
| 43 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * |
|---|
| 44 | % * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
|---|
| 45 | % * * |
|---|
| 46 | % * You should have received a copy of the GNU General Public License along with the * |
|---|
| 47 | % * RWTH - Mindstorms NXT Toolbox. If not, see <http: |
|---|
| 48 | % *********************************************************************************************** |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | if ~isa(obj,'NXTmotor') |
|---|
| 52 | error('MATLAB:RWTHMindstormsNXT:InvalidObject',... |
|---|
| 53 | 'No NXTmotor object.'); |
|---|
| 54 | end |
|---|
| 55 | |
|---|
| 56 | if nargout > 0 |
|---|
| 57 | timedOut = 0; |
|---|
| 58 | end |
|---|
| 59 | |
|---|
| 60 | if ~exist('timeout','var') |
|---|
| 61 | timeout = 0; |
|---|
| 62 | end |
|---|
| 63 | |
|---|
| 64 | starttime = now; |
|---|
| 65 | endtime = starttime + datenum(0,0,0,0,0,timeout); |
|---|
| 66 | |
|---|
| 67 | data = read(obj); |
|---|
| 68 | if data.TachoLimit == 0 && ~ strcmpi(data.RunState,'idle') |
|---|
| 69 | warning('RWTHMINDSTORMS:MotorWait',... |
|---|
| 70 | 'Motor running infinitely. Not waiting for this motor!'); |
|---|
| 71 | return; |
|---|
| 72 | end |
|---|
| 73 | |
|---|
| 74 | while ~ strcmpi(data.RunState,'idle') |
|---|
| 75 | if exist('timedOut', 'var') && timeout > 0 && now >= endtime |
|---|
| 76 | timedOut = 1; |
|---|
| 77 | break; |
|---|
| 78 | end |
|---|
| 79 | pause(0.002); |
|---|
| 80 | data = read(obj); |
|---|
| 81 | end |
|---|