NimbRo ROS Soccer Package
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
wak_tc_ros.h
1 // Walk and kick: Team communications ROS interface
2 // Author: Philipp Allgeuer <pallgeuer@ais.uni-bonn.de>
3 
4 // Ensure header is only included once
5 #ifndef WAK_TC_ROS_H
6 #define WAK_TC_ROS_H
7 
8 // Includes
9 #include <walk_and_kick/wak_common.h>
10 #include <walk_and_kick/TeamCommsData.h>
11 #include <boost/shared_ptr.hpp>
12 #include <ros/ros.h>
13 #include <vector>
14 
15 // Walk and kick namespace
16 namespace walk_and_kick
17 {
18  // Class declarations
19  class TCRobotListener;
20 
21  // Typedefs
22  typedef boost::shared_ptr<const TCRobotListener> TCRobotListenerConstPtr;
23  typedef std::vector<TCRobotListenerConstPtr> TCRobotListenerList;
24 
31  {
32  public:
33  // Constructor
34  TCRosInterface() : m_nh("~") {}
35  explicit TCRosInterface(ros::NodeHandle& nh) : m_nh(nh) {}
36 
37  // Reset function
38  void resetData();
39 
40  // Start/stop listening functions
41  void startListening(bool listenToSelf = false);
42  void stopListening();
43 
44  // Get functions
45  const TCRobotListenerList& teamComms() const { return m_teamComms; }
46  unsigned int nextRobotUID() const { return m_teamComms.size(); }
47 
48  private:
49  // ROS node handle
50  ros::NodeHandle m_nh;
51 
52  // List of robot team communications listeners
53  typedef boost::shared_ptr<TCRobotListener> TCRobotListenerPtr;
54  typedef std::vector<TCRobotListenerPtr> TCRobotListenerListInternal;
55  TCRobotListenerListInternal m_teamCommsInternal;
56  TCRobotListenerList m_teamComms;
57  };
58 
65  {
66  public:
67  // Constructor/destructor
68  TCRobotListener(ros::NodeHandle& nh, const std::string& robot, unsigned int robotUID) : robot(robot), robotUID(robotUID), m_lastEmbeddedStamp(0, 0) { m_subscriber = nh.subscribe("/remote/" + robot + "/walk_and_kick/teamPacket", 1, &TCRobotListener::handleTeamPacket, this); }
69  virtual ~TCRobotListener() { m_subscriber.shutdown(); }
70 
71  // Reset function
72  void resetData() { m_data = TeamCommsData(); m_lastEmbeddedStamp = ros::Time(); }
73 
74  // Robot name and UID
75  const std::string robot;
76  const unsigned int robotUID;
77 
78  // Get functions
79  const TeamCommsData& data() const { return m_data; }
80 
81  private:
82  // Last received team communications data
83  TeamCommsData m_data;
84 
85  // ROS subscriber
86  ros::Subscriber m_subscriber;
87  void handleTeamPacket(const TeamCommsDataConstPtr& msg);
88 
89  // Embedded time stamp of the last received data
90  ros::Time m_lastEmbeddedStamp;
91  };
92 }
93 
94 #endif
95 // EOF
An interface class to the ROS world for team communications.
Definition: wak_tc_ros.h:30
A class that listens to the team communications data of one particular robot.
Definition: wak_tc_ros.h:64