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

LOGO-style movement model, with the script coming from XML. More...

#include <TurtleMobility.h>

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

Public Member Functions

 TurtleMobility ()
 
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

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 resumeScript ()
 Process next statements from script. More...
 
virtual void executeStatement (cXMLElement *nextStatement)
 Execute the given statement. More...
 
virtual double getValue (const char *s)
 Parse attrs in the script – accepts things like "uniform(10,50) as well. More...
 
virtual void gotoNextStatement ()
 Advance nextStatement pointer. More...
 
virtual void computeMaxSpeed (cXMLElement *nodes)
 
- 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

cXMLElement * turtleScript
 
cXMLElement * nextStatement
 
double speed
 
double angle
 
BorderPolicy borderPolicy
 
std::stack< long > loopVars
 
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...
 

Detailed Description

LOGO-style movement model, with the script coming from XML.

See NED file for more info.

Author
Andras Varga

Constructor & Destructor Documentation

inet::TurtleMobility::TurtleMobility ( )
25  :
26  turtleScript(nullptr),
27  nextStatement(nullptr),
28  speed(0),
29  angle(0),
31  maxSpeed(0)
32 {
33 }
cXMLElement * nextStatement
Definition: TurtleMobility.h:43
double speed
Definition: TurtleMobility.h:44
reflect off the wall
Definition: MobilityBase.h:61
cXMLElement * turtleScript
Definition: TurtleMobility.h:40
double maxSpeed
Definition: TurtleMobility.h:48
BorderPolicy borderPolicy
Definition: TurtleMobility.h:46
double angle
Definition: TurtleMobility.h:45

Member Function Documentation

void inet::TurtleMobility::computeMaxSpeed ( cXMLElement *  nodes)
protectedvirtual

Referenced by initialize().

343 {
344  // Recursively traverse the whole config file, looking for
345  // speed attributes
346  cXMLElementList childs = nodes->getChildren();
347  for (auto & child : childs)
348  {
349  const char *speedAttr = child->getAttribute("speed");
350  if (speedAttr)
351  {
352  double speed = atof(speedAttr);
353  if (speed > maxSpeed)
354  maxSpeed = speed;
355  }
356  computeMaxSpeed(child);
357  }
358 }
double speed
Definition: TurtleMobility.h:44
double maxSpeed
Definition: TurtleMobility.h:48
virtual void computeMaxSpeed(cXMLElement *nodes)
Definition: TurtleMobility.cc:342
void inet::TurtleMobility::executeStatement ( cXMLElement *  nextStatement)
protectedvirtual

Execute the given statement.

Referenced by resumeScript().

