SMACC2
Loading...
Searching...
No Matches
cb_rgb_client_behavior_base.hpp
Go to the documentation of this file.
1// Copyright 2026 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 <atomic>
18#include <chrono>
19#include <cstdint>
20#include <optional>
21#include <string>
22
28
29namespace cl_rgb
30{
31
32// Base for cl_rgb client behaviors. The components are resolved and the completion signals
33// wired in onStateOrthogonalAllocation, on the state machine thread; onEntry, which runs on
34// the behavior's own thread, only computes (see the async thread locking rule in CLAUDE.md).
35//
36// Derived behaviors:
37// - override wireCompletionSignals() to connect a component signal, when they finish on one
38// - finish through postRgbSuccess()/postRgbFailure(), which post once however often called
39// - may arm a watchdog with setTimeout(); it is checked from update() and posts failure
42{
43public:
44 explicit CbRgbClientBehaviorBase(const std::string & cameraName) : cameraName_(cameraName) {}
45
47
48 template <typename TOrthogonal, typename TSourceObject>
60
61 // wall time after which the behavior posts failure; never when not called
62 void setTimeout(std::chrono::milliseconds timeout) { timeoutMs_ = timeout.count(); }
63
64 void update() override
65 {
66 const int64_t timeoutMs = timeoutMs_.load();
67 if (timeoutMs <= 0 || completed_)
68 {
69 return;
70 }
71 const auto now = std::chrono::steady_clock::now();
72 if (!watchdogStart_)
73 {
74 watchdogStart_ = now;
75 return;
76 }
77 if (now - *watchdogStart_ > std::chrono::milliseconds(timeoutMs))
78 {
79 RCLCPP_ERROR(
80 getLogger(), "[%s] Timed out after %ld ms without completing - posting failure",
81 getName().c_str(), static_cast<long>(timeoutMs));
82 this->postRgbFailure();
83 }
84 }
85
86protected:
87 virtual void wireCompletionSignals() {}
88
90 {
91 if (!completed_.exchange(true))
92 {
93 this->postSuccessEvent();
94 }
95 }
96
98 {
99 if (!completed_.exchange(true))
100 {
101 this->postFailureEvent();
102 }
103 }
104
105 bool completed() const { return completed_; }
106
107 std::string cameraName_;
108 CpCamera * camera_ = nullptr;
109 CpTablePlane * table_ = nullptr;
112
113private:
114 std::atomic<bool> completed_{false};
115 std::atomic<int64_t> timeoutMs_{0}; // 0 = watchdog disabled
116 std::optional<std::chrono::steady_clock::time_point> watchdogStart_;
117};
118
119} // namespace cl_rgb
std::optional< std::chrono::steady_clock::time_point > watchdogStart_
CbRgbClientBehaviorBase(const std::string &cameraName)
void setTimeout(std::chrono::milliseconds timeout)
virtual rclcpp::Logger getLogger() const
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)