42 #define PRINT_FUNC_CALL(state) \ 43 ROS_DEBUG_STREAM(#state << "() method called") 49 const std::string& name_space,
50 const std::string& name)
51 : node_namespace_(name_space),
53 state_request_callback_queue_()
82 << std::boolalpha << wait_for_supervisor_);
87 if (wait_for_supervisor_)
97 ROS_INFO_STREAM(
"autostart_after_reconfigure = " 98 << std::boolalpha << autostart_after_reconfigure_);
112 float heartbeat_rate;
113 ros::param::param<float>(
"~heartbeat_rate", heartbeat_rate, 1.0f);
114 ROS_INFO(
"heartbeat_rate = %.3f [Hz]", heartbeat_rate);
126 ros::param::param<bool>(
"~autostart",
autostart_,
false);
129 state_request_spinner_->start();
131 autostart_ = autostart_ || autostart;
132 ROS_INFO_STREAM(
"autostart = " << std::boolalpha << autostart_);
160 const std::string&
function,
161 const std::string& description)
164 << error_type <<
" function: " <<
function 165 <<
" description: " << description);
166 robot_activity_msgs::Error error_msg;
169 error_msg.error_type = error_type;
170 error_msg.function =
function;
171 error_msg.description = description;
177 const float& frequency,
182 auto isolated_async_timer = std::make_shared<IsolatedAsyncTimer>(
190 return isolated_async_timer;
198 return std::string();
275 const std::string& service_name,
276 const std::vector<State>& states)
279 "Registering state transition request for state " << service_name);
280 using std_srvs::Empty;
284 for (
const auto &
s : states)
291 auto options = ros::AdvertiseServiceOptions::create<Empty>(
313 if (starting_state == goal_state)
322 auto to_state =
static_cast<uint8_t
>(goal_state);
327 <<
"] to [" << goal_state <<
"]");
331 if (!transition_result)
334 <<
"] to [" << goal_state <<
"] has failed during [" 345 uint8_t to_state =
static_cast<uint8_t
>(new_state);
347 if (callback ==
nullptr)
351 <<
"] to [" << new_state <<
"]. Transition does NOT exist!");
354 bool transition_result = boost::bind(callback,
this)();
355 if (transition_result)
359 << new_state <<
"] succeeded!");
366 << new_state <<
"] failed!");
369 return transition_result;
383 os <<
"UNCONFIGURED";
398 os.setstate(std::ios_base::failbit);
406 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr},
418 {
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr}
void notifyError(uint8_t error_type, const std::string &function, const std::string &description)
Sends an error message to a global error topic "/error".
static const StateTransitions STATE_TRANSITIONS
2D array of direct state transitions with values being the corresponding functions to be called durin...
bool create()
Called automatically, when transition from LAUNCHING to UNCONFIGURED. RobotActivity calls this functi...
ros::ServiceServer stop_server_
ServiceServer for serving stop service.
boost::shared_ptr< void const > VoidConstPtr
static const StateTransitionPaths STATE_TRANSITIONS_PATHS
2D array of paths between states.
void publish(const boost::shared_ptr< M > &message) const
bool start()
Called automatically, when transition from STOPPED to PAUSED. RobotActivity calls this function direc...
virtual ~RobotActivity()
Virtual destructor.
virtual bool onPause()=0
Function to be defined by the user. Called at the end of transition from RUNNING to PAUSED...
bool wait_for_supervisor_
Whether to wait for the supervisor during the init function. Waiting means that there has to be at le...
bool(RobotActivity::* MemberLambdaCallback)()
std::vector< std::shared_ptr< robot_activity::IsolatedAsyncTimer > > process_timers_
Vector of shared pointers to isolated timers created by register isolated timer.
std::function< void(void)> LambdaCallback
ROSCPP_DECL bool isInitialized()
std::shared_ptr< ros::AsyncSpinner > state_request_spinner_
Async spinner for serving state change requests.
ros::NodeHandlePtr node_handle_private_
#define PRINT_FUNC_CALL(state)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ros::ServiceServer registerStateChangeRequest(const std::string &service_name, const std::vector< State > &states)
Registers a ROS service server listening for state change requests.
State StateTransitionPaths[static_cast< uint8_t >(State::Count)][static_cast< uint8_t >(State::Count)]
bool autostart_
Whether to automatically start (transition to RUNNING) at the end of init function if not the node wi...
bool changeState(const State &new_state)
Changes state from current to new. Direct transition must exist. The appropriate function will be cal...
ROSCPP_DECL const std::string & getName()
std::shared_ptr< ros::AsyncSpinner > global_callback_queue_spinner_
Shared pointer to async spinner that serves the global callback queue.
bool terminate()
Called automatically, when transition from UNCONFIGURED to TERMINATED. RobotActivity calls this funct...
boost::shared_ptr< NodeHandle > NodeHandlePtr
bool autostart_after_reconfigure_
Whether to autostart after reconfigure service is called.
bool stop()
Called automatically, when transition from PAUSED to STOPPED. RobotActivity calls this function direc...
boost::function< bool(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res) > EmptyServiceCallback
ros::Publisher process_error_pub_
ROS Publisher of error messages.
ros::Publisher process_state_pub_
ROS Publisher of heartbeat messages and state changes messages.
void notifyState() const
Sends a heartbeat message with the current state.
Class for adding node lifecycle to ROS processes.
State current_state_
Current state of the RobotActivity. Initially, LAUNCHING state.
bool unconfigure()
Called automatically, when transition from STOPPED to UNCONFIGURED. RobotActivity calls this function...
std::shared_ptr< robot_activity::IsolatedAsyncTimer > heartbeat_timer_
Shared pointer to the isolated timer that sends heartbeat messages.
virtual void onCreate()=0
Function to be defined by the user. Called at the end of transition from LAUNCHING to UNCONFIGURED...
State getState()
Returns the current state.
virtual bool onConfigure()=0
Function to be defined by the user. Called at the end of transition from UNCONFIGURED to STOPPED...
#define ROS_FATAL_STREAM_ONCE(args)
ros::ServiceServer reconfigure_server_
ServiceServer for serving reconfigure service.
ros::ServiceServer pause_server_
ServiceServer for serving pause service.
virtual bool onStart()=0
Function to be defined by the user. Called at the end of transition from STOPPED to PAUSED...
ros::ServiceServer restart_server_
ServiceServer for serving restart service, where restart means transition to STOPPED and then RUNNING...
std::shared_ptr< IsolatedAsyncTimer > registerIsolatedTimer(const IsolatedAsyncTimer::LambdaCallback &callback, const float &frequency, bool stoppable=true, bool autostart=false, bool oneshot=false)
Register an isolated async timer.
#define ROS_WARN_STREAM(args)
#define ROS_DEBUG_STREAM(args)
ros::ServiceServer start_server_
ServiceServer for serving start service.
RobotActivity class implements ROS node lifecycle.
MemberLambdaCallback StateTransitions[static_cast< uint8_t >(State::Count)][static_cast< uint8_t >(State::Count)]
ros::NodeHandlePtr node_handle_
virtual bool onStop()=0
Function to be defined by the user. Called at the end of transition from PAUSED to STOPPED...
virtual void onTerminate()=0
Function to be defined by the user. Called at the end of transition from UNCONFIGURED to TERMINATED...
virtual bool onUnconfigure()=0
Function to be defined by the user. Called at the end of transition from STOPPED to UNCONFIGURED...
bool pause()
Called automatically, when transition from RUNNING to PAUSED. RobotActivity calls this function direc...
uint32_t getNumSubscribers() const
bool resume()
Called automatically, when transition from PAUSED to RUNNING. RobotActivity calls this function direc...
#define ROS_INFO_STREAM(args)
State
RobotActivity state enum.
bool transitionToState(const State &new_state)
Transitions to a new state. Path must exists between the current and the new state. All appropriate function will be called when transition to the goal state.
std::string node_name_
Name of the actual ROS node.
std::ostream & operator<<(std::ostream &os, State state)
Overridden operator<< for easy State enum printing.
RobotActivity()=delete
Default constructor is deleted.
bool configure()
Called automatically, when transition from UNCONFIGURED to STOPPED. RobotActivity calls this function...
void runAsync(uint8_t threads=0)
Spins an amount of threads to serve the global callback queue. The call is non-blocking.
#define ROS_ERROR_STREAM(args)
std::string node_namespace_
Node's namespace, if empty then the private node handle resolves to ~ otheerwise private node handle ...
ros::ServiceServer terminate_server_
ServiceServer for serving terminate service.
virtual bool onResume()=0
Function to be defined by the user. Called at the end of transition from PAUSED to RUNNING...
std::string getNamespace() const
Returns the full private namespace.
RobotActivity & init(bool autostart=false)
Initializes the RobotActivity.
ros::CallbackQueue state_request_callback_queue_
Callback queue for state change request services.
void run(uint8_t threads=0)
Spins an amount of threads to serve the global callback queue. The call is blocking.
ROSCPP_DECL void waitForShutdown()