SMACC2
Loading...
Searching...
No Matches
cl_px4_mr::CbWaitForHeadingStable Class Reference

#include <cb_wait_for_heading_stable.hpp>

Inheritance diagram for cl_px4_mr::CbWaitForHeadingStable:
Inheritance graph
Collaboration diagram for cl_px4_mr::CbWaitForHeadingStable:
Collaboration graph

Public Member Functions

 CbWaitForHeadingStable (HeadingStableParams params={})
 
void onEntry () override
 
void onExit () override
 
- Public Member Functions inherited from cl_px4_mr::CbPx4ClientBehaviorBase
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
 ~CbPx4ClientBehaviorBase () override
 
void setTimeout (std::chrono::milliseconds timeout)
 
bool hasTimeout () const
 
void update () override
 
- Public Member Functions inherited from smacc2::SmaccAsyncClientBehavior
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
 SmaccAsyncClientBehavior ()=default
 
 ~SmaccAsyncClientBehavior () override
 
 SmaccAsyncClientBehavior (const SmaccAsyncClientBehavior &)=delete
 
SmaccAsyncClientBehavior & operator= (const SmaccAsyncClientBehavior &)=delete
 
 SmaccAsyncClientBehavior (SmaccAsyncClientBehavior &&)=delete
 
SmaccAsyncClientBehavior & operator= (SmaccAsyncClientBehavior &&)=delete
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onSuccess (TCallback callback, T *object)
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onFinished (TCallback callback, T *object)
 
template<typename TCallback , typename T >
smacc2::SmaccSignalConnection onFailure (TCallback callback, T *object)
 
void requestForceFinish ()
 
void executeOnEntry () override
 
void executeOnExit () override
 
void waitOnEntryThread (bool requestFinish)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onSuccess (TCallbackMethod callback, T *object)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onFinished (TCallbackMethod callback, T *object)
 
template<typename TCallbackMethod , typename T >
smacc2::SmaccSignalConnection onFailure (TCallbackMethod callback, T *object)
 
- Public Member Functions inherited from smacc2::ISmaccClientBehavior
 ISmaccClientBehavior ()
 
virtual ~ISmaccClientBehavior ()
 
 ISmaccClientBehavior (const ISmaccClientBehavior &)=delete
 
ISmaccClientBehavior & operator= (const ISmaccClientBehavior &)=delete
 
 ISmaccClientBehavior (ISmaccClientBehavior &&)=delete
 
ISmaccClientBehavior & operator= (ISmaccClientBehavior &&)=delete
 
ISmaccStateMachine * getStateMachine ()
 
std::string getName () const
 
template<typename SmaccClientType >
void requiresClient (SmaccClientType *&storage)
 
