From 6a31de4a0ec34edd341eaa2011534e62845e0a00 Mon Sep 17 00:00:00 2001
From: Tim Schubert <tim.schubert@tu-bs.de>
Date: Wed, 19 Aug 2020 19:41:25 +0200
Subject: [PATCH] Fix angle and delay computation

---
 examples/leo-delay-tracing-example.cc | 31 ++++++++++++++++-----------
 model/leo-propagation-loss-model.cc   | 27 ++++++++++++++++-------
 model/mock-channel.cc                 |  5 +++--
 3 files changed, 40 insertions(+), 23 deletions(-)

diff --git a/examples/leo-delay-tracing-example.cc b/examples/leo-delay-tracing-example.cc
index 3cdebdf..5a233ef 100644
--- a/examples/leo-delay-tracing-example.cc
+++ b/examples/leo-delay-tracing-example.cc
@@ -58,7 +58,7 @@ int main (int argc, char *argv[])
     }
 
   LeoGndNodeHelper ground;
-  NodeContainer stations = ground.Install ("contrib/leo/data/ground-stations/usa.waypoints");
+  NodeContainer stations = ground.Install ("contrib/leo/data/ground-stations/usa-60.waypoints");
 
   NetDeviceContainer islNet, utNet;
 
@@ -75,15 +75,15 @@ int main (int argc, char *argv[])
   // Install internet stack on nodes
   AodvHelper aodv;
   //aodv.Set ("HelloInterval", TimeValue (Minutes (1)));
-  aodv.Set ("TtlStart", UintegerValue (5));
-  aodv.Set ("TtlIncrement", UintegerValue (5));
-  aodv.Set ("TtlThreshold", UintegerValue (200));
-  aodv.Set ("RreqRetries", UintegerValue (1000));
-  aodv.Set ("RreqRateLimit", UintegerValue (100));
+  //aodv.Set ("TtlStart", UintegerValue (2));
+  //aodv.Set ("TtlIncrement", UintegerValue (1));
+  //aodv.Set ("TtlThreshold", UintegerValue (20));
+  //aodv.Set ("RreqRetries", UintegerValue (1000));
+  //aodv.Set ("RreqRateLimit", UintegerValue (1));
   //aodv.Set ("RerrRateLimit", UintegerValue (1));
   //aodv.Set ("ActiveRouteTimeout", TimeValue (Minutes (1)));
   //aodv.Set ("NextHopWait", TimeValue (MilliSeconds (200)));
-  aodv.Set ("NetDiameter", UintegerValue (100));
+  //aodv.Set ("NetDiameter", UintegerValue (300));
   //aodv.Set ("AllowedHelloLoss", UintegerValue (10000));
   //aodv.Set ("PathDiscoveryTime", TimeValue (Seconds (1)));
 
@@ -99,23 +99,28 @@ int main (int argc, char *argv[])
   ipv4.SetBase ("10.3.0.0", "255.255.0.0");
   Ipv4InterfaceContainer utIp = ipv4.Assign (utNet);
 
+  Ptr<Node> client = stations.Get (0);
+  Ptr<Node> server = stations.Get (1);
+
   // we want to ping terminals
   UdpServerHelper echoServer (9);
-  ApplicationContainer serverApps = echoServer.Install (stations.Get (100));
+  ApplicationContainer serverApps = echoServer.Install (server);
 
   // install a client on one of the terminals
   ApplicationContainer clientApps;
-  Address remote = stations.Get (100)->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ();//utIp.GetAddress (1, 0);
-  std::cout << "REMOTE=" << Ipv4Address::ConvertFrom (remote);
+  Address remote = server->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ();
   UdpClientHelper echoClient (remote, 9);
   echoClient.SetAttribute ("MaxPackets", UintegerValue (60));
   echoClient.SetAttribute ("Interval", TimeValue (Seconds (1)));
   echoClient.SetAttribute ("PacketSize", UintegerValue (1024));
-  clientApps.Add (echoClient.Install (stations.Get (103)));
+  clientApps.Add (echoClient.Install (client));
 
   Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::UdpServer/Rx",
   		   MakeCallback (&EchoRx));
 
+  std::cout << "LOCAL =" << client->GetId () << std::endl;
+  std::cout << "REMOTE=" << server->GetId () << std::endl;
+
   std::cout << "Context,Sequence Number,Timestamp,Delay" << std::endl;
 
   serverApps.Start (Seconds (1));
