INET Framework for OMNeT++/OMNEST
LinearMobility.cc
Go to the documentation of this file.
00001 //
00002 // Author: Emin Ilker Cetinbas (niw3_at_yahoo_d0t_com)
00003 // Copyright (C) 2005 Emin Ilker Cetinbas
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00017 //
00018 
00019 #include "LinearMobility.h"
00020 #include "FWMath.h"
00021 
00022 
00023 Define_Module(LinearMobility);
00024 
00025 
00026 void LinearMobility::initialize(int stage)
00027 {
00028     BasicMobility::initialize(stage);
00029 
00030     EV << "initializing LinearMobility stage " << stage << endl;
00031 
00032     if (stage == 0)
00033     {
00034         updateInterval = par("updateInterval");
00035         speed = par("speed");
00036         angle = par("angle");
00037         acceleration = par("acceleration");
00038         angle = fmod(angle,360);
00039 
00040         // if the initial speed is lower than 0, the node is stationary
00041         stationary = (speed == 0);
00042 
00043         // host moves the first time after some random delay to avoid synchronized movements
00044         if (!stationary)
00045             scheduleAt(simTime() + uniform(0, updateInterval), new cMessage("move"));
00046     }
00047 }
00048 
00049 
00054 void LinearMobility::handleSelfMsg(cMessage * msg)
00055 {
00056     move();
00057     updatePosition();
00058     if (!stationary)
00059         scheduleAt(simTime() + updateInterval, msg);
00060 }
00061 
00066 void LinearMobility::move()
00067 {
00068     pos.x += speed * cos(PI * angle / 180) * updateInterval;
00069     pos.y += speed * sin(PI * angle / 180) * updateInterval;
00070 
00071     // do something if we reach the wall
00072     Coord dummy;
00073     handleIfOutside(REFLECT, dummy, dummy, angle);
00074 
00075     // accelerate
00076     speed += acceleration * updateInterval;
00077     if (speed <= 0)
00078     {
00079         speed = 0;
00080         stationary = true;
00081     }
00082 
00083     EV << " xpos= " << pos.x << " ypos=" << pos.y << " speed=" << speed << endl;
00084 }