INET Framework for OMNeT++/OMNEST
inet::Ns2MotionMobility Class Reference

#include <Ns2MotionMobility.h>

Inheritance diagram for inet::Ns2MotionMobility:
inet::LineSegmentsMobilityBase inet::MovingMobilityBase inet::MobilityBase inet::IMobility

Public Member Functions

 Ns2MotionMobility ()
 
virtual ~Ns2MotionMobility ()
 
virtual double getMaxSpeed () const override
 Returns the maximum possible speed at any future time. More...
 
- Public Member Functions inherited from inet::LineSegmentsMobilityBase
 LineSegmentsMobilityBase ()
 
- Public Member Functions inherited from inet::MovingMobilityBase
virtual const CoordgetCurrentPosition () override
 Returns the position at the current simulation time. More...
 
virtual const CoordgetCurrentVelocity () override
 Returns the velocity at the current simulation time. More...
 
virtual const CoordgetCurrentAcceleration () override
 Returns the acceleration at the current simulation time. More...
 
virtual const QuaterniongetCurrentAngularPosition () override
 Returns the angular position at the current simulation time. More...
 
virtual const QuaterniongetCurrentAngularVelocity () override
 Returns the angular velocity at the current simulation time. More...
 
virtual const QuaterniongetCurrentAngularAcceleration () override
 Returns the angular acceleration at the current simulation time. More...
 
- Public Member Functions inherited from inet::MobilityBase
virtual const CoordgetConstraintAreaMax () const override
 Returns the maximum position along each axes for. More...
 
virtual const CoordgetConstraintAreaMin () const override
 Returns the minimum position along each axes for. More...
 
- Public Member Functions inherited from inet::IMobility
virtual ~IMobility ()
 

Protected Member Functions

void parseFile (const char *filename)
 
virtual int numInitStages () const override
 Returns the required number of initialize stages. More...
 
virtual void initialize (int stage) override
 Initializes mobility model parameters. More...
 
virtual void setInitialPosition () override
 Initializes the position according to the mobility model. More...
 
virtual void setTargetPosition () override
 Overridden from LineSegmentsMobilityBase. More...
 
virtual void move () override
 Overridden from LineSegmentsMobilityBase. More...
 
virtual void computeMaxSpeed ()
 
- Protected Member Functions inherited from inet::LineSegmentsMobilityBase
virtual void initializePosition () override
 Initializes mobility position. More...
 
- Protected Member Functions inherited from inet::MovingMobilityBase
 MovingMobilityBase ()
 
virtual ~MovingMobilityBase ()
 
virtual void handleSelfMessage (cMessage *message) override
 Called upon arrival of a self messages, subclasses must override. More...
 
void scheduleUpdate ()
 Schedules the move timer that will update the mobility state. More...
 
void moveAndUpdate ()
 Moves and notifies listeners. More...
 
virtual void orient ()
 
- Protected Member Functions inherited from inet::MobilityBase
 MobilityBase ()
 
virtual int getId () const override
 
virtual void checkPosition ()
 Checks whether the position is valid or not. More...
 
virtual void initializeOrientation ()
 Initializes the orientation from module parameters. More...
 
virtual void refreshDisplay () const override
 Moves the visual representation module's icon to the new position on the screen. More...
 
virtual void updateDisplayStringFromMobilityState () const
 
virtual void handleParameterChange (const char *name) override
 Allows changing parameters from the GUI. More...
 
virtual void handleMessage (cMessage *msg) override
 This modules should only receive self-messages. More...
 
virtual void emitMobilityStateChangedSignal ()
 Emits a signal with the updated mobility state. More...
 
virtual Coord getRandomPosition ()
 Returns a new random position satisfying the constraint area. More...
 
virtual cModule * findSubjectModule ()
 Returns the module that represents the object moved by this mobility module. More...
 
virtual bool isOutside ()
 Returns true if the mobility is outside of the constraint area. More...
 
virtual void reflectIfOutside (Coord &targetPosition, Coord &velocity, rad &angle, rad &elevation, Quaternion &quaternion)
 Utility function to reflect the node if it goes outside the constraint area. More...
 
virtual void wrapIfOutside (Coord &targetPosition)
 Utility function to wrap the node to the opposite edge (torus) if it goes outside the constraint area. More...
 
