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
27using 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
50void
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
73void
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
84void
85RosPosition3DThread::bb_interface_created(const char *type, const char *id) noexcept
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
107void
109 Uuid instance_serial) noexcept
110{
111 conditional_close(interface);
112}
113
114void
116 Uuid instance_serial) noexcept
117{
118 conditional_close(interface);
119}
120
121void
122RosPosition3DThread::conditional_close(Interface *interface) noexcept
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
144void
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}
virtual ~RosPosition3DThread()
Destructor.
virtual void init()
Initialize the thread.
virtual void bb_interface_data_refreshed(fawkes::Interface *interface) noexcept
BlackBoard data refreshed notification.
virtual void finalize()
Finalize the thread.
virtual void bb_interface_reader_removed(fawkes::Interface *interface, fawkes::Uuid instance_serial) noexcept
A reading instance has been closed for a watched interface.
virtual void bb_interface_writer_removed(fawkes::Interface *interface, fawkes::Uuid instance_serial) noexcept
A writing instance has been closed for a watched interface.
RosPosition3DThread()
Constructor.
virtual void bb_interface_created(const char *type, const char *id) noexcept
BlackBoard interface created notification.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
BlackBoard interface listener.
void bbil_add_data_interface(Interface *interface)
Add an interface to the data modification watch list.
void bbio_add_observed_create(const char *type_pattern, const char *id_pattern="*") noexcept
Add interface creation type to watch list.
virtual void unregister_observer(BlackBoardInterfaceObserver *observer)
Unregister BB interface observer.
Definition: blackboard.cpp:240
virtual void update_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Update BB event listener.
Definition: blackboard.cpp:197
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:225
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:212
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.
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:185
virtual void close(Interface *interface)=0
Close interface.
Configuration * config
This is the Configuration member used to access the configuration.
Definition: configurable.h:41
virtual std::string get_string(const char *path)=0
Get value from configuration which is of type string.
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual const char * what() const noexcept
Get primary string.
Definition: exception.cpp:639
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:80
const char * id() const
Get identifier of interface.
Definition: interface.cpp:661
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:479
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: multi.cpp:216
Position3DInterface Fawkes BlackBoard Interface.
int32_t visibility_history() const
Get visibility_history value.
char * frame() const
Get frame value.
double * rotation() const
Get rotation value.
double * translation() const
Get translation value.
LockPtr< ros::NodeHandle > rosnode
Central ROS node handle.
Definition: ros.h:47
Thread class encapsulation of pthreads.
Definition: thread.h:46
A convenience class for universally unique identifiers (UUIDs).
Definition: uuid.h:29
Fawkes library namespace.