SMACC2
Loading...
Searching...
No Matches
cl_multirole_sensor.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <optional>
18#include <rclcpp/rclcpp.hpp>
22
24{
25using namespace smacc2;
26
27template <typename TSource, typename TOrthogonal>
28struct EvTopicMessageTimeout : sc::event<EvTopicMessageTimeout<TSource, TOrthogonal>>
29{
30};
31
32using namespace smacc2::client_bases;
33
34// This class extends a ros topic subscriber object that reads from
35// some sensor source. It provides timeout event.
36template <typename MessageType>
38{
39public:
40 typedef MessageType TMessageType;
42
43 ClMultiroleSensor() : smacc2::client_bases::SmaccSubscriberClient<MessageType>()
44 {
45 initialized_ = false;
46 }
47
48 template <typename T>
49 boost::signals2::connection onMessageTimeout(void (T::*callback)(), T * object)
50 {
51 return this->getStateMachine()->createSignalConnection(onMessageTimeout_, callback, object);
52 }
53
54 std::function<void()> postTimeoutMessageEvent;
55
56 template <typename TOrthogonal, typename TSourceObject>
58 {
60 TOrthogonal, TSourceObject>();
61
62 this->postTimeoutMessageEvent = [this]()
63 {
64 this->onMessageTimeout_();
65
67 this->postEvent(event);
68 };
69 }
70
71 void onInitialize() override
72 {
73 if (!initialized_)
74 {
76
78
79 if (timeout_)
80 {
81 auto ros_clock = rclcpp::Clock::make_shared();
82 timeoutTimer_ = rclcpp::create_timer(
83 this->getNode(), this->getNode()->get_clock(), *timeout_,
85
86 timeoutTimer_->reset();
87 }
88 else
89 {
90 RCLCPP_WARN(
91 this->getLogger(),
92 "Timeout sensor client not set, skipping timeout watchdog funcionality");
93 }
94
95 initialized_ = true;
96 }
97 }
98
99 std::optional<rclcpp::Duration> timeout_;
100
101protected:
102 void resetTimer(const MessageType & /*msg*/)
103 {
104 //resetting the timer
105 timeoutTimer_->reset();
106 //timeoutTimer_->stop();
107 //timeoutTimer_->start();
108 }
109
110private:
111 rclcpp::TimerBase::SharedPtr timeoutTimer_;
113
115};
116} // namespace cl_multirole_sensor
rclcpp::TimerBase::SharedPtr timeoutTimer_
boost::signals2::connection onMessageTimeout(void(T::*callback)(), T *object)
std::optional< rclcpp::Duration > timeout_
rclcpp::Node::SharedPtr getNode()
Definition: client.cpp:60
ISmaccStateMachine * getStateMachine()
rclcpp::Logger getLogger()
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection onMessageReceived(void(T::*callback)(const MessageType &), T *object)