sat-markov-fading-trace-example.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Magister Solutions
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  */
21 
22 #include "ns3/core-module.h"
23 #include "ns3/gnuplot.h"
24 #include "ns3/satellite-module.h"
25 
26 #include <fstream>
27 #include <vector>
28 
29 NS_LOG_COMPONENT_DEFINE("sat-markov-trace-example");
30 
31 namespace ns3
32 {
33 
52 class SatMarkovFadingExamplePlot : public Object
53 {
54  public:
56  static TypeId GetTypeId();
57  void Run();
58 
59  private:
60  Gnuplot2dDataset GetGnuplotDataset(std::string title);
61  Gnuplot GetGnuplot(std::string outputName, std::string title);
62  void FadingTraceCb(std::string context,
63  double time,
65  double fadingValue);
66  double GetElevation();
67  double GetVelocity();
68 
69  double m_elevation;
70  double m_velocity;
71  std::vector<std::pair<double, double>> m_fadingValues;
72 };
73 
74 NS_OBJECT_ENSURE_REGISTERED(SatMarkovFadingExamplePlot);
75 
76 TypeId
78 {
79  static TypeId tid = TypeId("ns3::SatMarkovFadingExamplePlot")
80  .SetParent<Object>()
81  .AddConstructor<SatMarkovFadingExamplePlot>();
82  return tid;
83 }
84 
86 {
87  m_elevation = 45;
88  m_velocity = 0;
89 }
90 
91 void
93  double time,
95  double fadingValue)
96 {
97  std::cout << time << " " << chType << " " << 10 * log10(fadingValue) << std::endl;
98  m_fadingValues.push_back(std::make_pair(time, 10 * log10(fadingValue)));
99 }
100 
101 double
103 {
104  return m_elevation;
105 }
106 
107 double
109 {
110  return m_velocity;
111 }
112 
113 Gnuplot2dDataset
115 {
116  Gnuplot2dDataset ret;
117  ret.SetTitle(title);
118  ret.SetStyle(Gnuplot2dDataset::LINES);
119 
120  for (uint32_t i = 0; i < m_fadingValues.size(); i++)
121  {
122  ret.Add(m_fadingValues[i].first, m_fadingValues[i].second);
123  }
124  return ret;
125 }
126 
127 Gnuplot
128 SatMarkovFadingExamplePlot::GetGnuplot(std::string outputName, std::string title)
129 {
130  Gnuplot ret(outputName + ".png");
131  ret.SetTitle(title);
132  ret.SetTerminal("png");
133  ret.SetLegend("Time (s)", "Fading (dB)");
134  ret.AppendExtra("set key top right");
135  ret.AppendExtra("set grid xtics mxtics ytics");
136  return ret;
137 }
138 
139 void
141 {
143  Config::SetDefault("ns3::SatEnvVariables::SimulationCampaignName",
144  StringValue("example-markov-fading-trace"));
145  Config::SetDefault("ns3::SatEnvVariables::SimulationTag", StringValue(""));
146  Config::SetDefault("ns3::SatEnvVariables::EnableSimulationOutputOverwrite", BooleanValue(true));
147 
149  Ptr<SatMarkovConf> markovConf = CreateObject<SatMarkovConf>();
150 
152  MakeCallback(&SatMarkovFadingExamplePlot::GetElevation, this);
154  MakeCallback(&SatMarkovFadingExamplePlot::GetVelocity, this);
155 
157  Ptr<SatMarkovContainer> markovContainer =
158  CreateObject<SatMarkovContainer>(markovConf, elevationCb, velocityCb);
159 
160  markovContainer->TraceConnect("FadingTrace",
161  "The trace for fading values",
162  MakeCallback(&SatMarkovFadingExamplePlot::FadingTraceCb, this));
163 
164  Address macAddress;
165 
167  for (uint32_t i = 0; i < 100000; i++)
168  {
169  Simulator::Schedule(MilliSeconds(1 * i),
171  markovContainer,
172  macAddress,
174  }
175 
176  Simulator::Schedule(MilliSeconds(0),
178  markovContainer,
179  0,
180  0);
181  Simulator::Schedule(MilliSeconds(20000),
183  markovContainer,
184  0,
185  1);
186  Simulator::Schedule(MilliSeconds(40000),
188  markovContainer,
189  0,
190  2);
191  Simulator::Schedule(MilliSeconds(60000),
193  markovContainer,
194  0,
195  0);
196  Simulator::Schedule(MilliSeconds(80000),
198  markovContainer,
199  0,
200  1);
201 
202  Simulator::Run();
203 
204  Gnuplot2dDataset dataset = GetGnuplotDataset("Markov Fading Trace");
205  Gnuplot plot = GetGnuplot("markov_fading_trace", "Markov Fading Trace");
206  plot.AddDataset(dataset);
207 
208  std::string plotFileName = "markov_fading_trace.plt";
209  std::ofstream plotFile(plotFileName.c_str());
210  plot.GenerateOutput(plotFile);
211  plotFile.close();
212 
213  std::cout << "Output file written: " << plotFileName << std::endl;
214 
215  int result = system("gnuplot markov_fading_trace.plt");
216 
217  if (result < 0)
218  {
219  std::cout
220  << "Unable to open shell process for Gnuplot file conversion, conversion not done!"
221  << std::endl;
222  }
223  else
224  {
225  std::cout << "Output file converted to: markov_fading_trace.png" << std::endl;
226  }
227 
228  Simulator::Destroy();
229 }
230 
231 } // namespace ns3
232 
233 int
234 main(int argc, char* argv[])
235 {
236  ns3::Ptr<ns3::SatMarkovFadingExamplePlot> stub;
237  stub = ns3::CreateObject<ns3::SatMarkovFadingExamplePlot>();
238  ns3::Config::RegisterRootNamespaceObject(stub);
239  stub->Run();
240 
241  return 0;
242 }
Callback< double > VelocityCallback
Gets velocity in m/s.
Callback< double > ElevationCallback
Gets elevation angle in degrees.
ChannelType_t
Types of channel.
double DoGetFading(Address macAddress, SatEnums::ChannelType_t channeltype)
Function for getting the fading.
void LockToSetAndState(uint32_t newSet, uint32_t newState)
Function for locking the parameter set and state.
void FadingTraceCb(std::string context, double time, SatEnums::ChannelType_t chType, double fadingValue)
Gnuplot GetGnuplot(std::string outputName, std::string title)
Gnuplot2dDataset GetGnuplotDataset(std::string title)
std::vector< std::pair< double, double > > m_fadingValues
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.