Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
node_thread.cpp
1 
2 /***************************************************************************
3  * node_thread.cpp - ROS node handle providing Thread
4  *
5  * Created: Thu May 05 18:37:08 2011
6  * Copyright 2006-2011 Tim Niemueller [www.niemueller.de]
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 #include <ros/ros.h>
26 
27 using namespace fawkes;
28 
29 /** @class ROSNodeThread "node_thread.h"
30  * ROS node handle thread.
31  * This thread maintains a ROS node which can be used by other
32  * threads and is provided via the ROSAspect.
33  *
34  * @author Tim Niemueller
35  */
36 
37 /** Constructor. */
39  : Thread("ROSNodeThread", Thread::OPMODE_WAITFORWAKEUP),
40  BlockedTimingAspect(BlockedTimingAspect::WAKEUP_HOOK_POST_LOOP),
41  AspectProviderAspect("ROSAspect", &__ros_aspect_inifin)
42 {
43 }
44 
45 
46 /** Destructor. */
48 {
49 }
50 
51 
52 void
54 {
55  if (! ros::isInitialized()) {
56  int argc = 1;
57  const char *argv[] = {"fawkes"};
58  ros::init(argc, (char **)argv, "fawkes",
59  (uint32_t)ros::init_options::NoSigintHandler);
60  } else {
61  logger->log_warn(name(), "ROS node already initialized");
62  }
63 
64  if (ros::isStarted()) {
65  logger->log_warn(name(), "ROS node already *started*");
66  }
67 
68  __rosnode = new ros::NodeHandle();
69 
70  __ros_aspect_inifin.set_rosnode(__rosnode);
71 }
72 
73 
74 void
76 {
77  __rosnode->shutdown();
78 
79  __rosnode.clear();
80  __ros_aspect_inifin.set_rosnode(__rosnode);
81 }
82 
83 
84 void
86 {
87  ros::spinOnce();
88 }