Fawkes API  Fawkes Development Version
joint_thread.cpp
1 
2 /***************************************************************************
3  * joint_thread.cpp - Thread to publish JointStates to ROS
4  *
5  * Created: Wed Sep 25 18:27:26 2013
6  * Copyright 2013 Till Hofmann
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 "joint_thread.h"
23 
24 #include <ros/this_node.h>
25 #include <sensor_msgs/JointState.h>
26 
27 using namespace fawkes;
28 
29 /** @class RosJointThread "joint_thread.h"
30  * Thread to publish JointStates to ROS.
31  * This thread reads all Joint Blackboard Interfaces and publishes every
32  * joint to ROS.
33  * @author Till Hofmann
34  */
35 
36 /** Constructor. */
38 : Thread("RosJointThread", Thread::OPMODE_WAITFORWAKEUP),
39  BlackBoardInterfaceListener("RosJointThread")
40 {
41 }
42 
43 /** Destructor. */
45 {
46 }
47 
48 void
50 {
51  ros_pub_ = rosnode->advertise<sensor_msgs::JointState>("/joints", 100);
52  // check for open JointInterfaces
54  for (std::list<JointInterface *>::iterator it = ifs_.begin(); it != ifs_.end(); ++it) {
56  }
57  // watch for creation of new JointInterfaces
58  bbio_add_observed_create("JointInterface");
59 
60  // register to blackboard
63 }
64 
65 void
67 {
70  for (std::list<JointInterface *>::iterator it = ifs_.begin(); it != ifs_.end(); ++it) {
71  blackboard->close(*it);
72  }
73  ros_pub_.shutdown();
74 }
75 
76 void
77 RosJointThread::bb_interface_created(const char *type, const char *id) throw()
78 {
79  if (strncmp(type, "JointInterface", INTERFACE_TYPE_SIZE_) != 0)
80  return;
81  JointInterface *interface;
82  try {
83  interface = blackboard->open_for_reading<JointInterface>(id);
84  } catch (Exception &e) {
85  logger->log_warn(name(), "Failed to open %s:%s: %s", type, id, e.what());
86  return;
87  }
88  try {
89  bbil_add_data_interface(interface);
90  blackboard->update_listener(this);
91  ifs_.push_back(interface);
92  } catch (Exception &e) {
93  blackboard->close(interface);
94  logger->log_warn(name(), "Failed to register for %s:%s: %s", type, id, e.what());
95  return;
96  }
97 }
98 
99 void
101  unsigned int instance_serial) throw()
102 {
103  conditional_close(interface);
104 }
105 
106 void
108  unsigned int instance_serial) throw()
109 {
110  conditional_close(interface);
111 }
112 
113 void
114 RosJointThread::conditional_close(Interface *interface) throw()
115 {
116  // Verify it's a JointInterface
117  JointInterface *jiface = dynamic_cast<JointInterface *>(interface);
118  if (!jiface)
119  return;
120 
121  std::list<JointInterface *>::iterator it;
122  for (it = ifs_.begin(); it != ifs_.end(); ++it) {
123  if (*interface == **it) {
124  if (!interface->has_writer() && (interface->num_readers() == 1)) {
125  // It's only us
126  bbil_remove_data_interface(*it);
127  blackboard->update_listener(this);
128  blackboard->close(*it);
129  ifs_.erase(it);
130  break;
131  }
132  }
133  }
134 }
135 
136 void
138 {
139  JointInterface *jiface = dynamic_cast<JointInterface *>(interface);
140  if (!jiface)
141  return;
142  jiface->read();
143  sensor_msgs::JointState joint_state;
144  // std::string names[] = { "testJoint" };
145  joint_state.name.push_back(jiface->id());
146  joint_state.position.push_back(jiface->position());
147  joint_state.velocity.push_back(jiface->velocity());
148  ros_pub_.publish(joint_state);
149 }
fawkes::BlackBoard::register_listener
virtual void register_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Register BB event listener.
Definition: blackboard.cpp:185
fawkes::Interface::read
void read()
Read from BlackBoard into local copy.
Definition: interface.cpp:472
RosJointThread::bb_interface_created
virtual void bb_interface_created(const char *type, const char *id)
BlackBoard interface created notification.
Definition: joint_thread.cpp:77
fawkes::ROSAspect::rosnode
LockPtr< ros::NodeHandle > rosnode
Central ROS node handle.
Definition: ros.h:47
fawkes::BlackBoard::unregister_listener
virtual void unregister_listener(BlackBoardInterfaceListener *listener)
Unregister BB interface listener.
Definition: blackboard.cpp:212
fawkes::BlackBoardInterfaceListener
BlackBoard interface listener.
Definition: interface_listener.h:42
RosJointThread::RosJointThread
RosJointThread()
Constructor.
Definition: joint_thread.cpp:37
fawkes::Interface::id
const char * id() const
Get identifier of interface.
Definition: interface.cpp:652
RosJointThread::~RosJointThread
virtual ~RosJointThread()
Destructor.
Definition: joint_thread.cpp:44
fawkes::JointInterface::velocity
float velocity() const
Get velocity value.
Definition: JointInterface.cpp:106
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:119
RosJointThread::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: joint_thread.cpp:100
fawkes::MultiLogger::log_warn
virtual void log_warn(const char *component, const char *format,...)
Log warning message.
Definition: multi.cpp:216
RosJointThread::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: joint_thread.cpp:107
fawkes::BlackBoard::close
virtual void close(Interface *interface)=0
Close interface.
RosJointThread::finalize
virtual void finalize()
Finalize the thread.
Definition: joint_thread.cpp:66
fawkes
Fawkes library namespace.
fawkes::BlackBoard::register_observer
virtual void register_observer(BlackBoardInterfaceObserver *observer)
Register BB interface observer.
Definition: blackboard.cpp:225
fawkes::Interface
Base class for all Fawkes BlackBoard interfaces.
Definition: interface.h:79
fawkes::JointInterface
JointInterface Fawkes BlackBoard Interface.
Definition: JointInterface.h:34
fawkes::Exception::what
virtual const char * what() const
Get primary string.
Definition: exception.cpp:639
fawkes::Thread
Thread class encapsulation of pthreads.
Definition: thread.h:46
fawkes::BlackBoard::unregister_observer
virtual void unregister_observer(BlackBoardInterfaceObserver *observer)
Unregister BB interface observer.
Definition: blackboard.cpp:240
fawkes::BlackBoardAspect::blackboard
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
Definition: blackboard.h:44
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
Open multiple interfaces for reading.
fawkes::JointInterface::position
float position() const
Get position value.
Definition: JointInterface.cpp:72
RosJointThread::init
virtual void init()
Initialize the thread.
Definition: joint_thread.cpp:49
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:232
fawkes::BlackBoard::update_listener
virtual void update_listener(BlackBoardInterfaceListener *listener, ListenerRegisterFlag flag=BBIL_FLAG_ALL)
Update BB event listener.
Definition: blackboard.cpp:197
RosJointThread::bb_interface_data_changed
virtual void bb_interface_data_changed(fawkes::Interface *interface)
BlackBoard data changed notification.
Definition: joint_thread.cpp:137
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36