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

Revision 794, 8.4 kB (checked in by MelissaDuarte, 9 months ago)

Added the contention resolution code to the workshop exercises

Line 
1%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2% Transmitting and Receiving Data using Warplab to establish a
3% bidirectional link
4%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5% This MATLAB script uses Warplab to establish a bidirectional link
6% between two nodes. First node A will transmit to node B and then node B
7% will transmit to node A.
8
9% The specific steps implemented in this script are the following
10
11% 0. Initializaton and definition of parameters
12% 1. Generate the vector of samples that node A will transmit to node B and
13% the vector of samples that node B will transmit to node A, then download
14% the samples to the Warp boards (Sample Frequency is 40MHz)
15% 2. Prepare boards for transmission and reception from node A to node B
16% and send trigger to start transmission and reception (trigger is the SYNC
17% packet)
18% 3. Disable the radios
19% 4. Prepare boards for transmission and reception from node B to node A
20% and send trigger to start transmission and reception (trigger is the SYNC
21% packet)
22% 5. Disable the radios
23% 6. Read the received samples from the Warp boards
24% 7. Reset the boards and close sockets
25% 8. Plot the transmitted and received data
26
27
28%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
29% 0. Initializaton and definition of parameters
30%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31%Load some global definitions (packet types, etc.)
32warplab_siso_defines
33
34% Create Socket handles and intialize nodes
35[socketHandles, packetNum] = warplab_initialize;
36
37%Separate the socket handles for easier access
38% The first socket handle is always the magic SYNC
39% The rest can be arranged in any combination of Tx and Rx
40udp_Sync = socketHandles(1);
41udp_nodeA = socketHandles(2);
42udp_nodeB = socketHandles(3);
43
44% Define the warplab options (parameters)
45CaptOffset = 1000; %Number of noise samples per Rx capture; in [0:2^14]
46TxLength = 2^14-1000; %Length of transmission; in [0:2^14-CaptOffset]
47TxGainBB = 3; %Tx Baseband Gain in [0:3]
48TxGainRF = 40; %Tx RF Gain in [0:63]
49RxGainBB = 13; %Rx Baseband Gain in [0:31]
50RxGainRF = 1; %Rx RF Gain in [1:3]
51CarrierChannel = 11; % Channel in the 2.4 GHz band. In [1:14]
52
53% Define the options vector; the order of options is set by the FPGA's code
54% (C code)
55optionsVector = [CaptOffset TxLength-1 (RxGainBB + RxGainRF*2^16) (TxGainRF + TxGainBB*2^16) CarrierChannel];
56% Send options vector to the nodes
57warplab_setOptions(socketHandles,optionsVector);
58
59%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60% 1. Generate the vector of samples that node A will transmit to node B and
61% the vector of samples that node B will transmit to node A, then download
62% the samples to the Warp boards (Sample Frequency is 40MHz)
63%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
64% Create time vector.
65t = 0:(1/40e6):TxLength/40e6 - 1/40e6;
66
67% Create a signal to transmit from node A to node B
68TxDataAB = exp(t*j*2*pi*1e6); %Signal must be a row vector. The signal can
69% be real or complex, the only constraint is that the amplitude of the real
70% part must be in [-1:1] and the amplitude of the imaginary part must be
71% in [-1:1]
72
73% Download the samples to be transmitted
74warplab_writeSMWO(udp_nodeA, TxDataAB, RADIO2_TXDATA); % Download samples to node A
75
76% Create a signal to transmit from node B to node A
77TxDataBA = linspace(0,1,TxLength).*exp(t*j*2*pi*1e6);
78% Signal must be a row vector. The signal can be real or complex,
79% the only constraint is that the amplitude of the real part must be in
80% [-1:1] and the amplitude of the imaginary part must be in [-1:1]
81
82warplab_writeSMWO(udp_nodeB, TxDataBA, RADIO2_TXDATA); % Download samples to node B
83
84%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85% 2. Prepare boards for transmission and reception from node A to node B
86% and send trigger to start transmission and reception (trigger is the SYNC
87% packet)
88%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89% Enable transmission
90warplab_enableTx(udp_nodeA);
91
92% Enable reception
93warplab_enableRx(udp_nodeB);
94
95% Send the SYNC packet
96warplab_sendSync(udp_Sync);
97
98%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
99% 3. Disable the radios
100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101% Disable the receiver
102warplab_sendCmd(udp_nodeB, RADIO2_RXDIS, packetNum);
103
104% Disable the transmitter
105warplab_sendCmd(udp_nodeA, RADIO2_TXDIS, packetNum);
106
107%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108% 4. Prepare boards for transmission and reception from node B to node A
109% and send trigger to start transmission and reception (trigger is the SYNC
110% packet)
111%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112% Enable transmission
113warplab_enableTx(udp_nodeB);
114
115% Enable reception
116warplab_enableRx(udp_nodeA);
117
118% Send the SYNC packet
119warplab_sendSync(udp_Sync);
120
121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122% 5. Reset and disable the boards
123%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124% Disable the receiver
125warplab_sendCmd(udp_nodeA, RADIO2_RXDIS, packetNum);
126
127% Disable the transmitter
128warplab_sendCmd(udp_nodeB, RADIO2_TXDIS, packetNum);
129
130%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131% 6. Read the received samples from the Warp boards
132%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133% Read back the received samples sent from A to B
134[RawRxDataAB] = warplab_readSMRO(udp_nodeB, RADIO2_RXDATA, TxLength+CaptOffset);
135% Process the received samples to obtain meaningful data
136[RxDataAB,RxOTRAB] = warplab_processRawRxData(RawRxDataAB);
137% Read stored RSSI data corresponding to A to B transmission
138[RawRSSIDataAB] = warplab_readSMRO(udp_nodeB, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8);
139% Procecss Raw RSSI data to obtain meningful RSSI values
140[RxRSSIAB] = warplab_processRawRSSIData(RawRSSIDataAB);
141
142% Read back the received samples sent from B to A
143[RawRxDataBA] = warplab_readSMRO(udp_nodeA, RADIO2_RXDATA, TxLength+CaptOffset);
144% Process the received samples to obtain meaningful data
145[RxDataBA,RxOTRBA] = warplab_processRawRxData(RawRxDataBA);
146% Read stored RSSI data corresponding to B to A transmission
147[RawRSSIDataBA] = warplab_readSMRO(udp_nodeA, RADIO2_RSSIDATA, (TxLength+CaptOffset)/8);
148% Procecss Raw RSSI data to obtain meningful RSSI values
149[RxRSSIBA] = warplab_processRawRSSIData(RawRSSIDataBA);
150
151%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152% 7. Reset the boards and close sockets
153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154% Reset node A
155warplab_sendCmd(udp_nodeA, RX_DONEREADING, packetNum);
156
157% Reset node B
158warplab_sendCmd(udp_nodeB, RX_DONEREADING, packetNum);
159
160% Close sockets
161pnet('closeall');
162
163%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
164% 5. Plot the transmitted and received data
165%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166% Plot data from A to B transmissio
167figure;
168subplot(2,2,1);
169plot(real(TxDataAB));
170title('Tx I A to B');
171xlabel('n (samples)'); ylabel('Amplitude');
172axis([0 2^14 -1 1]); % Set axis ranges.
173subplot(2,2,2);
174plot(imag(TxDataAB));
175title('Tx Q A to B');
176xlabel('n (samples)'); ylabel('Amplitude');
177axis([0 2^14 -1 1]); % Set axis ranges.
178subplot(2,2,3);
179plot(real(RxDataAB));
180title('Rx I A to B');
181xlabel('n (samples)'); ylabel('Amplitude');
182axis([0 2^14 -1 1]); % Set axis ranges.
183subplot(2,2,4);
184plot(imag(RxDataAB));
185title('Rx Q A to B');
186xlabel('n (samples)'); ylabel('Amplitude');
187axis([0 2^14 -1 1]); % Set axis ranges.
188
189% Plot data from B to A transmission
190figure;
191subplot(2,2,1);
192plot(real(TxDataBA));
193title('Tx I B to A');
194xlabel('n (samples)'); ylabel('Amplitude');
195axis([0 2^14 -1 1]); % Set axis ranges.
196subplot(2,2,2);
197plot(imag(TxDataBA));
198title('Tx Q B to A');
199xlabel('n (samples)'); ylabel('Amplitude');
200axis([0 2^14 -1 1]); % Set axis ranges.
201subplot(2,2,3);
202plot(real(RxDataBA));
203title('Rx I B to A');
204xlabel('n (samples)'); ylabel('Amplitude');
205axis([0 2^14 -1 1]); % Set axis ranges.
206subplot(2,2,4);
207plot(imag(RxDataBA));
208title('Rx Q B to A');
209xlabel('n (samples)'); ylabel('Amplitude');
210axis([0 2^14 -1 1]); % Set axis ranges.
Note: See TracBrowser for help on using the browser.