Fawkes API  Fawkes Development Version
position_3d_thread.cpp
1 /***************************************************************************
2  * position_3d_thread.cpp - Publish 3D Position to ROS
3  *
4  * Created: Wed Jul 16 17:04:42 2014
5  * Copyright 2014 Till Hofmann
6  *
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 #include "position_3d_thread.h"
23 
24 #include <ros/this_node.h>
25 #include <fawkes_msgs/Position3D.h>
26 
27 using namespace fawkes;
28 
29 #define CFG_PREFIX "/ros/position-3d/"
30 
31 /** @class RosPosition3DThread "position_3d_thread.h"
32  * Thread to publish Position3Ds to ROS.
33  * This thread reads all Position3D Blackboard Interfaces and publishes every
34  * position to ROS.
35  * @author Till Hofmann
36  */
37 
38 /** Constructor. */
40  : Thread("RosPosition3DThread", Thread::OPMODE_WAITFORWAKEUP),
41  BlackBoardInterfaceListener("RosPosition3DThread")
42 {
43 }
44 
45 /** Destructor. */
47 {
48 }
49 
50 void
52 {
53  cfg_ros_topic_ = "/objects";
54  try {
55  cfg_ros_topic_ = config->get_string(CFG_PREFIX"ros_topic");
56  } catch (Exception &e) {} // use default
57 
58  ros_pub_ = rosnode->advertise<fawkes_msgs::Position3D>(cfg_ros_topic_, 100);
59  // check for open Position3DInterfaces
61  for (std::list<Position3DInterface *>::iterator it = ifs_.begin(); it != ifs_.end(); it++) {
63  }
64  // watch for creation of new Position3DInterfaces
65  bbio_add_observed_create("Position3DInterface");
66 
67  // register to blackboard
70 }
71 
72 void
74 {
77  for (std::list<Position3DInterface *>::iterator it = ifs_.begin(); it != ifs_.end(); it++) {
78  blackboard->close(*it);
79  }
80  ros_pub_.shutdown();
81 }
82 
83 void
84 RosPosition3DThread::bb_interface_created(const char *type, const char *id) throw()
85 {
86  if (strncmp(type, "Position3DInterface", __INTERFACE_TYPE_SIZE) != 0) return;
87  Position3DInterface *interface;
88  try {
89  interface = blackboard->open_for_reading<Position3DInterface>(id);
90  } catch (Exception &e) {
91  logger->log_warn(name(), "Failed to open %s:%s: %s", type, id, e.what());
92  return;
93  }
94  try {
95  bbil_add_data_interface(interface);
97  ifs_.push_back(interface);
98  } catch (Exception &e) {
99  blackboard->close(interface);
100  logger->log_warn(name(), "Failed to register for %s:%s: %s", type, id, e.what());
101  return;
102  }
103 }
104 
105 void
107  unsigned int instance_serial)
108  throw()
109 {
110  conditional_close(interface);
111 }
112 
113 
114 void
116  unsigned int instance_serial)
117  throw()
118 {
119  conditional_close(interface);
120 }
121 
122 void
123 RosPosition3DThread::conditional_close(Interface *interface) throw()
124 {
125  // Verify it's a Position3DInterface
126  Position3DInterface *iface = dynamic_cast<Position3DInterface *>(interface);
127  if (! iface) return;
128 
129  std::list<Position3DInterface *>::iterator it;
130  for (it = ifs_.begin(); it != ifs_.end(); ++it) {
131  if (*interface == **it) {
132  if (! interface->has_writer() && (interface->num_readers() == 1)) {
133  // It's only us
136  blackboard->close(*it);
137  ifs_.erase(it);
138  break;
139  }
140  }
141  }
142 }
143 
144 void
146 {
147  Position3DInterface *iface = dynamic_cast<Position3DInterface *>(interface);
148  if (!iface) return;
149  iface->read();
150  fawkes_msgs::Position3D position;
151  position.header.frame_id = iface->frame();
152  position.name = iface->id();
153  position.pose.position.x = iface->translation()[0];
154  position.pose.position.y = iface->translation()[1];
155  position.pose.position.z = iface->translation()[2];
156  position.pose.orientation.x = iface->rotation()[0];
157  position.pose.orientation.y = iface->rotation()[1];
158  position.pose.orientation.z = iface->rotation()[2];
159  position.pose.orientation.w = iface->rotation()[3];
160  position.visibility_history = iface->visibility_history();
161  ros_pub_.publish(position);
162 }
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:230
RosPosition3DThread()
Constructor.
double * rotation() const
Get rotation value.
char * frame() const
Get frame value.
virtual ~RosPosition3DThread()
Destructor.
Fawkes library namespace.
virtual void init()
Initialize the thread.
const char * id() const
Get identifier of interface.
Definition: interface.cpp:661
virtual const char * what() const
Get primary string.
Definition: exception.cpp:661
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:218
Thread class encapsulation of pthreads.
Definition: thread.h:42
virtual void update_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Update BB event listener.
Definition: blackboard.cpp:203
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
Logger * logger
This is the Logger member used to access the logger.
Definition: logging.h:44
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:190
virtual void bb_interface_reader_removed(fawkes::Interface *interface, unsigned int instance_serial)
A reading 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.
virtual void bb_interface_writer_removed(fawkes::Interface *interface, unsigned int instance_serial)
A writing instance has been closed for a watched interface.
Position3DInterface Fawkes BlackBoard Interface.
Base class for exceptions in Fawkes.
Definition: exception.h:36
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:477
virtual void bb_interface_data_changed(fawkes::Interface *interface)
BlackBoard data changed notification.
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
const char * name() const
Get name of thread.
Definition: thread.h:95
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
double * translation() const
Get translation value.
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.
LockPtr< ros::NodeHandle > rosnode
Central ROS node handle.
Definition: ros.h:48
virtual void bb_interface_created(const char *type, const char *id)
BlackBoard interface created notification.
int32_t visibility_history() const
Get visibility_history value.
unsigned int num_readers() const
Get the number of readers.
Definition: interface.cpp:863
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:44
virtual void finalize()
Finalize the thread.
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
BlackBoard interface listener.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.
virtual void close(Interface *interface)=0
Close interface.