virtual void placeRandomlyIfOutside (Coord &targetPosition)
 Utility function to place the node randomly if it goes outside the constraint area. More...
 
virtual void raiseErrorIfOutside ()
 Utility function to raise an error if the node gets outside the constraint area. More...
 
virtual void handleIfOutside (BorderPolicy policy, Coord &targetPosition, Coord &velocity)
 Invokes one of reflectIfOutside(), wrapIfOutside() and placeRandomlyIfOutside(), depending on the given border policy. More...
 
virtual void handleIfOutside (BorderPolicy policy, Coord &targetPosition, Coord &velocity, rad &heading)
 
virtual void handleIfOutside (BorderPolicy policy, Coord &targetPosition, Coord &velocity, rad &heading, rad &elevation)
 
virtual void handleIfOutside (BorderPolicy policy, Coord &targetPosition, Coord &velocity, rad &heading, rad &elevation, Quaternion &quaternion)
 

Protected Attributes

unsigned int vecpos
 
Ns2MotionFilens2File
 
int nodeId
 
double scrollX
 
double scrollY
 
double maxSpeed
 
- Protected Attributes inherited from inet::LineSegmentsMobilityBase
Coord targetPosition
 End position of current linear movement. More...
 
- Protected Attributes inherited from inet::MovingMobilityBase
cMessage * moveTimer
 The message used for mobility state changes. More...
 
simtime_t updateInterval
 The simulation time interval used to regularly signal mobility state changes. More...
 
bool stationary
 A mobility model may decide to become stationary at any time. More...
 
Coord lastVelocity
 The last velocity that was reported at lastUpdate. More...
 
Quaternion lastAngularVelocity
 The last angular velocity that was reported at lastUpdate. More...
 
simtime_t lastUpdate
 The simulation time when the mobility state was last updated. More...
 
simtime_t nextChange
 The next simulation time when the mobility module needs to update its internal state. More...
 
bool faceForward
 
- Protected Attributes inherited from inet::MobilityBase
cModule * subjectModule
 Pointer to visual representation module, to speed up repeated access. More...
 
const CanvasProjectioncanvasProjection
 The 2D projection used on the canvas. More...
 
Coord constraintAreaMin
 3 dimensional position and size of the constraint area (in meters). More...
 
Coord constraintAreaMax
 
Coord lastPosition
 The last position that was reported. More...
 
Quaternion lastOrientation
 The last orientation that was reported. More...
 
StringFormat format
 

Additional Inherited Members

- Public Types inherited from inet::MobilityBase
enum  BorderPolicy { REFLECT, WRAP, PLACERANDOMLY, RAISEERROR }
 Selects how a mobility module should behave if it reaches the edge of the constraint area. More...
 
- Static Public Attributes inherited from inet::IMobility
static simsignal_t mobilityStateChangedSignal = cComponent::registerSignal("mobilityStateChanged")
 A signal used to publish mobility state changes. More...
 

Constructor & Destructor Documentation

◆ Ns2MotionMobility()

inet::Ns2MotionMobility::Ns2MotionMobility ( )
25 {
26  vecpos = 0;
27  ns2File = nullptr;
28  nodeId = 0;
29  scrollX = 0;
30  scrollY = 0;
31  maxSpeed = 0;
32 }

◆ ~Ns2MotionMobility()

inet::Ns2MotionMobility::~Ns2MotionMobility ( )
virtual
45 {
46  if (ns2File)
47  delete ns2File;
48 }

Member Function Documentation

◆ computeMaxSpeed()

void inet::Ns2MotionMobility::computeMaxSpeed ( )
protectedvirtual
35 {
36  unsigned int fileSize = ns2File->lines.size();
37  for (unsigned int i = 0; i < fileSize; i++) {
38  const Ns2MotionFile::Line& vec = ns2File->lines[i];
39  if (vec[3] > maxSpeed)
40  maxSpeed = vec[3];
41  }
42 }

◆ getMaxSpeed()

virtual double inet::Ns2MotionMobility::getMaxSpeed ( ) const
inlineoverridevirtual

Returns the maximum possible speed at any future time.

Reimplemented from inet::MobilityBase.

72 { return maxSpeed; }

◆ initialize()

void inet::Ns2MotionMobility::initialize ( int  stage)
overrideprotectedvirtual

Initializes mobility model parameters.

Reimplemented from inet::MovingMobilityBase.

