| 1 | function [data BytesRead] = NXT_LSRead(port, varargin) |
|---|
| 2 | % Reads data from a low speed (digital) sensor |
|---|
| 3 | % |
|---|
| 4 | % Syntax |
|---|
| 5 | % |[data BytesRead] = NXT_LSRead(port)| |
|---|
| 6 | % |
|---|
| 7 | % |[data BytesRead] = NXT_LSRead(port, handle)| |
|---|
| 8 | % |
|---|
| 9 | % Description |
|---|
| 10 | % |[data BytesRead] = NXT_LSRead(port))| gets the |data| of the low speed (digital) sensor value |
|---|
| 11 | % of the given sensor |port|. The value |port| can be addressed by the symbolic constants |
|---|
| 12 | % |SENSOR_1|, |SENSOR_2|, |SENSOR_3| and |SENSOR_4| analog to the labeling on the NXT Brick. The |
|---|
| 13 | % return value |BytesRead| contains the number of bytes available to read. |
|---|
| 14 | % |
|---|
| 15 | % |[data BytesRead] = NXT_LSRead(port, handle)| uses the given Bluetooth connection |handle|. This should be a |
|---|
| 16 | % serial handle on a PC system and a file handle on a Linux system. |
|---|
| 17 | % |
|---|
| 18 | % If no Bluetooth handle is specified the default one (|BT_GetDefaultHandle|) is used. |
|---|
| 19 | % |
|---|
| 20 | % |
|---|
| 21 | % For more details see the official LEGO Mindstorms communication protocol. |
|---|
| 22 | % |
|---|
| 23 | % Note: |
|---|
| 24 | % For LS communication on the NXT, data lengths are limited to 16 bytes per command. Furthermore, |
|---|
| 25 | % this protocol does not support variable-length return packages, so the response will always |
|---|
| 26 | % contain 16 data bytes, with invalid data bytes padded with zeros. |
|---|
| 27 | % |
|---|
| 28 | % Before using LS commands, the sensor mode has to be set to |
|---|
| 29 | % |LOWSPEED_9V| using the NXT_SetInputMode command. |
|---|
| 30 | % |
|---|
| 31 | % Examples |
|---|
| 32 | %+ bt_handle = BT_OpenHandle('bluetooth.ini','check'); |
|---|
| 33 | %+ |
|---|
| 34 | %+ NXT_SetInputMode(SENSOR_1, 'LOWSPEED_9V', 'RAWMODE', 'dontreply'); |
|---|
| 35 | %+ % usually we would use NXT_LSWrite before, to request some sort of reply |
|---|
| 36 | %+ [data BytesRead] = NXT_LSRead(SENSOR_1, bt_handle); |
|---|
| 37 | % |
|---|
| 38 | % See also: NXT_SetInputMode, NXT_LSWrite, NXT_LSGetStatus |
|---|
| 39 | % |
|---|
| 40 | % |
|---|
| 41 | % Signature |
|---|
| 42 | % Author: Linus Atorf (see AUTHORS) |
|---|
| 43 | % Date: 2007/10/15 |
|---|
| 44 | % Copyright: 2007, RWTH Aachen University |
|---|
| 45 | % |
|---|
| 46 | ; |
|---|
| 47 | % |
|---|
| 48 | % *********************************************************************************************** |
|---|
| 49 | % * This file is part of the RWTH - Mindstorms NXT Toolbox. * |
|---|
| 50 | % * * |
|---|
| 51 | % * The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify * |
|---|
| 52 | % * it under the terms of the GNU General Public License as published by the Free Software * |
|---|
| 53 | % * Foundation, either version 3 of the License, or (at your option) any later version. * |
|---|
| 54 | % * * |
|---|
| 55 | % * The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful, * |
|---|
| 56 | % * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * |
|---|
| 57 | % * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
|---|
| 58 | % * * |
|---|
| 59 | % * You should have received a copy of the GNU General Public License along with the * |
|---|
| 60 | % * RWTH - Mindstorms NXT Toolbox. If not, see <http: |
|---|
| 61 | % *********************************************************************************************** |
|---|
| 62 | |
|---|
| 63 | %% Parameter check |
|---|
| 64 | % check if bluetooth handle is given; if not use default one |
|---|
| 65 | if nargin > 1 |
|---|
| 66 | handle = varargin{1}; |
|---|
| 67 | else |
|---|
| 68 | handle = BT_GetDefaultHandle; |
|---|
| 69 | end%if |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | % check if port number is valid |
|---|
| 73 | if port < 0 || port > 3 |
|---|
| 74 | error('MATLAB:RWTHMindstormsNXT:Sensor:invalidPort', 'Sensor port %d invalid! It has to be 0, 1, 2 or 3', port); |
|---|
| 75 | end%if |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | %% Use wrapper functions |
|---|
| 79 | NXT_LSRequestRead(port, handle); |
|---|
| 80 | [data BytesRead] = NXT_LSCollectRead(handle); |
|---|
| 81 | |
|---|
| 82 | end % end function |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | %% ### Function: Request LS Read Packet ### |
|---|
| 88 | function NXT_LSRequestRead(InputPort, varargin) |
|---|
| 89 | % Sends the "LSRequestRead" packet: Requests the current value of the low speed (digital) sensor (e.g. ultrasonic) |
|---|
| 90 | % |
|---|
| 91 | % Usage: NXT_LSRequestRead(InputPort, varargin) |
|---|
| 92 | % InputPort : inport port connected to the digital sensor (e.g. ultra sonic) |
|---|
| 93 | % varargin : bluetooth handle (optional) |
|---|
| 94 | % |
|---|
| 95 | |
|---|
| 96 | %% Parameter check |
|---|
| 97 | % check if bluetooth handle is given; if not use default one |
|---|
| 98 | if nargin > 1 |
|---|
| 99 | handle = varargin{1}; |
|---|
| 100 | else |
|---|
| 101 | handle = BT_GetDefaultHandle; |
|---|
| 102 | end%if |
|---|
| 103 | |
|---|
| 104 | % check if port number is valid |
|---|
| 105 | if InputPort < 0 || InputPort > 3 |
|---|
| 106 | error('MATLAB:RWTHMindstormsNXT:Sensor:invalidPort', 'NXT InputPort %d invalid! It has to be 0, 1, 2 or 3', InputPort); |
|---|
| 107 | end%if |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | %% Build bluetooth command |
|---|
| 111 | [type cmd] = name2commandbytes('LSREAD'); |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | %% Packet bluetooth command |
|---|
| 115 | packet = BT_CreatePacket(type, cmd, 'reply', InputPort); |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | %% Send bluetooth packet |
|---|
| 119 | BT_SendPacket(packet, handle); |
|---|
| 120 | |
|---|
| 121 | end % end function |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | %% ### Function: Collect LS Read Packet ### |
|---|
| 127 | function [data BytesRead] = NXT_LSCollectRead(varargin) |
|---|
| 128 | % Retrieves the previously requested low speed (gitial) sensor value (e.g. ultrasonic) |
|---|
| 129 | % |
|---|
| 130 | % Usage: [data BytesRead] = NXT_LSRead(port, varargin) |
|---|
| 131 | % port : port connected to the digital sensor (e.g. ultrasonic) |
|---|
| 132 | % varargin : bluetooth handle (optional) |
|---|
| 133 | % |
|---|
| 134 | % Returns: data : low speed (digital) sensor value |
|---|
| 135 | % BytesRead : number of bytes to read |
|---|
| 136 | % |
|---|
| 137 | |
|---|
| 138 | %% Parameter check |
|---|
| 139 | % check if bluetooth handle is given; if not use default one |
|---|
| 140 | if nargin > 0 |
|---|
| 141 | handle = varargin{1}; |
|---|
| 142 | else |
|---|
| 143 | handle = BT_GetDefaultHandle; |
|---|
| 144 | end%if |
|---|
| 145 | |
|---|
| 146 | |
|---|
| 147 | %% Get reference |
|---|
| 148 | [dontcare ExpectedCmd] = name2commandbytes('LSREAD'); |
|---|
| 149 | |
|---|
| 150 | %% Collect bluetooth packet |
|---|
| 151 | [type cmd status content] = BT_CollectPacket(handle); |
|---|
| 152 | |
|---|
| 153 | %% Check if packet is the right one |
|---|
| 154 | if cmd ~= ExpectedCmd || status ~= 0 |
|---|
| 155 | warning('MATLAB:RWTHMindstormsNXT:Bluetooth:discardingUnexpectedPacket', 'Received packed not expected. Discarding and trying to continue...'); |
|---|
| 156 | BytesRead = 0; |
|---|
| 157 | data = []; |
|---|
| 158 | return; |
|---|
| 159 | end%if |
|---|
| 160 | |
|---|
| 161 | %% Interpret packet content |
|---|
| 162 | if length(content) ~= 17 |
|---|
| 163 | warning('MATLAB:RWTHMindstormsNXT:Sensor:invalidLSReadDataLength', ... |
|---|
| 164 | ['LSRead reply does not contain 16 data bytes, but it should! ' ... |
|---|
| 165 | 'This is a condition that should never happen. If it is not a toolbox-bug, ' ... |
|---|
| 166 | 'check the I²C protocol. Maybe the NXT firmware or the custom sensor is not ' ... |
|---|
| 167 | 'working properly or does not follow the NXT direct commands protocol.']); |
|---|
| 168 | end%if |
|---|
| 169 | |
|---|
| 170 | BytesRead = content(1); |
|---|
| 171 | |
|---|
| 172 | %TODO see below |
|---|
| 173 | %FIXME fix outputting uint8, convert to double first! |
|---|
| 174 | |
|---|
| 175 | % avoid index out of bounds |
|---|
| 176 | tmpEnd = min(BytesRead + 1, length(content)); |
|---|
| 177 | if length(content) > 1 |
|---|
| 178 | data = content(2:tmpEnd); |
|---|
| 179 | else |
|---|
| 180 | data = []; |
|---|
| 181 | end%if |
|---|
| 182 | |
|---|
| 183 | end % end function |
|---|