INET Framework for OMNeT++/OMNEST
MassMobility.cc
Go to the documentation of this file.
00001 //
00002 // Author: Emin Ilker Cetinbas (niw3_at_yahoo_d0t_com)
00003 // Generalization: Andras Varga
00004 // Copyright (C) 2005 Emin Ilker Cetinbas, Andras Varga
00005 //
00006 // This program is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU General Public License
00008 // as published by the Free Software Foundation; either version 2
00009 // of the License, or (at your option) any later version.
00010 //
00011 // This program is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU General Public License
00017 // along with this program; if not, see <http://www.gnu.org/licenses/>.
00018 //
00019 
00020 
00021 #include "MassMobility.h"
00022 #include "FWMath.h"
00023 
00024 
00025 #define MK_UPDATE_POS 100
00026 #define MK_CHANGE_DIR 101
00027 
00028 Define_Module(MassMobility);
00029 
00030 
00037 void MassMobility::initialize(int stage)
00038 {
00039     BasicMobility::initialize(stage);
00040 
00041     EV << "initializing MassMobility stage " << stage << endl;
00042 
00043     if (stage == 0)
00044     {
00045         updateInterval = par("updateInterval");
00046 
00047         changeInterval = &par("changeInterval");
00048         changeAngleBy = &par("changeAngleBy");
00049         speed = &par("speed");
00050 
00051         // initial speed and angle
00052         currentSpeed = speed->doubleValue();
00053         currentAngle = uniform(0, 360);
00054         step.x = currentSpeed * cos(PI * currentAngle / 180) * updateInterval;
00055         step.y = currentSpeed * sin(PI * currentAngle / 180) * updateInterval;
00056 
00057         scheduleAt(simTime() + uniform(0, updateInterval), new cMessage("move", MK_UPDATE_POS));
00058         scheduleAt(simTime() + uniform(0, changeInterval->doubleValue()), new cMessage("turn", MK_CHANGE_DIR));
00059     }
00060 }
00061 
00062 
00066 void MassMobility::handleSelfMsg(cMessage * msg)
00067 {
00068     switch (msg->getKind())
00069     {
00070     case MK_UPDATE_POS:
00071         move();
00072         updatePosition();
00073         scheduleAt(simTime() + updateInterval, msg);
00074         break;
00075     case MK_CHANGE_DIR:
00076         currentAngle += changeAngleBy->doubleValue();
00077         currentSpeed = speed->doubleValue();
00078         step.x = currentSpeed * cos(PI * currentAngle / 180) * updateInterval;
00079         step.y = currentSpeed * sin(PI * currentAngle / 180) * updateInterval;
00080         scheduleAt(simTime() + changeInterval->doubleValue(), msg);
00081         break;
00082     default:
00083         opp_error("Unknown self message kind in MassMobility class");
00084         break;
00085     }
00086 
00087 }
00088 
00093 void MassMobility::move()
00094 {
00095     pos += step;
00096 
00097     // do something if we reach the wall
00098     Coord dummy;
00099     handleIfOutside(REFLECT, dummy, step, currentAngle);
00100 
00101     EV << " xpos= " << pos.x << " ypos=" << pos.y << " speed=" << currentSpeed << endl;
00102 }
00103