SMACC2
Loading...
Searching...
No Matches
cb_px4_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/*****************************************************************************************************************
16 *
17 * Authors: Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22
23#include <atomic>
24#include <chrono>
25#include <cstdint>
26#include <optional>
27
35
36namespace cl_px4_mr
37{
38
39// Base for cl_px4_mr client behaviors. Encodes the async-thread discipline
40// (see the locking rule in CLAUDE.md): the shared components are resolved and
41// completion signals wired in onStateOrthogonalAllocation, on the state
42// machine thread - never from the asynchronous onEntry thread, where
43// requiresComponent/createSignalConnection contend for the state machine
44// mutex and can deadlock against a concurrent transition.
45//
46// Derived behaviors:
47// - override wireCompletionSignals() to connect their completion signal
48// (goal checker reached, vehicle disarmed, ...) - it is invoked on the
49// state machine thread during allocation
50// - keep onEntry() as pure command issuance (setpoints, goals, mode switches)
51// - finish through postPx4Success()/postPx4Failure(), which latch completion
52// so the timeout watchdog and a late signal can never double-post
53//
54// Timeout watchdog: setTimeout() arms a deadline checked from update() (the
55// SignalDetector thread, ~20 Hz). On expiry the behavior posts failure -
56// previously no signal-driven PX4 behavior had ANY failure path, so a stuck
57// goal checker or a lost vehicle meant waiting forever. Poll-driven behaviors
58// that override update() must chain CbPx4ClientBehaviorBase::update() to keep
59// the watchdog.
62{
63public:
64 template <typename TOrthogonal, typename TSourceObject>
79
81
82 // arm the completion watchdog (checked from update() on the SignalDetector
83 // thread); disabled when never called. Atomic so it may be armed from the
84 // state machine thread (runtimeConfigure) or from the async onEntry thread
85 // (auto-timeouts derived from path length); a value set by the state machine
86 // always wins because runtimeConfigure runs before onEntry.
87 void setTimeout(std::chrono::milliseconds timeout) { timeoutMs_ = timeout.count(); }
88
89 // true once a timeout has been armed (by the state machine or the behavior)
90 bool hasTimeout() const { return timeoutMs_.load() > 0; }
91
92 void update() override
93 {
94 const int64_t timeoutMs = timeoutMs_.load();
95 if (timeoutMs <= 0 || completed_)
96 {
97 return;
98 }
99
100 auto now = std::chrono::steady_clock::now();
101 if (!watchdogStart_)
102 {
103 watchdogStart_ = now;
104 return;
105 }
106
107 if (now - *watchdogStart_ > std::chrono::milliseconds(timeoutMs))
108 {
109 RCLCPP_ERROR(
110 getLogger(), "[%s] Timed out after %ld ms without completing - posting failure",
111 getName().c_str(), static_cast<long>(timeoutMs));
112 this->postPx4Failure();
113 }
114 }
115
116protected:
117 // connect completion signals here (invoked on the state machine thread
118 // during state allocation)
119 virtual void wireCompletionSignals() {}
120
122 {
123 if (!completed_.exchange(true))
124 {
125 this->postSuccessEvent();
126 }
127 }
128
130 {
131 if (!completed_.exchange(true))
132 {
133 this->postFailureEvent();
134 }
135 }
136
143
144private:
145 std::atomic<bool> completed_{false};
146 std::atomic<int64_t> timeoutMs_{0}; // 0 = watchdog disabled
147 std::optional<std::chrono::steady_clock::time_point> watchdogStart_;
148};
149
150} // namespace cl_px4_mr
std::optional< std::chrono::steady_clock::time_point > watchdogStart_
void setTimeout(std::chrono::milliseconds timeout)
virtual rclcpp::Logger getLogger() const
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)