@@ -127,9 +132,9 @@ int main (int argc, char *argv[])
   Simulator::Run ();
   Simulator::Destroy ();
 
-  Ptr<UdpServer> server = StaticCast<UdpServer> (serverApps.Get (0));
+  Ptr<UdpServer> result = StaticCast<UdpServer> (serverApps.Get (0));
   std::cout << "Received,Lost" << std::endl
-    << server->GetReceived () << "," << server->GetLost () << std::endl;
+    << result->GetReceived () << "," << result->GetLost () << std::endl;
 
   return 0;
 }
diff --git a/model/leo-propagation-loss-model.cc b/model/leo-propagation-loss-model.cc
index b820be9..764cda2 100644
--- a/model/leo-propagation-loss-model.cc
+++ b/model/leo-propagation-loss-model.cc
@@ -23,13 +23,13 @@ LeoPropagationLossModel::GetTypeId (void)
     .AddConstructor<LeoPropagationLossModel> ()
     .AddAttribute ("MaxDistance",
                    "Cut-off distance for signal propagation",
-                   DoubleValue (3000),
+                   DoubleValue (4000.0),
                    MakeDoubleAccessor (&LeoPropagationLossModel::SetCutoffDistance,
 				       &LeoPropagationLossModel::GetCutoffDistance),
                    MakeDoubleChecker<double> ())
     .AddAttribute ("ElevationAngle",
                    "Cut-off angle for signal propagation",
-                   DoubleValue (M_PI / 9),
+                   DoubleValue (40.0),
                    MakeDoubleAccessor (&LeoPropagationLossModel::SetElevationAngle,
 				       &LeoPropagationLossModel::GetElevationAngle),
                    MakeDoubleChecker<double> ())
@@ -63,12 +63,23 @@ LeoPropagationLossModel::~LeoPropagationLossModel ()
 double
 LeoPropagationLossModel::GetAngle (Ptr<MobilityModel> a, Ptr<MobilityModel> b)
 {
-  Vector3D pa = a->GetPosition () - b->GetPosition ();
-  Vector3D pb = b->GetPosition ();
-  pb = Vector3D (-pb.x, -pb.y, -pb.z);
+  Vector3D x = a->GetPosition ();
+  Vector3D y = b->GetPosition ();
 
-  double prod = abs ((pa.x * pb.x) + (pa.y * pb.y) + (pa.z * pb.z));
-  double norm = pb.GetLength () * pa.GetLength ();
+  Vector3D pa, pb;
+  if (x.GetLength () < y.GetLength ())
+    {
+      pa = x - y;
+      pb = y;
+    }
+  else
+    {
+      pa = y - x;
+      pb = x;
+    }
+
+  double prod = (pa.x*-pb.x) + (pa.y*-pb.y) + (pa.z*-pb.z);
+  double norm = pa.GetLength () * pb.GetLength ();
 
   return acos (prod / norm);
 }
@@ -122,7 +133,7 @@ LeoPropagationLossModel::DoCalcRxPower (double txPowerDbm,
   // receiver loss and gain added at net device
   // P_{RX} = P_{TX} + G_{TX} - L_{TX} - L_{FS} - L_M + G_{RX} - L_{RX}
   double rxc = txPowerDbm - m_atmosphericLoss - m_freeSpacePathLoss - m_linkMargin;
-  NS_LOG_INFO ("LEO TRANSMIT: angle=" << angle <<";distance=" << distance << ";rxc=" << rxc);
+  NS_LOG_DEBUG ("LEO TRANSMIT: angle=" << angle <<";distance=" << distance << ";rxc=" << rxc);
 
   return rxc;
 }
diff --git a/model/mock-channel.cc b/model/mock-channel.cc
index 1256771..3e8845a 100644
--- a/model/mock-channel.cc
+++ b/model/mock-channel.cc
@@ -184,12 +184,13 @@ MockChannel::Deliver (
     	{
       	  // check if signal reaches destination
       	  rxPower = pLoss->CalcRxPower (txPower, srcMob, dstMob);
-      	  if (rxPower == -1000.0)
+      	  if (rxPower <= -1000.0)
     	    {
       	      return false;
     	    }
     	}
-      delay += GetPropagationDelay (srcMob, dstMob, txTime);
+      delay = GetPropagationDelay (srcMob, dstMob, txTime);
+      NS_LOG_DEBUG ("delay = "<<delay);
     }
 
   Simulator::ScheduleWithContext (dst->GetNode ()->GetId (),