root/ResearchApps/PHY/WARPLAB/WARPLAB_SISO/M_code/warplab_example_TxRx_WorkshopExercise.m

Revision 794, 7.6 kB (checked in by MelissaDuarte, 11 months ago)

Added the contention resolution code to the workshop exercises

Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Transmitting and Receiving Data using Warplab
3%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4% In this lab exercise you will write a matlab script to transmit and
5% receive data using Warplab.
6
7% Using Warplab, transmission and reception of a signal over a wireless
8% channel can be done by following the next six steps:
9
10% 0. Initializaton and definition of parameters
11% 1. Generate a vector of samples to transmit and send the samples to the
12% Warp board (Sample Frequency is 40MHz)
13% 2. Prepare boards for transmission and reception and send trigger to
14% start transmission and reception (trigger is the SYNC packet)
15% 3. Read the received samples from the Warp board
16% 4. Reset and disable the boards
17% 5. Plot the transmitted and received data (Optional)
18
19% In this lab exercise you will write a matlab script that implements the
20% six steps above. Part of the code is provided, some part of the code you
21% will write. Read the code below and fill in with your code wherever you
22% are asked to do so.
23
24% NOTE: To avoid conflict with other groups using the boards, please test
25% the code you write in this script in any of the following three ways:
26%
27% Option 1. Run this script from matlab's Command Window by entering the
28% name of the script (enter warplab_example_TxRx_WorkshopExercise in
29% matlab's Command Window).
30% Option 2. In the menu bar go to Debug and select Run. If there
31% are errors in the code, error messages will appear in the Command Window.
32% Option 3. Press F5. If the are errors in the code, error messages will
33% appear in the Command Window.
34%
35% DO NOT USE the Evaluate selection option and DO NOT run the script by
36% sections. To test any change, always run the whole script by following
37% any of the three options above.
38
39try,
40%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41% Code to avoid conflict between users, only needed for the workshop, go to
42% step 0 below to start the initialization and definition of parameters
43%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44fid = fopen('c:\boards_lock.txt');
45
46if(fid > -1)
47    fclose('all');
48        errordlg('Boards already in use - Please try again!');
49        return;
50end
51
52!echo > c:\boards_lock.txt
53
54%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55% 0. Initializaton and definition of parameters
56%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57%Load some global definitions (packet types, etc.)
58warplab_siso_defines
59
60% Create Socket handles and intialize nodes
61[socketHandles, packetNum] = warplab_initialize;
62
63%Separate the socket handles for easier access
64% The first socket handle is always the magic SYNC
65% The rest can be arranged in any combination of Tx and Rx
66udp_Sync = socketHandles(1);
67udp_Tx = socketHandles(2);
68udp_RxA = socketHandles(3);
69
70%-------------------------------------------------------------------------%
71% USER CODE HERE
72
73% Create the following variables and assign them valid values:
74% CaptOffset: Value of the Capture offset. In [0:2^14]
75% TxLength: Number of samples to transmit. In [0:2^14-CaptOffset]
76% TxGainBB: Transmitter BaseBand gain. In [0:3]
77% TxGainRF: Transmitter RF gain. In [0:63]
78% RxGainBB: Receiver Baseband gain. In[0:31]
79% RxGainRF: Receiver RF gain. In [1:3]
80% CarrierChannel: Channel in the 2.4 GHz band. In [1:14]
81
82% Note: Set TxGainBB, TxGainRF, RxGainBB, and RxGainRF to the same values
83% you used in the warplab_siso_GUI.
84
85
86%-------------------------------------------------------------------------%
87   
88% Define the options vector; the order of options is set by the FPGA's code
89% (C code)
90optionsVector = [CaptOffset TxLength-1 (RxGainBB + RxGainRF*2^16) (TxGainRF + TxGainBB*2^16) CarrierChannel];
91% Send options vector to the nodes
92warplab_setOptions(socketHandles,optionsVector);
93
94%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95% 1. Generate a vector of samples to transmit and send the samples to the
96% Warp board (Sample Frequency is 40MHz)
97%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98%Create time vector 't'
99t = 0:(1/40e6):TxLength/40e6 - 1/40e6;
100
101%-------------------------------------------------------------------------%
102% USER CODE HERE
103
104% Create a signal to transmit (a vector of samples to transmit).
105% The signal must be a row vector. The Signal is a function of the time
106% vector 't'. The signal can be real or complex, the only constraint is
107% that the amplitude of the real part must be in [-1:1] and the amplitude
108% of the imaginary part must be in [-1:1]. Store the signal to transmit in
109% a variable called TxData (TxData = your signal)
110%-------------------------------------------------------------------------%
111
112% Download the samples to be transmitted
113warplab_writeSMWO(udp_Tx, TxData, RADIO2_TXDATA);
114
115%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116% 2. Prepare boards for transmission and reception and send trigger to
117% start transmission and reception (trigger is the SYNC packet)
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119% Enable transmission
120warplab_enableTx(udp_Tx);
121
122% Enable reception
123warplab_enableRx(udp_RxA);
124
125% Send the SYNC packet
126warplab_sendSync(udp_Sync);
127
128%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
129% 3. Read the received samples from the Warp board
130%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131% Read back the received samples
132[RawRxData] = warplab_readSMRO(udp_RxA, RADIO2_RXDATA, TxLength+CaptOffset);
133% Process the received samples to obtain meaningful data
134[RxData,RxOTR] = warplab_processRawRxData(RawRxData);
135% Read stored RSSI data
136[RawRSSIData] = warplab_readSMRO(udp_RxA, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8);
137% Procecss Raw RSSI data to obtain meningful RSSI values
138[RxRSSI] = warplab_processRawRSSIData(RawRSSIData);
139
140%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141% 4. Reset and disable the boards
142%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143% Reset the receiver
144warplab_sendCmd(udp_RxA, RX_DONEREADING, packetNum);
145
146% Disable the receiver
147warplab_sendCmd(udp_RxA, RADIO2_RXDIS, packetNum);
148
149% Disable the transmitter
150warplab_sendCmd(udp_Tx, RADIO2_TXDIS, packetNum);
151
152% Close sockets
153pnet('closeall');
154
155
156%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157% 5. Plot the transmitted and received data
158%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
159figure;
160subplot(2,2,1);
161plot(real(TxData));
162title('Tx I');
163xlabel('n (samples)'); ylabel('Amplitude');
164axis([0 2^14 -1 1]); % Set axis ranges.
165subplot(2,2,2);
166plot(imag(TxData));
167title('Tx Q');
168xlabel('n (samples)'); ylabel('Amplitude');
169axis([0 2^14 -1 1]); % Set axis ranges.
170subplot(2,2,3);
171plot(real(RxData));
172title('Rx I');
173xlabel('n (samples)'); ylabel('Amplitude');
174axis([0 2^14 -1 1]); % Set axis ranges.
175subplot(2,2,4);
176plot(imag(RxData));
177title('Rx Q');
178xlabel('n (samples)'); ylabel('Amplitude');
179axis([0 2^14 -1 1]); % Set axis ranges.
180
181%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182% Code to avoid conflict between users, only needed for the workshop
183%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184pnet('closeall');
185!del c:\boards_lock.txt
186catch,
187% Close sockets
188pnet('closeall');
189!del c:\boards_lock.txt
190lasterr
191end
Note: See TracBrowser for help on using the browser.