NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
beh_search_for_ball.h
1 // Walk and kick behaviour state: Search for ball
2 // Author: Philipp Allgeuer <pallgeuer@ais.uni-bonn.de>
3 
4  // Ensure header is only included once
5 #ifndef BEH_SEARCH_FOR_BALL_H
6 #define BEH_SEARCH_FOR_BALL_H
7 
8 // Includes
9 #include <walk_and_kick/wak_beh_state.h>
10 #include <walk_and_kick/beh_states/gaze_beh_look_for_ball.h>
11 #include <rc_utils/ros_time.h>
12 
13 // Walk and kick namespace
14 namespace walk_and_kick
15 {
22  {
23  public:
24  // Search for ball walk state enumeration
25  enum SFBWalkState
26  {
27  SFB_WS_UNKNOWN = 0, // Unknown search for ball walk state
28  SFB_WS_STAYCOOL, // Stay cool and hope that the ball will turn up again soon by itself
29  SFB_WS_BACKUP, // Walk backwards a little
30  SFB_WS_SPIN, // Spin on the spot
31  SFB_WS_GOTOBALLHYP, // Go to a ball hypothesis
32  SFB_WS_GOTOCENTRE, // Go to the centre
33  SFB_WS_SPINHERE, // Spin on the spot
34  SFB_WS_WALKTOMARK, // Walk to a penalty mark
35  SFB_WS_WALKFWDS, // Just walk forwards
36  SFB_WS_COUNT
37  };
38  static bool sfbWalkStateValid(int state) { return (state > SFB_WS_UNKNOWN && state < SFB_WS_COUNT); }
39  static bool sfbWalkStateValid(SFBWalkState state) { return sfbWalkStateValid((int) state); }
40  static const std::string& sfbWalkStateName(SFBWalkState state) { if(sfbWalkStateValid(state)) return SFBWalkStateName[state]; else return SFBWalkStateName[SFB_WS_UNKNOWN]; }
41  private:
42  static const std::string SFBWalkStateName[SFB_WS_COUNT];
43 
44  public:
45  // Ball hypothesis type enumeration
46  enum BallHypType
47  {
48  BHT_NONE = 0, // No ball hypothesis
49  BHT_TEAM_COMMS, // Ball hypothesis from team communications
50  BHT_COUNT
51  };
52  static bool ballHypTypeValid(int type) { return (type >= BHT_NONE && type < BHT_COUNT); }
53  static bool ballHypTypeValid(BallHypType type) { return ballHypTypeValid((int) type); }
54  static const std::string& ballHypTypeName(BallHypType type) { if(ballHypTypeValid(type)) return BallHypTypeName[type]; else return BallHypTypeName[BHT_NONE]; }
55  private:
56  static const std::string BallHypTypeName[BHT_COUNT];
57 
58  public:
59  // Constructor
60  BehSearchForBall(WAKConfig& config, const SensorVars& SV, const WAKBehShared& WBS, const WAKGameShared& WGS, int ID);
61 
62  // Execute function
63  virtual void execute(ActuatorVars& AV, const ActuatorVars& lastAV, bool justActivated);
64 
65  // Reset search function
66  void resetSearch();
67 
68  // Request search for ball state functions (allows the request of a particular initial state for the next time search for ball activates)
69  void clearSfbStateRequest() { m_reqState = SFB_WS_UNKNOWN; m_reqData = 0; rc_utils::zeroRosTime(m_reqTime); }
70  void requestSfbState(SFBWalkState state, int data = 0) { if(sfbWalkStateValid(state)) { m_reqState = state; m_reqData = data; m_reqTime = SV.now; } else clearSfbStateRequest(); }
71  void refreshSfbStateRequest() { if(haveSfbStateRequest()) m_reqTime = SV.now; }
72  float timeSinceSfbStateRequest() const { return (m_reqTime.isZero() ? INFINITY : (SV.now - m_reqTime).toSec()); }
73  bool haveSfbStateRequest() const { return (sfbWalkStateValid(m_reqState) && timeSinceSfbStateRequest() < config.sfbStateRequestTimeout()); }
74 
75  protected:
76  // Handle activation function
77  virtual void handleActivation(bool nowActive);
78 
79  private:
80  // Persistent variables struct
81  struct PersistentVars
82  {
83  PersistentVars() { reset(); }
84  void reset();
85  SFBWalkState nextState;
86  BallHypType ballHypType;
87  float timeout;
88  Vec2f target;
89  int dirn;
90  };
91 
92  // State helper functions
93  void changeSfbState(SFBWalkState newSfbState, SFBWalkState nextState = SFB_WS_UNKNOWN); // Worker function to actually perform a change of the current search for ball walk state
94 
95  // Suspend/resume functions
96  void suspendState();
97  void resumeState();
98  void clearSuspendedState();
99  bool haveSuspendedState() const;
100 
101  // Helper functions
102  float calcBallHypValue(const Vec2f& ballHypPose);
103  float calcTimeout(const Vec2f& target);
104  int calcSpinDirnToTarget(const Vec2f& target);
105  bool spinningToTargetIsBetter(const Vec2f& target);
106  float backupFactorFromLine(float margin, float distToLine, float cosAlpha);
107  float backupFactorFromPoint(float margin, float vecToPointX, float vecToPointY, float theta);
108 
109  // Search for ball state request variables
110  SFBWalkState m_reqState;
111  int m_reqData;
112  ros::Time m_reqTime;
113 
114  // Last activated state variables
115  SFBWalkState m_lastActState;
116  ros::Time m_lastActTime;
117  ros::Time m_lastActSpinTime;
118  float m_lastActElapsed;
119 
120  // Current search for ball walk state
121  SFBWalkState m_walkState;
122  ros::Time m_walkStateTime;
123  bool m_walkStateIsRequest;
124  bool m_walkStateIsResumed;
125  bool m_walkStateIsNew;
126 
127  // Search for ball variables
128  PersistentVars m_PV;
129  float m_factor;
130  float m_walkFwdTime;
131  Counter m_doneCounter;
132  Counter m_failCounter;
133 
134  // Search for ball resume variables
135  SFBWalkState m_resumeWalkState;
136  float m_resumeElapsed;
137  PersistentVars m_resumePV;
138 
139  // Team communications variables
140  Vec2f m_lastBallHypPose;
141  bool m_lastBallHypPoseValid;
142 
143  // Visualisation variables
144  BallHypType m_visBallHypType;
145  };
146 }
147 
148 
149 #endif
150 // EOF
Configuration struct for the walk and kick node.
Definition: wak_config.h:20
An interface class for encapsulating all of the ROS input data to the walk and kick node...
Definition: wak_sensor_vars.h:29
A class that shares the required walk and kick variables amongst the game state classes.
Definition: wak_game_shared.h:31
A simple counter class for counting events.
Definition: wak_utils.h:63
A walk and kick gaze behaviour state that looks for the ball.
Definition: gaze_beh_look_for_ball.h:20
A class that shares the required walk and kick variables amongst the behaviour state classes...
Definition: wak_beh_shared.h:48
A walk and kick behaviour state that searches for the ball.
Definition: beh_search_for_ball.h:21
An interface class for encapsulating all of the data that the walk and kick behaviour states should c...
Definition: wak_actuator_vars.h:22