SMACC2
Loading...
Searching...
No Matches
cb_timer_countdown_loop.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft 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
18#include <smacc2/smacc.hpp>
19
20namespace cl_ros2_timer
21{
23{
24public:
25 explicit CbTimerCountdownLoop(rclcpp::Duration loopDuration) : loopDuration_(loopDuration) {}
26
27 // Catch the old tick-count integer API and emit a migration hint at compile time.
28 template <typename T, std::enable_if_t<std::is_integral<T>::value, int> = 0>
30 {
31 static_assert(
32 !std::is_integral<T>::value,
33 "\n\nCbTimerCountdownLoop no longer accepts a tick count.\n"
34 "Pass a chrono duration literal instead:\n"
35 " configure_orthogonal<OrTimer, CbTimerCountdownLoop>(3s); // every 3 seconds\n"
36 " configure_orthogonal<OrTimer, CbTimerCountdownLoop>(500ms); // every 500 ms\n"
37 "Migration guide: https://smacc2.robosoft.ai/how-to/how-to-cl-ros2-timer.html\n");
38 }
39
40 void onEntry() override
41 {
42 auto node = this->getNode();
43 auto clock = node->get_clock();
44 wallTimer_ = rclcpp::create_timer(
45 node, clock, std::chrono::nanoseconds(loopDuration_.nanoseconds()),
46 [this]()
47 {
48 onTimerTick_();
49 postCountDownEvent_();
50 });
51 }
52
53 void onExit() override
54 {
55 if (wallTimer_)
56 {
57 wallTimer_->cancel();
58 wallTimer_.reset();
59 }
60 }
61
62 template <typename TOrthogonal, typename TSourceObject>
64 {
65 this->postCountDownEvent_ = [=]()
67 }
68
69 template <typename T>
70 smacc2::SmaccSignalConnection onTimerTick(void (T::*callback)(), T * object)
71 {
72 return this->getStateMachine()->createSignalConnection(onTimerTick_, callback, object);
73 }
74
75private:
76 rclcpp::Duration loopDuration_;
77 rclcpp::TimerBase::SharedPtr wallTimer_;
78
79 std::function<void()> postCountDownEvent_;
81};
82} // namespace cl_ros2_timer
CbTimerCountdownLoop(rclcpp::Duration loopDuration)
smacc2::SmaccSignalConnection onTimerTick(void(T::*callback)(), T *object)
virtual rclcpp::Node::SharedPtr getNode() const
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
boost::signals2::connection SmaccSignalConnection