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

Revision 289, 7.5 KB (checked in by atorf, 5 years ago)

* Added a toolbox-consistency-check to COM_OpenNXTEx
* Edited FIXMEs, TODOs and NOTEs
* Updated "See also:"-lines
* …

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