Fawkes API  Fawkes Development Version
node_thread.cpp
1 
2 /***************************************************************************
3  * node_thread.cpp - Gazebo node handle providing Thread
4  *
5  * Created: Fri Aug 24 11:04:04 2012
6  * Author Bastian Klingen, Frederik Zwilling (2013)
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL file in the doc directory.
21  */
22 
23 #include "node_thread.h"
24 
25 // from Gazebo
26 #include <gazebo/gazebo_config.h>
27 #include <google/protobuf/message.h>
28 
29 #include <gazebo/msgs/msgs.hh>
30 #include <gazebo/transport/Node.hh>
31 #include <gazebo/transport/TransportIface.hh>
32 #include <gazebo/transport/TransportTypes.hh>
33 
34 using namespace fawkes;
35 
36 /** @class GazeboNodeThread "node_thread.h"
37  * Gazebo node handle thread.
38  * This thread maintains a Gazebo node which can be used by other
39  * threads and is provided via the GazeboAspect.
40  *
41  * @author Bastian Klingen, Frederik Zwilling
42  */
43 
44 /** Constructor. */
46 : Thread("GazeboNodeThread", Thread::OPMODE_WAITFORWAKEUP),
47  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP),
48  AspectProviderAspect(&gazebo_aspect_inifin_)
49 {
50 }
51 
52 /** Destructor. */
54 {
55 }
56 
57 void
59 {
60  //read config values
61  robot_channel =
62  config->get_string("/gazsim/world-name") + "/" + config->get_string("/gazsim/robot-name");
63 
64  world_name = config->get_string("/gazsim/world-name");
65 
66  if (gazebo::transport::is_stopped()) {
67  gazebo::transport::init();
68  gazebo::transport::run();
69  } else {
70  logger->log_warn(name(), "Gazebo already running ");
71  }
72 
73  //Initialize Communication nodes:
74  //the common one for the robot
75  gazebo::transport::NodePtr node(new gazebo::transport::Node());
76  gazebonode_ = node;
77  //initialize node (this node only communicates with nodes that were initialized with the same string)
78  gazebonode_->Init(robot_channel.c_str());
79  gazebo_aspect_inifin_.set_gazebonode(gazebonode_);
80 
81  //and the node for world change messages
82  gazebo_world_node_ = gazebo::transport::NodePtr(new gazebo::transport::Node());
83  gazebo_world_node_->Init(world_name.c_str());
84  gazebo_aspect_inifin_.set_gazebo_world_node(gazebo_world_node_);
85 }
86 
87 void
89 {
90  gazebonode_->Fini();
91  gazebonode_.reset();
92  gazebo_aspect_inifin_.set_gazebonode(gazebonode_);
93  gazebo_world_node_->Fini();
94  gazebo_world_node_.reset();
95  gazebo_aspect_inifin_.set_gazebonode(gazebo_world_node_);
96 }
97 
98 void
100 {
101 }
fawkes::GazeboAspectIniFin::set_gazebonode
void set_gazebonode(gazebo::transport::NodePtr gazebonode)
Set the Gazebo node handle to use for aspect initialization.
Definition: gazebo_inifin.cpp:90
fawkes::AspectProviderAspect
Definition: aspect_provider.h:39
fawkes::BlockedTimingAspect
Definition: blocked_timing.h:54
fawkes::Thread::name
const char * name() const
Definition: thread.h:99
fawkes::GazeboAspectIniFin::set_gazebo_world_node
void set_gazebo_world_node(gazebo::transport::NodePtr gazebo_world_node)
Set the Gazebo node handle to use for aspect initialization.
Definition: gazebo_inifin.cpp:99
fawkes::LoggingAspect::logger
Logger * logger
Definition: logging.h:50
fawkes
fawkes::Logger::log_warn
virtual void log_warn(const char *component, const char *format,...)=0
fawkes::ConfigurableAspect::config
Configuration * config
Definition: configurable.h:50
GazeboNodeThread::~GazeboNodeThread
virtual ~GazeboNodeThread()
Destructor.
Definition: node_thread.cpp:52
GazeboNodeThread::GazeboNodeThread
GazeboNodeThread()
Constructor.
Definition: node_thread.cpp:44
GazeboNodeThread::loop
virtual void loop()
Code to execute in the thread.
Definition: node_thread.cpp:98
fawkes::Thread
Definition: thread.h:44
fawkes::Configuration::get_string
virtual std::string get_string(const char *path)=0
GazeboNodeThread::init
virtual void init()
Initialize the thread.
Definition: node_thread.cpp:57
GazeboNodeThread::finalize
virtual void finalize()
Finalize the thread.
Definition: node_thread.cpp:87