121 {
123 
124  EV_TRACE << "initializing Ns2MotionMobility stage " << stage << endl;
125  if (stage == INITSTAGE_LOCAL) {
126  scrollX = par("scrollX");
127  scrollY = par("scrollY");
128  nodeId = par("nodeId");
129  if (nodeId == -1)
130  nodeId = getContainingNode(this)->getIndex();
131  const char *fname = par("traceFile");
132  ns2File = new Ns2MotionFile;
133  parseFile(fname);
134  vecpos = 0;
135  WATCH(nodeId);
136  }
137 }

◆ move()

void inet::Ns2MotionMobility::move ( )
overrideprotectedvirtual

Overridden from LineSegmentsMobilityBase.

Reimplemented from inet::LineSegmentsMobilityBase.

189 {
192 }

◆ numInitStages()

virtual int inet::Ns2MotionMobility::numInitStages ( ) const
inlineoverrideprotectedvirtual

Returns the required number of initialize stages.

Reimplemented from inet::MobilityBase.

53 { return NUM_INIT_STAGES; }

◆ parseFile()

void inet::Ns2MotionMobility::parseFile ( const char *  filename)
protected
51 {
52  std::ifstream in(filename, std::ios::in);
53 
54  if (in.fail())
55  throw cRuntimeError("Cannot open file '%s'", filename);
56  ns2File->initial[0] = ns2File->initial[1] = ns2File->initial[2] = -1;
57  std::string line;
58  std::string subline;
59 
60  while (std::getline(in, line)) {
61  // '#' line
62  int num_node = -1;
63  std::string::size_type found = line.find('#');
64  if (found == 0)
65  continue;
66  if (found != std::string::npos)
67  subline = line;
68  else
69  subline = line.substr(0, found);
70  found = subline.find("$node_");
71  if (found == std::string::npos)
72  continue;
73  // Node Id
74  std::string::size_type pos1 = subline.find('(');
75  std::string::size_type pos2 = subline.find(')');
76  if (pos2 - pos1 > 1)
77  num_node = std::atoi(subline.substr(pos1 + 1, pos2 - 1).c_str());
78  if (num_node != nodeId)
79  continue;
80  // Initial position
81  found = subline.find("set ");
82  if (found != std::string::npos) {
83  // Initial position
84  found = subline.find("X_");
85  if (found != std::string::npos) {
86  ns2File->initial[0] = std::atof(subline.substr(found + 3, std::string::npos).c_str());
87  }
88  found = subline.find("Y_");
89  if (found != std::string::npos) {
90  ns2File->initial[1] = std::atof(subline.substr(found + 3, std::string::npos).c_str());
91  }
92 
93  found = subline.find("Z_");
94  if (found != std::string::npos) {
95  ns2File->initial[2] = std::atof(subline.substr(found + 3, std::string::npos).c_str());
96  }
97  }
98  found = subline.find("setdest");
99  if (found != std::string::npos) {
100  ns2File->lines.push_back(Ns2MotionFile::Line());
101  Ns2MotionFile::Line& vec = ns2File->lines.back();
102  // initial time
103  found = subline.find("at");
104  vec.push_back(std::atof(subline.substr(found + 3).c_str()));
105 
106  std::string parameters = subline.substr(subline.find("setdest ") + 8, std::string::npos);
107 
108  std::stringstream linestream(parameters);
109  double d;
110  while (linestream >> d)
111  vec.push_back(d);
112  }
113  }
114  in.close();
115  // exist data?
116  if (ns2File->initial[0] == -1 || ns2File->initial[1] == -1 || ns2File->initial[2] == -1)
117  throw cRuntimeError("node '%d' Error ns2 motion file '%s'", nodeId, filename);
118 }

Referenced by initialize().

◆ setInitialPosition()

void inet::Ns2MotionMobility::setInitialPosition ( )
overrideprotectedvirtual

Initializes the position according to the mobility model.

Reimplemented from inet::MobilityBase.

140 {
143 }

◆ setTargetPosition()

void inet::Ns2MotionMobility::setTargetPosition ( )
overrideprotectedvirtual

Overridden from LineSegmentsMobilityBase.

Implements inet::LineSegmentsMobilityBase.

