TractorMobility.cc

00001 //
00002 // Copyright (C) 2007 Peterpaul Klein Haneveld
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 "TractorMobility.h"
00020 
00021 #include <FWMath.h>
00022 
00023 
00024 Define_Module(TractorMobility);
00025 
00026 
00030 void TractorMobility::initialize(int stage)
00031 {
00032   BaseMobility::initialize(stage);
00033 
00034   debugEV << "initializing TractorMobility stage " << stage << endl;
00035 
00036   if (stage == 0)
00037   {
00038     x1 = par("x1");
00039     y1 = par("y1");
00040     x2 = par("x2");
00041     y2 = par("y2");
00042     rows = par("rows");
00043 
00044     move.setSpeed(par("speed"));
00045 
00046     dx = x2-x1;
00047     dy = y2-y1;
00048     row_length = dx;
00049     row_width = dy / rows;
00050 
00051     path_length = rows * (row_length + row_width);
00052     position = 0.0;
00053 
00054     calculateXY();
00055 
00056     move.setStart(targetPos);
00057   }
00058   else
00059   {
00060     if(!world->use2D()) {
00061       opp_warning("This mobility module does not yet support 3 dimensional movement."\
00062             "Movements will probably be incorrect.");
00063     }
00064   }
00065 }
00066 
00067 
00068 void TractorMobility::makeMove()
00069 {
00070   position += move.getSpeed() * updateInterval.dbl();
00071 
00072   calculateXY();
00073 
00074   fixIfHostGetsOutside();
00075 }
00076 
00077 void TractorMobility::fixIfHostGetsOutside()
00078 {
00079   Coord dummy(world->use2D());
00080   double dum;
00081 
00082   handleIfOutside( RAISEERROR, targetPos, dummy, dummy, dum );
00083 }
00084 
00085 void TractorMobility::calculateXY()
00086 {
00087   // update the position
00088   move.setStart(targetPos, simTime());
00089 
00090   // calculate new target position
00091   bool moving_away = fmod(position / path_length, 2.0) < 1.0;
00092   double path_position = fmod(position, (path_length));
00093   double row_number = fmod(position / (row_length + row_width), rows * 2);
00094   /* row position can be either in a row or on the edge, so
00095    * the range for row_position is [0, row_length + row_width)
00096    */
00097   double row_position = fmod(path_position, (row_length + row_width));
00098   bool on_the_edge = row_position >= row_length;
00099 
00100   if (moving_away) {
00101     if (on_the_edge) {
00102       targetPos.setX(x1 + (fmod(row_number, 2.0) < 1.0 ? row_length : (0)));
00103             targetPos.setY(y1 + (FWMath::floorToZero(row_number) * row_width + row_position - row_length));
00104     } else {
00105       targetPos.setX(x1 + (fmod(row_number, 2.0) < 1.0 ? row_position : (row_length - row_position)));
00106       targetPos.setY(y1 + (FWMath::floorToZero(row_number) * row_width));
00107     }
00108   } else {
00109     if (on_the_edge) {
00110       targetPos.setX(x1 + (fmod(row_number, 2.0) < 1.0 ? row_length : (0)));
00111       targetPos.setY(y2 - ((FWMath::floorToZero(row_number) - rows) * row_width + row_position - row_length));
00112     } else {
00113       targetPos.setX(x1 + (fmod(row_number, 2.0) < 1.0 ? row_position : (row_length - row_position)));
00114       targetPos.setY(y2 - ((FWMath::floorToZero(row_number) - rows) * row_width));
00115     }
00116   }
00117 
00118   move.setDirectionByTarget(targetPos);
00119 }