root/branches/atorf/USB_ToolboxTest_Windows/NXT_LSRead.m @ 100

Revision 100, 6.6 KB (checked in by atorf, 5 years ago)

Added simple Accelerator demo, improved basic functions

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% ***********************************************************************************************
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://www.gnu.org/licenses/>.                 *
61% ***********************************************************************************************
62
63%% Parameter check
64% check if bluetooth handle is given; if not use default one
65if nargin > 1
66        handle = varargin{1};
67else
68    handle = BT_GetDefaultHandle;
69end%if
70
71
72% check if port number is valid
73if port < 0 || port > 3
74    error('MATLAB:RWTHMindstormsNXT:Sensor:invalidPort', 'Sensor port %d invalid! It has to be 0, 1, 2 or 3', port);
75end%if
76
77
78%% Use wrapper functions
79NXT_LSRequestRead(port, handle);
80[data BytesRead] = NXT_LSCollectRead(handle);
81
82end % end function
83
84
85
86
87%% ### Function: Request LS Read Packet ###
88function 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
98if nargin > 1
99        handle = varargin{1};
100else
101    handle = BT_GetDefaultHandle;
102end%if
103
104% check if port number is valid
105if InputPort < 0 || InputPort > 3
106    error('MATLAB:RWTHMindstormsNXT:Sensor:invalidPort', 'NXT InputPort %d invalid! It has to be 0, 1, 2 or 3', InputPort);
107end%if
108
109
110%% Build bluetooth command
111[type cmd] = name2commandbytes('LSREAD');
112
113
114%% Packet bluetooth command
115packet = BT_CreatePacket(type, cmd, 'reply', InputPort);
116
117
118%% Send bluetooth packet
119BT_SendPacket(packet, handle);
120
121end % end function
122
123
124
125
126%% ### Function: Collect LS Read Packet ###
127function [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
140if nargin > 0
141        handle = varargin{1};
142else
143    handle = BT_GetDefaultHandle;
144end%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
154if 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;
159end%if
160
161%% Interpret packet content
162if 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.']);
168end%if
169
170BytesRead = content(1);
171
172%TODO see below
173%FIXME fix outputting uint8, convert to double first!
174
175% avoid index out of bounds
176tmpEnd = min(BytesRead + 1, length(content));
177if length(content) > 1
178    data = content(2:tmpEnd);
179else
180    data = [];
181end%if
182
183end % end function
Note: See TracBrowser for help on using the browser.