Fawkes API  Fawkes Development Version
tf_thread.h
1 
2 /***************************************************************************
3  * tf_thread.h - Thread to exchange transforms
4  *
5  * Created: Wed Oct 26 00:50:12 2011
6  * Copyright 2011 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Library General Public License for more details.
18  *
19  * Read the full text in the LICENSE.GPL file in the doc directory.
20  */
21 
22 #ifndef __PLUGINS_ROS_TF_THREAD_H_
23 #define __PLUGINS_ROS_TF_THREAD_H_
24 
25 #include <core/threading/thread.h>
26 #include <aspect/blocked_timing.h>
27 #include <aspect/clock.h>
28 #include <aspect/configurable.h>
29 #include <aspect/logging.h>
30 #include <aspect/blackboard.h>
31 #include <aspect/tf.h>
32 #include <plugins/ros/aspect/ros.h>
33 #include <blackboard/interface_listener.h>
34 #include <blackboard/interface_observer.h>
35 #include <interfaces/TransformInterface.h>
36 #include <core/threading/mutex.h>
37 
38 #include <list>
39 #include <queue>
40 
41 // from ROS
42 #include <ros/common.h>
43 #include <ros/node_handle.h>
44 #include <tf/tfMessage.h>
45 #ifdef HAVE_TF2_MSGS
46 # include <tf2_msgs/TFMessage.h>
47 #endif
48 
50 : public fawkes::Thread,
51  public fawkes::ClockAspect,
52  public fawkes::LoggingAspect,
57  public fawkes::ROSAspect,
60 {
61  public:
62  RosTfThread();
63  virtual ~RosTfThread();
64 
65  virtual void init();
66  virtual void loop();
67  virtual void finalize();
68 
69  // for BlackBoardInterfaceObserver
70  virtual void bb_interface_created(const char *type, const char *id) throw();
71 
72  // for BlackBoardInterfaceListener
73  virtual void bb_interface_data_changed(fawkes::Interface *interface) throw();
74  virtual void bb_interface_writer_removed(fawkes::Interface *interface,
75  unsigned int instance_serial) throw();
76  virtual void bb_interface_reader_removed(fawkes::Interface *interface,
77  unsigned int instance_serial) throw();
78 
79  private:
80  void tf_message_cb(const ros::MessageEvent<::tf::tfMessage const> &msg_evt);
81 #ifdef HAVE_TF2_MSGS
82  void tf_message_cb_dynamic(const ros::MessageEvent<tf2_msgs::TFMessage const> &msg_evt);
83  void tf_message_cb_static(const ros::MessageEvent<tf2_msgs::TFMessage const> &msg_evt);
84  void tf_message_cb(const ros::MessageEvent<tf2_msgs::TFMessage const> &msg_evt, bool static_tf);
85 #endif
86 
87  void conditional_close(fawkes::Interface *interface) throw();
88  void publish_static_transforms_to_ros();
89  void publish_transform_to_fawkes(const geometry_msgs::TransformStamped &ts,
90  bool static_tf = false);
91  geometry_msgs::TransformStamped create_transform_stamped(fawkes::TransformInterface *tfif,
92  const fawkes::Time *time = NULL);
93 
94  /** Stub to see name in backtrace for easier debugging. @see Thread::run() */
95  protected: virtual void run() { Thread::run(); }
96 
97  private:
98  bool __cfg_use_tf2;
99  float __cfg_update_interval;
100 
101 
102  std::list<std::string> __ros_frames;
103  std::list<fawkes::TransformInterface *> __tfifs;
104 
105  ros::Subscriber __sub_tf;
106  ros::Subscriber __sub_static_tf;
107  ros::Publisher __pub_tf;
108  ros::Publisher __pub_static_tf;
109 
110  fawkes::Mutex *__tf_msg_queue_mutex;
111  unsigned int __active_queue;
112  std::queue<::tf::tfMessage::ConstPtr> __tf_msg_queues[2];
113 #ifdef HAVE_TF2_MSGS
114  std::queue<std::pair<bool, tf2_msgs::TFMessage::ConstPtr> > __tf2_msg_queues[2];
115 #endif
116 
117  fawkes::Mutex *__seq_num_mutex;
118  unsigned int __seq_num;
119 
120  fawkes::Time *__last_update;
121 
122 };
123 
124 #endif
Thread aspect to access to BlackBoard.
Definition: blackboard.h:34
Thread aspect that allows to obtain the current time from the clock.
Definition: clock.h:36
Thread aspect to get access to a ROS node handle.
Definition: ros.h:39
virtual void bb_interface_data_changed(fawkes::Interface *interface)
BlackBoard data changed notification.
Definition: tf_thread.cpp:170
Thread to exchange transforms between Fawkes and ROS.
Definition: tf_thread.h:49
A class for handling time.
Definition: time.h:91
Thread class encapsulation of pthreads.
Definition: thread.h:42
TransformInterface Fawkes BlackBoard Interface.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
Thread aspect to access the transform system.
Definition: tf.h:42
Thread aspect to use blocked timing.
virtual void finalize()
Finalize the thread.
Definition: tf_thread.cpp:106
Thread aspect to log output.
Definition: logging.h:35
BlackBoard interface observer.
Thread aspect to access configuration data.
Definition: configurable.h:35
RosTfThread()
Constructor.
Definition: tf_thread.cpp:38
virtual void loop()
Code to execute in the thread.
Definition: tf_thread.cpp:123
virtual void bb_interface_reader_removed(fawkes::Interface *interface, unsigned int instance_serial)
A reading instance has been closed for a watched interface.
Definition: tf_thread.cpp:244
virtual void bb_interface_writer_removed(fawkes::Interface *interface, unsigned int instance_serial)
A writing instance has been closed for a watched interface.
Definition: tf_thread.cpp:237
Mutex mutual exclusion lock.
Definition: mutex.h:32
virtual void init()
Initialize the thread.
Definition: tf_thread.cpp:60
virtual void bb_interface_created(const char *type, const char *id)
BlackBoard interface created notification.
Definition: tf_thread.cpp:204
virtual ~RosTfThread()
Destructor.
Definition: tf_thread.cpp:50
BlackBoard interface listener.
virtual void run()
Stub to see name in backtrace for easier debugging.
Definition: tf_thread.h:95