| 1 | function status = NXT_SetInputMode(InputPort, SensorTypeDesc, SensorModeDesc, ReplyMode, varargin) |
|---|
| 2 | % Sets a sensor mode, configures and initializes a sensor to be read out |
|---|
| 3 | % |
|---|
| 4 | % Syntax |
|---|
| 5 | % |status = NXT_SetInputMode(port, SensorTypeDesc, SensorModeDesc, ReplyMode)| |
|---|
| 6 | % |
|---|
| 7 | % |status = NXT_SetInputMode(port, SensorTypeDesc, SensorModeDesc, ReplyMode, handle)| |
|---|
| 8 | % |
|---|
| 9 | % Description |
|---|
| 10 | % |status = NXT_SetInputMode(InputPort, SensorTypeDesc, SensorModeDesc, ReplyMode)| sets mode, |
|---|
| 11 | % configures and initializes the given sensor |port| to be ready to be read out. The value |port| |
|---|
| 12 | % can be addressed by the symbolic constants |SENSOR_1|, |SENSOR_2|, |SENSOR_3| and |SENSOR_4| |
|---|
| 13 | % analog to the labeling on the NXT Brick. The value |SensorTypeDesc| determines the sensor type. |
|---|
| 14 | % See all valid types below. |SensorModeDesc| represents the sensor mode. It specifies what mode |
|---|
| 15 | % the |.ScaledVal| from |NXT_GetInputValues| should be. Valid parameters see below. By the |
|---|
| 16 | % |ReplyMode| one can request an acknowledgement for the packet transmission. The two strings |
|---|
| 17 | % |'reply'| and |'dontreply'| are valid. The return value |status| indicates if an error occures |
|---|
| 18 | % by the packet transmission. |
|---|
| 19 | % |
|---|
| 20 | % |status = NXT_SetInputMode(InputPort, SensorTypeDesc, SensorModeDesc, ReplyMode, handle)| |
|---|
| 21 | % uses the given NXT connection |handle|. This should be a struct containing a serial handle on a PC system and |
|---|
| 22 | % a file handle on a Linux system. |
|---|
| 23 | % |
|---|
| 24 | % If no NXT handle is specified the default one (|COM_GetDefaultNXT|) is used. |
|---|
| 25 | % |
|---|
| 26 | % Input: |
|---|
| 27 | % *|SensorTypeDesc|:* Valid types are (all strings): |
|---|
| 28 | % |
|---|
| 29 | % |NO_SENSOR| (nothing, use to close sensor port) |
|---|
| 30 | % |
|---|
| 31 | % |SWITCH| (NXT touch sensor, "binary") |
|---|
| 32 | % |
|---|
| 33 | % |LIGHT_ACTIVE| (NXT light sensor, red LED is on) |
|---|
| 34 | % |
|---|
| 35 | % |LIGHT_INACTIVE| (NXT light sensor, red LED is off) |
|---|
| 36 | % |
|---|
| 37 | % |SOUND_DB| (NXT sound sensor, unit dB) |
|---|
| 38 | % |
|---|
| 39 | % |SOUND_DBA| (NXT sound sensor, unit dBA) |
|---|
| 40 | % |
|---|
| 41 | % |LOWSPEED| (NXT, passive digital sensor) |
|---|
| 42 | % |
|---|
| 43 | % |LOWSPEED_9V| (NXT, active digital sensor, e.g. UltraSonic) |
|---|
| 44 | % |
|---|
| 45 | % |HIGHSPEED| (NXT, probably digital sensor on highspeed port 4) |
|---|
| 46 | % |
|---|
| 47 | % |TEMPERATURE| (old RCX sensor) |
|---|
| 48 | % |
|---|
| 49 | % |REFLECTION| (old RCX sensor) |
|---|
| 50 | % |
|---|
| 51 | % |ANGLE| (old RCX sensor) |
|---|
| 52 | % |
|---|
| 53 | % |COLORFULL| (NXT 2.0 Color sensor, full RGB mode) |
|---|
| 54 | % |
|---|
| 55 | % |COLORRED| (NXT 2.0 Color sensor, red LED only) |
|---|
| 56 | % |
|---|
| 57 | % |COLORGREEN| (NXT 2.0 Color sensor, green LED only) |
|---|
| 58 | % |
|---|
| 59 | % |COLORBLUE| (NXT 2.0 Color sensor, blue LED only) |
|---|
| 60 | % |
|---|
| 61 | % |COLORNONE| (NXT 2.0 Color sensor, no LED) |
|---|
| 62 | % |
|---|
| 63 | % |
|---|
| 64 | %FIXME: add type for color nxt 2.0 |
|---|
| 65 | % |
|---|
| 66 | % *|SensorModeDesc|:* Valid modes are (all strings): |
|---|
| 67 | % |
|---|
| 68 | % |RAWMODE| (Fastest. RawADVal will be used) |
|---|
| 69 | % |
|---|
| 70 | % |BOOLEANMODE| (1 if above 45% threshold, else 0) |
|---|
| 71 | % |
|---|
| 72 | % |TRANSITIONCNTMODE| (count transitions of booleanmode) |
|---|
| 73 | % |
|---|
| 74 | % |PERIODCOUNTERMODE| (count periods (up and down |
|---|
| 75 | % transition) of boolean mode) |
|---|
| 76 | % |
|---|
| 77 | % |PCTFULLSCALEMODE| (normalized percentage between 0 |
|---|
| 78 | % and 100, use .NormalizedADVal |
|---|
| 79 | % instead!) |
|---|
| 80 | % |
|---|
| 81 | % |
|---|
| 82 | % More exotic modes are: |
|---|
| 83 | % |CELSIUSMODE| (RCX temperature only) |
|---|
| 84 | % |
|---|
| 85 | % |FAHRENHEITMODE| (RCX temperature only) |
|---|
| 86 | % |
|---|
| 87 | % |ANGLESTEPSMODE| (RCX rotation only) |
|---|
| 88 | % |
|---|
| 89 | % |SLOPEMASK| (what's this???) |
|---|
| 90 | % |
|---|
| 91 | % |MODEMASK| (what's this???) |
|---|
| 92 | % |
|---|
| 93 | % |
|---|
| 94 | % For more details see the official LEGO Mindstorms communication protocol. |
|---|
| 95 | % |
|---|
| 96 | % Examples |
|---|
| 97 | %+ status = NXT_SetInputMode(SENSOR_1, 'SOUND_DB', 'RAWMODE', 'dontreply'); |
|---|
| 98 | % |
|---|
| 99 | %+ handle = COM_OpenNXT('bluetooth.ini'); |
|---|
| 100 | %+ status = NXT_SetInputMode(SENSOR_3, 'LIGHT_ACTIVE', 'RAWMODE', 'dontreply', handle); |
|---|
| 101 | % |
|---|
| 102 | % See also: NXT_GetInputValues, OpenLight, OpenSound, OpenSwitch, OpenUltrasonic, CloseSensor, SENSOR_1, SENSOR_2, SENSOR_3, SENSOR_4 |
|---|
| 103 | % |
|---|
| 104 | % Signature |
|---|
| 105 | % Author: Linus Atorf (see AUTHORS) |
|---|
| 106 | % Date: 2007/10/15 |
|---|
| 107 | % Copyright: 2007-2010, RWTH Aachen University |
|---|
| 108 | % |
|---|
| 109 | ; |
|---|
| 110 | % |
|---|
| 111 | % *********************************************************************************************** |
|---|
| 112 | % * This file is part of the RWTH - Mindstorms NXT Toolbox. * |
|---|
| 113 | % * * |
|---|
| 114 | % * The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify * |
|---|
| 115 | % * it under the terms of the GNU General Public License as published by the Free Software * |
|---|
| 116 | % * Foundation, either version 3 of the License, or (at your option) any later version. * |
|---|
| 117 | % * * |
|---|
| 118 | % * The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful, * |
|---|
| 119 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * |
|---|
| 120 | % * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
|---|
| 121 | % * * |
|---|
| 122 | % * You should have received a copy of the GNU General Public License along with the * |
|---|
| 123 | % * RWTH - Mindstorms NXT Toolbox. If not, see <http: |
|---|
| 124 | % *********************************************************************************************** |
|---|
| 125 | |
|---|
| 126 | %% Parameter check |
|---|
| 127 | % check if bluetooth handle is given; if not use default one |
|---|
| 128 | if nargin > 4 |
|---|
| 129 | handle = varargin{1}; |
|---|
| 130 | else |
|---|
| 131 | handle = COM_GetDefaultNXT; |
|---|
| 132 | end%if |
|---|
| 133 | |
|---|
| 134 | % check if port number is valid |
|---|
| 135 | if InputPort < 0 || InputPort > 3 |
|---|
| 136 | error('MATLAB:RWTHMindstormsNXT:Sensor:invalidPort', 'NXT InputPort %d invalid! It has to be 0, 1, 2 or 3', InputPort); |
|---|
| 137 | end%if |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | %% Build bluetooth command |
|---|
| 141 | [type cmd] = name2commandbytes('SETINPUTMODE'); |
|---|
| 142 | |
|---|
| 143 | SensorTypeByte = sensortype2byte(SensorTypeDesc); |
|---|
| 144 | SensorModeByte = sensormode2byte(SensorModeDesc); |
|---|
| 145 | |
|---|
| 146 | content = [InputPort; SensorTypeByte; SensorModeByte]; |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | %% Packet bluetooth command |
|---|
| 150 | packet = COM_CreatePacket(type, cmd, ReplyMode, content); |
|---|
| 151 | |
|---|
| 152 | |
|---|
| 153 | %% Send bluetooth packet |
|---|
| 154 | COM_SendPacket(packet, handle); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | %% Receive status packet if reply mode is used |
|---|
| 158 | if strcmpi(ReplyMode, 'reply') |
|---|
| 159 | [type cmd status content] = COM_CollectPacket(handle); |
|---|
| 160 | else |
|---|
| 161 | status = 0; %ok |
|---|
| 162 | end%if |
|---|
| 163 | |
|---|
| 164 | end%function |
|---|