Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

Modules/PlayersLocator/GT2004PlayersLocator.cpp

Go to the documentation of this file.
00001 /**
00002 * @file GT2004PlayersLocator.cpp
00003 * 
00004 * This file contains a class for players localization.
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   // Own perceptes
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   // Teammessages
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   // own  
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   // opponent
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     //correct y_coordinate
00149     newY = yPosLeftSideline;
00150     //compute x_coordinate
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     //correct y_coordinate
00159     newY = yPosRightSideline;
00160     //compute x_coordinate
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     //correct x-coordinate
00169     newX = xPosOpponentGroundline;
00170     //compute y-coordinate
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     //correct x-coordinate
00179     newX = xPosOwnGroundline;
00180     //compute y-coordinate
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 * Change log :
00257 * 
00258 * $Log: GT2004PlayersLocator.cpp,v $
00259 * Revision 1.1  2004/07/10 00:18:31  spranger
00260 * renamed (readded) for coderelease
00261 *
00262 * Revision 1.1.1.1  2004/05/22 17:20:39  cvsadm
00263 * created new repository GT2004_WM
00264 *
00265 * Revision 1.2  2004/03/08 01:07:15  roefer
00266 * Interfaces should be const
00267 *
00268 * Revision 1.1  2003/10/06 14:10:15  cvsadm
00269 * Created GT2004 (M.J.)
00270 *
00271 * Revision 1.2  2003/07/02 19:14:53  loetzsch
00272 * cleaned up team message collection
00273 *
00274 * Revision 1.1.1.1  2003/07/02 09:40:24  cvsadm
00275 * created new repository for the competitions in Padova from the 
00276 * tamara CVS (Tuesday 2:00 pm)
00277 *
00278 * removed unused solutions
00279 *
00280 * Revision 1.8  2003/05/27 12:48:32  mkunz
00281 * parameter tuning
00282 *
00283 * Revision 1.7  2003/05/27 09:06:39  mkunz
00284 * adapted to changes in VAPoints
00285 *
00286 * Revision 1.6  2003/05/16 14:50:13  mkunz
00287 * more points again
00288 *
00289 * Revision 1.5  2003/05/14 13:19:57  mkunz
00290 * bigger reorganization
00291 *
00292 * Revision 1.4  2003/05/13 14:31:16  mkunz
00293 * full transfer toVAPoints
00294 *
00295 * Revision 1.3  2003/05/08 19:51:23  mkunz
00296 * switched to VAPoints
00297 *
00298 * Revision 1.2  2003/04/15 15:52:09  risler
00299 * DDD GO 2003 code integrated
00300 *
00301 * Revision 1.3  2003/04/12 17:01:27  mkunz
00302 * back again
00303 *
00304 * Revision 1.2  2003/03/31 18:17:43  max
00305 * tamara update
00306 *
00307 * Revision 1.1  2003/03/29 12:51:03  mkunz
00308 * just a copy of GT2001
00309 *
00310 *
00311 */

Generated on Thu Sep 23 19:57:31 2004 for GT2004 by doxygen 1.3.6