00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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
00088 move.setStart(targetPos, simTime());
00089
00090
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
00095
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 }