Fawkes API  Fawkes Development Version
transform_listener.cpp
1 /***************************************************************************
2  * transform_listener.cpp - Fawkes transform listener (based on ROS tf)
3  *
4  * Created: Mon Oct 24 18:47:00 2011
5  * Copyright 2011 Tim Niemueller [www.niemueller.de]
6  ****************************************************************************/
7 
8 /* This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version. A runtime exception applies to
12  * this software (see LICENSE.GPL_WRE file mentioned below for details).
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_WRE file in the doc directory.
20  */
21 
22 /* This code is based on ROS tf with the following copyright and license:
23  *
24  * Copyright (c) 2008, Willow Garage, Inc.
25  * All rights reserved.
26  *
27  * Redistribution and use in source and binary forms, with or without
28  * modification, are permitted provided that the following conditions are met:
29  *
30  * * Redistributions of source code must retain the above copyright
31  * notice, this list of conditions and the following disclaimer.
32  * * Redistributions in binary form must reproduce the above copyright
33  * notice, this list of conditions and the following disclaimer in the
34  * documentation and/or other materials provided with the distribution.
35  * * Neither the name of the Willow Garage, Inc. nor the names of its
36  * contributors may be used to endorse or promote products derived from
37  * this software without specific prior written permission.
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
40  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
42  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
43  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
44  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
45  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
46  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49  * POSSIBILITY OF SUCH DAMAGE.
50  */
51 
52 #include <tf/transform_listener.h>
53 #include <tf/transformer.h>
54 
55 #include <blackboard/blackboard.h>
56 #include <interfaces/TransformInterface.h>
57 
58 #include <cstring>
59 
60 namespace fawkes {
61  namespace tf {
62 #if 0 /* just to make Emacs auto-indent happy */
63  }
64 }
65 #endif
66 
67 /** @class TransformListener <tf/transform_listener.h>
68  * Receive transforms and answer queries.
69  * This class connects to the blackboard and listens to all interfaces
70  * publishing transforms. It opens all interfaces of type
71  * TransformInterface with a TF prefix. The data is internally
72  * cached. Queries are then resolved based on the received
73  * information.
74  * @author Tim Niemueller
75  */
76 
77 /** Constructor.
78  * @param bb blackboard to listen to
79  * @param tf_transformer transformer to add transforms to
80  */
82  : BlackBoardInterfaceListener("TransformListener"),
83  bb_(bb), tf_transformer_(tf_transformer)
84 {
85  if (bb_) {
86  tfifs_ = bb_->open_multiple_for_reading<TransformInterface>("/tf*");
87 
88  std::list<TransformInterface *>::iterator i;
89  for (i = tfifs_.begin(); i != tfifs_.end(); ++i) {
91  }
92  bb_->register_listener(this);
93 
94  bbio_add_observed_create("TransformInterface", "/tf*");
95  bb_->register_observer(this);
96  tf_transformer->set_enabled(true);
97  } else {
98  tf_transformer->set_enabled(false);
99  }
100 }
101 
102 
103 /** Destructor. */
105 {
106  if (bb_) {
107  bb_->unregister_listener(this);
108  bb_->unregister_observer(this);
109 
110  std::list<TransformInterface *>::iterator i;
111  for (i = tfifs_.begin(); i != tfifs_.end(); ++i) {
112  bb_->close(*i);
113  }
114  tfifs_.clear();
115  }
116 }
117 
118 
119 void
120 TransformListener::bb_interface_created(const char *type, const char *id) throw()
121 {
122  if (strncmp(type, "TransformInterface", __INTERFACE_TYPE_SIZE) != 0) return;
123 
124  TransformInterface *tfif;
125  try {
126  tfif = bb_->open_for_reading<TransformInterface>(id, "TF-Listener");
127  } catch (Exception &e) {
128  // ignored
129  return;
130  }
131 
132  try {
134  bb_->update_listener(this);
135  tfifs_.push_back(tfif);
136  } catch (Exception &e) {
137  bb_->close(tfif);
138  return;
139  }
140 }
141 
142 void
144  unsigned int instance_serial)
145  throw()
146 {
147  conditional_close(interface);
148 }
149 
150 
151 void
153  unsigned int instance_serial)
154  throw()
155 {
156  conditional_close(interface);
157 }
158 
159 
160 void
161 TransformListener::conditional_close(Interface *interface) throw()
162 {
163  // Verify it's a TransformInterface
164  TransformInterface *tfif = dynamic_cast<TransformInterface *>(interface);
165  if (! tfif) return;
166 
167  std::list<TransformInterface *>::iterator i;
168  for (i = tfifs_.begin(); i != tfifs_.end(); ++i) {
169  if (*interface == **i) {
170  if (! interface->has_writer() && (interface->num_readers() == 1)) {
171  // It's only us
173  bb_->update_listener(this);
174  bb_->close(*i);
175  tfifs_.erase(i);
176  break;
177  }
178  }
179  }
180 }
181 
182 
183 void
185 {
186  TransformInterface *tfif = dynamic_cast<TransformInterface *>(interface);
187  if (! tfif) return;
188 
189  tfif->read();
190 
191  std::string authority = tfif->writer();
192 
193  double *translation = tfif->translation();
194  double *rotation = tfif->rotation();
195  const Time *time = tfif->timestamp();
196  const std::string frame_id = tfif->frame();
197  const std::string child_frame_id = tfif->child_frame();
198 
199  Vector3 t(translation[0], translation[1], translation[2]);
200  Quaternion r(rotation[0], rotation[1], rotation[2], rotation[3]);
201  Transform tr(r, t);
202 
203  StampedTransform str(tr, *time, frame_id, child_frame_id);
204 
205  tf_transformer_->set_transform(str, authority, tfif->is_static_transform());
206 }
207 
208 } // end namespace tf
209 } // end namespace fawkes
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:230
std::string writer() const
Get owner name of writing interface instance.
Definition: interface.cpp:874
virtual void bb_interface_data_changed(Interface *interface)
BlackBoard data changed notification.
TransformListener(BlackBoard *bb, Transformer *tf_transformer)
Constructor.
Fawkes library namespace.
bool set_transform(const StampedTransform &transform, const std::string &authority, bool is_static=false)
Add transform information to the tf data structure.
void set_enabled(bool enabled)
Set enabled status of transformer.
A class for handling time.
Definition: time.h:91
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:218
virtual void update_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Update BB event listener.
Definition: blackboard.cpp:203
TransformInterface Fawkes BlackBoard Interface.
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:190
virtual void bb_interface_writer_removed(Interface *interface, unsigned int instance_serial)
A writing instance has been closed for a watched interface.
void bbio_add_observed_create(const char *type_pattern, const char *id_pattern="*")
Add interface creation type to watch list.
char * child_frame() const
Get child_frame value.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:477
bool is_static_transform() const
Get static_transform value.
Transform that contains a timestamp and frame IDs.
Definition: types.h:96
virtual void unregister_observer(BlackBoardInterfaceObserver *observer)
Unregister BB interface observer.
Definition: blackboard.cpp:244
bool has_writer() const
Check if there is a writer for the interface.
Definition: interface.cpp:834
double * translation() const
Get translation value.
const Time * timestamp() const
Get timestamp of last write.
Definition: interface.cpp:718
virtual std::list< Interface * > open_multiple_for_reading(const char *type_pattern, const char *id_pattern="*", const char *owner=NULL)=0
Open multiple interfaces for reading.
void bbil_remove_data_interface(Interface *interface)
Remove an interface to the data modification watch list.
double * rotation() const
Get rotation value.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
virtual void bb_interface_reader_removed(Interface *interface, unsigned int instance_serial)
A reading instance has been closed for a watched interface.
unsigned int num_readers() const
Get the number of readers.
Definition: interface.cpp:863
The BlackBoard abstract class.
Definition: blackboard.h:48
Coordinate transforms between any two frames in a system.
Definition: transformer.h:68
char * frame() const
Get frame value.
virtual ~TransformListener()
Destructor.
BlackBoard interface listener.
virtual void bb_interface_created(const char *type, const char *id)
BlackBoard interface created notification.
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.
virtual void close(Interface *interface)=0
Close interface.