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 Coord getCurrentPosition () override
 Returns the current position at the current simulation time. More...
 
virtual Coord getCurrentSpeed () override
 Returns the current speed at the current simulation time. More...
 
virtual EulerAngles getCurrentAngularPosition () override
 Returns the current angular position at the current simulation time. More...
 
- Public Member Functions inherited from inet::MobilityBase
virtual EulerAngles getCurrentAngularSpeed () override
 Returns the current angular speed at the current simulation time. More...
 
virtual Coord getConstraintAreaMax () const override
 Returns the current angular acceleration at the current simulation time. More...
 
virtual Coord getConstraintAreaMin () const override
 
- 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...
 
- Protected Member Functions inherited from inet::MobilityBase
 MobilityBase ()
 
virtual void checkPosition ()
 Checks whether the position is valid or not. More...
 
virtual void initializeOrientation ()
 Initializes the orientation from module parameters. More...
 
virtual void handleMessage (cMessage *msg) override
 This modules should only receive self-messages. More...
 
virtual void updateVisualRepresentation ()
 Moves the visual representation module's icon to the new position on the screen. 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 * findVisualRepresentation ()
 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 &speed, double &angle)
 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 &speed, double &angle)
 Invokes one of reflectIfOutside(), wrapIfOutside() and placeRandomlyIfOutside(), depending on the given border policy. More...
 

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 lastSpeed
 The last speed 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...
 
- Protected Attributes inherited from inet::MobilityBase
cModule * visualRepresentation
 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...
 
EulerAngles lastOrientation
 The last position that was reported. More...
 

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

inet::Ns2MotionMobility::Ns2MotionMobility ( )
36 {
37  vecpos = 0;
38  ns2File = nullptr;
39  nodeId = 0;
40  scrollX = 0;
41  scrollY = 0;
42  maxSpeed = 0;
43 }
Ns2MotionFile * ns2File
Definition: Ns2MotionMobility.h:58
unsigned int vecpos
Definition: Ns2MotionMobility.h:57
int nodeId
Definition: Ns2MotionMobility.h:59
double scrollX
Definition: Ns2MotionMobility.h:60
double scrollY
Definition: Ns2MotionMobility.h:61
double maxSpeed
Definition: Ns2MotionMobility.h:62
inet::Ns2MotionMobility::~Ns2MotionMobility ( )
virtual
57 {
58  if (ns2File)
59  delete ns2File;
60 }
Ns2MotionFile * ns2File
Definition: Ns2MotionMobility.h:58

Member Function Documentation

void inet::Ns2MotionMobility::computeMaxSpeed ( )
protectedvirtual
46 {
47  unsigned int fileSize = ns2File->lines.size();
48  for (unsigned int i = 0; i < fileSize; i++)
49  {
50  const Ns2MotionFile::Line& vec = ns2File->lines[i];
51  if (vec[3] > maxSpeed)
52  maxSpeed = vec[3];
53  }
54 }
Ns2MotionFile * ns2File
Definition: Ns2MotionMobility.h:58
std::vector< double > Line
Definition: Ns2MotionMobility.h:44
LineList lines
Definition: Ns2MotionMobility.h:50
double maxSpeed
Definition: Ns2MotionMobility.h:62
virtual double inet::Ns2MotionMobility::getMaxSpeed ( ) const
inlineoverridevirtual

Returns the maximum possible speed at any future time.

Reimplemented from inet::MobilityBase.

85 { return maxSpeed; }
double maxSpeed
Definition: Ns2MotionMobility.h:62
void inet::Ns2MotionMobility::initialize ( int  stage)
overrideprotectedvirtual

Initializes mobility model parameters.

Reimplemented from inet::MovingMobilityBase.

133 {
135 
136  EV_TRACE << "initializing Ns2MotionMobility stage " << stage << endl;
137  if (stage == INITSTAGE_LOCAL) {
138  scrollX = par("scrollX");
139  scrollY = par("scrollY");
140  nodeId = par("nodeId");
141  if (nodeId == -1)
142  nodeId = getContainingNode(this)->getIndex();
143  const char *fname = par("traceFile");
144  ns2File = new Ns2MotionFile;
145  parseFile(fname);
146  vecpos = 0;
147  WATCH(nodeId);
148  }
149 }
virtual void initialize(int stage) override
Initializes mobility model parameters.
Definition: MovingMobilityBase.cc:42
Ns2MotionFile * ns2File
Definition: Ns2MotionMobility.h:58
unsigned int vecpos
Definition: Ns2MotionMobility.h:57
void parseFile(const char *filename)
Definition: Ns2MotionMobility.cc:62
int nodeId
Definition: Ns2MotionMobility.h:59
Local initializations.
Definition: InitStages.h:35
cModule * getContainingNode(const cModule *from)
Find the node containing the given module.
Definition: ModuleAccess.cc:65
double scrollX
Definition: Ns2MotionMobility.h:60
double scrollY
Definition: Ns2MotionMobility.h:61
void inet::Ns2MotionMobility::move ( )
overrideprotectedvirtual

