CircleMobility.cc

00001 //
00002 // Copyright (C) 2005 Andras Varga
00003 //
00004 // This program is free software; you can redistribute it and/or
00005 // modify it under the terms of the GNU General Public License
00006 // as published by the Free Software Foundation; either version 2
00007 // of the License, or (at your option) any later version.
00008 //
00009 // This program is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with this program; if not, write to the Free Software
00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 //
00018 
00019 #include "CircleMobility.h"
00020 
00021 #include <FWMath.h>
00022 
00023 
00024 Define_Module(CircleMobility);
00025 
00026 
00027 void CircleMobility::initialize(int stage)
00028 {
00029     BaseMobility::initialize(stage);
00030 
00031     debugEV << "initializing CircleMobility stage " << stage << endl;
00032 
00033 
00034 
00035 
00036     if (stage == 0)
00037     {
00038         // read parameters
00039         center.setX(par("cx"));
00040         center.setY(par("cy"));
00041         r = par("r");
00042         ASSERT(r>0);
00043         angle = par("startAngle").doubleValue()/180.0*PI;
00044         move.setSpeed(par("speed"));
00045         omega = move.getSpeed()/r;
00046 
00047         // calculate initial position
00048         move.setStart( Coord(center.getX() + r * cos(angle), center.getY() + r * sin(angle)) );
00049 
00050         targetPos = move.getStartPos();
00051     }
00052     else
00053   {
00054     if(!world->use2D()) {
00055       opp_warning("This mobility module does not yet support 3 dimensional movement."\
00056             "Movements will probably be incorrect.");
00057     }
00058   }
00059 }
00060 
00061 
00062 void CircleMobility::makeMove()
00063 {
00064     move.setStart(targetPos, simTime());
00065 
00066     angle += omega * updateInterval.dbl();
00067     targetPos.setX(center.getX() + r * cos(angle));
00068     targetPos.setY(center.getY() + r * sin(angle));
00069 
00070     move.setDirectionByTarget(targetPos);
00071 
00072     fixIfHostGetsOutside();
00073 }
00074 
00075 void CircleMobility::fixIfHostGetsOutside()
00076 {
00077     Coord dummy(world->use2D());
00078     double dum;
00079 
00080     handleIfOutside( WRAP, targetPos, center, dummy, dum);
00081 }