104 {
105  ASSERT(nextChange != -1);
106  const char *tag = stmt->getTagName();
107 
108  EV_DEBUG << "doing <" << tag << ">\n";
109 
110  if (!strcmp(tag, "repeat")) {
111  const char *nAttr = stmt->getAttribute("n");
112  long n = -1; // infinity -- that's the default
113 
114  if (nAttr) {
115  n = (long)getValue(nAttr);
116 
117  if (n < 0)
118  throw cRuntimeError("<repeat>: negative repeat count at %s", stmt->getSourceLocation());
119  }
120 
121  loopVars.push(n);
122  }
123  else if (!strcmp(tag, "set")) {
124  const char *speedAttr = stmt->getAttribute("speed");
125  const char *angleAttr = stmt->getAttribute("angle");
126  const char *xAttr = stmt->getAttribute("x");
127  const char *yAttr = stmt->getAttribute("y");
128  const char *bpAttr = stmt->getAttribute("borderPolicy");
129 
130  if (speedAttr)
131  speed = getValue(speedAttr);
132 
133  if (angleAttr)
134  angle = getValue(angleAttr);
135 
136  if (xAttr)
138 
139  if (yAttr)
141 
142  if (speed <= 0)
143  throw cRuntimeError("<set>: speed is negative or zero at %s", stmt->getSourceLocation());
144 
145  if (bpAttr) {
146  if (!strcmp(bpAttr, "reflect"))
148  else if (!strcmp(bpAttr, "wrap"))
149  borderPolicy = WRAP;
150  else if (!strcmp(bpAttr, "placerandomly"))
152  else if (!strcmp(bpAttr, "error"))
154  else
155  throw cRuntimeError("<set>: value for attribute borderPolicy is invalid, should be "
156  "'reflect', 'wrap', 'placerandomly' or 'error' at %s",
157  stmt->getSourceLocation());
158  }
159  }
160  else if (!strcmp(tag, "forward")) {
161  const char *dAttr = stmt->getAttribute("d");
162  const char *tAttr = stmt->getAttribute("t");
163 
164  if (!dAttr && !tAttr)
165  throw cRuntimeError("<forward>: must have at least attribute 't' or 'd' (or both) at %s", stmt->getSourceLocation());
166 
167  double d, t;
168 
169  if (tAttr && dAttr) {
170  // cover distance d in time t (current speed is ignored)
171  d = getValue(dAttr);
172  t = getValue(tAttr);
173  }
174  else if (dAttr) {
175  // travel distance d at current speed
176  d = getValue(dAttr);
177  t = d / speed;
178  }
179  else { // tAttr only
180  // travel for time t at current speed
181  t = getValue(tAttr);
182  d = speed * t;
183  }
184 
185  if (t < 0)
186  throw cRuntimeError("<forward>: time (attribute t) is negative at %s", stmt->getSourceLocation());
187 
188  if (d < 0)
189  throw cRuntimeError("<forward>: distance (attribute d) is negative at %s", stmt->getSourceLocation());
190 
191  // FIXME handle zeros properly...
192  targetPosition.x += d * cos(M_PI * angle / 180);
193  targetPosition.y += d * sin(M_PI * angle / 180);
194  nextChange += t;
195  }
196  else if (!strcmp(tag, "turn")) {
197  const char *angleAttr = stmt->getAttribute("angle");
198 
199  if (!angleAttr)
200  throw cRuntimeError("<turn>: required attribute 'angle' missing at %s", stmt->getSourceLocation());
201 
202  angle += getValue(angleAttr);
203  }
204  else if (!strcmp(tag, "wait")) {
205  const char *tAttr = stmt->getAttribute("t");
206 
207  if (!tAttr)
208  throw cRuntimeError("<wait>: required attribute 't' missing at %s", stmt->getSourceLocation());
209 
210  double t = getValue(tAttr);
211 
212  if (t < 0)
213  throw cRuntimeError("<wait>: time (attribute t) is negative (%g) at %s", t, stmt->getSourceLocation());
214 
215  nextChange += t; // targetPosition is unchanged
216  }
217  else if (!strcmp(tag, "moveto")) {
218  const char *xAttr = stmt->getAttribute("x");
219  const char *yAttr = stmt->getAttribute("y");
220  const char *tAttr = stmt->getAttribute("t");
221 
222  if (xAttr)
223  targetPosition.x = getValue(xAttr);
224 
225  if (yAttr)
226  targetPosition.y = getValue(yAttr);
227 
228  // travel to targetPosition at current speed, or get there in time t (ignoring current speed then)
229  double t = tAttr ? getValue(tAttr) : lastPosition.distance(targetPosition) / speed;
230 
231  if (t < 0)
232  throw cRuntimeError("<wait>: time (attribute t) is negative at %s",
233  stmt->getSourceLocation());
234 
235  nextChange += t;
236  }
237  else if (!strcmp(tag, "moveby")) {
238  const char *xAttr = stmt->getAttribute("x");
239  const char *yAttr = stmt->getAttribute("y");
240  const char *tAttr = stmt->getAttribute("t");
241 
242  if (xAttr)
243  targetPosition.x += getValue(xAttr);
244 
245  if (yAttr)
246  targetPosition.y += getValue(yAttr);
247 
248  // travel to targetPosition at current speed, or get there in time t (ignoring current speed then)
249  double t = tAttr ? getValue(tAttr) : lastPosition.distance(targetPosition) / speed;
250 
251  if (t < 0)
252  throw cRuntimeError("<wait>: time (attribute t) is negative at %s",
253  stmt->getSourceLocation());
254 
255  nextChange += t;
256  }
257 }
Value cos(const value< Value, Unit > &angle)
Definition: Units.h:1201
double speed
Definition: TurtleMobility.h:44
reflect off the wall
Definition: MobilityBase.h:61
reappear at the opposite edge (torus)
Definition: MobilityBase.h:62
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
#define M_PI
Definition: PlotFigure.cc:27
virtual double getValue(const char *s)
Parse attrs in the script – accepts things like "uniform(10,50) as well.
Definition: TurtleMobility.cc:259
BorderPolicy borderPolicy
Definition: TurtleMobility.h:46
stop the simulation with error
Definition: MobilityBase.h:64
double angle
Definition: TurtleMobility.h:45
double y
Definition: Coord.h:50
double x
Definition: Coord.h:49
std::stack< long > loopVars
Definition: TurtleMobility.h:47
Value sin(const value< Value, Unit > &angle)
Definition: Units.h:1195
placed at a randomly chosen position within the constraint area
Definition: MobilityBase.h:63
virtual double inet::TurtleMobility::getMaxSpeed ( ) const
inlineoverridevirtual

Returns the maximum possible speed at any future time.

Reimplemented from inet::MobilityBase.

