INET Framework for OMNeT++/OMNEST
ConstSpeedMobility.cc
Go to the documentation of this file.
00001 /***************************************************************************
00002  * file:        ConstSpeedMobility.cc
00003  *
00004  * author:      Steffen Sroka
00005  *
00006  * copyright:   (C) 2004 Telecommunication Networks Group (TKN) at
00007  *              Technische Universitaet Berlin, Germany.
00008  *
00009  *              This program is free software; you can redistribute it
00010  *              and/or modify it under the terms of the GNU General Public
00011  *              License as published by the Free Software Foundation; either
00012  *              version 2 of the License, or (at your option) any later
00013  *              version.
00014  *              For further information see file COPYING
00015  *              in the top level directory
00016  ***************************************************************************
00017  * part of:     framework implementation developed by tkn
00018  **************************************************************************/
00019 
00020 
00021 #include "ConstSpeedMobility.h"
00022 
00023 #include "FWMath.h"
00024 
00025 
00026 Define_Module(ConstSpeedMobility);
00027 
00028 
00035 void ConstSpeedMobility::initialize(int stage)
00036 {
00037     BasicMobility::initialize(stage);
00038 
00039     EV << "initializing ConstSpeedMobility stage " << stage << endl;
00040 
00041     if (stage == 0)
00042     {
00043         updateInterval = par("updateInterval");
00044         vHost = par("vHost");
00045 
00046         // if the initial speed is lower than 0, the node is stationary
00047         stationary = (vHost <= 0);
00048 
00049         //calculate the target position of the host if the host moves
00050         if (!stationary)
00051         {
00052             setTargetPosition();
00053             //host moves the first time after some random delay to avoid synchronized movements
00054             scheduleAt(simTime() + uniform(0, updateInterval), new cMessage("move"));
00055         }
00056     }
00057 }
00058 
00059 
00064 void ConstSpeedMobility::handleSelfMsg(cMessage * msg)
00065 {
00066     move();
00067     updatePosition();
00068     scheduleAt(simTime() + updateInterval, msg);
00069 }
00070 
00071 
00076 void ConstSpeedMobility::setTargetPosition()
00077 {
00078     targetPos = getRandomPosition();
00079     double distance = pos.distance(targetPos);
00080     double totalTime = distance / vHost;
00081     numSteps = FWMath::round(totalTime / updateInterval);
00082 
00083     stepSize = (targetPos - pos) / numSteps;
00084 
00085     step = 0;
00086 
00087     EV << "distance=" << distance << " xpos= " << targetPos.x << " ypos=" << targetPos.y
00088         << "totalTime=" << totalTime << " numSteps=" << numSteps << " vHost=" << vHost << endl;
00089 }
00090 
00091 
00096 void ConstSpeedMobility::move()
00097 {
00098     step++;
00099 
00100     if (step <= numSteps)
00101     {
00102         EV << "stepping forward. step #=" << step << " xpos= " << pos.x << " ypos=" << pos.
00103             y << endl;
00104 
00105         pos += stepSize;
00106     }
00107     else
00108     {
00109         EV << "destination reached.\n xpos= " << pos.x << " ypos=" << pos.y << endl;
00110 
00111         setTargetPosition();
00112     }
00113 }