root/branches/atorf/RWTHMindstormsNXT/NXT_LSRead.m @ 303

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