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 <fawkes_msgs/Position3D.h>
25 #include <ros/this_node.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) {
57  } // use default
58 
59  ros_pub_ = rosnode->advertise<fawkes_msgs::Position3D>(cfg_ros_topic_, 100);
60  // check for open Position3DInterfaces
62  for (std::list<Position3DInterface *>::iterator it = ifs_.begin(); it != ifs_.end(); ++it) {
64  }
65  // watch for creation of new Position3DInterfaces
66  bbio_add_observed_create("Position3DInterface");
67 
68  // register to blackboard
71 }
72 
73 void
75 {
78  for (std::list<Position3DInterface *>::iterator it = ifs_.begin(); it != ifs_.end(); ++it) {
79  blackboard->close(*it);
80  }
81  ros_pub_.shutdown();
82 }
83 
84 void
85 RosPosition3DThread::bb_interface_created(const char *type, const char *id) throw()
86 {
87  if (strncmp(type, "Position3DInterface", INTERFACE_TYPE_SIZE_) != 0)
88  return;
89  Position3DInterface *interface;
90  try {
91  interface = blackboard->open_for_reading<Position3DInterface>(id);
92  } catch (Exception &e) {
93  logger->log_warn(name(), "Failed to open %s:%s: %s", type, id, e.what());
94  return;
95  }
96  try {
97  bbil_add_data_interface(interface);
98  blackboard->update_listener(this);
99  ifs_.push_back(interface);
100  } catch (Exception &e) {
101  blackboard->close(interface);
102  logger->log_warn(name(), "Failed to register for %s:%s: %s", type, id, e.what());
103  return;
104  }
105 }
106 
107 void
109  unsigned int instance_serial) throw()
110 {
111  conditional_close(interface);
112 }
113 
114 void
116  unsigned int instance_serial) throw()
117 {
118  conditional_close(interface);
119 }
120 
121 void
122 RosPosition3DThread::conditional_close(Interface *interface) throw()
123 {
124  // Verify it's a Position3DInterface
125  Position3DInterface *iface = dynamic_cast<Position3DInterface *>(interface);
126  if (!iface)
127  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
134  bbil_remove_data_interface(*it);
135  blackboard->update_listener(this);
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)
149  return;
150  iface->read();
151  fawkes_msgs::Position3D position;
152  position.header.frame_id = iface->frame();
153  position.name = iface->id();
154  position.pose.position.x = iface->translation()[0];
155  position.pose.position.y = iface->translation()[1];
156  position.pose.position.z = iface->translation()[2];
157  position.pose.orientation.x = iface->rotation()[0];
158  position.pose.orientation.y = iface->rotation()[1];
159  position.pose.orientation.z = iface->rotation()[2];
160  position.pose.orientation.w = iface->rotation()[3];
161  position.visibility_history = iface->visibility_history();
162  ros_pub_.publish(position);
163 }
RosPosition3DThread::bb_interface_reader_removed
virtual void bb_interface_reader_removed(fawkes::Interface *interface, unsigned int instance_serial)
A reading instance has been closed for a watched interface.
Definition: position_3d_thread.cpp:115
fawkes::Position3DInterface::visibility_history
int32_t visibility_history() const
Get visibility_history value.
Definition: Position3DInterface.cpp:121
fawkes::BlackBoard::register_listener
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:190
fawkes::Interface::read
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:477
fawkes::Position3DInterface::frame
char * frame() const
Get frame value.
Definition: Position3DInterface.cpp:81
RosPosition3DThread::bb_interface_created
virtual void bb_interface_created(const char *type, const char *id)
BlackBoard interface created notification.
Definition: position_3d_thread.cpp:85
fawkes::ROSAspect::rosnode
LockPtr< ros::NodeHandle > rosnode
Definition: ros.h:47
fawkes::BlackBoard::unregister_listener
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:217
fawkes::BlackBoardInterfaceListener
Definition: interface_listener.h:47
fawkes::Position3DInterface::rotation
double * rotation() const
Get rotation value.
Definition: Position3DInterface.cpp:160
RosPosition3DThread::bb_interface_data_changed
virtual void bb_interface_data_changed(fawkes::Interface *interface)
BlackBoard data changed notification.
Definition: position_3d_thread.cpp:145
RosPosition3DThread::~RosPosition3DThread
virtual ~RosPosition3DThread()
Destructor.
Definition: position_3d_thread.cpp:46
fawkes::Interface::id
const char * id() const
Get identifier of interface.
Definition: interface.cpp:654
fawkes::BlackBoardInterfaceObserver::bbio_add_observed_create
void bbio_add_observed_create(const char *type_pattern, const char *id_pattern="*")
Add interface creation type to watch list.
Definition: interface_observer.cpp:125
fawkes::Position3DInterface::translation
double * translation() const
Get translation value.
Definition: Position3DInterface.cpp:228
RosPosition3DThread::RosPosition3DThread
RosPosition3DThread()
Constructor.
Definition: position_3d_thread.cpp:39
fawkes::MultiLogger::log_warn
virtual void log_warn(const char *component, const char *format,...)
Definition: multi.cpp:222
RosPosition3DThread::init
virtual void init()
Initialize the thread.
Definition: position_3d_thread.cpp:51
fawkes::BlackBoard::close
virtual void close(Interface *interface)=0
RosPosition3DThread::finalize
virtual void finalize()
Finalize the thread.
Definition: position_3d_thread.cpp:74
fawkes
fawkes::BlackBoard::register_observer
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:230
fawkes::Interface
Definition: interface.h:78
fawkes::ConfigurableAspect::config
Configuration * config
Definition: configurable.h:53
fawkes::Exception::what
virtual const char * what() const
Get primary string.
Definition: exception.cpp:639
fawkes::Thread
Definition: thread.h:45
fawkes::BlackBoard::unregister_observer
virtual void unregister_observer(BlackBoardInterfaceObserver *observer)
Unregister BB interface observer.
Definition: blackboard.cpp:245
fawkes::Position3DInterface
Definition: Position3DInterface.h:39
fawkes::BlackBoardAspect::blackboard
BlackBoard * blackboard
Definition: blackboard.h:49
fawkes::Configuration::get_string
virtual std::string get_string(const char *path)=0
fawkes::BlackBoard::open_multiple_for_reading
virtual std::list< Interface * > open_multiple_for_reading(const char *type_pattern, const char *id_pattern="*", const char *owner=NULL)=0
RosPosition3DThread::bb_interface_writer_removed
virtual void bb_interface_writer_removed(fawkes::Interface *interface, unsigned int instance_serial)
A writing instance has been closed for a watched interface.
Definition: position_3d_thread.cpp:108
fawkes::BlackBoardInterfaceListener::bbil_add_data_interface
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.
Definition: interface_listener.cpp:238
fawkes::BlackBoard::update_listener
virtual void update_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Update BB event listener.
Definition: blackboard.cpp:202
fawkes::Exception
Definition: exception.h:41