template<typename SmaccComponentType >
void requiresComponent (SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
template<typename SmaccComponentType >
void requiresComponent (const std::string &name, SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
- Public Member Functions inherited from smacc2::ISmaccUpdatable
 ISmaccUpdatable ()
 
 ISmaccUpdatable (rclcpp::Duration duration)
 
virtual ~ISmaccUpdatable ()=default
 
 ISmaccUpdatable (const ISmaccUpdatable &)=delete
 
ISmaccUpdatable & operator= (const ISmaccUpdatable &)=delete
 
 ISmaccUpdatable (ISmaccUpdatable &&)=delete
 
ISmaccUpdatable & operator= (ISmaccUpdatable &&)=delete
 
void executeUpdate (rclcpp::Node::SharedPtr node)
 
void setUpdatePeriod (rclcpp::Duration duration)
 

Private Attributes

HeadingStableParams params_
 

Additional Inherited Members

- Protected Member Functions inherited from cl_px4_mr::CbPx4ClientBehaviorBase
virtual void wireCompletionSignals ()
 
void postPx4Success ()
 
void postPx4Failure ()
 
- Protected Member Functions inherited from smacc2::SmaccAsyncClientBehavior
void postSuccessEvent ()
 
void postFailureEvent ()
 
void dispose () override
 
bool isShutdownRequested ()
 onEntry is executed in a new thread. However the current state cannot be left until the onEntry thread finishes. This flag can be checked from the onEntry thread to force finishing the thread.
 
- Protected Member Functions inherited from smacc2::ISmaccClientBehavior
virtual void runtimeConfigure ()
 
template<typename EventType >
void postEvent (const EventType &ev)
 
template<typename EventType >
void postEvent ()
 
ISmaccState * getCurrentState ()
 
void postInitializationFailureEvent (const std::string &what)
 
virtual rclcpp::Node::SharedPtr getNode () const
 
virtual rclcpp::Logger getLogger () const
 
std::string getTraceStateName () const
 
std::string getTraceOrthogonalName () const
 
- Protected Member Functions inherited from smacc2::ISmaccUpdatable
- Protected Attributes inherited from cl_px4_mr::CbPx4ClientBehaviorBase
CpVehicleCommand * vehicleCommand_ = nullptr
 
CpVehicleStatus * vehicleStatus_ = nullptr
 
CpTrajectorySetpoint * trajectorySetpoint_ = nullptr
 
CpGoalChecker * goalChecker_ = nullptr
 
CpVehicleLocalPosition * localPosition_ = nullptr
 
CpOffboardKeepAlive * offboardKeepAlive_ = nullptr
 

Detailed Description

Definition at line 47 of file cb_wait_for_heading_stable.hpp.

Constructor & Destructor Documentation

◆ CbWaitForHeadingStable()

cl_px4_mr::CbWaitForHeadingStable::CbWaitForHeadingStable ( HeadingStableParams params = {})
explicit

Definition at line 35 of file cb_wait_for_heading_stable.cpp.

35: params_(params) {}

Member Function Documentation

◆ onEntry()

void cl_px4_mr::CbWaitForHeadingStable::onEntry ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 37 of file cb_wait_for_heading_stable.cpp.

38{
39 if (localPosition_ == nullptr)
40 {
41 RCLCPP_ERROR(getLogger(), "CbWaitForHeadingStable: no CpVehicleLocalPosition");
42 this->postPx4Failure();
43 return;
44 }
45 const bool checkExpected = std::isfinite(params_.expectedHeadingRad);
46 RCLCPP_INFO(
47 getLogger(),
48 "CbWaitForHeadingStable: waiting for the heading to hold within %.2f deg/s over %.0f s%s "
49 "(timeout %.0f s)",
50 params_.maxDriftRadS * 180.0 / M_PI, params_.windowS,
51 checkExpected ? " and match the parked heading" : "", params_.timeoutS);
52
53 // Sample the heading at 10 Hz and keep the last windowS seconds of samples.
54 using clock = std::chrono::steady_clock;
55 const auto start = clock::now();
56 std::deque<std::pair<double, float>> samples; // (t since start, heading)
57 rclcpp::Rate rate(10.0);
58 double lastReport = -10.0;
59
60 while (!this->isShutdownRequested())
61 {
62 const double t = std::chrono::duration<double>(clock::now() - start).count();
64 {
65 samples.emplace_back(t, localPosition_->getHeading());
66 }
67 while (!samples.empty() && t - samples.front().first > params_.windowS)
68 {
69 samples.pop_front();
70 }
71
72 bool windowFull =
73 !samples.empty() && samples.back().first - samples.front().first >= 0.9 * params_.windowS;
74 float drift = 0.0f, offset = 0.0f;
75 bool stable = false, aligned = true;
76 if (windowFull)
77 {
78 // unwrap over the window: max excursion from the first sample
79 float lo = 0.0f, hi = 0.0f;
80 for (const auto & s : samples)
81 {
82 const float d = wrapPi(s.second - samples.front().second);
83 lo = std::min(lo, d);
84 hi = std::max(hi, d);
85 }
86 drift = hi - lo;
87 stable = drift <= static_cast<float>(params_.maxDriftRadS * params_.windowS);
88 if (checkExpected)
89 {
90 offset = wrapPi(samples.back().second - params_.expectedHeadingRad);
91 aligned = std::fabs(offset) <= params_.headingTolRad;
92 }
93 }
94
95 // Done when a full window drifted less than the limit and matches the expected heading.
96 if (windowFull && stable && aligned)
97 {
98 RCLCPP_INFO(
99 getLogger(),
100 "CbWaitForHeadingStable: heading %.1f deg stable (%.2f deg over %.0f s%s) - posting "
101 "success",
102 samples.back().second * 180.0 / M_PI, drift * 180.0 / M_PI, params_.windowS,
103 checkExpected ? (", offset " + std::to_string(offset * 180.0 / M_PI) + " deg").c_str()
104 : "");
105 this->postPx4Success();
106 return;
107 }
108
109 // Report progress every 2 s: still collecting, or why the heading is not ready.
110 if (t - lastReport >= 2.0)
111 {
112 lastReport = t;
113 if (!windowFull)
114 {
115 RCLCPP_INFO(
116 getLogger(), "CbWaitForHeadingStable: collecting (%zu samples, position valid=%d)",
117 samples.size(), static_cast<int>(localPosition_->isValid()));
118 }
119 else
120 {
121 RCLCPP_WARN(
122 getLogger(),
123 "CbWaitForHeadingStable: heading %.1f deg NOT ready: drift %.2f deg/s%s - the EKF "
124 "yaw is not trustworthy for takeoff",
125 samples.back().second * 180.0 / M_PI, drift / params_.windowS * 180.0 / M_PI,
126 checkExpected
127 ? (", offset from parked heading " + std::to_string(offset * 180.0 / M_PI) + " deg")
128 .c_str()
129 : "");
130 }
131 }
132
133 // Give up after timeoutS and post failure, so the machine does not arm on an untrusted yaw.
134 if (t > params_.timeoutS)
135 {
136 RCLCPP_ERROR(
137 getLogger(),
138 "CbWaitForHeadingStable: timeout (%.0f s): heading %.1f deg, drift %.2f deg/s. "
139 "Refusing to arm; restart the estimator / SITL",
140 params_.timeoutS, samples.empty() ? 0.0 : samples.back().second * 180.0 / M_PI,
141 drift / params_.windowS * 180.0 / M_PI);
142 this->postPx4Failure();
143 return;
144 }
145 rate.sleep();
146 }
147 RCLCPP_WARN(getLogger(), "CbWaitForHeadingStable: shutdown requested");
148 this->postPx4Failure();
149}
virtual rclcpp::Logger getLogger() const
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...
float wrapPi(float angle)

References cl_px4_mr::HeadingStableParams::expectedHeadingRad, cl_px4_mr::CpVehicleLocalPosition::getHeading(), smacc2::ISmaccClientBehavior::getLogger(), cl_px4_mr::HeadingStableParams::headingTolRad, smacc2::SmaccAsyncClientBehavior::isShutdownRequested(), cl_px4_mr::CpVehicleLocalPosition::isValid(), cl_px4_mr::CbPx4ClientBehaviorBase::localPosition_, cl_px4_mr::HeadingStableParams::maxDriftRadS, params_, cl_px4_mr::CbPx4ClientBehaviorBase::postPx4Failure(), cl_px4_mr::CbPx4ClientBehaviorBase::postPx4Success(), cl_px4_mr::HeadingStableParams::timeoutS, cl_px4_mr::HeadingStableParams::windowS, and cl_px4_mr::wrapPi().

Here is the call graph for this function:

◆ onExit()

void cl_px4_mr::CbWaitForHeadingStable::onExit ( )
inlineoverridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 53 of file cb_wait_for_heading_stable.hpp.

53{}

Member Data Documentation

◆ params_

HeadingStableParams cl_px4_mr::CbWaitForHeadingStable::params_
private

Definition at line 56 of file cb_wait_for_heading_stable.hpp.

Referenced by onEntry().


The documentation for this class was generated from the following files: