| 1 | function 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: 2008/12/05 |
|---|
| 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: |
|---|
| 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 | %% Check valid-flag, re-request data if necessary |
|---|
| 79 | if ~in.Valid |
|---|
| 80 | % init timeout-counter |
|---|
| 81 | startTime = clock(); |
|---|
| 82 | timeOut = 0.1; % in seconds |
|---|
| 83 | % loop until valid |
|---|
| 84 | while (~in.Valid) && (etime(clock, startTime) < timeOut) |
|---|
| 85 | in = NXT_GetInputValues(f_sensorport, handle); |
|---|
| 86 | end%while |
|---|
| 87 | %TODO please note that we DO NOT warn or do anything else if this |
|---|
| 88 | %procedure above times out -- that seems ok, since we didn't do |
|---|
| 89 | %that before (old ver. up to 2.02) either... |
|---|
| 90 | % in that case, the returned value will probably be invalid... |
|---|
| 91 | % dangerous. |
|---|
| 92 | % solutions: either WARN, or return NaN... |
|---|
| 93 | end%if |
|---|
| 94 | |
|---|
| 95 | %% Do threshold decision |
|---|
| 96 | % 511 should be ok, being right in the middle between both "peaks" |
|---|
| 97 | if in.NormalizedADVal > 511 |
|---|
| 98 | % note how this val is "inverted"... |
|---|
| 99 | out = false; |
|---|
| 100 | else |
|---|
| 101 | out = true; |
|---|
| 102 | end%if |
|---|
| 103 | |
|---|
| 104 | end |
|---|
| 105 | |
|---|