146 {
147  if (ns2File->lines.size() == 0) {
148  stationary = true;
149  return;
150  }
151 
152  if (vecpos >= ns2File->lines.size()) {
153  stationary = true;
154  return;
155  }
156 
157  if (ns2File->lines.begin() + vecpos == ns2File->lines.end()) {
158  stationary = true;
159  return;
160  }
161 
162  const Ns2MotionFile::Line& vec = ns2File->lines[vecpos];
163  double time = vec[0];
164  simtime_t now = simTime();
165  // TODO this code is dubious at best
166  if (now < time) {
167  nextChange = time;
169  }
170  else if (vec[3] == 0) { // the node is stopped
171  const Ns2MotionFile::Line& vec = ns2File->lines[vecpos + 1];
172  double time = vec[0];
173  nextChange = time;
175  vecpos++;
176  }
177  else {
178  targetPosition.x = vec[1] + scrollX;
179  targetPosition.y = vec[2] + scrollY;
180  double speed = vec[3];
181  double distance = lastPosition.distance(targetPosition);
182  double travelTime = distance / speed;
183  nextChange = now + travelTime;
184  vecpos++;
185  }
186 }

Member Data Documentation

◆ maxSpeed

double inet::Ns2MotionMobility::maxSpeed
protected

◆ nodeId

int inet::Ns2MotionMobility::nodeId
protected

◆ ns2File

◆ scrollX

double inet::Ns2MotionMobility::scrollX
protected

◆ scrollY

double inet::Ns2MotionMobility::scrollY
protected

◆ vecpos

unsigned int inet::Ns2MotionMobility::vecpos
protected

The documentation for this class was generated from the following files:
inet::Ns2MotionMobility::scrollX
double scrollX
Definition: Ns2MotionMobility.h:46
inet::MobilityBase::lastPosition
Coord lastPosition
The last position that was reported.
Definition: MobilityBase.h:73
inet::LineSegmentsMobilityBase::targetPosition
Coord targetPosition
End position of current linear movement.
Definition: LineSegmentsMobilityBase.h:28
inet::getContainingNode
cModule * getContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:40
inet::MovingMobilityBase::stationary
bool stationary
A mobility model may decide to become stationary at any time.
Definition: MovingMobilityBase.h:32
inet::Coord::x
double x
Definition: Coord.h:36
inet::Coord::distance
double distance(const Coord &a) const
Returns the distance to Coord 'a'.
Definition: Coord.h:251
inet::LineSegmentsMobilityBase::move
virtual void move() override
Moves according to the mobility model to the current simulation time.
Definition: LineSegmentsMobilityBase.cc:31
inet::MovingMobilityBase::initialize
virtual void initialize(int stage) override
Initializes mobility model parameters.
Definition: MovingMobilityBase.cc:26
inet::MovingMobilityBase::nextChange
simtime_t nextChange
The next simulation time when the mobility module needs to update its internal state.
Definition: MovingMobilityBase.h:46
inet::Ns2MotionMobility::scrollY
double scrollY
Definition: Ns2MotionMobility.h:47
inet::Ns2MotionFile::Line
std::vector< double > Line
Definition: Ns2MotionMobility.h:30
inet::INITSTAGE_LOCAL
INET_API InitStage INITSTAGE_LOCAL
Initialization of local state that don't use or affect other modules includes:
inet::MobilityBase::raiseErrorIfOutside
virtual void raiseErrorIfOutside()
Utility function to raise an error if the node gets outside the constraint area.
Definition: MobilityBase.cc:343
inet::Ns2MotionMobility::vecpos
unsigned int vecpos
Definition: Ns2MotionMobility.h:43
NUM_INIT_STAGES
#define NUM_INIT_STAGES
Definition: InitStageRegistry.h:73
inet::Ns2MotionMobility::maxSpeed
double maxSpeed
Definition: Ns2MotionMobility.h:48
inet::Ns2MotionFile::lines
LineList lines
Definition: Ns2MotionMobility.h:36
inet::Ns2MotionFile::initial
double initial[3]
Definition: Ns2MotionMobility.h:31
inet::Ns2MotionMobility::ns2File
Ns2MotionFile * ns2File
Definition: Ns2MotionMobility.h:44
inet::Ns2MotionMobility::parseFile
void parseFile(const char *filename)
Definition: Ns2MotionMobility.cc:50
inet::Ns2MotionMobility::nodeId
int nodeId
Definition: Ns2MotionMobility.h:45
inet::Coord::y
double y
Definition: Coord.h:37