SMACC2
Loading...
Searching...
No Matches
cp_vehicle_status.cpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft 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
16
17namespace cl_px4_mr
18{
19
21
23
25{
26 auto node = this->getNode();
27 subscriber_ = node->create_subscription<px4_msgs::msg::VehicleStatus>(
28 "/fmu/out/vehicle_status_v1", rclcpp::SensorDataQoS(),
29 std::bind(&CpVehicleStatus::onStatusMessage, this, std::placeholders::_1));
30 RCLCPP_INFO(getLogger(), "CpVehicleStatus: subscribed to /fmu/out/vehicle_status");
31}
32
33void CpVehicleStatus::onStatusMessage(const px4_msgs::msg::VehicleStatus::SharedPtr msg)
34{
35 std::lock_guard<std::mutex> lock(mutex_);
38 armingState_ = msg->arming_state;
39 navState_ = msg->nav_state;
40
41 // Detect arming state changes
43 {
44 if (armingState_ == px4_msgs::msg::VehicleStatus::ARMING_STATE_ARMED)
45 {
46 RCLCPP_INFO(getLogger(), "CpVehicleStatus: ARMED");
47 onArmed_();
48 }
49 else if (armingState_ == px4_msgs::msg::VehicleStatus::ARMING_STATE_DISARMED)
50 {
51 RCLCPP_INFO(getLogger(), "CpVehicleStatus: DISARMED");
53 }
54 }
55
56 // Detect nav state changes
58 {
59 RCLCPP_INFO(
60 getLogger(), "CpVehicleStatus: nav_state changed %u -> %u", prevNavState_, navState_);
62 }
63}
64
66{
67 std::lock_guard<std::mutex> lock(mutex_);
68 return armingState_ == px4_msgs::msg::VehicleStatus::ARMING_STATE_ARMED;
69}
70
72{
73 std::lock_guard<std::mutex> lock(mutex_);
74 return armingState_ == px4_msgs::msg::VehicleStatus::ARMING_STATE_DISARMED;
75}
76
78{
79 std::lock_guard<std::mutex> lock(mutex_);
80 return navState_;
81}
82
84{
85 std::lock_guard<std::mutex> lock(mutex_);
86 return armingState_;
87}
88
89} // namespace cl_px4_mr
smacc2::SmaccSignal< void()> onModeChanged_
rclcpp::Subscription< px4_msgs::msg::VehicleStatus >::SharedPtr subscriber_
void onStatusMessage(const px4_msgs::msg::VehicleStatus::SharedPtr msg)
smacc2::SmaccSignal< void()> onDisarmed_
smacc2::SmaccSignal< void()> onArmed_
rclcpp::Logger getLogger() const
rclcpp::Node::SharedPtr getNode()