From c1ceb7dadadfccb9eff03a89e8fb29134ee98365 Mon Sep 17 00:00:00 2001 From: Phillip Sitbon Date: Tue, 5 Oct 2010 01:26:15 -0700 Subject: [PATCH] bugs 892 and 893: waypoint mobility model initial SetPosition() handling and LazyNotify attribute --- src/mobility/waypoint-mobility-model.cc | 175 ++++++++++++++++++++++-- src/mobility/waypoint-mobility-model.h | 52 +++++-- 2 files changed, 204 insertions(+), 23 deletions(-) diff --git a/src/mobility/waypoint-mobility-model.cc b/src/mobility/waypoint-mobility-model.cc index 6566fd279..d7b0d65c0 100644 --- a/src/mobility/waypoint-mobility-model.cc +++ b/src/mobility/waypoint-mobility-model.cc @@ -22,7 +22,12 @@ #include "ns3/simulator.h" #include "ns3/uinteger.h" #include "ns3/log.h" +#include "ns3/boolean.h" #include "waypoint-mobility-model.h" +#include "ns3/config.h" +#include "ns3/test.h" +#include "ns3/node-container.h" +#include "ns3/mobility-helper.h" NS_LOG_COMPONENT_DEFINE ("WaypointMobilityModel"); @@ -48,19 +53,29 @@ WaypointMobilityModel::GetTypeId (void) UintegerValue (0), MakeUintegerAccessor (&WaypointMobilityModel::WaypointsLeft), MakeUintegerChecker ()) - ; + .AddAttribute ("LazyNotify", "Only call NotifyCourseChange when position is calculated.", + BooleanValue (false), + MakeBooleanAccessor (&WaypointMobilityModel::m_lazyNotify), + MakeBooleanChecker ()) + .AddAttribute ("InitialPositionIsWaypoint", "Calling SetPosition with no waypoints creates a waypoint.", + BooleanValue (false), + MakeBooleanAccessor (&WaypointMobilityModel::m_initialPositionIsWaypoint), + MakeBooleanChecker ()) + ; return tid; } WaypointMobilityModel::WaypointMobilityModel () - : m_first (true) + : m_first (true), + m_lazyNotify (false), + m_initialPositionIsWaypoint (false) { } WaypointMobilityModel::~WaypointMobilityModel () { } -void +void WaypointMobilityModel::DoDispose (void) { MobilityModel::DoDispose (); @@ -79,6 +94,11 @@ WaypointMobilityModel::AddWaypoint (const Waypoint &waypoint) "Waypoints must be added in ascending time order"); m_waypoints.push_back (waypoint); } + + if ( !m_lazyNotify ) + { + Simulator::Schedule (waypoint.time, &WaypointMobilityModel::Update, this); + } } Waypoint WaypointMobilityModel::GetNextWaypoint (void) const @@ -90,7 +110,7 @@ uint32_t WaypointMobilityModel::WaypointsLeft (void) const { Update (); - return m_waypoints.size(); + return m_waypoints.size (); } void WaypointMobilityModel::Update (void) const @@ -98,7 +118,7 @@ WaypointMobilityModel::Update (void) const const Time now = Simulator::Now (); bool newWaypoint = false; - if ( now <= m_current.time ) + if ( now < m_current.time ) { return; } @@ -109,6 +129,11 @@ WaypointMobilityModel::Update (void) const { if ( m_current.time <= m_next.time ) { + /* + Set m_next.time = -1 to make sure this doesn't happen more than once. + The comparison here still needs to be '<=' in the case of mobility with one waypoint. + */ + m_next.time = Seconds (-1.0); m_current.position = m_next.position; m_current.time = now; m_velocity = Vector (0,0,0); @@ -134,12 +159,14 @@ WaypointMobilityModel::Update (void) const m_velocity.z = (m_next.position.z - m_current.position.z) / t_span; } - - const double t_diff = (now - m_current.time).GetSeconds(); - m_current.position.x += m_velocity.x * t_diff; - m_current.position.y += m_velocity.y * t_diff; - m_current.position.z += m_velocity.z * t_diff; - m_current.time = now; + if ( now > m_current.time ) // Won't ever be less, but may be equal + { + const double t_diff = (now - m_current.time).GetSeconds (); + m_current.position.x += m_velocity.x * t_diff; + m_current.position.y += m_velocity.y * t_diff; + m_current.position.z += m_velocity.z * t_diff; + m_current.time = now; + } if ( newWaypoint ) { @@ -156,11 +183,23 @@ void WaypointMobilityModel::DoSetPosition (const Vector &position) { const Time now = Simulator::Now (); + + if ( m_first && m_initialPositionIsWaypoint ) + { + AddWaypoint (Waypoint (now, position)); + return; + } + Update (); m_current.time = std::max (now, m_next.time); m_current.position = position; m_velocity = Vector (0,0,0); - NotifyCourseChange (); + + if ( !m_first && (now >= m_current.time) ) + { + // This is only a course change if the node is actually moving + NotifyCourseChange (); + } } void WaypointMobilityModel::EndMobility (void) @@ -176,5 +215,117 @@ WaypointMobilityModel::DoGetVelocity (void) const return m_velocity; } +class WaypointMobilityModelNotifyTest : public TestCase +{ +public: + WaypointMobilityModelNotifyTest (bool lazy) + : TestCase (lazy ? "Check Waypoint Mobility Model LAZY notification accuracy" + : "Check Waypoint Mobility Model NON-LAZY notification accuracy"), + lazyNotify (lazy) + { + } + virtual ~WaypointMobilityModelNotifyTest () + { + } + +private: + NodeContainer nodes; + uint32_t nodeCount; + uint32_t waypointCount; + std::deque waypoints; + bool lazyNotify; +private: + virtual bool DoRun (void); + void ForceUpdates (void); + void CourseChangeCallback (std::string path, Ptr model); +}; +bool +WaypointMobilityModelNotifyTest::DoRun (void) +{ + nodeCount = 1; + waypointCount = 100; + nodes.Create (nodeCount); + MobilityHelper mobility; + mobility.SetMobilityModel ("ns3::WaypointMobilityModel", + "LazyNotify", BooleanValue (lazyNotify)); + mobility.Install (nodes); + + Waypoint wpt (Seconds (0.0), Vector (0.0, 0.0, 0.0)); + + // Create waypoints + for ( uint32_t iw = 0; iw < waypointCount; ++iw ) + { + wpt.time += Seconds (1.0); + waypoints.push_back (wpt); + } + + // Add the same waypoints to each node + for ( NodeContainer::Iterator i = nodes.Begin (); i != nodes.End (); ++i ) + { + Ptr mob = (*i)->GetObject (); + + for ( std::deque::iterator w = waypoints.begin (); w != waypoints.end (); ++w ) + { + mob->AddWaypoint (*w); + } + } + + // Schedule updates at non-waypoint times to make sure lazy notifications don't happen + for ( double updateTime = 0.5; updateTime <= ((double)waypointCount + 1.5); updateTime += 1.0 ) + { + Simulator::Schedule (Seconds (updateTime), &WaypointMobilityModelNotifyTest::ForceUpdates, this); + } + + Config::Connect ("/NodeList/*/$ns3::WaypointMobilityModel/CourseChange", + MakeCallback (&WaypointMobilityModelNotifyTest::CourseChangeCallback, this)); + + Simulator::Stop (Seconds ((double)waypointCount + 2.0)); + Simulator::Run (); + Simulator::Destroy (); + + return GetErrorStatus (); +} +void +WaypointMobilityModelNotifyTest::ForceUpdates (void) +{ + for ( NodeContainer::Iterator i = nodes.Begin (); i != nodes.End (); ++i ) + { + Ptr mob = (*i)->GetObject (); + mob->Update (); + } +} +void +WaypointMobilityModelNotifyTest::CourseChangeCallback (std::string path, Ptr model) +{ + const Time now = Simulator::Now (); + const double sec = now.GetSeconds (); + Ptr mob = model->GetObject (); + + NS_TEST_EXPECT_MSG_EQ (now, mob->m_current.time, "Waypoint time not properly updated"); + + if ( !lazyNotify ) + { + // All waypoints are on second boundaries only + NS_TEST_EXPECT_MSG_EQ (sec - ((double)((int)sec)) + sec, sec, + "Course didn't change on one second time boundary with NON-LAZY notifcations"); + } + else + { + // Updates should happen at the times they are forced, in between waypoints. + NS_TEST_EXPECT_MSG_EQ (sec - ((double)((int)sec)), 0.5, + "Course didn't change between waypoints with LAZY notifications"); + } +} + +static struct WaypointMobilityModelTestSuite : public TestSuite +{ + WaypointMobilityModelTestSuite () : TestSuite ("waypoint-mobility-model", UNIT) + { + AddTestCase (new WaypointMobilityModelNotifyTest (true)); + AddTestCase (new WaypointMobilityModelNotifyTest (false)); + } +} g_waypointMobilityModelTestSuite; + + } // namespace ns3 diff --git a/src/mobility/waypoint-mobility-model.h b/src/mobility/waypoint-mobility-model.h index 85d6a759c..1820e3b71 100644 --- a/src/mobility/waypoint-mobility-model.h +++ b/src/mobility/waypoint-mobility-model.h @@ -33,16 +33,15 @@ namespace ns3 { * \brief Waypoint-based mobility model. * * Each object determines its velocity and position at a given time - * from a set of ns3::Waypoint objects. The position of each object - * is not updated unless queried, and past waypoints are discarded + * from a set of ns3::Waypoint objects. Past waypoints are discarded * after the current simulation time greater than their time value. - * - * The initial position of each object corresponds to the position of - * the first waypoint, and the initial velocity of each object is zero. - * Upon reaching the last waypoint, object positions becomes static and - * velocity is zero. * - * When a node is in between waypoints (in time), it moves with a constant + * By default, the initial position of each object corresponds to the + * position of the first waypoint, and the initial velocity of each + * object is zero. Upon reaching the last waypoint, object position + * becomes static and velocity is zero. + * + * When a node is in between waypoint times, it moves with a constant * velocity between the position at the previous waypoint and the position * at the current waypoint. To make a node hold a certain position for a * time interval, two waypoints with the same position (but different times) @@ -54,10 +53,37 @@ namespace ns3 { * no more waypoints in which case it will not change without user * intervention. * + * The model has two attributes with methods that allow clients to get + * the next waypoint value (NextWaypoint) and the number of waypoints left + * (WaypointsLeft) beyond (but not including) the next waypoint. + * + * In addition, there are two attributes that govern model behavior. The + * first, LazyNotify, governs how the model calls the CourseChange trace. + * By default, LazyNotify is false, which means that each time that a + * waypoint time is hit, an Update() is forced and the CourseChange + * callback will be called. When LazyNotify is true, Update() is suppressed + * at waypoint times, and CourseChange callbacks will only occur when + * there later are actual calls to Update () (typically when calling + * GetPosition ()). This option may be enabled for execution run-time + * performance improvements, but when enabled, users should note that + * course change listeners will in general not be notified at waypoint + * times but instead at the next Update() following a waypoint time, + * and some waypoints may not be notified to course change listeners. + * + * The second, InitialPositionIsWaypoint, is false by default. Recall + * that the first waypoint will set the initial position and set velocity + * equal to 0 until the first waypoint time. In such a case, the + * call to SetPosition(), such as from a PositionAllocator, will be + * ignored. However, if InitialPositionIsWaypoint is set to true + * and SetPosition() is called before any waypoints have been added, + * the SetPosition() call is treated as an initial waypoint at time zero. + * In such a case, when SetPosition() is treated as an initial waypoint, + * it should be noted that attempts to add a waypoint at the same time + * will cause the program to fail. */ class WaypointMobilityModel : public MobilityModel { - public: +public: static TypeId GetTypeId (void); /** @@ -73,7 +99,7 @@ class WaypointMobilityModel : public MobilityModel * be greater than the previous waypoint added, otherwise * a fatal error occurs. The first waypoint is set as the * current position with a velocity of zero. - * + * */ void AddWaypoint (const Waypoint &waypoint); @@ -96,7 +122,9 @@ class WaypointMobilityModel : public MobilityModel */ void EndMobility (void); - private: +private: + friend class WaypointMobilityModelNotifyTest; // To allow Update() calls and access to m_current + void Update (void) const; virtual void DoDispose (void); virtual Vector DoGetPosition (void) const; @@ -104,6 +132,8 @@ class WaypointMobilityModel : public MobilityModel virtual Vector DoGetVelocity (void) const; bool m_first; + bool m_lazyNotify; + bool m_initialPositionIsWaypoint; mutable std::deque m_waypoints; mutable Waypoint m_current; mutable Waypoint m_next;