Overridden from LineSegmentsMobilityBase.

Reimplemented from inet::LineSegmentsMobilityBase.

201 {
204 }
virtual void raiseErrorIfOutside()
Utility function to raise an error if the node gets outside the constraint area.
Definition: MobilityBase.cc:259
virtual void move() override
Moves according to the mobility model to the current simulation time.
Definition: LineSegmentsMobilityBase.cc:40
virtual int inet::Ns2MotionMobility::numInitStages ( ) const
inlineoverrideprotectedvirtual

Returns the required number of initialize stages.

Reimplemented from inet::MobilityBase.

67 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::Ns2MotionMobility::parseFile ( const char *  filename)
protected

Referenced by initialize().

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

Initializes the position according to the mobility model.

Reimplemented from inet::MobilityBase.

152 {
155 }
double initial[3]
Definition: Ns2MotionMobility.h:45
Ns2MotionFile * ns2File
Definition: Ns2MotionMobility.h:58
Coord lastPosition
The last position that was reported.
Definition: MobilityBase.h:78
double scrollX
Definition: Ns2MotionMobility.h:60
double y
Definition: Coord.h:50
double x
Definition: Coord.h:49
double scrollY
Definition: Ns2MotionMobility.h:61
void inet::Ns2MotionMobility::setTargetPosition ( )
overrideprotectedvirtual

Overridden from LineSegmentsMobilityBase.

Implements inet::LineSegmentsMobilityBase.

158 {
159  if (ns2File->lines.size() == 0) {
160  stationary = true;
161  return;
162  }
163 
164  if (vecpos >= ns2File->lines.size()) {
165  stationary = true;
166  return;
167  }
168 
169  if (ns2File->lines.begin() + vecpos == ns2File->lines.end()) {
170  stationary = true;
171  return;
172  }
173 
174  const Ns2MotionFile::Line& vec = ns2File->lines[vecpos];
175  double time = vec[0];
176  simtime_t now = simTime();
177  // TODO: this code is dubious at best
178  if (now < time) {
179  nextChange = time;
181  }
182  else if (vec[3] == 0) { // the node is stopped
183  const Ns2MotionFile::Line& vec = ns2File->lines[vecpos + 1];
184  double time = vec[0];
185  nextChange = time;
187  vecpos++;
188  }
189  else {
190  targetPosition.x = vec[1] + scrollX;
191  targetPosition.y = vec[2] + scrollY;
192  double speed = vec[3];
193  double distance = lastPosition.distance(targetPosition);
194  double travelTime = distance / speed;
195  nextChange = now + travelTime;
196  vecpos++;
197  }
198 }
Ns2MotionFile * ns2File
Definition: Ns2MotionMobility.h:58
unsigned int vecpos
Definition: Ns2MotionMobility.h:57
std::vector< double > Line
Definition: Ns2MotionMobility.h:44
LineList lines
Definition: Ns2MotionMobility.h:50
Coord lastPosition
The last position that was reported.
Definition: MobilityBase.h:78
simtime_t nextChange
The next simulation time when the mobility module needs to update its internal state.
Definition: MovingMobilityBase.h:60
Coord targetPosition
End position of current linear movement.
Definition: LineSegmentsMobilityBase.h:41
double distance(const Coord &a) const
Returns the distance to Coord &#39;a&#39;.
Definition: Coord.h:201
bool stationary
A mobility model may decide to become stationary at any time.
Definition: MovingMobilityBase.h:49
double scrollX
Definition: Ns2MotionMobility.h:60
double y
Definition: Coord.h:50
double x
Definition: Coord.h:49
double scrollY
Definition: Ns2MotionMobility.h:61

Member Data Documentation

double inet::Ns2MotionMobility::maxSpeed
protected
int inet::Ns2MotionMobility::nodeId
protected
double inet::Ns2MotionMobility::scrollX
protected
double inet::Ns2MotionMobility::scrollY
protected
unsigned int inet::Ns2MotionMobility::vecpos
protected

The documentation for this class was generated from the following files: