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 ()
 
void onExit () override
 
- Public Member Functions inherited from cl_px4_mr::CbPx4ClientBehaviorBase
template<typename TOrthogonal , typename TSourceObject >
void onStateOrthogonalAllocation ()
 
virtual ~CbPx4ClientBehaviorBase ()
 
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 ()
 
virtual ~SmaccAsyncClientBehavior ()
 
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 ()
 
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 (std::string name, SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
 
- Public Member Functions inherited from smacc2::ISmaccUpdatable
 ISmaccUpdatable ()
 
 ISmaccUpdatable (rclcpp::Duration duration)
 
void executeUpdate (rclcpp::Node::SharedPtr node)
 
void setUpdatePeriod (rclcpp::Duration duration)
 

Private Attributes

CpMicroRosAgent * microRosAgent_ = nullptr
 
double timeoutSec_
 
rclcpp::Rate rate_
 
rclcpp::Subscription< px4_msgs::msg::FailsafeFlags >::SharedPtr failsafeSub_
 
std::atomic< bool > healthOk_ {false}
 
std::atomic< bool > attitudeInvalid_ {true}
 
std::atomic< bool > localAltitudeInvalid_ {true}
 
std::atomic< bool > localPositionInvalid_ {true}
 

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 ()
 
virtual 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 ()
 
virtual rclcpp::Node::SharedPtr getNode () const
 
virtual rclcpp::Logger getLogger () 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 29 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 29 of file cb_connect_micro_ros_agent.cpp.

30{
32 {
33 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: launching micro_ros_agent...");
35 }
36
37 std::string targetNodeName = microRosAgent_->getNodeName();
38 RCLCPP_INFO(
39 getLogger(), "CbConnectMicroRosAgent: waiting for node '%s' (timeout %.1fs)",
40 targetNodeName.c_str(), timeoutSec_);
41
42 auto startTime = std::chrono::steady_clock::now();
43 bool found = false;
44
45 while (!this->isShutdownRequested() && !found)
46 {
47 // Check timeout
48 auto elapsed = std::chrono::steady_clock::now() - startTime;
49 double elapsedSec = std::chrono::duration<double>(elapsed).count();
50 if (elapsedSec > timeoutSec_)
51 {
52 RCLCPP_ERROR(
53 getLogger(), "CbConnectMicroRosAgent: timeout (%.1fs) waiting for '%s'", timeoutSec_,
54 targetNodeName.c_str());
55 this->postPx4Failure();
56 return;
57 }
58
59 // Poll for node
60 std::stringstream ss;
61 auto nodeNames = getNode()->get_node_names();
62
63 for (const auto & n : nodeNames)
64 {
65 ss << " - " << n << std::endl;
66
67 if (n == targetNodeName)
68 {
69 found = true;
70 }
71 }
72
73 RCLCPP_INFO_STREAM(
74 getLogger(), "[" << getName() << "] listing nodes (" << nodeNames.size() << ")" << std::endl
75 << ss.str());
76
77 rate_.sleep();
78 }
79
80 if (!found)
81 {
82 RCLCPP_WARN(getLogger(), "CbConnectMicroRosAgent: shutdown requested before node found");
83 this->postPx4Failure();
84 return;
85 }
86
87 RCLCPP_INFO(
88 getLogger(), "CbConnectMicroRosAgent: node '%s' detected - starting failsafe health check",
89 targetNodeName.c_str());
90
91 // Phase 2: Wait for PX4 failsafe health flags to clear
92 healthOk_.store(false);
93 attitudeInvalid_.store(true);
94 localAltitudeInvalid_.store(true);
95 localPositionInvalid_.store(true);
96
97 failsafeSub_ = getNode()->create_subscription<px4_msgs::msg::FailsafeFlags>(
98 "/fmu/out/failsafe_flags", rclcpp::SensorDataQoS(),
99 [this](const px4_msgs::msg::FailsafeFlags::SharedPtr msg)
100 {
101 attitudeInvalid_.store(msg->attitude_invalid);
102 localAltitudeInvalid_.store(msg->local_altitude_invalid);
103 localPositionInvalid_.store(msg->local_position_invalid);
104 healthOk_.store(
105 !msg->attitude_invalid && !msg->local_altitude_invalid && !msg->local_position_invalid);
106 });
107
108 // the failsafe flags say an estimate exists; the local position and velocity
109 // also have to be valid. NOT heading_good_for_control: with a magnetometer
110 // heading PX4 only sets it after the in-flight yaw alignment (moving with GPS
111 // course), so on the ground it is false by design; it is logged for diagnosis.
112 auto controlReady = [this]()
113 {
114 return localPosition_ == nullptr ||
116 };
117 auto flag = [this](bool (CpVehicleLocalPosition::*f)() const)
118 { return localPosition_ ? static_cast<int>((localPosition_->*f)()) : -1; };
119 rclcpp::Rate healthRate(2.0);
120 while (!this->isShutdownRequested() && !(healthOk_.load() && controlReady()))
121 {
122 auto elapsed = std::chrono::steady_clock::now() - startTime;
123 double elapsedSec = std::chrono::duration<double>(elapsed).count();
124 if (elapsedSec > timeoutSec_)
125 {
126 RCLCPP_ERROR(
127 getLogger(),
128 "CbConnectMicroRosAgent: timeout (%.1fs) waiting for health check. "
129 "attitude_invalid=%d, local_altitude_invalid=%d, local_position_invalid=%d, "
130 "heading_good_for_control=%d, velocity_valid=%d",
134 this->postPx4Failure();
135 return;
136 }
137 RCLCPP_INFO(
138 getLogger(),
139 "CbConnectMicroRosAgent: health check: attitude_invalid=%d, "
140 "local_altitude_invalid=%d, local_position_invalid=%d, heading_good_for_control=%d, "
141 "velocity_valid=%d",
145 healthRate.sleep();
146 }
147 if (healthOk_.load() && controlReady())
148 {
149 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: health check passed - posting success");
150 this->postPx4Success();
151 }
152 else
153 {
154 RCLCPP_WARN(
155 getLogger(), "CbConnectMicroRosAgent: shutdown requested before health check passed");
156 this->postPx4Failure();
157 }
158}
rclcpp::Subscription< px4_msgs::msg::FailsafeFlags >::SharedPtr failsafeSub_
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 attitudeInvalid_, failsafeSub_, smacc2::ISmaccClientBehavior::getLogger(), smacc2::ISmaccClientBehavior::getName(), smacc2::ISmaccClientBehavior::getNode(), cl_px4_mr::CpMicroRosAgent::getNodeName(), healthOk_, cl_px4_mr::CpVehicleLocalPosition::isHeadingGoodForControl(), cl_px4_mr::CpMicroRosAgent::isLaunched(), smacc2::SmaccAsyncClientBehavior::isShutdownRequested(), cl_px4_mr::CpVehicleLocalPosition::isValid(), cl_px4_mr::CpVehicleLocalPosition::isVelocityValid(), cl_px4_mr::CpMicroRosAgent::launch(), localAltitudeInvalid_, cl_px4_mr::CbPx4ClientBehaviorBase::localPosition_, localPositionInvalid_, microRosAgent_, cl_px4_mr::CbPx4ClientBehaviorBase::postPx4Failure(), cl_px4_mr::CbPx4ClientBehaviorBase::postPx4Success(), rate_, and timeoutSec_.

Here is the call graph for this function:

◆ onExit()

void cl_px4_mr::CbConnectMicroRosAgent::onExit ( )
overridevirtual

Reimplemented from smacc2::ISmaccClientBehavior.

Definition at line 160 of file cb_connect_micro_ros_agent.cpp.

160{ failsafeSub_.reset(); }

References failsafeSub_.

◆ onStateOrthogonalAllocation()

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

Member Data Documentation

◆ attitudeInvalid_

std::atomic<bool> cl_px4_mr::CbConnectMicroRosAgent::attitudeInvalid_ {true}
private

Definition at line 51 of file cb_connect_micro_ros_agent.hpp.

51{true};

Referenced by onEntry().

◆ failsafeSub_

rclcpp::Subscription<px4_msgs::msg::FailsafeFlags>::SharedPtr cl_px4_mr::CbConnectMicroRosAgent::failsafeSub_
private

Definition at line 49 of file cb_connect_micro_ros_agent.hpp.

Referenced by onEntry(), and onExit().

◆ healthOk_

std::atomic<bool> cl_px4_mr::CbConnectMicroRosAgent::healthOk_ {false}
private

Definition at line 50 of file cb_connect_micro_ros_agent.hpp.

50{false};

Referenced by onEntry().

◆ localAltitudeInvalid_

std::atomic<bool> cl_px4_mr::CbConnectMicroRosAgent::localAltitudeInvalid_ {true}
private

Definition at line 52 of file cb_connect_micro_ros_agent.hpp.

52{true};

Referenced by onEntry().

◆ localPositionInvalid_

std::atomic<bool> cl_px4_mr::CbConnectMicroRosAgent::localPositionInvalid_ {true}
private

Definition at line 53 of file cb_connect_micro_ros_agent.hpp.

53{true};

Referenced by onEntry().

◆ microRosAgent_

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

Definition at line 45 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 47 of file cb_connect_micro_ros_agent.hpp.

Referenced by onEntry().

◆ timeoutSec_

double cl_px4_mr::CbConnectMicroRosAgent::timeoutSec_
private

Definition at line 46 of file cb_connect_micro_ros_agent.hpp.

Referenced by onEntry().


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