root/tags/version-1.00/RWTHMindstormsNXT/NXT_LSRead.m @ 3

Revision 3, 7.2 KB (checked in by behrens, 5 years ago)

add tag and trunk toolbox

Line 
1function [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% *  This file is part of the RWTH - Mindstorms NXT Toolbox.                                    *
49% *                                                                                             *
50% *  The RWTH - Mindstorms NXT Toolbox is free software: you can redistribute it and/or modify  *
51% *  it under the terms of the GNU General Public License as published by the Free Software     *
52% *  Foundation, either version 3 of the License, or (at your option) any later version.        *
53% *                                                                                             *
54% *  The RWTH - Mindstorms NXT Toolbox is distributed in the hope that it will be useful,       *
55% *  but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS  *
56% *  FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
57% *                                                                                             *
58% *  You should have received a copy of the GNU General Public License along with the           *
59% *  RWTH - Mindstorms NXT Toolbox. If not, see <http://www.gnu.org/licenses/>.                 *
60% ***********************************************************************************************
61
62%% Parameter check
63% check if bluetooth handle is given; if not use default one
64if nargin > 1
65    if (ispc && isa(varargin{1}, 'serial')) || (isunix && isscalar(varargin{1}))
66        handle = varargin{1};
67    else
68        error('MATLAB:RWTHMindstormsNXT:Bluetooth:invalidHandle', 'Optional NXT bluetooth handle specified, but not a valid serial port handle');
69    end%if
70else
71    handle = BT_GetDefaultHandle;
72end%if
73
74
75% check if port number is valid
76if port < 0 || port > 3
77    error('MATLAB:RWTHMindstormsNXT:Sensor:invalidPort', 'Sensor port %d invalid! It has to be 0, 1, 2 or 3', port);
78end%if
79
80
81%% Use wrapper functions
82NXT_LSRequestRead(port, handle);
83[data BytesRead] = NXT_LSCollectRead(handle);
84
85end % end function
86
87
88
89
90%% ### Function: Request LS Read Packet ###
91function NXT_LSRequestRead(InputPort, varargin)
92% Sends the "LSRequestRead" packet: Requests the current value of the low speed (digital) sensor (e.g. ultrasonic)
93%
94% Usage: NXT_LSRequestRead(InputPort, varargin)
95%               InputPort    :  inport port connected to the digital sensor (e.g. ultra sonic)
96%               varargin     :  bluetooth handle (optional)
97%
98
99%% Parameter check
100% check if bluetooth handle is given; if not use default one
101if nargin > 1
102    if (ispc && isa(varargin{1}, 'serial')) || (isunix && isscalar(varargin{1}))
103        handle = varargin{1};
104    else
105        error('MATLAB:RWTHMindstormsNXT:Bluetooth:invalidHandle', 'Optional NXT bluetooth handle specified, but not a valid serial port handle');
106    end%if
107else
108    handle = BT_GetDefaultHandle;
109end%if
110
111% check if port number is valid
112if InputPort < 0 || InputPort > 3
113    error('MATLAB:RWTHMindstormsNXT:Sensor:invalidPort', 'NXT InputPort %d invalid! It has to be 0, 1, 2 or 3', InputPort);
114end%if
115
116
117%% Build bluetooth command
118[type cmd] = name2commandbytes('LSREAD');
119
120
121%% Packet bluetooth command
122packet = BT_CreatePacket(type, cmd, 'reply', InputPort);
123
124
125%% Send bluetooth packet
126BT_SendPacket(packet, handle);
127
128end % end function
129
130
131
132
133%% ### Function: Collect LS Read Packet ###
134function [data BytesRead] = NXT_LSCollectRead(varargin)
135% Retrieves the previously requested low speed (gitial) sensor value (e.g. ultrasonic)
136%
137% Usage: [data BytesRead] = NXT_LSRead(port, varargin)
138%            port         :  port connected to the digital sensor (e.g. ultrasonic)
139%            varargin     :  bluetooth handle (optional)
140%
141% Returns:    data        : low speed (digital) sensor value
142%             BytesRead   : number of bytes to read
143%
144
145%% Parameter check
146% check if bluetooth handle is given; if not use default one
147if nargin > 0
148    if (ispc && isa(varargin{1}, 'serial')) || (isunix && isscalar(varargin{1}))
149        handle = varargin{1};
150    else
151        error('MATLAB:RWTHMindstormsNXT:Bluetooth:invalidHandle', 'Optional NXT bluetooth handle specified, but not a valid serial port handle');
152    end%if
153else
154    handle = BT_GetDefaultHandle;
155end%if
156
157
158%% Get reference
159[dontcare ExpectedCmd] = name2commandbytes('LSREAD');
160
161%% Collect bluetooth packet
162[type cmd status content] = BT_CollectPacket(handle);
163
164%% Check if packet is the right one
165if cmd ~= ExpectedCmd || status ~= 0
166    warning('MATLAB:RWTHMindstormsNXT:Bluetooth:discardingUnexpectedPacket', 'Received packed not expected. Discarding and trying to continue...');
167    BytesRead = 0;
168    data = [];
169    return;
170end%if
171
172%% Interpret packet content
173if length(content) ~= 17
174    warning('MATLAB:RWTHMindstormsNXT:Sensor:invalidLSReadDataLength', ...
175            ['LSRead reply does not contain 16 data bytes, but it should! ' ...
176            'This is a condition that should never happen. If it is not a toolbox-bug, ' ...
177            'check the I²C protocol. Maybe the NXT firmware or the custom sensor is not ' ...
178            'working properly or does not follow the NXT direct commands protocol.']);
179end%if
180
181BytesRead = content(1);
182% avoid index out of bounds
183tmpEnd = min(BytesRead + 1, length(content));
184if length(content) > 1
185    data = content(2:tmpEnd);
186else
187    data = [];
188end%if
189
190end % end function
Note: See TracBrowser for help on using the browser.