Robowflex  v0.1
Making MoveIt Easy
util.cpp
Go to the documentation of this file.
1 /* Author: Zachary Kingston */
2 #include <csignal>
3 
4 #include <ros/init.h>
5 #include <ros/master.h>
6 
10 
11 #if IS_BOOST_164
12 #include <boost/process.hpp>
13 #endif
14 
15 using namespace robowflex;
16 
17 namespace
18 {
20 
21 #if IS_BOOST_164
22  static boost::process::child ROSCORE;
23  static bool ROSCORE_INIT{false};
24 #endif
25 
26  void shutdown(int sig)
27  {
28  if (sig)
29  RBX_INFO("Shutting down with signal %s.", strsignal(sig));
30  else
31  RBX_INFO("Shutting down.");
32 
33  if (SPINNER)
34  SPINNER->stop();
35 
36  // Some stuff for later
37  ros::shutdown();
38 
39 #if IS_BOOST_164
40  if (ROSCORE_INIT)
41  ROSCORE.terminate();
42 #endif
43 
44  exit(0);
45  }
46 
47  void startup()
48  {
49  if (!ros::master::check())
50  {
51  RBX_ERROR("rosmaster is not running!");
52 #if IS_BOOST_164
53  RBX_WARN("Booting rosmaster...");
54  ROSCORE = boost::process::child("rosmaster", //
55  boost::process::std_in.close(), //
56  boost::process::std_out > boost::process::null, //
57  boost::process::std_err > boost::process::null //
58  );
59 
60  ROSCORE_INIT = true;
61 #endif
62  }
63 
64  ros::start();
65  }
66 } // namespace
67 
68 ROS::ROS(int argc, char **argv, const std::string &name, unsigned int threads) : argc_(argc), argv_(argv)
69 {
70  ros::init(argc, argv, name, ros::init_options::NoSigintHandler);
71  startup();
72 
73  signal(SIGINT, shutdown);
74  signal(SIGSEGV, shutdown);
75 
76  if (threads)
77  {
78  SPINNER.reset(new ros::AsyncSpinner(threads));
79  SPINNER->start();
80  }
81 }
82 
84 {
85  shutdown(0);
86 }
87 
89 {
91  ros::removeROSArgs(argc_, argv_, args);
92 
93  return args;
94 }
95 
96 void ROS::wait() const
97 {
98  ros::waitForShutdown();
99 }
100 
102 {
103  raise(SIGSEGV);
104 }
char ** argv_
Arguments.
Definition: util.h:79
ROS(int argc, char **argv, const std::string &name="robowflex", unsigned int threads=1)
Constructor. Start-up ROS. If Boost version is greater than 1.64, rosmaster is started if it is not a...
Definition: util.cpp:68
std::vector< std::string > getArgs() const
Get command-line arguments without ROS parameters.
Definition: util.cpp:88
void wait() const
Waits for the process to be killed via some means (normally Ctrl-C)
Definition: util.cpp:96
~ROS()
Destructor. Shutdown ROS.
Definition: util.cpp:83
int argc_
Argument count.
Definition: util.h:78
T exit(T... args)
#define RBX_WARN(fmt,...)
Output a warning logging message.
Definition: log.h:110
#define RBX_ERROR(fmt,...)
Output a error logging message.
Definition: log.h:102
#define RBX_INFO(fmt,...)
Output a info logging message.
Definition: log.h:118
Main namespace. Contains all library classes and functions.
Definition: scene.cpp:25
void explode()
Trigger a SEGSEGV.
Definition: util.cpp:101
T reset(T... args)