Loading...
Searching...
No Matches
satellite-mobility-test.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: Sami Rantanen <sami.rantanen@magister.fi>
19 */
20
26
27// Include a header file from your module to test.
31
32#include "ns3/boolean.h"
33#include "ns3/config.h"
34#include "ns3/log.h"
35#include "ns3/mobility-helper.h"
36#include "ns3/simulator.h"
37#include "ns3/singleton.h"
38#include "ns3/string.h"
39#include "ns3/test.h"
40
41#include <iostream>
42
43using namespace ns3;
44
45static void
46SatCourseChange(std::string context, Ptr<const SatMobilityModel> position)
47{
48 GeoCoordinate pos = position->GetGeoPosition();
49 Vector pos2 = pos.ToVector();
50
51 // sets number of decimal places
52
53 std::cout << std::cout.precision(15) << std::cout.setf(std::ios::fixed, std::ios::floatfield)
54 << Simulator::Now() << ", x=" << pos2.x << ", y=" << pos2.y << ", z=" << pos2.z
55 << ", longitude=" << pos.GetLongitude() << ", latitude=" << pos.GetLatitude()
56 << ", altitude=" << pos.GetAltitude() << std::endl;
57}
58
76class SatMobilityRandomTestCase : public TestCase
77{
78 public:
81
82 private:
83 virtual void DoRun(void);
84};
85
87 : TestCase("Test satellite mobility (constant model) with random box allocator.")
88{
89}
90
94
95void
97{
98 // Set simulation output details
99 SatEnvVariables::GetInstance()->DoInitialize();
100 SatEnvVariables::GetInstance()->SetOutputVariables("test-sat-mobility", "random", true);
101
102 MobilityHelper mobility;
103 mobility.SetPositionAllocator("ns3::SatRandomBoxPositionAllocator",
104 "Latitude",
105 StringValue("ns3::UniformRandomVariable[Min=-45.0|Max=80.0]"),
106 "Longitude",
107 StringValue("ns3::UniformRandomVariable[Min=-10.0|Max=180.0]"),
108 "Altitude",
109 StringValue("ns3::UniformRandomVariable[Min=0.0|Max=100.0]"));
110
111 NodeContainer c;
112 c.Create(10);
113
114 mobility.SetMobilityModel("ns3::SatConstantPositionMobilityModel");
115 mobility.Install(c);
116
117 for (uint32_t i = 0; i < c.GetN(); i++)
118 {
119 c.Get(i)->GetObject<SatMobilityModel>()->TraceConnect("SatCourseChange",
120 "SatCourseChangeTrace",
121 MakeCallback(SatCourseChange));
122 Ptr<SatMobilityModel> model = c.Get(i)->GetObject<SatMobilityModel>();
123 GeoCoordinate pos = model->GetGeoPosition();
124
125 // check that position is in limit
126 NS_TEST_ASSERT_MSG_LT(pos.GetLatitude(), 80.1, "Latitude is too big.");
127 NS_TEST_ASSERT_MSG_LT(pos.GetLongitude(), 180.1, "Longitude is too big.");
128 NS_TEST_ASSERT_MSG_LT(pos.GetAltitude(), 100.1, "Altitude is too big.");
129
130 NS_TEST_ASSERT_MSG_GT(pos.GetLatitude(), -45.1, "Latitude is too small.");
131 NS_TEST_ASSERT_MSG_GT(pos.GetLongitude(), -10.1, "Longitude is too small.");
132 NS_TEST_ASSERT_MSG_GT(pos.GetAltitude(), -0.1, "Altitude is too small.");
133 }
134
135 Simulator::Destroy();
136
137 SatEnvVariables::GetInstance()->DoDispose();
138}
139
163class SatMobilityList1TestCase : public TestCase
164{
165 public:
168
169 private:
170 virtual void DoRun(void);
171};
172
174 : TestCase(
175 "Test satellite mobility (constant model, no conversion) with list position allocator.")
176{
177}
178
182
183void
185{
186 // Set simulation output details
187 SatEnvVariables::GetInstance()->DoInitialize();
188 SatEnvVariables::GetInstance()->SetOutputVariables("test-sat-mobility", "list1", true);
189
190 MobilityHelper mobility;
191 Ptr<SatListPositionAllocator> positionAlloc = CreateObject<SatListPositionAllocator>();
192
193 // set positions to allocator
194 int j = 0;
195 for (int i = -180; i <= 180; i += 30)
196 {
197 positionAlloc->Add(GeoCoordinate(i / 2, i, i * 30));
198 j++;
199 }
200
201 // create nodes
202 NodeContainer c;
203 c.Create(j);
204
205 // set allocator and mobility model to helper
206 mobility.SetPositionAllocator(positionAlloc);
207 mobility.SetMobilityModel("ns3::SatConstantPositionMobilityModel");
208
209 // install mobility to nodes
210 mobility.Install(c);
211
212 // now check that model is set and position is correct
213 for (int i = 0; i < j; i++)
214 {
215 Ptr<SatMobilityModel> model = c.Get(i)->GetObject<SatMobilityModel>();
216 GeoCoordinate pos = model->GetGeoPosition();
217
218 double longitude = -180 + i * 30;
219 double latitude = longitude / 2;
220 double altitude = longitude * 30;
221
222 // check that position is equal with set one
223 NS_TEST_ASSERT_MSG_EQ(pos.GetLatitude(), latitude, "Latitude is different.");
224 NS_TEST_ASSERT_MSG_EQ(pos.GetLongitude(), longitude, "Longitude is different.");
225 NS_TEST_ASSERT_MSG_EQ(pos.GetAltitude(), altitude, "Altitude is different.");
226 }
227
228 Simulator::Destroy();
229
230 SatEnvVariables::GetInstance()->DoDispose();
231}
232
260class SatMobilityList2TestCase : public TestCase
261{
262 public:
265
266 private:
267 virtual void DoRun(void);
268};
269
271 : TestCase("Test satellite mobility (constant model, conversion) with list position allocator.")
272{
273}
274
278
279void
281{
282 // Set simulation output details
283 SatEnvVariables::GetInstance()->DoInitialize();
284 SatEnvVariables::GetInstance()->SetOutputVariables("test-sat-mobility", "list2", true);
285
286 // now do same with Cartesian coordinates used by SatMobilityModel SetPosition
287 // and SatPositionAllocator GetNext
288 Config::SetDefault("ns3::SatMobilityModel::AsGeoCoordinates", BooleanValue(false));
289 Config::SetDefault("ns3::SatPositionAllocator::AsGeoCoordinates", BooleanValue(false));
290
291 MobilityHelper mobility;
292 Ptr<SatListPositionAllocator> positionAlloc = CreateObject<SatListPositionAllocator>();
293
294 positionAlloc->Add(GeoCoordinate(30, 45, 1000));
295
296 // create node
297 NodeContainer c;
298 c.Create(1);
299
300 // set allocator and mobility model to helper
301 mobility.SetPositionAllocator(positionAlloc);
302 mobility.SetMobilityModel("ns3::SatConstantPositionMobilityModel");
303
304 // install mobility to nodes
305 mobility.Install(c);
306
307 // now check that model is set and position is correct but not too accurate because now
308 // is made conversion from Cartesian to Geodetic and back
309 // lon=39, lat=45 alt=1000 is value which doesn't converts exact
310 Ptr<SatMobilityModel> model = c.Get(0)->GetObject<SatMobilityModel>();
311 GeoCoordinate pos = model->GetGeoPosition();
312
313 // check that position is close enough
314 NS_TEST_ASSERT_MSG_LT(std::abs(pos.GetLatitude() - 30),
315 0.000001,
316 "Latitude difference too big!");
317 NS_TEST_ASSERT_MSG_LT(std::abs(pos.GetLongitude() - 45),
318 0.000001,
319 "Longitude difference too big!");
320 NS_TEST_ASSERT_MSG_LT(std::abs(pos.GetAltitude() - 1000),
321 0.000001,
322 "Altitude difference too big!");
323
324 Simulator::Destroy();
325
326 SatEnvVariables::GetInstance()->DoDispose();
327}
328
333class SatMobilityTestSuite : public TestSuite
334{
335 public:
337};
338
340 : TestSuite("sat-mobility-test", Type::SYSTEM)
341{
342 AddTestCase(new SatMobilityRandomTestCase, TestCase::Duration::QUICK);
343 AddTestCase(new SatMobilityList1TestCase, TestCase::Duration::QUICK);
344 AddTestCase(new SatMobilityList2TestCase, TestCase::Duration::QUICK);
345}
346
347// Do allocate an instance of this TestSuite
Test case to unit test satellite mobility's position setting from list position allocator.
Test case to unit test satellite mobility's position setting from list position allocator.
Test case to unit test satellite mobility's position setting from random box position allocator.
Test suite for Satellite mobility unit test cases.
GeoCoordinate class is used to store and operate with geodetic coordinates.
double GetAltitude() const
Gets altitude value of coordinate.
double GetLatitude() const
Gets latitude value of coordinate.
double GetLongitude() const
Gets longitude value of coordinate.
Vector ToVector() const
Converts Geodetic coordinates to Cartesian coordinates.
static Ptr< SatEnvVariables > GetInstance()
Keep track of the current position and velocity of an object in satellite network.
SatArqSequenceNumber is handling the sequence numbers for the ARQ process.
static SatMobilityTestSuite satSatMobilityTestSuite
static void SatCourseChange(std::string context, Ptr< const SatMobilityModel > position)