00001 /** 00002 * @file BallModel.h 00003 * 00004 * Declaration of class BallPosition 00005 * 00006 * @author Martin Lötzsch 00007 * @author <A href=mailto:roefer@tzi.de>Thomas Röfer</A> 00008 * @author <a href="mailto:stefanuhrig@gmx.net">Stefan Uhrig</a> 00009 */ 00010 00011 #ifndef __BallModel_h_ 00012 #define __BallModel_h_ 00013 00014 #include "Tools/Streams/InOut.h" 00015 #include "Tools/Math/Pose2D.h" 00016 #include "Tools/Math/Geometry.h" 00017 #include "Tools/Debugging/DebugDrawings.h" 00018 #include "Tools/Streams/Streamable.h" 00019 00020 00021 /** 00022 * @class BallState 00023 * 00024 * Base class for a ball state. 00025 */ 00026 class BallState : public Streamable 00027 { 00028 public: 00029 /** 00030 * Standard constructor initializing the ball state. 00031 */ 00032 BallState(); 00033 00034 public: 00035 00036 /** 00037 * Resets all data of the ball state. 00038 */ 00039 void reset(); 00040 00041 /** 00042 * Sets the ball data with parameters relative to the robot. The parameters 00043 * relative to the field are *automatically* generated. 00044 * @param robotPose The robot pose. 00045 * @param px x-position of the ball relative to the robot (in mm). 00046 * @param py y-position of the ball relative to the robot (in mm). 00047 * @param vx x-speed of the ball relative to the robot (in mm/s). 00048 * @param vy y-speed of the ball relative to the robot (in mm/s). 00049 */ 00050 void setBallDataRelativeToRobot( 00051 const RobotPose& robotPose, 00052 double px, 00053 double py, 00054 double vx, 00055 double vy); 00056 00057 /** 00058 * Sets the ball data with parameters relative to the field. The parameters 00059 * relative to the robot are *automatically* generated. 00060 * @param robotPose The robot pose. 00061 * @param px x-position of the ball relative to the field (in mm). 00062 * @param py y-position of the ball relative to the field (in mm). 00063 * @param vx x-speed of the ball relative to the field (in mm/s). 00064 * @param vy y-speed of the ball relative to the field (in mm/s). 00065 */ 00066 void setBallDataRelativeToField( 00067 const RobotPose& robotPose, 00068 double px, 00069 double py, 00070 double vx, 00071 double vy); 00072 00073 /** 00074 * Sets the gaussian distribution of the ball position relative to the 00075 * robot. The distribution relative to the field is *automatically* 00076 * generated. 00077 * @param robotPose The robot pose. 00078 * @param orientation The angle from the x-axis of the robot to the major 00079 * axis of the ellipsis (in radians). 00080 * @param majorAxis The length of the major axis (that is the standard 00081 * deviation in the major direction) (in mm). 00082 * @param minorAxis The length of the minor axis (that is the standard 00083 * deviation in the minor direction) (in mm). 00084 */ 00085 void setPositionGaussianDataRelativeToRobot( 00086 const RobotPose& robotPose, 00087 double orientation, 00088 double majorAxis, 00089 double minorAxis); 00090 00091 /** 00092 * Sets the gaussian distribution of the ball position relative to the 00093 * field. The distribution relative to the robot is *automatically* 00094 * generated. 00095 * @param robotPose The robot pose. 00096 * @param orientation The angle from the x-axis of the field to the major 00097 * axis of the ellipsis (in radians). 00098 * @param majorAxis The length of the major axis (that is the standard 00099 * deviation in the major direction) (in mm). 00100 * @param minorAxis The length of the minor axis (that is the standard 00101 * deviation in the minor direction) (in mm). 00102 */ 00103 void setPositionGaussianDataRelativeToField( 00104 const RobotPose& robotPose, 00105 double orientation, 00106 double majorAxis, 00107 double minorAxis); 00108 00109 00110 /** 00111 * Sets the gaussian distribution of the ball speed relative to the 00112 * robot. The distribution relative to the field is *automatically* 00113 * generated. 00114 * @param robotPose The robot pose. 00115 * @param orientation The angle from the x-axis of the robot to the major 00116 * axis of the ellipsis (in radians). 00117 * @param majorAxis The length of the major axis (that is the standard 00118 * deviation in the major direction) (in mm). 00119 * @param minorAxis The length of the minor axis (that is the standard 00120 * deviation in the minor direction) (in mm). 00121 */ 00122 void setSpeedGaussianDataRelativeToRobot( 00123 const RobotPose& robotPose, 00124 double orientation, 00125 double majorAxis, 00126 double minorAxis); 00127 00128 /** 00129 * Sets the gaussian distribution of the ball speed relative to the 00130 * field. The distribution relative to the robot is *automatically* 00131 * generated. 00132 * @param robotPose The robot pose. 00133 * @param orientation The angle from the x-axis of the field to the major 00134 * axis of the ellipsis (in radians). 00135 * @param majorAxis The length of the major axis (that is the standard 00136 * deviation in the major direction) (in mm). 00137 * @param minorAxis The length of the minor axis (that is the standard 00138 * deviation in the minor direction) (in mm). 00139 */ 00140 void setSpeedGaussianDataRelativeToField( 00141 const RobotPose& robotPose, 00142 double orientation, 00143 double majorAxis, 00144 double minorAxis); 00145 00146 /** 00147 * Returns a position validity in the range from 0.0 to 1.0. This validity 00148 * is calculated from the position gauss if present. Probably there are 00149 * more sophisticated ways to calc the validity from the gaussian 00150 * distribution. 00151 * @return Validity calculated from the position gauss in the range from 00152 * 0.0 to 1.0. Return value is 1.0 if no position gauss present. 00153 */ 00154 double getPositionValidity() const; 00155 00156 /** 00157 * Returns a speed validity in the range from 0.0 to 1.0. This validity 00158 * is calculated from the speed gauss if present. Probably there are 00159 * more sophisticated ways to calc the validity from the gaussian 00160 * distribution. 00161 * @return Validity calculated from the speed gauss in the range from 00162 * 0.0 to 1.0. Return value is 1.0 if no speed gauss present. 00163 */ 00164 double getSpeedValidity() const; 00165 00166 /** 00167 * Draws the position of the ball to the field. Drawing must be finished 00168 * by the caller with DEBUG_DRAWING_FINISHED(ballLocatorField). 00169 * @param color The color to use for the drawing. 00170 */ 00171 void drawPosition(Drawings::Color color) const; 00172 00173 /** 00174 * Draws the speed as arrow to the field. Drawing must be finished 00175 * by the caller with DEBUG_DRAWING_FINISHED(ballLocatorField). 00176 * @param color The color to use for the drawing. 00177 */ 00178 void drawSpeed(Drawings::Color color) const; 00179 00180 /** 00181 * Draws the position gaussian (if set) as the two axes of the ellipsis. 00182 * Drawing must be finished by the caller with 00183 * DEBUG_DRAWING_FINISHED(ballLocatorField). 00184 * @param color The color to use for the drawing. 00185 */ 00186 void drawPositionGaussian(Drawings::Color color) const; 00187 00188 /** 00189 * Draws the speed gaussian (if set) as the two axes of the ellipsis. 00190 * Drawing must be finished by the caller with 00191 * DEBUG_DRAWING_FINISHED(ballLocatorField). 00192 * @param color The color to use for the drawing. 00193 */ 00194 void drawSpeedGaussian(Drawings::Color color) const; 00195 00196 //Functions for streaming with TeamMessagesMessage: 00197 // int read(In& stream, int length); 00198 //void write(Out& stream); 00199 // unsigned int length(); 00200 00201 protected: 00202 00203 /** 00204 * Sets several ball flags from the actual ball state. 00205 */ 00206 void setBallFlags(); 00207 00208 /** 00209 * Converts a position vector relative to the robot to a position vector 00210 * relative to the field. 00211 * @param robotPose The robot pose. 00212 * @param pv The position vector relative to the robot. 00213 * @return The position vector relative to the field. 00214 */ 00215 static Vector2<double> positionVectorFromRobotToField( 00216 const RobotPose& robotPose, 00217 const Vector2<double>& pv); 00218 00219 /** 00220 * Converts a direction vector relative to the robot to a direction vector 00221 * relative to the field. 00222 * @param robotPose The robot pose. 00223 * @param dv The direction vector relative to the robot. 00224 * @return The direction vector relative to the field. 00225 */ 00226 static Vector2<double> directionVectorFromRobotToField( 00227 const RobotPose& robotPose, 00228 const Vector2<double>& dv); 00229 00230 /** 00231 * Converts an orientation (in radians) relative to the robot to an 00232 * orientation relative to the field. 00233 * @param robotPose The robot pose. 00234 * @param orientation The orientation in radians relative to the robot. 00235 * @return The orientation in radians relative to the field. 00236 */ 00237 static double orientationFromRobotToField( 00238 const RobotPose& robotPose, 00239 double orientation); 00240 00241 /** 00242 * Converts a position vector relative to the field to a position vector 00243 * relative to the robot. 00244 * @param robotPose The robot pose. 00245 * @param pv The position vector relative to the field. 00246 * @return The position vector relative to the robot. 00247 */ 00248 static Vector2<double> positionVectorFromFieldToRobot( 00249 const RobotPose& robotPose, 00250 const Vector2<double>& pv); 00251 00252 /** 00253 * Converts a direction vector relative to the field to a direction vector 00254 * relative to the robot. 00255 * @param robotPose The robot pose. 00256 * @param pv The direction vector relative to the field. 00257 * @return The direction vector relative to the robot. 00258 */ 00259 static Vector2<double> directionVectorFromFieldToRobot( 00260 const RobotPose& robotPose, 00261 const Vector2<double>& dv); 00262 00263 /** 00264 * Converts an orientation (in radians) relative to the field to an 00265 * orientation relative to the robot. 00266 * @param robotPose The robot pose. 00267 * @param orientation The orientation in radians relative to the field. 00268 * @return The orientation in radians relative to the robot. 00269 */ 00270 static double orientationFromFieldToRobot( 00271 const RobotPose& robotPose, 00272 double orientation); 00273 00274 00275 public: 00276 00277 /** 00278 * If a ball locator calculates a ball position and velocity probability 00279 * by itself it can set this variable to true and set the variable 00280 * positionProb and velocityProb and the functions getPositionValidity 00281 * and getSpeedValidity will return those values. 00282 */ 00283 bool useGivenProbabilities; 00284 double positionProb; 00285 double velocityProb; 00286 00287 /// The position of the ball relative to the robot (in mm) 00288 Vector2<double> positionRobot; 00289 00290 /// The speed of the ball relative to the robot (in mm/s) 00291 Vector2<double> speedRobot; 00292 00293 /// The position of the ball relative to the field (in mm) 00294 Vector2<double> positionField; 00295 00296 /// The speed of the ball relative to the field (in mm/s) 00297 Vector2<double> speedField; 00298 00299 /// The robot pose 00300 RobotPose robotPose; 00301 00302 /// If true, the line from robot to ball crosses the goal line 00303 bool ballInFrontOfOpponentGoal; 00304 00305 /// If true, the ball rolls by left in a short distance 00306 bool ballRollsByLeft; 00307 bool ballRollsFarByLeft; 00308 00309 /// If true, the ball rolls by right in a short distance 00310 bool ballRollsByRight; 00311 bool ballRollsFarByRight; 00312 00313 /// If true, the ball rolls towards the robot 00314 bool ballRollsTowardsRobot; 00315 00316 /// If true, the ball has a high speed in any direction 00317 bool ballRollsFast; 00318 00319 /// this is the projected distance of the ball on the Y axis of the robot 00320 /// (in mm) 00321 double projectedDistanceOnYAxis; 00322 00323 /// time in ms which the ball needs to cross the robots y axis 00324 long timeTillBallCrossesYAxis; 00325 00326 /// True if the position gauss was set 00327 bool positionGaussSet; 00328 00329 /// The angle of the robot x-axis to the ellipse's major axis in radians. 00330 double positionGaussOrientationRobot; 00331 00332 /// The angle of the field x-axis to ellipse's major axis in radians. 00333 double positionGaussOrientationField; 00334 00335 /// The length of the ellipsis major axis (that is the standard deviation) 00336 /// (in mm) 00337 double positionGaussMajAxis; 00338 00339 /// The length of the ellipsis minor axis (that is the standard deviation) 00340 /// (in mm) 00341 double positionGaussMinAxis; 00342 00343 /// True if the position gauss was set 00344 bool speedGaussSet; 00345 00346 /// The angle from the robot x-axis to ellipsis major axis in radians. 00347 double speedGaussOrientationRobot; 00348 00349 /// The angle from the field x-axis to ellipsis major axis in radians. 00350 double speedGaussOrientationField; 00351 00352 /// The length of the ellipsis major axis (that is the standard deviation) 00353 /// (in mm) 00354 double speedGaussMajAxis; 00355 00356 /// The length of the ellipsis minor axis (that is the standard deviation) 00357 /// (in mm) 00358 double speedGaussMinAxis; 00359 00360 //this is for streaming (with specifications) 00361 public: 00362 virtual void serialize(In *in, Out *out); 00363 }; 00364 //------------------------------------------------------------------------------ 00365 /** 00366 * Writes a ball state to a stream. 00367 * @param stream The stream the ball position is written to. 00368 * @param bs The ball state to write. 00369 * @return Reference to the stream. 00370 */ 00371 //Out& operator<<(Out& stream, const BallState& bs); 00372 /** 00373 * Reads a ball state from a stream. 00374 * @param stream The stream the ball state is read from. 00375 * @param bs The ball state the read data is stored to. 00376 * @return Reference to the stream. 00377 */ 00378 //In& operator>>(In& stream, BallState& bs); 00379 //------------------------------------------------------------------------------ 00380 /** 00381 * @class SeenBallState 00382 * 00383 * The knowledge abot the ball that was obtained by own observations. 00384 */ 00385 class SeenBallState : public BallState 00386 { 00387 public: 00388 00389 /** 00390 * Constructor 00391 */ 00392 SeenBallState(); 00393 00394 public: 00395 00396 /** 00397 * Resets all data of the ball position. 00398 */ 00399 void reset(); 00400 00401 /** 00402 * If this is positive, we see the ball for that time consecutively. 00403 * If this is negative, we have not seen the ball consecutively for -that time. 00404 */ 00405 long getConsecutivelySeenTime() const; 00406 00407 public: 00408 00409 /** 00410 * The time when the ball was last seen by the own camera. 00411 * "position" and speed were set at that point in time. 00412 */ 00413 unsigned long timeWhenLastSeen; 00414 00415 /** 00416 * The time when the ball was last seen consecutively, i.e. 00417 * it has been seen "a lot" but not necessarily in all frames; 00418 * gaps of n frames or more are okay 00419 */ 00420 unsigned long timeWhenFirstSeenConsecutively; 00421 00422 /** 00423 * The time until the ball has been seen consecutively, i.e. 00424 * it has been seen "a lot" but not necessarily in all frames 00425 * until the time stored here; 00426 */ 00427 unsigned long timeUntilSeenConsecutively; 00428 //this is for streaming (with specifications) 00429 public: 00430 virtual void serialize(In *in, Out *out); 00431 //Functions for streaming with TeamMessagesMessage: 00432 //int read(In& stream, int length); 00433 //void write(Out& stream); 00434 //unsigned int length(); 00435 00436 }; 00437 //------------------------------------------------------------------------------ 00438 /** 00439 * Writes a seen ball state to a stream. 00440 * @param stream The stream the ball position is written to. 00441 * @param sbs The ball state to write. 00442 * @return Reference to the stream. 00443 */ 00444 //Out& operator<<(Out& stream, const SeenBallState& sbs); 00445 /** 00446 * Reads a seen ball state from a stream. 00447 * @param stream The stream the ball state is read from. 00448 * @param sbs The ball state the read data is stored to. 00449 * @return Reference to the stream. 00450 */ 00451 //In& operator>>(In& stream, SeenBallState& sbs); 00452 //------------------------------------------------------------------------------ 00453 /* 00454 * @class PropagatedBallState 00455 * 00456 * The position of the ball estimated from own observations. It is estimated 00457 * even if the ball is not seen. 00458 */ 00459 class PropagatedBallState : public BallState 00460 { 00461 public: 00462 00463 /** 00464 * Constructor 00465 */ 00466 PropagatedBallState(); 00467 00468 public: 00469 00470 /** 00471 * Resets all data of the ball position. 00472 */ 00473 void reset(); 00474 00475 public: 00476 00477 /// The last seen ball position 00478 SeenBallState lastSeenBallState; 00479 00480 //this is for streaming (with specifications) 00481 public: 00482 virtual void serialize(In *in, Out *out); 00483 }; 00484 //------------------------------------------------------------------------------ 00485 /** 00486 * Writes a propagated ball state to a stream. 00487 * @param stream The stream the ball position is written to. 00488 * @param pbs The ball state to write. 00489 * @return Reference to the stream. 00490 */ 00491 //Out& operator<<(Out& stream, const PropagatedBallState& pbs); 00492 /** 00493 * Reads a propagated ball state from a stream. 00494 * @param stream The stream the ball state is read from. 00495 * @param pbs The ball state the read data is stored to. 00496 * @return Reference to the stream. 00497 */ 00498 //In& operator>>(In& stream, PropagatedBallState& pbs); 00499 //------------------------------------------------------------------------------ 00500 /* 00501 * @class CommunicatedBallState 00502 * 00503 * The position of the ball estimated from own observations and from 00504 * observation of other players. 00505 */ 00506 class CommunicatedBallState : public BallState 00507 { 00508 public: 00509 00510 /** 00511 * Constructor 00512 */ 00513 CommunicatedBallState(); 00514 00515 public: 00516 00517 /** 00518 * Resets all data of the ball position. 00519 */ 00520 void reset(); 00521 00522 public: 00523 00524 /// Time when the observation was made by another robot 00525 unsigned long timeWhenLastObserved; 00526 00527 //this is for streaming (with specifications) 00528 virtual void serialize(In *in, Out *out); 00529 }; 00530 //------------------------------------------------------------------------------ 00531 /** 00532 * Writes a communicated ball state to a stream. 00533 * @param stream The stream the ball position is written to. 00534 * @param cbs The ball state to write. 00535 * @return Reference to the stream. 00536 */ 00537 //Out& operator<<(Out& stream, const CommunicatedBallState& cbs); 00538 /** 00539 * Reads a communicated ball state from a stream. 00540 * @param stream The stream the ball state is read from. 00541 * @param cbs The ball state the read data is stored to. 00542 * @return Reference to the stream. 00543 */ 00544 //In& operator>>(In& stream, CommunicatedBallState& cbs); 00545 //------------------------------------------------------------------------------ 00546 class HypotheticalBallState : public BallState 00547 { 00548 public: 00549 00550 /** 00551 * Constructor 00552 */ 00553 HypotheticalBallState(); 00554 }; 00555 //------------------------------------------------------------------------------ 00556 /** 00557 * Writes a hypothetical ball state to a stream. 00558 * @param stream The stream the ball position is written to. 00559 * @param hbs The ball state to write. 00560 * @return Reference to the stream. 00561 */ 00562 //Out& operator<<(Out& stream, const HypotheticalBallState& hbs); 00563 /** 00564 * Reads a hypothetical ball state from a stream. 00565 * @param stream The stream the ball state is read from. 00566 * @param hbs The ball state the read data is stored to. 00567 * @return Reference to the stream. 00568 */ 00569 //In& operator>>(In& stream, HypotheticalBallState& cbs); 00570 //------------------------------------------------------------------------------ 00571 /** 00572 * @class BallModel 00573 * 00574 * Contains the modeled knowledge about the ball. 00575 */ 00576 class BallModel : public Streamable 00577 { 00578 public: 00579 /** 00580 * The time for the behaviorControl after that communicated ball 00581 * positions are accepted 00582 */ 00583 enum 00584 { 00585 behaviorControlTimeAfterWhichCommunicatedBallsAreAccepted = 2000, 00586 timeAfterWhichPropagatedAreUsed = 100, 00587 maxDistanceToUseSeen = 500, 00588 minDistanceToUseSeen = 150 00589 }; 00590 00591 /** 00592 * Constructor. 00593 */ 00594 BallModel(); 00595 00596 /** 00597 * Resets the ball model. 00598 */ 00599 void reset(); 00600 00601 /** 00602 * Sets the frame number of the image this model was created from. 00603 * @param frameNumber The frame number. 00604 */ 00605 void setFrameNumber(unsigned long frameNumber); 00606 00607 /** 00608 * Returns a "known" ball position relative to the field. 00609 * If the ball was seen, the seen ball position is used. If the ball 00610 * was not seen for a timeAfterThatCommunicatedBallsAreAccepted, 00611 * the communicatedBallPosition is returned, if there was received a 00612 * ball position. 00613 * @param timeAfterWhichCommunicatedBallsAreAccepted The time after that 00614 * the communicated position is returned 00615 */ 00616 const Vector2<double>& getKnownPosition( 00617 unsigned long timeAfterWhichCommunicatedBallsAreAccepted) const; 00618 00619 const Vector2<double>& getKnownPosition( 00620 unsigned long timeAfterWhichCommunicatedBallsAreAccepted, 00621 unsigned long timeAfterWhichPropagatedAreUsed, 00622 double maxDistanceToUseSeen, 00623 double minDistanceToUseSeen) const; 00624 00625 const BallState& getKnownBallState( 00626 unsigned long timeAfterWhichCommunicatedBallsAreAccepted = behaviorControlTimeAfterWhichCommunicatedBallsAreAccepted, 00627 unsigned long timeAfterPropagatedAreUsed = timeAfterWhichPropagatedAreUsed, 00628 double maxDistanceUseSeen = maxDistanceToUseSeen, 00629 double minDistanceUseSeen = minDistanceToUseSeen) const; 00630 00631 /** 00632 * Returns the time since the ball was seen or communicated. 00633 * @param timeAfterWhichCommunicatedBallsAreAccepted as in getKnownPosition() 00634 */ 00635 unsigned long getTimeSinceLastKnown( 00636 unsigned long timeAfterWhichCommunicatedBallsAreAccepted) const; 00637 00638 public: 00639 00640 /// The frame number of the image this model was created from. 00641 unsigned long frameNumber; 00642 00643 /// The number of consecutive images without ball percept. 00644 int numberOfImagesWithoutBallPercept; 00645 00646 /// The number of consecutive images with ball percept. 00647 int numberOfImagesWithBallPercept; 00648 00649 /// True if the ball was seen (a ball percept exists) 00650 bool ballWasSeen; 00651 00652 /// The state of the ball estimated from own observations 00653 SeenBallState seen; 00654 00655 /** 00656 * The state of the ball estimated from own observations; 00657 * it is propagated even if the ball is not seen 00658 */ 00659 PropagatedBallState propagated; 00660 00661 /** 00662 * The state of the ball estimated from own observations and from 00663 * observation of other players. 00664 */ 00665 CommunicatedBallState communicated; 00666 00667 /** 00668 * The state of the ball guessed from own negative information 00669 * after ball was lost 00670 */ 00671 HypotheticalBallState hypothetical; 00672 00673 //this is for streaming (with specifications) 00674 public: 00675 virtual void serialize(In *in, Out *out) 00676 { 00677 STREAM_REGISTER_BEGIN(); 00678 STREAM( frameNumber); 00679 STREAM( numberOfImagesWithoutBallPercept); 00680 STREAM( numberOfImagesWithBallPercept); 00681 STREAM( ballWasSeen); 00682 STREAM( seen); 00683 STREAM( propagated); 00684 STREAM( communicated); 00685 STREAM( hypothetical); 00686 STREAM_REGISTER_FINISH(); 00687 } 00688 }; 00689 //------------------------------------------------------------------------------ 00690 /** 00691 * Writes a ball model to a stream. 00692 * @param stream The stream the ball model is written to. 00693 * @param hbs The ball model to write. 00694 * @return Reference to the stream. 00695 */ 00696 //Out& operator<<(Out& stream, const BallModel& bm); 00697 /** 00698 * Reads a ball model from a stream. 00699 * @param stream The stream the ball model is read from. 00700 * @param hbs The ball model the read data is stored to. 00701 * @return Reference to the stream. 00702 */ 00703 //In& operator>>(In& stream, BallModel& bm); 00704 //------------------------------------------------------------------------------ 00705 #endif //__BallModel_h_
1.3.6