00001
00002
00003
00004
00005
00006
00007 #include "GT2004PlayersLocator.h"
00008
00009 #include "Tools/FieldDimensions.h"
00010
00011 #include "Tools/Player.h"
00012
00013 #include "Tools/Debugging/Debugging.h"
00014
00015 #include "Platform/SystemCall.h"
00016
00017 const double GT2004PlayersLocator::TEAM_MESSAGE_VALIDITY = 0.3;
00018 const double GT2004PlayersLocator::MIN_MATCH_DIST = 500;
00019
00020 GT2004PlayersLocator::GT2004PlayersLocator(const PlayersLocatorInterfaces& interfaces)
00021 : PlayersLocator(interfaces),
00022 opponentPointsWithValidityAndAge(NUMBER_OF_POINTS_FOR_OPPONENT_PLAYERS, NUMBER_OF_OPPONENT_PLAYERS_TO_LOCATE),
00023 ownPointsWithValidityAndAge(NUMBER_OF_POINTS_FOR_OWN_PLAYERS, NUMBER_OF_OWN_PLAYERS_TO_LOCATE)
00024 {
00025 }
00026
00027 void GT2004PlayersLocator::execute()
00028 {
00029 Player::teamColor ownColor = getPlayer().getTeamColor();
00030
00031
00032
00033 if(ownColor == Player::red)
00034 {
00035 addOppPlayerPercepts(
00036 playersPercept.bluePlayers,
00037 playersPercept.numberOfBluePlayers,
00038 robotPose,
00039 1.0,
00040 SystemCall::getCurrentSystemTime());
00041
00042 addOwnPlayerPercepts(
00043 playersPercept.redPlayers,
00044 playersPercept.numberOfRedPlayers,
00045 robotPose,
00046 1.0,
00047 SystemCall::getCurrentSystemTime());
00048 }
00049 else
00050 {
00051 addOwnPlayerPercepts(
00052 playersPercept.bluePlayers,
00053 playersPercept.numberOfBluePlayers,
00054 robotPose,
00055 1.0,
00056 SystemCall::getCurrentSystemTime());
00057
00058 addOppPlayerPercepts(
00059 playersPercept.redPlayers,
00060 playersPercept.numberOfRedPlayers,
00061 robotPose,
00062 1.0,
00063 SystemCall::getCurrentSystemTime());
00064 }
00065
00066
00067 int t;
00068
00069 for(t=0; t<teamMessageCollection.numberOfTeamMessages; t++)
00070 {
00071 if (teamMessageCollection[t].sendRobotPose)
00072 {
00073 ownPointsWithValidityAndAge.addPoint(
00074 (int) teamMessageCollection[t].robotPose.getPose().translation.x,
00075 (int) teamMessageCollection[t].robotPose.getPose().translation.y,
00076 teamMessageCollection[t].robotPose.getValidity(),
00077 teamMessageCollection[t].getTimeStampInOwnTime());
00078 }
00079 }
00080
00081 int i, x, y;
00082 double validity;
00083
00084
00085 ownPointsWithValidityAndAge.searchMaxima();
00086 playerPoseCollection.numberOfOwnPlayers = 0;
00087 for(i=0; i < NUMBER_OF_OWN_PLAYERS_TO_LOCATE; i++)
00088 {
00089 if(ownPointsWithValidityAndAge.getMaximum(i, x, y, validity))
00090 {
00091 playerPoseCollection.setOwnPlayerPose(i, x, y, 0, validity);
00092 playerPoseCollection.numberOfOwnPlayers++;
00093 }
00094 }
00095
00096
00097 opponentPointsWithValidityAndAge.searchMaxima();
00098
00099 playerPoseCollection.numberOfOpponentPlayers = 0;
00100 for(i=0; i < NUMBER_OF_OPPONENT_PLAYERS_TO_LOCATE; i++)
00101 {
00102 if(opponentPointsWithValidityAndAge.getMaximum(i, x, y, validity))
00103 {
00104 playerPoseCollection.setOpponentPlayerPose(i, x, y, 0, validity);
00105 playerPoseCollection.numberOfOpponentPlayers++;
00106 }
00107 }
00108
00109 int bestMatch;
00110 double currDist, nearestDist;
00111
00112 for(i=0; i < playerPoseCollection.numberOfOwnPlayers; i++)
00113 {
00114 bestMatch = -1;
00115 nearestDist = MIN_MATCH_DIST;
00116
00117 for(t=0; t<teamMessageCollection.numberOfTeamMessages; t++)
00118 {
00119 currDist = (playerPoseCollection.getOwnPlayerPose(i).getPose().translation - teamMessageCollection[t].robotPose.getPose().translation).abs();
00120 if (currDist < nearestDist)
00121 {
00122 bestMatch = t;
00123 nearestDist = currDist;
00124 }
00125 }
00126
00127 if (bestMatch != -1)
00128 {
00129 playerPoseCollection.setOwnPlayerPose(i,
00130 playerPoseCollection.getOwnPlayerPose(i).getPose().translation.x,
00131 playerPoseCollection.getOwnPlayerPose(i).getPose().translation.y,
00132 teamMessageCollection[bestMatch].robotPose.getPose().getAngle(),
00133 playerPoseCollection.getOwnPlayerPose(i).getValidity());
00134 }
00135 }
00136
00137 }
00138
00139 bool GT2004PlayersLocator::correctObstaclePosition
00140 (int oldX, int oldY, int &newX, int &newY)
00141 {
00142 newX = oldX;
00143 newY = oldY;
00144 bool isCorrected = false;
00145
00146 if(newY > yPosLeftSideline)
00147 {
00148
00149 newY = yPosLeftSideline;
00150
00151 newX = (int)(((double)oldX / (double)oldY) * (double)newY);
00152 oldX = newX;
00153 oldY = newY;
00154 isCorrected = true;
00155 }
00156 else if(newY < yPosRightSideline)
00157 {
00158
00159 newY = yPosRightSideline;
00160
00161 newX = (int)(((double)oldX / (double)oldY) * (double)newY);
00162 oldX = newX;
00163 oldY = newY;
00164 isCorrected = true;
00165 }
00166 if(newX > xPosOpponentGroundline)
00167 {
00168
00169 newX = xPosOpponentGroundline;
00170
00171 newY = (int)(((double)newX / (double)oldX) * (double)oldY);
00172 oldX = newX;
00173 oldY = newY;
00174 isCorrected = true;
00175 }
00176 else if(newX < xPosOwnGroundline)
00177 {
00178
00179 newX = xPosOwnGroundline;
00180
00181 newY = (int)(((double)newX / (double)oldX) * (double)oldY);
00182 oldX = newX;
00183 oldY = newY;
00184 isCorrected = true;
00185 }
00186 return (isCorrected);
00187 }
00188
00189 void GT2004PlayersLocator::addOwnPlayerPercepts(
00190 const SinglePlayerPercept playerPercepts[],
00191 int numberOfPlayerPercepts,
00192 const RobotPose& startRobotPose,
00193 const double collectionValidity,
00194 const unsigned long collectionTimestamp)
00195 {
00196 Vector2<double> p;
00197 int playerPositionX = 0,
00198 playerPositionY = 0;
00199 int correctedPlayerPositionX = 0,
00200 correctedPlayerPositionY = 0;
00201
00202 for(int players=0; players < numberOfPlayerPercepts; players++)
00203 {
00204 p = (startRobotPose.getPose() +
00205 Pose2D(playerPercepts[players].offset)).translation;
00206
00207 playerPositionX = (int)p.x;
00208 playerPositionY = (int)p.y;
00209
00210 correctObstaclePosition(
00211 playerPositionX, playerPositionY,
00212 correctedPlayerPositionX, correctedPlayerPositionY);
00213
00214 ownPointsWithValidityAndAge.addPoint(
00215 correctedPlayerPositionX,
00216 correctedPlayerPositionY,
00217 playerPercepts[players].validity * startRobotPose.getValidity() * collectionValidity,
00218 collectionTimestamp);
00219 }
00220 }
00221
00222 void GT2004PlayersLocator::addOppPlayerPercepts(
00223 const SinglePlayerPercept playerPercepts[],
00224 int numberOfPlayerPercepts,
00225 const RobotPose& startRobotPose,
00226 const double collectionValidity,
00227 const unsigned long collectionTimestamp)
00228 {
00229 Vector2<double> p;
00230 int playerPositionX = 0,
00231 playerPositionY = 0;
00232 int correctedPlayerPositionX = 0,
00233 correctedPlayerPositionY = 0;
00234
00235 for(int players=0; players < numberOfPlayerPercepts; players++)
00236 {
00237 p = (startRobotPose.getPose() +
00238 Pose2D(playerPercepts[players].offset)).translation;
00239
00240 playerPositionX = (int)p.x;
00241 playerPositionY = (int)p.y;
00242
00243 correctObstaclePosition(
00244 playerPositionX, playerPositionY,
00245 correctedPlayerPositionX, correctedPlayerPositionY);
00246
00247 opponentPointsWithValidityAndAge.addPoint(
00248 correctedPlayerPositionX,
00249 correctedPlayerPositionY,
00250 playerPercepts[players].validity * startRobotPose.getValidity() * collectionValidity,
00251 collectionTimestamp);
00252 }
00253 }
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311