MassMobility.cc

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, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 //
00020 
00021 
00022 #include "MassMobility.h"
00023 
00024 #include <FWMath.h>
00025 
00026 
00027 Define_Module(MassMobility);
00028 
00029 
00036 void MassMobility::initialize(int stage)
00037 {
00038     BaseMobility::initialize(stage);
00039 
00040     debugEV << "initializing MassMobility stage " << stage << endl;
00041 
00042     if (stage == 0)
00043     {
00044         changeInterval = &par("changeInterval");
00045         changeAngleBy = &par("changeAngleBy");
00046 
00047         // initial speed and angle
00048         move.setSpeed(par("speed"));
00049         currentAngle = uniform(0, 360);
00050 
00051         step.setX(move.getSpeed() * cos(PI * currentAngle / 180) * updateInterval.dbl());
00052         step.setY(move.getSpeed() * sin(PI * currentAngle / 180) * updateInterval.dbl());
00053 
00054     }
00055     else if( stage == 1 )
00056     {
00057       if(!world->use2D()) {
00058       opp_warning("This mobility module does not yet support 3 dimensional movement."\
00059             "Movements will probably be incorrect.");
00060     }
00061 
00062     // start moving: set direction and start time
00063     move.setDirectionByTarget(move.getStartPos() + step);
00064     move.setStart(move.getStartPos(), simTime());
00065 
00066     targetPos = move.getStartPos();
00067 
00068     scheduleAt(simTime() + uniform(0, changeInterval->doubleValue()), new cMessage("turn", MK_CHANGE_DIR));
00069     }
00070 }
00071 
00072 
00076 void MassMobility::handleSelfMsg(cMessage * msg)
00077 {
00078     Coord dummy(world->use2D());
00079 
00080     switch (msg->getKind()){
00081     case MOVE_HOST:
00082     BaseMobility::handleSelfMsg( msg );
00083     break;
00084     case MK_CHANGE_DIR:
00085     currentAngle += changeAngleBy->doubleValue();
00086 
00087     step.setX(move.getSpeed() * cos(PI * currentAngle / 180) * updateInterval.dbl());
00088     step.setY(move.getSpeed() * sin(PI * currentAngle / 180) * updateInterval.dbl());
00089 
00090     move.setDirectionByTarget(move.getStartPos() + step);
00091 
00092     scheduleAt(simTime() + changeInterval->doubleValue(), msg);
00093     break;
00094     default:
00095     opp_error("Unknown self message kind in MassMobility class");
00096     break;
00097     }
00098 
00099 }
00100 
00105 void MassMobility::makeMove()
00106 {
00107     move.setStart(targetPos, simTime());
00108     targetPos += step;
00109 
00110     // do something if we reach the wall
00111     fixIfHostGetsOutside();
00112 }
00113 
00114 void MassMobility::fixIfHostGetsOutside()
00115 {
00116     Coord dummy(world->use2D());
00117 
00118     handleIfOutside( REFLECT, targetPos, dummy, step, currentAngle );
00119 }