root/trunk/mfiles/GetSwitch.m @ 307

Revision 307, 3.9 KB (checked in by atorf, 5 years ago)

Merged new developments to trunk/mfiles

Line 
1function out = GetSwitch(f_sensorport, varargin)
2% Reads the current value of the NXT switch / touch sensor
3%
4% Syntax
5%   |switch = GetSwitch(port)|
6%
7%   |switch = GetSwitch(port, handle)|
8%
9% Description
10%   |switch = GetSwitch(port)| returns the current switch value |switch| of the NXT switch / touch
11%   sensor. The measurement value |switch| represents the pressing mode of the switch / touch
12%   sensor. |true| is returned if the switch / touch sensor is being pressed and |false| if it is
13%   being released. The given |port| number specifies the connection port. The value |port| can be
14%   addressed by the symbolic constants |SENSOR_1|, |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to
15%   the labeling on the NXT Brick.
16%
17%   The last optional argument can be a valid NXT handle. If none is
18%   specified, the default handle will be used (call |COM_SetDefaultNXT| to
19%   set one).
20%
21%   For more complex settings the function |NXT_GetInputValues| can be used.
22%
23%
24% Example
25%+   OpenSwitch(SENSOR_4);
26%+   switchState = GetSwitch(SENSOR_4);
27%+   CloseSensor(SENSOR_4);
28%
29% See also: NXT_GetInputValues, OpenSwitch, CloseSensor, SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4
30%
31% Signature
32%   Author: Linus Atorf, Alexander Behrens (see AUTHORS)
33%   Date: 2007/10/15
34%   Copyright: 2007-2008, RWTH Aachen University
35%
36;
37%
38% ***********************************************************************************************
39% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    *
40% *                                                                                             *
41% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  *
42% *  it under the terms of the GNU General Public License as published by the Free Software     *
43% *  Foundation, either version 3 of the License, or (at your option) any later version.        *
44% *                                                                                             *
45% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       *
46% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *
47% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
48% *                                                                                             *
49% *  You should have received a copy of the GNU General Public License along with the           *
50% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 *
51% ***********************************************************************************************
52
53% We're using RAWMODE here, because BOOLEAN mode would be nice, but isn't
54% really necessary. We can use the .NormalizedADVal from GetInputValues, as
55% we have this variable ALLWAYS. If we were to use BOOLEAN mode, and then
56% use the .ScaledVal (what we had to do in this case because that is where
57% the BOOLEAN mode val is effectively stored), we'd take away ourselves the
58% possibility to use PERIODCNTMODE later on to detect "taps" or something
59% else.
60% Also this function would stop working if someone used NXT_SetInputMode
61% with something other then BOOLEANMODE. And we certainly want to save
62% the possibility to do so later on. That's why we take the
63% .NormalizedADVal and compare it against the empirically found
64% threshold...
65%
66
67%% check if handle is given; if not use default one
68    if nargin > 1
69        handle = varargin{1};
70    else
71        handle = COM_GetDefaultNXT;
72    end%if
73
74
75%% Call NXT_GetInputValues function
76    in = NXT_GetInputValues(f_sensorport, handle);
77   
78%% Do threshold decision
79    % 511 should be ok, being right in the middle between both "peaks"
80    if in.NormalizedADVal > 511
81        % note how this val is "inverted"...
82        out = false;
83    else
84        out = true;
85    end%if
86
87end   
88   
Note: See TracBrowser for help on using the browser.