|
|
| 76 |
vector<string> svals; // string value for each token |
76 |
vector<string> svals; // string value for each token |
| 77 |
}; |
77 |
}; |
| 78 |
|
78 |
|
|
|
79 |
struct DestinationPoint |
| 80 |
{ |
| 81 |
Vector m_startPosition; |
| 82 |
Vector m_speed; |
| 83 |
Vector m_finalPosition; // Final destination which is actually reached |
| 84 |
EventId m_stopEvent; // Event scheduling node's stop |
| 85 |
double m_travelStartTime; |
| 86 |
double m_targetArrivalTime; // When a station arrives to a destination |
| 87 |
DestinationPoint () : |
| 88 |
m_startPosition (Vector (0,0,0)), |
| 89 |
m_speed (Vector (0,0,0)), |
| 90 |
m_finalPosition (Vector (0,0,0)), |
| 91 |
m_travelStartTime (0), |
| 92 |
m_targetArrivalTime (0) |
| 93 |
{}; |
| 94 |
}; |
| 95 |
|
| 79 |
|
96 |
|
| 80 |
// Parses a line of ns2 mobility |
97 |
// Parses a line of ns2 mobility |
| 81 |
static ParseResult ParseNs2Line (const string& str); |
98 |
static ParseResult ParseNs2Line (const string& str); |
|
|
| 115 |
static bool IsSchedMobilityPos (ParseResult pr); |
132 |
static bool IsSchedMobilityPos (ParseResult pr); |
| 116 |
|
133 |
|
| 117 |
// Set waypoints and speed for movement. |
134 |
// Set waypoints and speed for movement. |
| 118 |
static Vector SetMovement (Ptr<ConstantVelocityMobilityModel> model, Vector lastPos, double at, |
135 |
static DestinationPoint SetMovement (Ptr<ConstantVelocityMobilityModel> model, Vector lastPos, double at, |
| 119 |
double xFinalPosition, double yFinalPosition, double speed); |
136 |
double xFinalPosition, double yFinalPosition, double speed); |
| 120 |
|
137 |
|
| 121 |
// Set initial position for a node |
138 |
// Set initial position for a node |
| 122 |
static Vector SetInitialPosition (Ptr<ConstantVelocityMobilityModel> model, string coord, double coordVal); |
139 |
static Vector SetInitialPosition (Ptr<ConstantVelocityMobilityModel> model, string coord, double coordVal); |
|
|
| 155 |
void |
172 |
void |
| 156 |
Ns2MobilityHelper::ConfigNodesMovements (const ObjectStore &store) const |
173 |
Ns2MobilityHelper::ConfigNodesMovements (const ObjectStore &store) const |
| 157 |
{ |
174 |
{ |
| 158 |
map<int, Vector> last_pos; // Vector containing lasts positions for each node |
175 |
map<int, DestinationPoint> last_pos; // Vector containing lasts positions for each node |
| 159 |
|
176 |
|
| 160 |
std::ifstream file (m_filename.c_str (), std::ios::in); |
177 |
std::ifstream file (m_filename.c_str (), std::ios::in); |
| 161 |
if (file.is_open ()) |
178 |
if (file.is_open ()) |
|
|
| 209 |
*/ |
226 |
*/ |
| 210 |
if (IsSetInitialPos (pr)) |
227 |
if (IsSetInitialPos (pr)) |
| 211 |
{ |
228 |
{ |
| 212 |
// coord coord value |
229 |
DestinationPoint point; |
| 213 |
last_pos[iNodeId] = SetInitialPosition (model, pr.tokens[2], pr.dvals[3]); |
230 |
// coord coord value |
|
|
231 |
point.m_finalPosition = SetInitialPosition (model, pr.tokens[2], pr.dvals[3]); |
| 232 |
last_pos[iNodeId] = point; |
| 214 |
|
233 |
|
| 215 |
// Log new position |
234 |
// Log new position |
| 216 |
NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId << |
235 |
NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId << |
| 217 |
" x=" << last_pos[iNodeId].x << " y=" << last_pos[iNodeId].y << " z=" << last_pos[iNodeId].z); |
236 |
" position = " << last_pos[iNodeId].m_finalPosition); |
| 218 |
} |
237 |
} |
| 219 |
|
238 |
|
| 220 |
else // NOW EVENTS TO BE SCHEDULED |
239 |
else // NOW EVENTS TO BE SCHEDULED |
|
|
| 245 |
*/ |
264 |
*/ |
| 246 |
if (IsSchedMobilityPos (pr)) |
265 |
if (IsSchedMobilityPos (pr)) |
| 247 |
{ |
266 |
{ |
|
|
267 |
if (last_pos[iNodeId].m_targetArrivalTime > at) |
| 268 |
{ |
| 269 |
NS_LOG_LOGIC ("Did not reach a destination! stoptime = " << last_pos[iNodeId].m_targetArrivalTime << ", at = "<< at); |
| 270 |
double actuallytraveled = at - last_pos[iNodeId].m_travelStartTime; |
| 271 |
Vector reached = Vector ( |
| 272 |
last_pos[iNodeId].m_startPosition.x + last_pos[iNodeId].m_speed.x * actuallytraveled, |
| 273 |
last_pos[iNodeId].m_startPosition.y + last_pos[iNodeId].m_speed.y * actuallytraveled, |
| 274 |
0 |
| 275 |
); |
| 276 |
NS_LOG_LOGIC ("Final point = " << last_pos[iNodeId].m_finalPosition << ", actually reached = " << reached); |
| 277 |
last_pos[iNodeId].m_stopEvent.Cancel (); |
| 278 |
last_pos[iNodeId].m_finalPosition = reached; |
| 279 |
} |
| 248 |
// last position time X coord Y coord velocity |
280 |
// last position time X coord Y coord velocity |
| 249 |
last_pos[iNodeId] = SetMovement (model, last_pos[iNodeId], at, pr.dvals[5], pr.dvals[6], pr.dvals[7]); |
281 |
last_pos[iNodeId] = SetMovement (model, last_pos[iNodeId].m_finalPosition, at, pr.dvals[5], pr.dvals[6], pr.dvals[7]); |
| 250 |
|
282 |
|
| 251 |
// Log new position |
283 |
// Log new position |
| 252 |
NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId << |
284 |
NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId << " position =" << last_pos[iNodeId].m_finalPosition); |
| 253 |
" x=" << last_pos[iNodeId].x << " y=" << last_pos[iNodeId].y << " z=" << last_pos[iNodeId].z); |
|
|
| 254 |
} |
285 |
} |
| 255 |
|
286 |
|
| 256 |
|
287 |
|
|
|
| 261 |
else if (IsSchedSetPos (pr)) |
292 |
else if (IsSchedSetPos (pr)) |
| 262 |
{ |
293 |
{ |
| 263 |
// time coordinate coord value |
294 |
// time coordinate coord value |
| 264 |
last_pos[iNodeId] = SetSchedPosition (model, at, pr.tokens[5], pr.dvals[6]); |
295 |
last_pos[iNodeId].m_finalPosition = SetSchedPosition (model, at, pr.tokens[5], pr.dvals[6]); |
| 265 |
|
296 |
if (last_pos[iNodeId].m_targetArrivalTime > at) |
|
|
297 |
{ |
| 298 |
last_pos[iNodeId].m_stopEvent.Cancel (); |
| 299 |
} |
| 300 |
last_pos[iNodeId].m_targetArrivalTime = at; |
| 301 |
last_pos[iNodeId].m_travelStartTime = at; |
| 266 |
// Log new position |
302 |
// Log new position |
| 267 |
NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId << |
303 |
NS_LOG_DEBUG ("Positions after parse for node " << iNodeId << " " << nodeId << |
| 268 |
" x=" << last_pos[iNodeId].x << " y=" << last_pos[iNodeId].y << " z=" << last_pos[iNodeId].z); |
304 |
" position =" << last_pos[iNodeId].m_finalPosition); |
| 269 |
} |
305 |
} |
| 270 |
else |
306 |
else |
| 271 |
{ |
307 |
{ |
|
|
| 574 |
|
610 |
|
| 575 |
} |
611 |
} |
| 576 |
|
612 |
|
| 577 |
Vector |
613 |
DestinationPoint |
| 578 |
SetMovement (Ptr<ConstantVelocityMobilityModel> model, Vector last_pos, double at, |
614 |
SetMovement (Ptr<ConstantVelocityMobilityModel> model, Vector last_pos, double at, |
| 579 |
double xFinalPosition, double yFinalPosition, double speed) |
615 |
double xFinalPosition, double yFinalPosition, double speed) |
| 580 |
{ |
616 |
{ |
| 581 |
Vector position; |
617 |
DestinationPoint retval; |
| 582 |
position.x = last_pos.x; |
618 |
retval.m_startPosition = last_pos; |
| 583 |
position.y = last_pos.y; |
619 |
retval.m_finalPosition = last_pos; |
| 584 |
position.z = last_pos.z; |
620 |
retval.m_travelStartTime = at; |
|
|
621 |
retval.m_targetArrivalTime = at; |
| 585 |
|
622 |
|
| 586 |
if (speed == 0) |
623 |
if (speed == 0) |
| 587 |
{ |
624 |
{ |
| 588 |
// We have to maintain last position, and stop the movement |
625 |
// We have to maintain last position, and stop the movement |
| 589 |
Simulator::Schedule (Seconds (at), &ConstantVelocityMobilityModel::SetVelocity, model, |
626 |
retval.m_stopEvent = Simulator::Schedule (Seconds (at), &ConstantVelocityMobilityModel::SetVelocity, model, |
| 590 |
Vector (0, 0, 0)); |
627 |
Vector (0, 0, 0)); |
|
|
628 |
return retval; |
| 591 |
} |
629 |
} |
| 592 |
else if (speed > 0) |
630 |
if (speed > 0) |
| 593 |
{ |
631 |
{ |
| 594 |
// first calculate the time; time = distance / speed |
632 |
// first calculate the time; time = distance / speed |
| 595 |
double time = sqrt (pow (xFinalPosition - position.x, 2) + pow (yFinalPosition - position.y, 2)) / speed; |
633 |
double time = sqrt (pow (xFinalPosition - retval.m_finalPosition.x, 2) + pow (yFinalPosition - retval.m_finalPosition.y, 2)) / speed; |
| 596 |
NS_LOG_DEBUG ("at=" << at << " time=" << time); |
634 |
NS_LOG_DEBUG ("at=" << at << " time=" << time); |
| 597 |
// now calculate the xSpeed = distance / time |
635 |
// now calculate the xSpeed = distance / time |
| 598 |
double xSpeed = (xFinalPosition - position.x) / time; |
636 |
double xSpeed = (xFinalPosition - retval.m_finalPosition.x) / time; |
| 599 |
double ySpeed = (yFinalPosition - position.y) / time; // & same with ySpeed |
637 |
double ySpeed = (yFinalPosition - retval.m_finalPosition.y) / time; // & same with ySpeed |
|
|
638 |
retval.m_speed = Vector (xSpeed, ySpeed, 0); |
| 600 |
|
639 |
|
| 601 |
// quick and dirty set zSpeed = 0 |
640 |
// quick and dirty set zSpeed = 0 |
| 602 |
double zSpeed = 0; |
641 |
double zSpeed = 0; |
|
|
| 604 |
NS_LOG_DEBUG ("Calculated Speed: X=" << xSpeed << " Y=" << ySpeed << " Z=" << zSpeed); |
643 |
NS_LOG_DEBUG ("Calculated Speed: X=" << xSpeed << " Y=" << ySpeed << " Z=" << zSpeed); |
| 605 |
|
644 |
|
| 606 |
// Set the Values |
645 |
// Set the Values |
| 607 |
Simulator::Schedule (Seconds (at), &ConstantVelocityMobilityModel::SetVelocity, model, |
|
|
| 608 |
Vector (xSpeed, ySpeed, zSpeed)); |
| 609 |
|
| 610 |
if (time >= 0) |
646 |
if (time >= 0) |
| 611 |
{ |
647 |
{ |
| 612 |
Simulator::Schedule (Seconds (at + time), &ConstantVelocityMobilityModel::SetVelocity, |
648 |
Simulator::Schedule (Seconds (at), &ConstantVelocityMobilityModel::SetVelocity, model, Vector (xSpeed, ySpeed, zSpeed)); |
| 613 |
model, Vector (0, 0, 0)); |
|
|
| 614 |
} |
649 |
} |
| 615 |
|
650 |
retval.m_stopEvent = Simulator::Schedule (Seconds (at + time), &ConstantVelocityMobilityModel::SetVelocity, model, Vector (0, 0, 0)); |
| 616 |
position.x = xFinalPosition; |
651 |
retval.m_finalPosition.x += xSpeed * time; |
| 617 |
position.y = yFinalPosition; |
652 |
retval.m_finalPosition.y += ySpeed * time; |
| 618 |
position.z = 0; |
653 |
retval.m_targetArrivalTime += time; |
| 619 |
} |
654 |
} |
| 620 |
|
655 |
return retval; |
| 621 |
return position; |
|
|
| 622 |
} |
656 |
} |
| 623 |
|
657 |
|
| 624 |
|
658 |
|