DESERT 3.5.1
Loading...
Searching...
No Matches
uwoptical-phy.cpp
Go to the documentation of this file.
1//
2// Copyright (c) 2017 Regents of the SIGNET lab, University of Padova.
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions
7// are met:
8// 1. Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// 2. Redistributions in binary form must reproduce the above copyright
11// notice, this list of conditions and the following disclaimer in the
12// documentation and/or other materials provided with the distribution.
13// 3. Neither the name of the University of Padova (SIGNET lab) nor the
14// names of its contributors may be used to endorse or promote products
15// derived from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29
39#include "uwoptical-phy.h"
41#include <float.h>
42
43const double K = 1.38 * 1.E-23; // Boltzmann constant
44const double q = 1.6 * 1.E-19; // electronic charge
45
46static class UwOpticalPhyClass : public TclClass
47{
48public:
50 : TclClass("Module/UW/OPTICAL/PHY")
51 {
52 }
53 TclObject *
54 create(int, const char *const *)
55 {
56 return (new UwOpticalPhy);
57 }
59
61 : lut_file_name_("")
62 , lut_token_separator_('\t')
63 , use_woss_(false)
64 , variable_temperature_(false)
65{
66 if (!MPhy_Bpsk::initialized) {
67 MPhy_Bpsk::modid =
68 MPhy::registerModulationType(OPTICAL_MODULATION_TYPE);
69 MPhy_Bpsk::initialized = true;
70 }
71 MPhy_Bpsk();
72 bind("Id_", &Id);
73 bind("Il_", &Il);
74 bind("R_", &R);
75 bind("S_", &S);
76 bind("T_", &T);
77 bind("Ar_", &Ar_);
78}
79
80int
81UwOpticalPhy::command(int argc, const char *const *argv)
82{
83 if (argc == 2) {
84 if (strcasecmp(argv[1], "useLUT") == 0) {
86 return TCL_OK;
87 } else if (strcasecmp(argv[1], "useWOSS") == 0) {
88 use_woss_ = true;
89 ((UwOpticalMPropagation *) propagation_)->setWoss(use_woss_);
90 return TCL_OK;
91 } else if (strcasecmp(argv[1], "setVariableTemperature") == 0) {
93 return TCL_OK;
94 }
95 } else if (argc == 3) {
96 if (strcasecmp(argv[1], "setLUTFileName") == 0) {
97 string tmp_ = ((char *) argv[2]);
98 if (tmp_.size() == 0) {
99 fprintf(stderr, "Empty string for the file name");
100 return TCL_ERROR;
101 }
102 lut_file_name_ = tmp_;
103 return TCL_OK;
104 } else if (strcasecmp(argv[1], "setLUTSeparator") == 0) {
105 string tmp_ = ((char *) argv[2]);
106 if (tmp_.size() == 0) {
107 fprintf(stderr, "Empty char for the file name");
108 return TCL_ERROR;
109 }
110 lut_token_separator_ = tmp_.at(0);
111 return TCL_OK;
112 }
113 }
114 return MPhy_Bpsk::command(argc, argv);
115}
116
117void
119{
120 hdr_MPhy *ph = HDR_MPHY(p);
121 if ((PktRx == 0) && (txPending == false)) {
122 double snr_dB = getSNRdB(p);
123 if (snr_dB > MPhy_Bpsk::getAcquisitionThreshold()) {
124 if (ph->modulationType == MPhy_Bpsk::modid) {
125 PktRx = p;
126 Phy2MacStartRx(p);
127 return;
128 } else {
129 if (debug_)
130 cout << "UwOpticalPhy::Drop Packet::Wrong modulation"
131 << endl;
132 }
133 } else {
134 if (debug_)
135 cout << "UwOpticalPhy::Drop Packet::Below Threshold : snrdb = "
136 << snr_dB
137 << ", threshold = " << MPhy_Bpsk::getAcquisitionThreshold()
138 << endl;
139 }
140 } else {
141 if (debug_)
142 cout << "UwOpticalPhy::Drop Packet::Synced onto another packet "
143 "PktRx = "
144 << PktRx << ", pending = " << txPending << endl;
145 }
146}
147
148double
150{
151 hdr_MPhy *ph = HDR_MPHY(p);
152 double snr_linear = pow((S * ph->Pr), 2) / ph->Pn;
153 return snr_linear ? 10 * log10(snr_linear) : -DBL_MAX;
154}
155
156double
158{
159 hdr_MPhy *ph = HDR_MPHY(p);
160 Position *dest = ph->dstPosition;
161 assert(dest);
162 double depth = use_woss_ ? -dest->getAltitude() : -dest->getZ();
163 return ((UwOpticalMPropagation *) propagation_)->getTemperature(depth);
164}
165
166double
168{
169 hdr_MPhy *ph = HDR_MPHY(p);
170 Position *dest = ph->dstPosition;
171 assert(dest);
172 double depth = use_woss_ ? -dest->getAltitude() : -dest->getZ();
173 double lut_value = lut_map.empty() ? 0 : lookUpLightNoiseE(depth);
174 double t = variable_temperature_ ? getVarTemperature(p) : T;
175 double il = (Il == IL_ILLEGAL ? S * ph->Pr : Il);
176 t = t > NOT_VARIABLE_TEMPERATURE ? t : T;
177 double circuit_noise = 2 * q * (Id + il) * ph->srcSpectralMask->getBandwidth() +
178 (4 * K * t * ph->srcSpectralMask->getBandwidth()) / R;
179 return circuit_noise + pow(lut_value * Ar_ * S, 2); // right now returns 0, due to not bias
180 // the snr calculation with unexpected
181 // values
182}
183
184void
186{
187 hdr_cmn *ch = HDR_CMN(p);
188 if (MPhy_Bpsk::PktRx != 0) {
189 if (MPhy_Bpsk::PktRx == p) {
190 if (interference_) {
191 double interference_power =
192 interference_->getInterferencePower(p);
193 if (interference_power == 0) {
194 // no interference
195 ch->error() = 0;
196 } else {
197 // at least one interferent packet
198 ch->error() = 1;
199 if (debug_)
200 cout << "UwOpticalPhy::endRx interference power = "
201 << interference_power << endl;
202 }
203 } else {
204 // no interference model set
205 ch->error() = 0;
206 }
207
208 sendUp(p);
209 PktRx = 0;
210 } else {
211 dropPacket(p);
212 }
213 } else {
214 dropPacket(p);
215 }
216}
217
218double
220{
221 // TODO: search noise Energy in the lookup table
222 DepthMap::iterator it = lut_map.lower_bound(depth);
223 if (it != lut_map.end() && it->first == depth) {
224 if (debug_)
225 std::cout << depth << " " << it->first << " " << it->second
226 << std::endl;
227 return it->second;
228 }
229 if (it == lut_map.end() || it == lut_map.begin()) {
230 if (debug_)
231 std::cout << depth << " Nothing returned depth = " << depth
232 << std::endl;
233
234 return NOT_FOUND_VALUE;
235 }
236 DepthMap::iterator u_it = it;
237 it--;
238 if (debug_)
239 std::cout << depth << " " << it->first << " " << it->second << " "
240 << u_it->first << " " << u_it->second << std::endl;
241 return linearInterpolator(
242 depth, it->first, it->second, u_it->first, u_it->second);
243}
244
245double
247 double x, double x1, double y1, double x2, double y2)
248{
249 assert(x1 != x2);
250 double m = (y1 - y2) / (x1 - x2);
251 double q = y1 - m * x1;
252 return m * x + q;
253}
254
255void
257{
258 ifstream input_file_;
259 string line_;
260 input_file_.open(lut_file_name_.c_str());
261 if (input_file_.is_open()) {
262 // skip first 2 lines
263 for (int i = 0; i < 2; ++i) {
264 std::getline(input_file_, line_);
265 }
266 double d;
267 double n;
268 while (std::getline(input_file_, line_)) {
269 ::std::stringstream line_stream(line_);
270 line_stream >> d;
271 line_stream.ignore(256, lut_token_separator_);
272 line_stream >> n;
273 lut_map[d] = n;
274 }
275 input_file_.close();
276 } else {
277 cerr << "Impossible to open file " << lut_file_name_ << endl;
278 }
279}
Class used to represents the UWOPTICAL_MPROPAGATION.
TclObject * create(int, const char *const *)
virtual void initializeLUT()
Inizialize LUT of c_variable values.
virtual double lookUpLightNoiseE(double depth)
DepthMap lut_map
Lookup table map of the solar noise versus the depth.
bool variable_temperature_
Flag to set whether the temperature is costant or varialbe with the depth.
virtual void startRx(Packet *p)
virtual double getNoisePower(Packet *p)
virtual void endRx(Packet *p)
string lut_file_name_
char lut_token_separator_
double getVarTemperature(Packet *p)
virtual double getSNRdB(Packet *p)
bool use_woss_
Flag to set whether woss is employed.
UwOpticalPhy()
Constructor of UwMultiPhy class.
virtual int command(int, const char *const *)
TCL command interpreter.
virtual double linearInterpolator(double x, double x1, double y1, double x2, double y2)
Definition of UwOpticalMPropagation class.
#define NOT_VARIABLE_TEMPERATURE
const double q
UwOpticalPhyClass class_module_optical
const double K
Definition of UwOptical class.
#define OPTICAL_MODULATION_TYPE
#define NOT_FOUND_VALUE
#define IL_ILLEGAL
const double q
const double K