83 { return maxSpeed; }
double maxSpeed
Definition: TurtleMobility.h:48
double inet::TurtleMobility::getValue ( const char *  s)
protectedvirtual

Parse attrs in the script – accepts things like "uniform(10,50) as well.

Referenced by executeStatement().

260 {
261  // first, textually replace $MAXX and $MAXY with their actual values
262  std::string str;
263  if (strchr(s, '$')) {
264  char strMinX[32], strMinY[32];
265  char strMaxX[32], strMaxY[32];
266  sprintf(strMinX, "%g", constraintAreaMin.x);
267  sprintf(strMinY, "%g", constraintAreaMin.y);
268  sprintf(strMaxX, "%g", constraintAreaMax.x);
269  sprintf(strMaxY, "%g", constraintAreaMax.y);
270 
271  str = s;
272  std::string::size_type pos;
273 
274  while ((pos = str.find("$MINX")) != std::string::npos)
275  str.replace(pos, sizeof("$MINX") - 1, strMinX);
276 
277  while ((pos = str.find("$MINY")) != std::string::npos)
278  str.replace(pos, sizeof("$MINY") - 1, strMinY);
279 
280  while ((pos = str.find("$MAXX")) != std::string::npos)
281  str.replace(pos, sizeof("$MAXX") - 1, strMaxX);
282 
283  while ((pos = str.find("$MAXY")) != std::string::npos)
284  str.replace(pos, sizeof("$MAXY") - 1, strMaxY);
285 
286  s = str.c_str();
287  }
288 
289  // then use cDynamicExpression to evaluate the string
290  try {
291  cDynamicExpression expr;
292  expr.parse(s);
293  return expr.doubleValue(this);
294  }
295  catch (std::exception& e) {
296  throw cRuntimeError("Wrong value '%s' around %s: %s", s,
297  nextStatement->getSourceLocation(), e.what());
298  }
299 }
cXMLElement * nextStatement
Definition: TurtleMobility.h:43
Coord constraintAreaMin
3 dimensional position and size of the constraint area (in meters).
Definition: MobilityBase.h:75
const value< double, units::C > e(1.602176487e-19)
value< double, units::s > s
Definition: Units.h:1049
double y
Definition: Coord.h:50
double x
Definition: Coord.h:49
Coord constraintAreaMax
Definition: MobilityBase.h:75
void inet::TurtleMobility::gotoNextStatement ( )
protectedvirtual

Advance nextStatement pointer.

Referenced by resumeScript().

302 {
303  // "statement either doesn't have a child, or it's a <repeat> and loop count is already pushed on the stack"
304  ASSERT(!nextStatement->getFirstChild() || (!strcmp(nextStatement->getTagName(), "repeat")
305  && !loopVars.empty()));
306 
307  if (nextStatement->getFirstChild() && (loopVars.top() != 0 || (loopVars.pop(), false))) { // !=0: positive or -1
308  // statement must be a <repeat> if it has children; repeat count>0 must be
309  // on the stack; let's start doing the body.
310  nextStatement = nextStatement->getFirstChild();
311  }
312  else if (!nextStatement->getNextSibling()) {
313  // no sibling -- either end of <repeat> body, or end of script
314  ASSERT(nextStatement->getParentNode() == turtleScript ? loopVars.empty() : !loopVars.empty());
315 
316  if (!loopVars.empty()) {
317  // decrement and check loop counter
318  if (loopVars.top() != -1) // -1 means infinity
319  loopVars.top()--;
320 
321  if (loopVars.top() != 0) { // positive or -1
322  // go to beginning of <repeat> block again
323  nextStatement = nextStatement->getParentNode()->getFirstChild();
324  }
325  else {
326  // end of loop -- locate next statement after the <repeat>
327  nextStatement = nextStatement->getParentNode();
329  }
330  }
331  else {
332  // end of script
333  nextStatement = nullptr;
334  }
335  }
336  else {
337  // go to next statement (must exist -- see "if" above)
338  nextStatement = nextStatement->getNextSibling();
339  }
340 }
cXMLElement * nextStatement
Definition: TurtleMobility.h:43
cXMLElement * turtleScript
Definition: TurtleMobility.h:40
std::stack< long > loopVars
Definition: TurtleMobility.h:47
virtual void gotoNextStatement()
Advance nextStatement pointer.
Definition: TurtleMobility.cc:301
void inet::TurtleMobility::initialize ( int  stage)
overrideprotectedvirtual

Initializes mobility model parameters.

Reimplemented from inet::MovingMobilityBase.

