Skip to content

Commit

Permalink
parametric-speed-drone-mobility-model: Extended to Geocentric
Browse files Browse the repository at this point in the history
  • Loading branch information
GiovanniGrieco committed Jul 13, 2023
1 parent 1af075e commit 980a6fe
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 41 deletions.
201 changes: 168 additions & 33 deletions src/mobility/parametric-speed/parametric-speed-drone-mobility-model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
*/
#include "parametric-speed-drone-mobility-model.h"

#include <ns3/boolean.h>
#include <ns3/double-vector.h>
#include <ns3/double.h>
#include <ns3/integer.h>
#include <ns3/log.h>
#include <ns3/object-vector.h>
#include <ns3/simulator.h>
Expand All @@ -36,20 +38,29 @@ ParametricSpeedDroneMobilityModel::GetTypeId()

static TypeId tid =
TypeId("ns3::ParametricSpeedDroneMobilityModel")
.SetParent<MobilityModel>()
.SetParent<GeocentricMobilityModel>()
.SetGroupName("Mobility")
.AddConstructor<ParametricSpeedDroneMobilityModel>()
.AddAttribute(
"UseGeodedicSystem",
"Enable the use of Earth geographic coordinate system, instead of the classical "
"cartesian one.",
BooleanValue(false),
MakeBooleanAccessor(&ParametricSpeedDroneMobilityModel::m_useGeodedicSystem),
MakeBooleanChecker())
.AddAttribute(
"SpeedCoefficients",
"Coefficients to construct speed curve.",
DoubleVectorValue(),
MakeDoubleVectorAccessor(&ParametricSpeedDroneMobilityModel::SetSpeedCoefficients),
MakeDoubleVectorChecker())
.AddAttribute("FlightPlan",
"The ideal trajectory that the drone should run across.",
FlightPlanValue(),
MakeFlightPlanAccessor(&ParametricSpeedDroneMobilityModel::m_flightPlan),
MakeFlightPlanChecker())
.AddAttribute(
"FlightPlan",
"The ideal trajectory that the drone should run across.",
FlightPlanValue(),
MakeFlightPlanAccessor(&ParametricSpeedDroneMobilityModel::GetFlightPlan,
&ParametricSpeedDroneMobilityModel::SetFlightPlan),
MakeFlightPlanChecker())
.AddAttribute("CurveStep",
"The step of the curve to generate. Lower step means more points "
"generated, hence higher resolution.",
Expand All @@ -62,7 +73,8 @@ ParametricSpeedDroneMobilityModel::GetTypeId()

ParametricSpeedDroneMobilityModel::ParametricSpeedDroneMobilityModel()
: m_flightParams{{}},
m_lastUpdate{-1}
m_lastUpdate{-1},
m_useGeodedicSystem{false}
{
NS_LOG_FUNCTION(this);
}
Expand All @@ -71,43 +83,143 @@ ParametricSpeedDroneMobilityModel::~ParametricSpeedDroneMobilityModel()
{
}

void
ParametricSpeedDroneMobilityModel::Update() const
FlightPlan
ParametricSpeedDroneMobilityModel::ProjectedToGeographicCoordinates(
const FlightPlan& flightPlan,
GeographicPositions::EarthSpheroidType earthType)
{
NS_LOG_FUNCTION(this);
NS_LOG_FUNCTION(flightPlan << earthType);

const Time t = Simulator::Now();
if (t.Compare(m_lastUpdate) <= 0)
FlightPlan fpOut;
for (const auto& point : flightPlan)
{
NS_LOG_LOGIC("Update is being suppressed.");
return;
const auto geographicPosition =
GeographicPositions::ProjectedToGeographicCoordinates(point->GetPosition(), earthType);
NS_LOG_LOGIC("Translated position from Projected "
<< point->GetPosition() << " to Geographic " << geographicPosition);
auto p = CreateObjectWithAttributes<ProtoPoint>("Position",
VectorValue(geographicPosition),
"Interest",
IntegerValue(point->GetInterest()),
"RestTime",
TimeValue(point->GetRestTime()));
fpOut.Add(p);
}

m_lastUpdate = t;
return fpOut;
}

m_planner.Update(t);
m_position = m_planner.GetPosition();
m_velocity = m_planner.GetVelocity();
FlightPlan
ParametricSpeedDroneMobilityModel::GeographicToProjectedCoordinates(
const FlightPlan& flightPlan,
GeographicPositions::EarthSpheroidType earthType)
{
NS_LOG_FUNCTION(flightPlan << earthType);

NotifyCourseChange();
FlightPlan fpOut;
for (const auto& point : flightPlan)
{
const auto projectedPosition =
GeographicPositions::GeographicToProjectedCoordinates(point->GetPosition(), earthType);
NS_LOG_LOGIC("Translated position from Geographic "
<< point->GetPosition() << " to Projected " << projectedPosition);
auto p = CreateObjectWithAttributes<ProtoPoint>("Position",
VectorValue(projectedPosition),
"Interest",
IntegerValue(point->GetInterest()),
"RestTime",
TimeValue(point->GetRestTime()));
fpOut.Add(p);
}

return fpOut;
}

Vector
ParametricSpeedDroneMobilityModel::DoGetPosition() const
void
ParametricSpeedDroneMobilityModel::DoInitialize()
{
NS_LOG_FUNCTION(this);

m_planner = Planner<ParametricSpeedParam, ParametricSpeedFlight>(m_flightPlan,
m_flightParams,
m_curveStep);

MobilityModel::DoInitialize();
}

void
ParametricSpeedDroneMobilityModel::DoDispose()
{
NS_LOG_FUNCTION(this);
NS_LOG_LOGIC("position: " << m_position);

MobilityModel::DoDispose();
}

Vector
ParametricSpeedDroneMobilityModel::DoGetPosition(PositionType type) const
{
NS_LOG_FUNCTION(this);
NS_LOG_LOGIC("position before update: " << m_position);
Update();
NS_LOG_LOGIC("position after update: " << m_position);

switch (type)
{
case PositionType::TOPOCENTRIC:
return GeographicPositions::GeographicToTopocentricCoordinates(
m_position,
GetCoordinateTranslationReferencePoint(),
GetEarthSpheroidType());
case PositionType::GEOCENTRIC:
return GeographicPositions::GeographicToCartesianCoordinates(m_position.x,
m_position.y,
m_position.z,
GetEarthSpheroidType());
case PositionType::PROJECTED:
return GeographicPositions::GeographicToProjectedCoordinates(m_position,
GetEarthSpheroidType());
case PositionType::GEOGRAPHIC:
default:
return m_position;
}

return m_position;
}

void
ParametricSpeedDroneMobilityModel::DoSetPosition(const Vector& position)
ParametricSpeedDroneMobilityModel::DoSetPosition(Vector position, PositionType type)
{
NS_LOG_FUNCTION(position);
NS_LOG_FUNCTION(this << position << type);

switch (type)
{
case PositionType::TOPOCENTRIC:
m_position = GeographicPositions::TopocentricToGeographicCoordinates(
position,
GetCoordinateTranslationReferencePoint(),
GetEarthSpheroidType());
break;
case PositionType::GEOCENTRIC:
m_position =
GeographicPositions::CartesianToGeographicCoordinates(position, GetEarthSpheroidType());
break;
case PositionType::PROJECTED:
m_position =
GeographicPositions::ProjectedToGeographicCoordinates(position, GetEarthSpheroidType());
break;
case PositionType::GEOGRAPHIC:
default:
m_position = position;
break;
}

NS_ASSERT_MSG((m_position.x >= -90) && (m_position.x <= 90),
"Latitude must be between -90 deg and +90 deg");
NS_ASSERT_MSG((m_position.y >= -180) && (m_position.y <= 180),
"Longitude must be between -180 deg and +180 deg");
NS_ASSERT_MSG(m_position.z >= 0, "Altitude must be higher or equal 0 meters");

m_position = position;
NotifyCourseChange();
}

Vector
Expand All @@ -122,23 +234,46 @@ ParametricSpeedDroneMobilityModel::DoGetVelocity() const
}

