source: ResearchApps/PHY/WARPLAB/WARPLab_v05_2/WorkshopExercises/warplab_siso_example_ContinuousTx_WorkshopExercise.m

Last change on this file was 1455, checked in by sgupta, 14 years ago

workshop exercises

File size: 15.8 KB
RevLine 
[1455]1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Transmit sum of two sinusoids in Continous Transmission mode using WARPLab
3% (SISO configuration)
4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5
6% The specific steps implemented in this script are the following
7
8% 0. Initializaton and definition of parameters
9% 1. Generate a vector of samples to transmit and send the samples to the
10% WARP board (Sample Frequency is 40MHz).  Vector represents a sum of two
11% sinusoids with different frequency.
12% 2. Prepare WARP boards for transmission and reception and send trigger to
13% start transmission and reception (trigger is the SYNC packet)
14% 3. Leave continuous transmitter on for n seconds and then stop continuous
15% transmission.
16% 4. Read the received samples from the WARP board.
17% 5. Reset and disable the boards.
18% 6. Plot the first 2^14 received samples and close sockets
19
20% In this lab exercise you will write a matlab script that implements the
21% seven steps above. Part of the code is provided, some part of the code you
22% will write. Read the code below and fill in with your code wherever you
23% are asked to do so.
24
25% NOTE: To avoid conflict with other groups using the boards, please test
26% the code you write in this script in any of the following three ways:
27%
28% Option 1. Run this script from matlab's Command Window by entering the
29% name of the script (enter warplab_siso_example_ContinuousTx_WorkshopExercise in
30% matlab's Command Window).
31% Option 2. In the menu bar go to Debug and select Run. If there
32% are errors in the code, error messages will appear in the Command Window.
33% Option 3. Press F5. If the are errors in the code, error messages will
34% appear in the Command Window.
35%
36% DO NOT USE the Evaluate selection option and DO NOT run the script by
37% sections. To test any change, always run the whole script by following
38% any of the three options above.
39
40try,
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42% Code to avoid conflict between users, only needed for the workshop, go to
43% step 0 below to start the initialization and definition of parameters
44%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
45% fid = fopen('c:\boards_lock.txt');
46%
47% if(fid > -1)
48%     fclose('all');
49%   errordlg('Boards already in use - Please try again!');
50%   return;
51% end
52%
53% !echo > c:\boards_lock.txt
54
55%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
56% 0. Initializaton and definition of parameters
57%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58%Load some global definitions (packet types, etc.)
59warplab_defines
60
61% Create Socket handles and intialize nodes
62[socketHandles, packetNum] = warplab_initialize;
63
64% Separate the socket handles for easier access
65% The first socket handle is always the magic SYNC
66% The rest of the handles are the handles to the WARP nodes
67udp_Sync = socketHandles(1);
68udp_node1 = socketHandles(2);
69udp_node2 = socketHandles(3);
70
71% Define WARPLab parameters.
72%-------------------------------------------------------------------------%
73% USER CODE HERE
74
75% Create the following variables and assign them valid values:
76% TxDelay: Value of the Transmitter Delay. In continuous transmission mode
77%             the receiver captures TxDelay samples of noise and the
78%             first 2^14-TxDelay samples of data.
79% TxLength : Length of transmission or number of samples to transmit.
80%            In [0:2^14-TxDelay]
81%            In continuous transmission mode the Tx will continue
82%            transmitting the first TxLength samples in the Tx buffer until
83%            the user manually disables the transmitter.
84% TxMode: % Transmission mode. In [0:1]
85           % 0: Single Transmission
86           % 1: Continuous Transmission. Tx board will continue
87           % transmitting the vector of samples until the user manually
88           % disables the transmitter.
89           % For this exercise set TxMode = 1;
90% CarrierChannel: Channel in the 2.4 GHz band. In [1:14]
91% Node1_Radio2_TxGain_BB: Tx Baseband Gain. In [0:3]
92% Node1_Radio2_TxGain_RF: Tx RF Gain. In [0:63]
93% Node2_Radio2_RxGain_BB: Rx Baseband Gain. In [0:31]
94% Node2_Radio2_RxGain_RF: Rx RF Gain. In [1:3] 
95
96% Note: For this experiment node 1 will be set as the transmitter and node
97% 2 will be set as the receiver (this is done later in the code), hence,
98% there is no need to define receive gains for node 1 and there is no
99% need to define transmitter gains for node 2.
100
101%-------------------------------------------------------------------------%
102
103% Download the WARPLab parameters to the WARP nodes.
104% The nodes store the TxDelay, TxLength, and TxMode parameters in
105% registers defined in the WARPLab sysgen model. The nodes set radio
106% related parameters CarrierChannel, TxGains, and RxGains, using the
107% radio controller functions.
108
109%-------------------------------------------------------------------------%
110% USER CODE HERE
111
112% Download the TxDelay, TxLength, and TxMode parameters to node 1 using the
113% 'warplab_writeRegister' function.
114
115% The TxDelay, TxLength, and TxMode parameters need to be known at the transmitter;
116% the receiver doesn't require knowledge of these parameters (the receiver
117% will always capture 2^14 samples). For this exercise node 1 will be set as
118% the transmitter (this is done later in the code). Since TxDelay, TxLength and
119% TxMode are only required at the transmitter download the TxDelay, TxLength and
120% TxMode parameters only to the transmitter node (node 1).
121
122% Hints:
123
124% 1. The first argument of the 'warplab_writeRegister' function identifies
125% the node to which the parameter will be downloaded to. The id or handle
126% to node 1 is 'udp_node1'.
127
128% 2. The second argument of the 'warplab_writeRegister' function identifies
129% the parameter that will be downloaded. The id for the TxDelay
130% parameter is 'TX_DELAY, the id for the TxLength parameter is 'TX_LENGTH'
131% and the id for the TxMode parameter is 'TX_MODE'.
132
133% 3. The third argument of the 'warplab_writeRegister' function is the
134% value the parameter must be set to. The values to download have been
135% stored in the 'TxDelay', 'TxLength', and 'TxMode' variables.
136
137% 4. The 'warplab_writeRegister' function has been used in previous
138% exercises.
139
140% 5. Call the 'warplab_writeRegister' three times. One time to download the
141% TxDelay, one time to download the TxLength, and one time to download the
142% TxMode. The 'warplab_writeRegister' function can only set one register (parameter) per node at a time
143
144%-------------------------------------------------------------------------%
145
146%-------------------------------------------------------------------------%
147% USER CODE HERE
148
149% Download the CarrierChannel parameter to both nodes using the
150% 'warplab_setRadioParameter' function.
151
152% The CarrierChannel parameter must be downloaded to all nodes 
153
154% Hints:
155
156% 1. The first argument of the 'warplab_setRadioParameter' function
157% identifies the node to which the parameter will be downloaded to.
158% The id or handle to node 1 is 'udp_node1' and the id or handle to node 2
159% is 'udp_node2'
160
161% 2. The second argument of the 'warplab_setRadioParameter' function
162% identifies the parameter that will be downloaded. The id for the
163% CarrierChannel parameter is 'CARRIER_CHANNEL'
164
165% 3. The third argument of the 'warplab_setRadioParameter' function is the
166% value the parameter must be set to. This value has been stored in the
167% CarrierChannel variable.
168
169% 4. The 'warplab_setRadioParameter' function has been used in previous
170% exercises.
171
172% 5. Call the 'warplab_setRadioParameter' twice. One time to download the
173% CarrierChannel to node 1 and one time to download the CarrierChannel to
174% node 2. The 'warplab_setRadioParameter' function can only set one
175% radio parameter per node at a time
176
177%-------------------------------------------------------------------------%
178
179% Node 1 will be set as the transmitter so download Tx gains to node 1.
180warplab_setRadioParameter(udp_node1,RADIO2_TXGAINS,(Node1_Radio2_TxGain_RF + Node1_Radio2_TxGain_BB*2^16));
181% Node 2 will be set as the receiver so download Rx gains to node 2.
182warplab_setRadioParameter(udp_node2,RADIO2_RXGAINS,(Node2_Radio2_RxGain_BB + Node2_Radio2_RxGain_RF*2^16));
183
184%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185% 1. Generate a vector of samples to transmit and send the samples to the
186% WARP board (Sample Frequency is 40MHz).  Vector represents a sum of two
187% sinusoids with different frequency.
188%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189% Prepare some data to be transmitted
190t = 0:(1/40e6):TxLength/40e6 - 1/40e6; % Create time vector.
191
192% Create a signal to transmit, the signal can be real or complex.
193% The signal must meet the following requirements:
194% - Signal to transmit must be a row vector.
195% - The amplitude of the real part must be in [-1:1] and the amplitude
196% of the imaginary part must be in [-1:1].
197% - Highest frequency component is limited to 9.5 MHz (signal bandwidth
198% is limited to 19 MHz)
199% - Lowest frequency component is limited to 30 kHz
200f1 = 1e6; 
201f2 = 6e6;
202Node1_Radio2_TxData = exp(t*j*2*pi*f1)+exp(t*j*2*pi*f2); % Create a signal to transmit.
203% Signal is the sum of two sinusoids with frequencies f1 and f2.
204
205% Scale signal so that amplitude of the real and
206% imaginary part is in [-1:1]. We want the signal to span [-1,1] range
207% so it uses the full range of the DAC at the tranmitter.
208scale = 1 / max( [ max(real(Node1_Radio2_TxData)) , max(imag(Node1_Radio2_TxData)) ] );
209Node1_Radio2_TxData = scale*Node1_Radio2_TxData; 
210
211% Download the samples to be transmitted
212warplab_writeSMWO(udp_node1, RADIO2_TXDATA, Node1_Radio2_TxData);
213
214%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215% 2. Prepare WARP boards for transmission and reception and send trigger to
216% start transmission and reception (trigger is the SYNC packet)
217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218% The following lines of code set node 1 as transmitter and node 2 as
219% receiver; transmission and capture are triggered by sending the SYNC
220% packet.
221
222% Enable transmitter radio path in radio 2 in node 1 (enable radio 2 in
223% node 1 as transmitter)
224warplab_sendCmd(udp_node1, RADIO2_TXEN, packetNum);
225
226% Enable transmission of node1's radio 2 Tx buffer (enable transmission
227% of samples stored in radio 2 Tx Buffer in node 1)
228warplab_sendCmd(udp_node1, RADIO2TXBUFF_TXEN, packetNum);
229
230% Enable receiver radio path in radio 2 in node 2 (enable radio 2 in
231% node 2 as receiver)
232warplab_sendCmd(udp_node2, RADIO2_RXEN, packetNum);
233
234% Enable capture in node2's radio 2 Rx Buffer (enable radio 2 rx buffer in
235% node 2 for storage of samples)
236warplab_sendCmd(udp_node2, RADIO2RXBUFF_RXEN, packetNum);
237
238% Prime transmitter state machine in node 1. Node 1 will be
239% waiting for the SYNC packet. Transmission from node 1 will be triggered
240% when node 1 receives the SYNC packet.
241warplab_sendCmd(udp_node1, TX_START, packetNum);
242
243% Prime receiver state machine in node 2. Node 2 will be waiting
244% for the SYNC packet. Capture at node 2 will be triggered when node 2
245% receives the SYNC packet.
246warplab_sendCmd(udp_node2, RX_START, packetNum);
247
248% Send the SYNC packet
249warplab_sendSync(udp_Sync);
250
251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
252% 3. Leave continuous transmitter on for n seconds and then stop continuous
253% transmission
254%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255%-------------------------------------------------------------------------%
256% USER CODE HERE
257
258% Use matlab's pause command to pause execution for n seconds. Because you
259% are sharing the board with other users, please pause for only less than 5
260% seconds: n < 5
261
262% To learn more about the pause function enter 'help pause' in the Matlab
263% command window.
264
265% IMPORTANT: Use pause(n) (with an argument). If you just use pause it will
266% pause until you press a key, since you are sharing the boards with other
267% users it is better to use pause(n) to avoid one user retaining the boards
268% for too long.
269
270%-------------------------------------------------------------------------%
271
272%-------------------------------------------------------------------------%
273% USER CODE HERE
274
275% Stop transmission by sending the TX_STOP command using the
276% 'warplab_sendCmd' function. This function has been used in all the
277% previous exercises.
278
279% Hints:
280
281% 1. The first argument of the 'warplab_sendCmd' function identifies the
282% node to which the command will be sent. The TX_STOP command must be sent
283% to the transmitter node so use 'udp_node1' as the first argument.
284
285% 2. The second argument of the 'warplab_sendCmd' function identifies the
286% instruction or command to be sent. In this case, the command to send is
287% the TX_STOP command.
288
289% 3. The third argument of the 'warplab_sendCmd' command is a field that is
290% not used at the moment, it may be used in future versions of WARPLab to
291% keep track of packets. Use 'packetNum' as the third argument of the
292% 'warplab_sendCmd' command.
293 
294%-------------------------------------------------------------------------%
295
296%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297% 4. Read the received samples from the WARP board
298%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
299% In continuous transmitter mode the receiver stores CaptOffset samples of
300% noise and the first 2^14-CaptOffset samples transmitted.
301
302% Read back the received samples
303[Node2_Radio2_RawRxData] = warplab_readSMRO(udp_node2, RADIO2_RXDATA, 2^14);
304% Process the received samples to obtain meaningful data
305[Node2_Radio2_RxData,Node2_Radio2_RxOTR] = warplab_processRawRxData(Node2_Radio2_RawRxData);
306% Read stored RSSI data
307[Node2_Radio2_RawRSSIData] = warplab_readSMRO(udp_node2, RADIO2_RSSIDATA, 2^14/8);
308% Procecss Raw RSSI data to obtain meningful RSSI values
309[Node2_Radio2_RSSIData] = warplab_processRawRSSIData(Node2_Radio2_RawRSSIData);
310
311%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312% 5. Reset and disable the boards
313%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314% Set radio 2 Tx buffer in node 1 back to Tx disabled mode
315warplab_sendCmd(udp_node1, RADIO2TXBUFF_TXDIS, packetNum);
316
317% Disable the transmitter radio
318warplab_sendCmd(udp_node1, RADIO2_TXDIS, packetNum);
319
320% Set radio 2 Rx buffer in node 2 back to Rx disabled mode
321warplab_sendCmd(udp_node2, RADIO2RXBUFF_RXDIS, packetNum);
322
323% Disable the receiver radio
324warplab_sendCmd(udp_node2, RADIO2_RXDIS, packetNum);
325
326% Disable continous tranmsission mode
327TxMode = 0;
328warplab_writeRegister(udp_node1,TX_MODE,TxMode);
329
330%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
331% 6. Plot the transmitted and received data and close sockets
332%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
333figure;
334subplot(2,2,1);
335plot(real(Node1_Radio2_TxData));
336title('Tx Node 1 Radio 2 I');
337xlabel('n (samples)'); ylabel('Amplitude');
338axis([0 2^14 -1 1]); % Set axis ranges.
339subplot(2,2,2);
340plot(imag(Node1_Radio2_TxData));
341title('Tx Node 1 Radio 2 Q');
342xlabel('n (samples)'); ylabel('Amplitude');
343axis([0 2^14 -1 1]); % Set axis ranges.
344subplot(2,2,3);
345plot(real(Node2_Radio2_RxData));
346title('Rx Node 2 Radio 2 I');
347xlabel('n (samples)'); ylabel('Amplitude');
348axis([0 2^14 -1 1]); % Set axis ranges.
349subplot(2,2,4);
350plot(imag(Node2_Radio2_RxData));
351title('Rx Node 2 Radio 2 Q');
352xlabel('n (samples)'); ylabel('Amplitude');
353axis([0 2^14 -1 1]); % Set axis ranges.
354
355% Close sockets
356pnet('closeall');
357
358%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
359% Code to avoid conflict between users, only needed for the workshop
360%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361% !del c:\boards_lock.txt
362catch,
363% Reset nodes
364warplab_reset2x2Node(udp_node1);
365warplab_reset2x2Node(udp_node2);
366% Close sockets
367pnet('closeall');
368% !del c:\boards_lock.txt
369lasterr
370end
371
Note: See TracBrowser for help on using the repository browser.