36 {
38 
39  EV_TRACE << "initializing TurtleMobility stage " << stage << endl;
40  if (stage == INITSTAGE_LOCAL) {
41  WATCH(speed);
42  WATCH(angle);
43  WATCH(borderPolicy);
44  computeMaxSpeed(par("turtleScript"));
45  }
46 }
virtual void initialize(int stage) override
Initializes mobility model parameters.
Definition: MovingMobilityBase.cc:42
double speed
Definition: TurtleMobility.h:44
Local initializations.
Definition: InitStages.h:35
BorderPolicy borderPolicy
Definition: TurtleMobility.h:46
double angle
Definition: TurtleMobility.h:45
virtual void computeMaxSpeed(cXMLElement *nodes)
Definition: TurtleMobility.cc:342
void inet::TurtleMobility::move ( )
overrideprotectedvirtual

Overridden from LineSegmentsMobilityBase.

Reimplemented from inet::LineSegmentsMobilityBase.

77 {
79  Coord dummy;
81 }
virtual void move() override
Moves according to the mobility model to the current simulation time.
Definition: LineSegmentsMobilityBase.cc:40
Coord targetPosition
End position of current linear movement.
Definition: LineSegmentsMobilityBase.h:41
virtual void handleIfOutside(BorderPolicy policy, Coord &targetPosition, Coord &speed, double &angle)
Invokes one of reflectIfOutside(), wrapIfOutside() and placeRandomlyIfOutside(), depending on the giv...
Definition: MobilityBase.cc:269
BorderPolicy borderPolicy
Definition: TurtleMobility.h:46
double angle
Definition: TurtleMobility.h:45
virtual int inet::TurtleMobility::numInitStages ( ) const
inlineoverrideprotectedvirtual

Returns the required number of initialize stages.

Reimplemented from inet::MobilityBase.

51 { return NUM_INIT_STAGES; }
The number of initialization stages.
Definition: InitStages.h:116
void inet::TurtleMobility::resumeScript ( )
protectedvirtual

Process next statements from script.

Will set a new nextChange and targetPosition.

Referenced by setInitialPosition(), and setTargetPosition().

87 {
88  simtime_t now = simTime();
89 
90  while (nextChange == now) {
91  if (nextStatement != nullptr) {
94  }
95  else {
96  nextChange = -1;
97  stationary = true;
99  }
100  }
101 }
cXMLElement * nextStatement
Definition: TurtleMobility.h:43
virtual void executeStatement(cXMLElement *nextStatement)
Execute the given statement.
Definition: TurtleMobility.cc:103
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
bool stationary
A mobility model may decide to become stationary at any time.
Definition: MovingMobilityBase.h:49
virtual void gotoNextStatement()
Advance nextStatement pointer.
Definition: TurtleMobility.cc:301
void inet::TurtleMobility::setInitialPosition ( )
overrideprotectedvirtual

Initializes the position according to the mobility model.

Reimplemented from inet::MobilityBase.

49 {
51 
52  turtleScript = par("turtleScript");
53  nextStatement = turtleScript->getFirstChild();
54 
55  speed = 1;
56  angle = 0;
58 
59  // a dirty trick to extract starting position out of the script
60  // (start doing it, but then rewind to the beginning)
61  nextChange = simTime();
62  resumeScript();
64  nextChange = simTime();
65  nextStatement = turtleScript->getFirstChild();
66 
67  while (!loopVars.empty())
68  loopVars.pop();
69 }
cXMLElement * nextStatement
Definition: TurtleMobility.h:43
double speed
Definition: TurtleMobility.h:44
reflect off the wall
Definition: MobilityBase.h:61
virtual void resumeScript()
Process next statements from script.
Definition: TurtleMobility.cc:86
Coord lastPosition
The last position that was reported.
Definition: MobilityBase.h:78
virtual void setInitialPosition()
Initializes the position from the display string or from module parameters.
Definition: MobilityBase.cc:99
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
cXMLElement * turtleScript
Definition: TurtleMobility.h:40
BorderPolicy borderPolicy
Definition: TurtleMobility.h:46
double angle
Definition: TurtleMobility.h:45
std::stack< long > loopVars
Definition: TurtleMobility.h:47
void inet::TurtleMobility::setTargetPosition ( )
overrideprotectedvirtual

Overridden from LineSegmentsMobilityBase.

Invokes resumeScript().

Implements inet::LineSegmentsMobilityBase.

72 {
73  resumeScript();
74 }
virtual void resumeScript()
Process next statements from script.
Definition: TurtleMobility.cc:86

Member Data Documentation

double inet::TurtleMobility::angle
protected
BorderPolicy inet::TurtleMobility::borderPolicy
protected
std::stack<long> inet::TurtleMobility::loopVars
protected
double inet::TurtleMobility::maxSpeed
protected

Referenced by computeMaxSpeed().

cXMLElement* inet::TurtleMobility::nextStatement
protected
double inet::TurtleMobility::speed
protected
cXMLElement* inet::TurtleMobility::turtleScript
protected

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