void
ParametricSpeedDroneMobilityModel::DoInitialize()
ParametricSpeedDroneMobilityModel::Update() const
{
NS_LOG_FUNCTION(this);

m_planner = Planner<ParametricSpeedParam, ParametricSpeedFlight>(m_flightPlan,
m_flightParams,
m_curveStep);
const Time t = Simulator::Now();
if (t.Compare(m_lastUpdate) <= 0)
{
NS_LOG_LOGIC("Update is being suppressed.");
return;
}

MobilityModel::DoInitialize();
m_lastUpdate = t;

m_planner.Update(t);
m_position =
(m_useGeodedicSystem)
? GeographicPositions::ProjectedToGeographicCoordinates(m_planner.GetPosition(),
GetEarthSpheroidType())
: m_planner.GetPosition();
m_velocity = m_planner.GetVelocity();

NotifyCourseChange();
}

void
ParametricSpeedDroneMobilityModel::DoDispose()
FlightPlan
ParametricSpeedDroneMobilityModel::GetFlightPlan() const
{
NS_LOG_FUNCTION(this);
return (m_useGeodedicSystem)
? ProjectedToGeographicCoordinates(m_flightPlan, GetEarthSpheroidType())
: m_flightPlan;
}

MobilityModel::DoDispose();
void
ParametricSpeedDroneMobilityModel::SetFlightPlan(const FlightPlan& flightPlan)
{
NS_LOG_FUNCTION(this << flightPlan);
m_flightPlan = (m_useGeodedicSystem)
? GeographicToProjectedCoordinates(flightPlan, GetEarthSpheroidType())
: flightPlan;
}

