satellite-markov-container.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013 Magister Solutions Ltd.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Frans Laakso <frans.laakso@magister.fi>
19  */
20 
22 
23 #include "satellite-utils.h"
24 
25 #include <ns3/log.h>
26 
27 namespace ns3
28 {
29 
30 NS_OBJECT_ENSURE_REGISTERED(SatMarkovContainer);
31 NS_LOG_COMPONENT_DEFINE("SatMarkovContainer");
32 
33 TypeId
35 {
36  static TypeId tid =
37  TypeId("ns3::SatMarkovContainer")
38  .SetParent<SatBaseFading>()
39  .AddConstructor<SatMarkovContainer>()
40  .AddTraceSource("FadingTrace",
41  "The trace for fading values",
42  MakeTraceSourceAccessor(&SatMarkovContainer::m_fadingTrace),
43  "ns3::SatMarkovContainer::FadingTraceCallback");
44  return tid;
45 }
46 
48  : m_markovModel(NULL),
49  m_markovConf(NULL),
50  m_fader_up(NULL),
51  m_fader_down(NULL),
52  m_numOfStates(),
53  m_numOfSets(),
54  m_currentSet(),
55  m_currentState(),
56  m_cooldownPeriodLength(),
57  m_minimumPositionChangeInMeters(),
58  m_latestCalculatedFadingValue_up(),
59  m_latestCalculatedFadingValue_down(),
60  m_latestCalculationTime_up(),
61  m_latestCalculationTime_down(),
62  m_enableSetLock(false),
63  m_enableStateLock(false),
64  m_velocity(),
65  m_latestStateChangeTime(),
66  m_useDecibels(false)
67 {
68  NS_LOG_FUNCTION(this);
69  NS_FATAL_ERROR("SatMarkovContainer::SatMarkovContainer - Constructor not in use");
70 }
71 
72 SatMarkovContainer::SatMarkovContainer(Ptr<SatMarkovConf> markovConf,
75  : m_markovModel(NULL),
76  m_markovConf(markovConf),
77  m_fader_up(NULL),
78  m_fader_down(NULL),
79  m_numOfStates(markovConf->GetStateCount()),
80  m_numOfSets(markovConf->GetNumOfSets()),
81  m_currentState(markovConf->GetInitialState()),
82  m_cooldownPeriodLength(markovConf->GetCooldownPeriod()),
83  m_minimumPositionChangeInMeters(markovConf->GetMinimumPositionChange()),
84  m_latestCalculatedFadingValue_up(0.0),
85  m_latestCalculatedFadingValue_down(0.0),
86  m_latestCalculationTime_up(Now()),
87  m_latestCalculationTime_down(Now()),
88  m_enableSetLock(false),
89  m_enableStateLock(false),
90  m_velocity(velocity),
91  m_latestStateChangeTime(Now()),
92  m_currentElevation(elevation),
93  m_useDecibels(markovConf->AreDecibelsUsed())
94 {
95  NS_LOG_FUNCTION(this);
96 
98  m_markovModel = CreateObject<SatMarkovModel>(m_numOfStates, m_currentState);
99 
101  m_currentSet = m_markovConf->GetProbabilitySetID(m_currentElevation());
103  m_markovModel->DoTransition();
104 
106  CreateFaders(m_markovConf->GetFaderType());
107 
111 
112  NS_LOG_INFO("Creating SatMarkovContainer, States: "
113  << m_numOfStates << " Elevation: " << m_currentElevation()
114  << " Current Set ID: " << m_currentSet
115  << " Cool down Period Length In Seconds: " << m_cooldownPeriodLength.GetSeconds()
116  << " Minimum Position Change In Meters: " << m_minimumPositionChangeInMeters);
117 }
118 
120 {
121  NS_LOG_FUNCTION(this);
122 
123  Reset();
124 }
125 
126 void
128 {
129  NS_LOG_FUNCTION(this);
130 
131  Reset();
132  SatBaseFading::DoDispose();
133 }
134 
135 void
137 {
138  NS_LOG_FUNCTION(this);
139 
140  m_markovConf = NULL;
141  m_fader_up = NULL;
142  m_fader_down = NULL;
143  m_markovModel = NULL;
144 
145  m_currentElevation.Nullify();
146  m_velocity.Nullify();
147 }
148 
149 void
151 {
152  NS_LOG_FUNCTION(this << faderType);
153 
154  switch (faderType)
155  {
157  m_fader_up = CreateObject<SatLooModel>(m_markovConf->GetLooConf(),
159  m_currentSet,
161  m_fader_down = CreateObject<SatLooModel>(m_markovConf->GetLooConf(),
163  m_currentSet,
165  break;
166  }
168  m_fader_up = CreateObject<SatRayleighModel>(m_markovConf->GetRayleighConf(),
169  m_currentSet,
171  m_fader_down = CreateObject<SatRayleighModel>(m_markovConf->GetRayleighConf(),
172  m_currentSet,
174  break;
175  }
176  default: {
177  NS_FATAL_ERROR("SatMarkovContainer::CreateFaders - Invalid fader type");
178  }
179  }
180 }
181 
182 double
184 {
185  NS_LOG_FUNCTION(this << channelType);
186 
187  double fadingValue;
188 
189  NS_LOG_INFO("Getting fading");
190 
191  if (HasCooldownPeriodPassed(channelType))
192  {
193  NS_LOG_INFO("Cool down period has passed, calculating new fading value");
194 
195  if (m_velocity() > 0)
196  {
197  EvaluateStateChange(channelType);
198  }
199  fadingValue = CalculateFading(channelType);
200  }
201  else
202  {
203  NS_LOG_INFO("Cool down period in effect, using old fading value");
204  fadingValue = GetCachedFadingValue(channelType);
205  }
206 
207  m_fadingTrace(Now().GetSeconds(), channelType, fadingValue);
208 
209  return fadingValue;
210 }
211 
212 double
214 {
215  NS_LOG_FUNCTION(this << channelType);
216 
217  switch (channelType)
218  {
222  }
226  }
227  default: {
228  NS_FATAL_ERROR("SatMarkovContainer::GetCachedFadingValue - Invalid channel type");
229  }
230  }
231  return 0;
232 }
233 
234 void
236 {
237  NS_LOG_FUNCTION(this);
238 
240  {
241  uint32_t newSetId;
242 
243  if (!m_enableSetLock)
244  {
245  newSetId = m_markovConf->GetProbabilitySetID(m_currentElevation());
246 
247  if (m_currentSet != newSetId)
248  {
249  NS_LOG_INFO("Elevation: " << m_currentElevation() << ", set ID [old, new]: ["
250  << m_currentSet << "," << newSetId << "]");
251 
252  m_currentSet = newSetId;
254  }
255  }
256 
257  if (!m_enableStateLock)
258  {
259  m_latestStateChangeTime = Now();
260  m_markovModel->DoTransition();
261  }
262  }
263 }
264 
265 bool
267 {
268  NS_LOG_FUNCTION(this << channelType);
269 
270  switch (channelType)
271  {
274  if ((Now().GetSeconds() - m_latestCalculationTime_up.GetSeconds()) >
275  m_cooldownPeriodLength.GetSeconds())
276  {
277  return true;
278  }
279  break;
280  }
283  if ((Now().GetSeconds() - m_latestCalculationTime_down.GetSeconds()) >
284  m_cooldownPeriodLength.GetSeconds())
285  {
286  return true;
287  }
288  break;
289  }
290  default: {
291  NS_FATAL_ERROR("SatMarkovContainer::HasCooldownPeriodPassed - Invalid channel type");
292  }
293  }
294  return false;
295 }
296 
297 void
299 {
300  NS_LOG_FUNCTION(this << set);
301 
302  std::vector<std::vector<double>> probabilities = m_markovConf->GetElevationProbabilities(set);
303 
304  NS_LOG_INFO("Updating probabilities...");
305 
306  for (uint32_t i = 0; i < m_numOfStates; ++i)
307  {
308  for (uint32_t j = 0; j < m_numOfStates; ++j)
309  {
310  m_markovModel->SetProbability(i, j, probabilities[i][j]);
311  }
312  NS_LOG_INFO("------");
313  }
314 }
315 
316 double
318 {
319  NS_LOG_FUNCTION(this << channelType);
320 
321  double fadingValue;
322 
323  if (!m_enableStateLock)
324  {
325  m_currentState = m_markovModel->GetState();
326  }
327 
328  NS_ASSERT((m_currentState >= 0) && (m_currentState < m_numOfStates));
329 
330  switch (channelType)
331  {
334  m_fader_up->UpdateParameters(m_currentSet, m_currentState);
335 
336  if (m_useDecibels)
337  {
338  m_latestCalculatedFadingValue_up = m_fader_down->GetChannelGainDb();
339  }
340  else
341  {
343  }
344 
345  NS_LOG_INFO("Calculated feeder fading value " << m_latestCalculatedFadingValue_up);
346 
348 
349  fadingValue = m_latestCalculatedFadingValue_up;
350  break;
351  }
354  m_fader_down->UpdateParameters(m_currentSet, m_currentState);
355 
356  if (m_useDecibels)
357  {
358  m_latestCalculatedFadingValue_down = m_fader_down->GetChannelGainDb();
359  }
360  else
361  {
363  }
364 
365  NS_LOG_INFO("Calculated return fading value " << m_latestCalculatedFadingValue_down);
366 
368 
370  break;
371  }
372  default: {
373  NS_FATAL_ERROR("SatMarkovContainer::CalculateFading - Invalid channel type");
374  }
375  }
376  return fadingValue;
377 }
378 
379 void
380 SatMarkovContainer::LockToSetAndState(uint32_t newSet, uint32_t newState)
381 {
382  NS_LOG_FUNCTION(this << newSet << " " << newState);
383 
384  if (newState >= m_numOfStates)
385  {
386  NS_FATAL_ERROR("SatMarkovContainer::LockToSetAndState - Invalid state");
387  }
388  if (newSet >= m_numOfSets)
389  {
390  NS_FATAL_ERROR("SatMarkovContainer::LockToSetAndState - Invalid set");
391  }
392 
393  m_currentSet = newSet;
394  m_currentState = newState;
395 
397 
398  m_enableSetLock = true;
399  m_enableStateLock = true;
400 }
401 
402 void
404 {
405  NS_LOG_FUNCTION(this << newSet);
406 
407  if (newSet >= m_numOfSets)
408  {
409  NS_FATAL_ERROR("SatMarkovContainer::LockToSet - Invalid set");
410  }
411 
412  m_currentSet = newSet;
413 
415 
416  m_enableSetLock = true;
417  m_enableStateLock = false;
418 }
419 
420 void
422 {
423  NS_LOG_FUNCTION(this);
424 
425  uint32_t newSet = 0;
426  uint32_t newState = 0;
427 
428  if (m_numOfSets > 1)
429  {
430  newSet = (rand() % (m_numOfSets - 1));
431  }
432 
433  if (m_numOfStates > 1)
434  {
435  newState = (rand() % (m_numOfStates - 1));
436  }
437 
438  LockToSetAndState(newSet, newState);
439 }
440 
441 void
443 {
444  NS_LOG_FUNCTION(this);
445 
446  LockToSet(set);
447 
448  uint32_t newState = 0;
449 
450  if (m_numOfStates > 1)
451  {
452  newState = (rand() % (m_numOfStates - 1));
453  }
454 
455  m_currentState = newState;
456 
457  m_enableStateLock = true;
458 }
459 
460 void
462 {
463  NS_LOG_FUNCTION(this);
464 
465  m_enableSetLock = false;
466  m_enableStateLock = false;
467 }
468 
469 double
471 {
472  NS_LOG_FUNCTION(this);
473 
474  return (Now().GetSeconds() - m_latestStateChangeTime.GetSeconds()) * m_velocity();
475 }
476 
477 } // namespace ns3
Base class for fading models such as Markov-based fading or fading trace.
Callback< double > VelocityCallback
Gets velocity in m/s.
Callback< double > ElevationCallback
Gets elevation angle in degrees.
ChannelType_t
Types of channel.
MarkovFaderType_t
Possible types of Markov state faders.
double DoGetFading(Address macAddress, SatEnums::ChannelType_t channeltype)
Function for getting the fading.
void UpdateProbabilities(uint32_t set)
Function for updating the state change probabilities.
Time m_latestCalculationTime_up
Latest calculation time for uplink fading value.
bool m_useDecibels
Defines whether the calculations should return the fading value in decibels or not.
double m_minimumPositionChangeInMeters
Minimum state change distance in meters.
void UnlockSetAndState()
Function for unlocking the parameter set and state.
void EvaluateStateChange(SatEnums::ChannelType_t channelType)
Function for evaluating state change.
void DoDispose()
Do needed dispose actions.
uint32_t m_currentState
Current state.
double m_latestCalculatedFadingValue_down
Latest calculated downlink fading value.
Time m_latestCalculationTime_down
Latest calculation time for downlink fading value.
uint32_t m_numOfSets
Number of parameter sets available.
double GetCachedFadingValue(SatEnums::ChannelType_t channelType)
Function for getting the cached fading values.
double m_latestCalculatedFadingValue_up
Latest calculated uplink fading value.
void LockToSet(uint32_t newSet)
Function for locking the parameter set.
void RandomizeLockedSetAndState()
Function for locking the parameter set and state to random values.
Ptr< SatMarkovConf > m_markovConf
Markoc model configuration.
TracedCallback< double, SatEnums::ChannelType_t, double > m_fadingTrace
Fading trace function.
bool m_enableSetLock
Variable for disabling the parameter set change.
uint32_t m_numOfStates
Number of states available.
double CalculateFading(SatEnums::ChannelType_t channelType)
Function for calculating the fading value.
VelocityCallback m_velocity
Node movement velocity.
void Reset()
Clear used variables.
double CalculateDistanceSinceLastStateChange()
Function for calculating the distance since latest state change position.
static TypeId GetTypeId(void)
NS-3 function for type id.
bool HasCooldownPeriodPassed(SatEnums::ChannelType_t channelType)
Function for checking whether the cooldown period has passed.
uint32_t m_currentSet
Current parameter set.
ElevationCallback m_currentElevation
Current elevation value.
bool m_enableStateLock
Variable for disabling state changes.
void RandomizeLockedState(uint32_t set)
Function for locking the state to random value.
Ptr< SatBaseFader > m_fader_up
Uplink fader.
Time m_cooldownPeriodLength
Cooldown period length in time.
Time m_latestStateChangeTime
Latest calculation time for state change.
Ptr< SatBaseFader > m_fader_down
Downlink fader.
void LockToSetAndState(uint32_t newSet, uint32_t newState)
Function for locking the parameter set and state.
void CreateFaders(SatMarkovConf::MarkovFaderType_t faderType)
Function for creating the Markov state faders.
Ptr< SatMarkovModel > m_markovModel
Markov model object.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.