NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wak_tc_vars.h
1 // Walk and kick: Team communications variables
2 // Author: Philipp Allgeuer <pallgeuer@ais.uni-bonn.de>
3 
4 // Ensure header is only included once
5 #ifndef WAK_TC_VARS_H
6 #define WAK_TC_VARS_H
7 
8 // Includes
9 #include <walk_and_kick/wak_common.h>
10 #include <walk_and_kick/wak_config.h>
11 #include <walk_and_kick/wak_utils.h>
12 #include <walk_and_kick/TeamCommsData.h>
13 #include <boost/shared_ptr.hpp>
14 #include <map>
15 
16 // Defines
17 #define DEFAULT_TCFRESH_TIME 5.0f
18 
19 // Walk and kick namespace
20 namespace walk_and_kick
21 {
22  // Class declarations
23  class SensorVars;
24  class WAKMarkerMan;
25  class TCRosInterface;
26  class TCRobotVars;
27 
28  // Typedefs
29  typedef boost::shared_ptr<const TCRobotVars> TCRobotVarsConstPtr;
30  typedef std::map<unsigned int, TCRobotVarsConstPtr> TCRobotDataMap;
31 
37  class TCVars
38  {
39  public:
40  // Constructor
41  TCVars(const TCRosInterface& TCRI, WAKConfig* config, plot_msgs::PlotManagerFS* PM, WAKMarkerMan* MM) : config(config), TCRI(TCRI), PM(PM), MM(MM) { update(ros::Time(), NULL); }
42 
43  // Config parameters
44  WAKConfig* const config;
45 
46  // Field dimensions
47  const FieldDimensions field;
48 
49  // Update function
50  bool update(const ros::Time& now, const SensorVars* SV); // Returns whether any fresh and valid team communications data is available
51 
52  // Robot team communications data (read only)
53  TCRobotDataMap robotDataMap; // This is guaranteed to contain the robot data of exactly every robot for which we have a *fresh* data packet available, and no more. To use this data, just check the dataValid field of each entry first.
54  float timeSinceData;
55 
56  private:
57  // Internal modifiable robot data map
58  typedef boost::shared_ptr<TCRobotVars> TCRobotVarsPtr;
59  typedef std::map<unsigned int, TCRobotVarsPtr> TCRobotDataMapInternal;
60  TCRobotDataMapInternal m_robotDataMap;
61 
62  // Helper functions
63  bool dataIsFresh(const ros::Time& now, const TeamCommsData& data) const { return (!data.timestamp.isZero() && (now - data.timestamp).toSec() <= (config ? config->tcFreshTime() : DEFAULT_TCFRESH_TIME)); }
64 
65  // Team communications that are assumed to be updated regularly by the ROS interface
66  const TCRosInterface& TCRI;
67 
68  // Plot manager
69  plot_msgs::PlotManagerFS* PM;
70 
71  // Marker manager
72  WAKMarkerMan* MM;
73  };
74 
81  {
82  public:
83  // Reason invalid enumeration
84  enum ReasonInvalid
85  {
86  RI_VALID = 0x0000, // The robot data is valid
87  RI_IS_PENALTY = 0x0001, // The robot is in penalty shootout mode
88  RI_FIELD_MISMATCH = 0x0002, // The robot is playing on a different field size to us
89  RI_INVALID_KICKOFF_TYPE = 0x0004, // The claimed kickoff type is invalid
90  RI_INVALID_BUTTON_STATE = 0x0008, // The claimed button state is invalid
91  RI_INVALID_GAME_CMD = 0x0010, // The claimed game command is invalid
92  RI_INVALID_GAME_ROLE = 0x0020, // The claimed game role is invalid
93  RI_INVALID_PLAY_STATE = 0x0040, // The claimed play state is invalid
94  RI_SAME_ROBOT_NUMBER = 0x0080, // The robot has the same robot number as us
95  RI_WRONG_DIRN_OF_PLAY = 0x0100, // The robot is playing on the opposite goal to us
96  RI_OLD_DATA = 0x0200, // The robot data has timed out because it is too old
97  RI_MISCELLANEOUS = 0x0400, // Invalid for a miscellaneous reason
98  RI_COUNT = 11 // ReasonInvalid = (1 << ReasonIndex)
99  };
100  static const bool reasonInvalidIndexValid(int reasonIndex) { return (reasonIndex >= 0 && reasonIndex < RI_COUNT); }
101  static const std::string& reasonInvalidName(int reasonIndex) { if(reasonInvalidIndexValid(reasonIndex)) return ReasonInvalidName[reasonIndex]; else return ReasonInvalidName[RI_MISCELLANEOUS]; }
102  static const std::string& reasonInvalidFlagName(ReasonInvalid reason) { if(ReasonInvalidMap.find(reason) != ReasonInvalidMap.end()) return ReasonInvalidMap.at(reason); else return ReasonInvalidMap.at(RI_MISCELLANEOUS); }
103  static std::string reasonsInvalidString(int reasonInvalid, const std::string& separator = " | ");
104  private:
105  static const std::string ReasonInvalidName[RI_COUNT];
106  static const std::map<ReasonInvalid, std::string> ReasonInvalidMap;
107  static std::map<ReasonInvalid, std::string> reasonInvalidMap();
108 
109  public:
110  // Constructor
111  TCRobotVars(WAKConfig* config, const FieldDimensions& field, const std::string& robot, unsigned int robotUID) : config(config), field(field), robot(robot), robotUID(robotUID) { update(TeamCommsData(), NULL); dataValid = false; }
112  TCRobotVars(WAKConfig* config, const FieldDimensions& field, const std::string& robot, unsigned int robotUID, const TeamCommsData& newData) : config(config), field(field), robot(robot), robotUID(robotUID) { update(newData, NULL); }
113 
114  // Config parameters
115  WAKConfig* const config;
116 
117  // Field dimensions
118  const FieldDimensions& field;
119 
120  // Robot name and UID
121  const std::string robot;
122  const unsigned int robotUID;
123 
124  // Update function
125  void update(const TeamCommsData& newData, const SensorVars* SV);
126 
127  // Data valid flag (this should always be checked before using any of the data variables below)
128  bool dataValid;
129  int reasonInvalid;
130 
131  // Processed team communications data
132  FieldModel::FieldType fieldType;
133  KickoffType kickoffType;
134  ButtonState buttonState;
135  GameCommand gameCommand;
136  GameRole gameRole;
137  PlayState playState;
138 
139  // Raw team communications packet
140  TeamCommsData data;
141  };
142 }
143 
144 #endif
145 // EOF
Configuration struct for the walk and kick node.
Definition: wak_config.h:20
A class that encapsulates all of the team communications data received from one particular robot...
Definition: wak_tc_vars.h:80
Visualisation marker manager for the walk and kick node.
Definition: wak_vis.h:21
An interface class for encapsulating all of the ROS input data to the walk and kick node...
Definition: wak_sensor_vars.h:29
An interface class to the ROS world for team communications.
Definition: wak_tc_ros.h:30
A class that encapsulates all of the team communication input data to the walk and kick node...
Definition: wak_tc_vars.h:37
A simple class for abstracting away the source of the field dimensions.
Definition: wak_utils.h:216