void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@

#include <ns3/double-vector.h>
#include <ns3/flight-plan.h>
#include <ns3/mobility-model.h>
#include <ns3/geocentric-mobility-model.h>
#include <ns3/planner.h>
#include <ns3/proto-point.h>
#include <ns3/vector.h>

namespace ns3
{

class ParametricSpeedDroneMobilityModel : public MobilityModel
class ParametricSpeedDroneMobilityModel : public GeocentricMobilityModel
{
public:
/**
Expand All @@ -43,31 +43,42 @@ class ParametricSpeedDroneMobilityModel : public MobilityModel
ParametricSpeedDroneMobilityModel();
~ParametricSpeedDroneMobilityModel();

// TODO: Put into generic Utility object
static FlightPlan ProjectedToGeographicCoordinates(
const FlightPlan& flightPlan,
GeographicPositions::EarthSpheroidType earthType);
static FlightPlan GeographicToProjectedCoordinates(
const FlightPlan& flightPlan,
GeographicPositions::EarthSpheroidType earthType);

private:
/// Initialize the object instance.
virtual void DoInitialize();
/// Destroy the object instance.
virtual void DoDispose();

virtual void Update() const;
virtual void DoSetPosition(const Vector& position);
virtual Vector DoGetPosition() const;
virtual Vector DoGetPosition(PositionType type) const;
virtual void DoSetPosition(Vector position, PositionType type);
virtual Vector DoGetVelocity() const;

virtual void Update() const;
FlightPlan GetFlightPlan() const;
void SetFlightPlan(const FlightPlan& fp);

void SetSpeedCoefficients(const DoubleVector& a);

protected:
mutable Vector m_position;
mutable Vector m_velocity;

double m_acceleration;
double m_maxSpeed;

FlightPlan m_flightPlan;
ParametricSpeedParam m_flightParams;
Planner<ParametricSpeedParam, ParametricSpeedFlight> m_planner;

mutable Time m_lastUpdate;

float m_curveStep;
bool m_useGeodedicSystem;
};

} // namespace ns3
Expand Down

0 comments on commit 980a6fe

Please sign in to comment.