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

#include <cb_connect_micro_ros_agent.hpp>

Inheritance diagram for cl_px4_mr::CbConnectMicroRosAgent:
Inheritance graph
Collaboration diagram for cl_px4_mr::CbConnectMicroRosAgent:
Collaboration graph

Public Member Functions

 CbConnectMicroRosAgent (double timeoutSec=30.0)
 
void onEntry () override
 
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
- 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)
 
virtual void onExit ()
 
- 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

CpMicroRosAgent * microRosAgent_ = nullptr
 
CpFailsafeFlags * failsafeFlags_ = nullptr
 
double timeoutSec_
 
rclcpp::Rate rate_
 

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 27 of file cb_connect_micro_ros_agent.hpp.

Constructor & Destructor Documentation

◆ CbConnectMicroRosAgent()

cl_px4_mr::CbConnectMicroRosAgent::CbConnectMicroRosAgent ( double timeoutSec = 30.0)

Member Function Documentation

◆ onEntry()

void cl_px4_mr::CbConnectMicroRosAgent::onEntry ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 30 of file cb_connect_micro_ros_agent.cpp.

31{
33 {
34 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: launching micro_ros_agent...");
36 }
37
38 std::string targetNodeName = microRosAgent_->getNodeName();
39 RCLCPP_INFO(
40 getLogger(), "CbConnectMicroRosAgent: waiting for node '%s' (timeout %.1fs)",
41 targetNodeName.c_str(), timeoutSec_);
42
43 auto startTime = std::chrono::steady_clock::now();
44 bool found = false;
45
46 while (!this->isShutdownRequested() && !found)
47 {
48 // Check timeout
49 auto elapsed = std::chrono::steady_clock::now() - startTime;
50 double elapsedSec = std::chrono::duration<double>(elapsed).count();
51 if (elapsedSec > timeoutSec_)
52 {
53 RCLCPP_ERROR(
54 getLogger(), "CbConnectMicroRosAgent: timeout (%.1fs) waiting for '%s'", timeoutSec_,
55 targetNodeName.c_str());
56 this->postPx4Failure();
57 return;
58 }
59
60 // Poll for node
61 std::stringstream ss;
62 auto nodeNames = getNode()->get_node_names();
63
64 for (const auto & n : nodeNames)
65 {
66 ss << " - " << n << "\n";
67
68 if (n == targetNodeName)
69 {
70 found = true;
71 }
72 }
73
74 RCLCPP_INFO_STREAM(
75 getLogger(), "[" << getName() << "] listing nodes (" << nodeNames.size() << ")\n"
76 << ss.str());
77
78 rate_.sleep();
79 }
80
81 if (!found)
82 {
83 RCLCPP_WARN(getLogger(), "CbConnectMicroRosAgent: shutdown requested before node found");
84 this->postPx4Failure();
85 return;
86 }
87
88 RCLCPP_INFO(
89 getLogger(), "CbConnectMicroRosAgent: node '%s' detected - starting failsafe health check",
90 targetNodeName.c_str());
91
92 // Phase 2: Wait for PX4 failsafe health flags to clear, read from the state-machine-lifetime
93 // CpFailsafeFlags component, so no subscription callback can run into this behavior at dispose.
94 if (failsafeFlags_ == nullptr)
95 {
96 RCLCPP_ERROR(getLogger(), "CbConnectMicroRosAgent: no CpFailsafeFlags component on the client");
97 this->postPx4Failure();
98 return;
99 }
100
101 // the failsafe flags say an estimate exists; the local position and velocity
102 // also have to be valid. NOT heading_good_for_control: with a magnetometer
103 // heading PX4 only sets it after the in-flight yaw alignment (moving with GPS
104 // course), so on the ground it is false by design; it is logged for diagnosis.
105 auto controlReady = [this]()
106 {
107 return localPosition_ == nullptr ||
109 };
110 auto flag = [this](bool (CpVehicleLocalPosition::*f)() const)
111 { return localPosition_ ? static_cast<int>((localPosition_->*f)()) : -1; };
112 rclcpp::Rate healthRate(2.0);
113 while (!this->isShutdownRequested() && !(failsafeFlags_->isHealthOk() && controlReady()))
114 {
115 auto elapsed = std::chrono::steady_clock::now() - startTime;
116 double elapsedSec = std::chrono::duration<double>(elapsed).count();
117 if (elapsedSec > timeoutSec_)
118 {
119 RCLCPP_ERROR(
120 getLogger(),
121 "CbConnectMicroRosAgent: timeout (%.1fs) waiting for health check. "
122 "attitude_invalid=%d, local_altitude_invalid=%d, local_position_invalid=%d, "
123 "heading_good_for_control=%d, velocity_valid=%d",
128 this->postPx4Failure();
129 return;
130 }
131 RCLCPP_INFO(
132 getLogger(),
133 "CbConnectMicroRosAgent: health check: attitude_invalid=%d, "
134 "local_altitude_invalid=%d, local_position_invalid=%d, heading_good_for_control=%d, "
135 "velocity_valid=%d",
140 healthRate.sleep();
141 }
142 if (failsafeFlags_->isHealthOk() && controlReady())
143 {
144 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: health check passed - posting success");
145 this->postPx4Success();
146 }
147 else
148 {
149 RCLCPP_WARN(
150 getLogger(), "CbConnectMicroRosAgent: shutdown requested before health check passed");
151 this->postPx4Failure();
152 }
153}
virtual rclcpp::Logger getLogger() const
virtual rclcpp::Node::SharedPtr getNode() const
bool isShutdownRequested()
onEntry is executed in a new thread. However the current state cannot be left until the onEntry threa...

References failsafeFlags_, smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::getNode(), cl_px4_mr::CpMicroRosAgent::getNodeName(), cl_px4_mr::CpFailsafeFlags::isAttitudeInvalid(), cl_px4_mr::CpVehicleLocalPosition::isHeadingGoodForControl(), cl_px4_mr::CpFailsafeFlags::isHealthOk(), cl_px4_mr::CpMicroRosAgent::isLaunched(), cl_px4_mr::CpFailsafeFlags::isLocalAltitudeInvalid(), cl_px4_mr::CpFailsafeFlags::isLocalPositionInvalid(), smacc2::SmaccAsyncClientBehavior::isShutdownRequested(), cl_px4_mr::CpVehicleLocalPosition::isValid(), cl_px4_mr::CpVehicleLocalPosition::isVelocityValid(), cl_px4_mr::CpMicroRosAgent::launch(), cl_px4_mr::CbPx4ClientBehaviorBase::localPosition_, microRosAgent_, cl_px4_mr::CbPx4ClientBehaviorBase::postPx4Failure(), cl_px4_mr::CbPx4ClientBehaviorBase::postPx4Success(), rate_, and timeoutSec_.

Here is the call graph for this function:

◆ onStateOrthogonalAllocation()

template<typename TOrthogonal , typename TSourceObject >
void cl_px4_mr::CbConnectMicroRosAgent::onStateOrthogonalAllocation ( )
inline

Member Data Documentation

◆ failsafeFlags_

CpFailsafeFlags* cl_px4_mr::CbConnectMicroRosAgent::failsafeFlags_ = nullptr
private

Definition at line 44 of file cb_connect_micro_ros_agent.hpp.

Referenced by onEntry(), and onStateOrthogonalAllocation().

◆ microRosAgent_

CpMicroRosAgent* cl_px4_mr::CbConnectMicroRosAgent::microRosAgent_ = nullptr
private

Definition at line 43 of file cb_connect_micro_ros_agent.hpp.

Referenced by onEntry(), and onStateOrthogonalAllocation().

◆ rate_

rclcpp::Rate cl_px4_mr::CbConnectMicroRosAgent::rate_
private

Definition at line 46 of file cb_connect_micro_ros_agent.hpp.

Referenced by onEntry().

◆ timeoutSec_

double cl_px4_mr::CbConnectMicroRosAgent::timeoutSec_
private

Definition at line 45 of file cb_connect_micro_ros_agent.hpp.

Referenced by onEntry().


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