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)
 
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 ()
 
ISmaccStateMachinegetStateMachine ()
 
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

CpMicroRosAgentmicroRosAgent_ = nullptr
 
double timeoutSec_
 
rclcpp::Rate rate_
 
rclcpp::Subscription< px4_msgs::msg::FailsafeFlags >::SharedPtr failsafeSub_
 
std::atomic< boolhealthOk_ {false}
 
std::atomic< boolattitudeInvalid_ {true}
 
std::atomic< boollocalAltitudeInvalid_ {true}
 
std::atomic< boollocalPositionInvalid_ {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 ()
 
ISmaccStategetCurrentState ()
 
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
CpVehicleCommandvehicleCommand_ = nullptr
 
CpVehicleStatusvehicleStatus_ = nullptr
 
CpTrajectorySetpointtrajectorySetpoint_ = nullptr
 
CpGoalCheckergoalChecker_ = nullptr
 
CpVehicleLocalPositionlocalPosition_ = nullptr
 
CpOffboardKeepAliveoffboardKeepAlive_ = 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 28 of file cb_connect_micro_ros_agent.cpp.

29{
31 {
32 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: launching micro_ros_agent...");
34 }
35
36 std::string targetNodeName = microRosAgent_->getNodeName();
37 RCLCPP_INFO(
38 getLogger(), "CbConnectMicroRosAgent: waiting for node '%s' (timeout %.1fs)",
39 targetNodeName.c_str(), timeoutSec_);
40
41 auto startTime = std::chrono::steady_clock::now();
42 bool found = false;
43
44 while (!this->isShutdownRequested() && !found)
45 {
46 // Check timeout
47 auto elapsed = std::chrono::steady_clock::now() - startTime;
48 double elapsedSec = std::chrono::duration<double>(elapsed).count();
49 if (elapsedSec > timeoutSec_)
50 {
51 RCLCPP_ERROR(
52 getLogger(), "CbConnectMicroRosAgent: timeout (%.1fs) waiting for '%s'", timeoutSec_,
53 targetNodeName.c_str());
54 this->postPx4Failure();
55 return;
56 }
57
58 // Poll for node
59 std::stringstream ss;
60 auto nodeNames = getNode()->get_node_names();
61
62 for (const auto & n : nodeNames)
63 {
64 ss << " - " << n << std::endl;
65
66 if (n == targetNodeName)
67 {
68 found = true;
69 }
70 }
71
72 RCLCPP_INFO_STREAM(
73 getLogger(), "[" << getName() << "] listing nodes (" << nodeNames.size() << ")" << std::endl
74 << ss.str());
75
76 rate_.sleep();
77 }
78
79 if (!found)
80 {
81 RCLCPP_WARN(getLogger(), "CbConnectMicroRosAgent: shutdown requested before node found");
82 this->postPx4Failure();
83 return;
84 }
85
86 RCLCPP_INFO(
87 getLogger(), "CbConnectMicroRosAgent: node '%s' detected - starting failsafe health check",
88 targetNodeName.c_str());
89
90 // Phase 2: Wait for PX4 failsafe health flags to clear
91 healthOk_.store(false);
92 attitudeInvalid_.store(true);
93 localAltitudeInvalid_.store(true);
94 localPositionInvalid_.store(true);
95
96 failsafeSub_ = getNode()->create_subscription<px4_msgs::msg::FailsafeFlags>(
97 "/fmu/out/failsafe_flags", rclcpp::SensorDataQoS(),
98 [this](const px4_msgs::msg::FailsafeFlags::SharedPtr msg)
99 {
100 attitudeInvalid_.store(msg->attitude_invalid);
101 localAltitudeInvalid_.store(msg->local_altitude_invalid);
102 localPositionInvalid_.store(msg->local_position_invalid);
103 healthOk_.store(
104 !msg->attitude_invalid && !msg->local_altitude_invalid && !msg->local_position_invalid);
105 });
106
107 rclcpp::Rate healthRate(2.0);
108 while (!this->isShutdownRequested() && !healthOk_.load())
109 {
110 auto elapsed = std::chrono::steady_clock::now() - startTime;
111 double elapsedSec = std::chrono::duration<double>(elapsed).count();
112 if (elapsedSec > timeoutSec_)
113 {
114 RCLCPP_ERROR(
115 getLogger(),
116 "CbConnectMicroRosAgent: timeout (%.1fs) waiting for health check. "
117 "attitude_invalid=%d, local_altitude_invalid=%d, local_position_invalid=%d",
119 localPositionInvalid_.load());
120 this->postPx4Failure();
121 return;
122 }
123
124 RCLCPP_INFO(
125 getLogger(),
126 "CbConnectMicroRosAgent: health check: attitude_invalid=%d, "
127 "local_altitude_invalid=%d, local_position_invalid=%d",
129
130 healthRate.sleep();
131 }
132
133 if (healthOk_.load())
134 {
135 RCLCPP_INFO(getLogger(), "CbConnectMicroRosAgent: health check passed - posting success");
136 this->postPx4Success();
137 }
138 else
139 {
140 RCLCPP_WARN(
141 getLogger(), "CbConnectMicroRosAgent: shutdown requested before health check passed");
142 this->postPx4Failure();
143 }
144}
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::CpMicroRosAgent::isLaunched(), smacc2::SmaccAsyncClientBehavior::isShutdownRequested(), cl_px4_mr::CpMicroRosAgent::launch(), localAltitudeInvalid_, 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 146 of file cb_connect_micro_ros_agent.cpp.

146{ 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: