SMACC
Loading...
Searching...
No Matches
cl_multirole_sensor.h
Go to the documentation of this file.
1#pragma once
2
4#include <boost/statechart/event.hpp>
5
6#include <ros/ros.h>
7#include <ros/duration.h>
8#include <boost/signals2.hpp>
9#include <boost/optional/optional_io.hpp>
10
12{
13using namespace smacc;
14
15template <typename TSource, typename TOrthogonal>
16struct EvTopicMessageTimeout : sc::event<EvTopicMessageTimeout<TSource, TOrthogonal>>
17{
18 ros::TimerEvent timerData;
19};
20
21using namespace smacc::client_bases;
22
23//---------------------------------------------------------------
24template <typename MessageType>
26{
27public:
28 typedef MessageType TMessageType;
29 SmaccSignal<void(const ros::TimerEvent &)> onMessageTimeout_;
30
32 : smacc::client_bases::SmaccSubscriberClient<MessageType>()
33 {
34 ROS_INFO("[ClMultiroleSensor] constructor");
35 initialized_ = false;
36 }
37
38 template <typename T>
39 boost::signals2::connection onMessageTimeout(void (T::*callback)(const ros::TimerEvent &), T *object)
40 {
41 return this->getStateMachine()->createSignalConnection(onMessageTimeout_, callback, object);
42 }
43
44 std::function<void(const ros::TimerEvent &ev)> postTimeoutMessageEvent;
45
46 template <typename TOrthogonal, typename TSourceObject>
48 {
49 SmaccSubscriberClient<MessageType>::template onOrthogonalAllocation<TOrthogonal, TSourceObject>();
50
51 this->postTimeoutMessageEvent = [=](auto &timerdata) {
52 onMessageTimeout_(timerdata);
53
55 event->timerData = timerdata;
56 this->postEvent(event);
57 };
58 }
59
60 virtual void initialize() override
61 {
62 if (!initialized_)
63 {
65
67
68 if (timeout_)
69 {
70 timeoutTimer_ = this->nh_.createTimer(*timeout_, boost::bind(&ClMultiroleSensor<MessageType>::timeoutCallback, this, _1));
71 timeoutTimer_.start();
72 }
73 else
74 {
75 ROS_WARN("Timeout sensor client not set, skipping timeout watchdog funcionality");
76 }
77
78 initialized_ = true;
79 }
80 }
81
82 boost::optional<ros::Duration> timeout_;
83
84protected:
85 void resetTimer(const MessageType &msg)
86 {
87 //resetting the timer
88 this->timeoutTimer_.stop();
89 this->timeoutTimer_.start();
90 }
91
92private:
93 ros::Timer timeoutTimer_;
95
96 void timeoutCallback(const ros::TimerEvent &timerdata)
97 {
98 postTimeoutMessageEvent(timerdata);
99 }
100};
101} // namespace cl_multirole_sensor
boost::optional< ros::Duration > timeout_
std::function< void(const ros::TimerEvent &ev)> postTimeoutMessageEvent
boost::signals2::connection onMessageTimeout(void(T::*callback)(const ros::TimerEvent &), T *object)
void timeoutCallback(const ros::TimerEvent &timerdata)
void resetTimer(const MessageType &msg)
SmaccSignal< void(const ros::TimerEvent &)> onMessageTimeout_
ISmaccStateMachine * getStateMachine()
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection onMessageReceived(void(T::*callback)(const MessageType &), T *object)