diff --git a/src/mobility/parametric-speed/parametric-speed-drone-mobility-model.cc b/src/mobility/parametric-speed/parametric-speed-drone-mobility-model.cc index affb99d..631e38d 100644 --- a/src/mobility/parametric-speed/parametric-speed-drone-mobility-model.cc +++ b/src/mobility/parametric-speed/parametric-speed-drone-mobility-model.cc @@ -17,8 +17,10 @@ */ #include "parametric-speed-drone-mobility-model.h" +#include #include #include +#include #include #include #include @@ -36,20 +38,29 @@ ParametricSpeedDroneMobilityModel::GetTypeId() static TypeId tid = TypeId("ns3::ParametricSpeedDroneMobilityModel") - .SetParent() + .SetParent() .SetGroupName("Mobility") .AddConstructor() + .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.", @@ -62,7 +73,8 @@ ParametricSpeedDroneMobilityModel::GetTypeId() ParametricSpeedDroneMobilityModel::ParametricSpeedDroneMobilityModel() : m_flightParams{{}}, - m_lastUpdate{-1} + m_lastUpdate{-1}, + m_useGeodedicSystem{false} { NS_LOG_FUNCTION(this); } @@ -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("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("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(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 @@ -122,23 +234,46 @@ ParametricSpeedDroneMobilityModel::DoGetVelocity() const } void -ParametricSpeedDroneMobilityModel::DoInitialize() +ParametricSpeedDroneMobilityModel::Update() const { NS_LOG_FUNCTION(this); - m_planner = Planner(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 diff --git a/src/mobility/parametric-speed/parametric-speed-drone-mobility-model.h b/src/mobility/parametric-speed/parametric-speed-drone-mobility-model.h index 715aa29..f84987c 100644 --- a/src/mobility/parametric-speed/parametric-speed-drone-mobility-model.h +++ b/src/mobility/parametric-speed/parametric-speed-drone-mobility-model.h @@ -23,7 +23,7 @@ #include #include -#include +#include #include #include #include @@ -31,7 +31,7 @@ namespace ns3 { -class ParametricSpeedDroneMobilityModel : public MobilityModel +class ParametricSpeedDroneMobilityModel : public GeocentricMobilityModel { public: /** @@ -43,24 +43,34 @@ 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 m_planner; @@ -68,6 +78,7 @@ class ParametricSpeedDroneMobilityModel : public MobilityModel mutable Time m_lastUpdate; float m_curveStep; + bool m_useGeodedicSystem; }; } // namespace ns3