SMACC2
Loading...
Searching...
No Matches
cp_ros2_timer.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 <chrono>
18#include <optional>
19#include <rclcpp/rclcpp.hpp>
20#include <smacc2/common.hpp>
21#include <smacc2/component.hpp>
23
24namespace smacc2
25{
26namespace client_core_components
27{
28using namespace smacc2::default_events;
29
31{
32public:
33 CpRos2Timer(rclcpp::Duration duration, bool oneshot = false)
34 : duration_(duration), oneshot_(oneshot), initialized_(false)
35 {
36 }
37
38 virtual ~CpRos2Timer()
39 {
40 if (timer_)
41 {
42 timer_->cancel();
43 }
44 }
45
46 void onInitialize() override
47 {
48 if (!initialized_)
49 {
50 RCLCPP_INFO_STREAM(
51 getLogger(), "[" << this->getName() << "] Initializing ROS2 Timer with duration: "
52 << duration_.seconds() << "s, oneshot: " << (oneshot_ ? "true" : "false"));
53
54 auto clock = this->getNode()->get_clock();
55 timer_ = rclcpp::create_timer(
56 this->getNode(), clock, duration_, std::bind(&CpRos2Timer::timerCallback, this));
57
58 this->initialized_ = true;
59 }
60 }
61
63
65 {
66 if (timer_ && timer_->is_canceled())
67 {
68 RCLCPP_INFO_STREAM(getLogger(), "[" << this->getName() << "] Starting timer");
69 timer_->reset();
70 }
71 }
72
73 void stopTimer()
74 {
75 if (timer_)
76 {
77 RCLCPP_INFO_STREAM(getLogger(), "[" << this->getName() << "] Stopping timer");
78 timer_->cancel();
79 }
80 }
81
83 {
84 if (timer_)
85 {
86 RCLCPP_INFO_STREAM(getLogger(), "[" << this->getName() << "] Cancelling timer");
87 timer_->cancel();
88 }
89 }
90
91 template <typename T>
92 boost::signals2::connection onTimerTick(void (T::*callback)(), T * object)
93 {
94 return this->getStateMachine()->createSignalConnection(onTimerTick_, callback, object);
95 }
96
97private:
99 {
100 RCLCPP_DEBUG_STREAM(getLogger(), "[" << this->getName() << "] Timer tick");
101
102 if (!onTimerTick_.empty())
103 {
104 onTimerTick_();
105 }
106
107 if (oneshot_)
108 {
109 RCLCPP_INFO_STREAM(
110 getLogger(), "[" << this->getName() << "] Oneshot timer completed, cancelling");
111 timer_->cancel();
112 }
113 }
114 rclcpp::TimerBase::SharedPtr timer_;
115 rclcpp::Duration duration_;
118};
119
120} // namespace client_core_components
121} // namespace smacc2
ISmaccStateMachine * getStateMachine()
virtual std::string getName() const
rclcpp::Logger getLogger() const
rclcpp::Node::SharedPtr getNode()
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection onTimerTick(void(T::*callback)(), T *object)
CpRos2Timer(rclcpp::Duration duration, bool oneshot=false)
smacc2::SmaccSignal< void()> onTimerTick_