SMACC2
Loading...
Searching...
No Matches
cp_vehicle_local_position.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::VehicleLocalPosition>(
28 "/fmu/out/vehicle_local_position", rclcpp::SensorDataQoS(),
29 std::bind(&CpVehicleLocalPosition::onPositionMessage, this, std::placeholders::_1));
30 RCLCPP_INFO(getLogger(), "CpVehicleLocalPosition: subscribed to /fmu/out/vehicle_local_position");
31}
32
34 const px4_msgs::msg::VehicleLocalPosition::SharedPtr msg)
35{
36 bool resetDetected = false;
37
38 {
39 std::lock_guard<std::mutex> lock(mutex_);
40 x_ = msg->x;
41 y_ = msg->y;
42 z_ = msg->z;
43 heading_ = msg->heading;
44 valid_ = msg->xy_valid && msg->z_valid;
45
46 // global reference of the local frame
47 xyGlobal_ = msg->xy_global;
48 zGlobal_ = msg->z_global;
49 refAlt_ = msg->ref_alt;
50 if (msg->xy_global && (!projection_.isInitialized() || msg->ref_timestamp != refTimestamp_))
51 {
52 refLat_ = msg->ref_lat;
53 refLon_ = msg->ref_lon;
54 refTimestamp_ = msg->ref_timestamp;
56 RCLCPP_INFO(
57 getLogger(),
58 "CpVehicleLocalPosition: global reference set - NED origin at lat=%.7f lon=%.7f "
59 "alt=%.1f m AMSL",
60 refLat_, refLon_, static_cast<double>(refAlt_));
61 }
62
63 // EKF origin resets
64 if (!firstMessage_)
65 {
66 if (msg->xy_reset_counter != xyResetCounter_)
67 {
68 RCLCPP_WARN(
69 getLogger(),
70 "CpVehicleLocalPosition: local XY reset #%u (delta_xy=[%.2f, %.2f]); running paths "
71 "are NOT re-projected",
72 msg->xy_reset_counter, static_cast<double>(msg->delta_xy[0]),
73 static_cast<double>(msg->delta_xy[1]));
74 resetDetected = true;
75 }
76 if (msg->z_reset_counter != zResetCounter_)
77 {
78 RCLCPP_WARN(
79 getLogger(),
80 "CpVehicleLocalPosition: local Z reset #%u (delta_z=%.2f); running paths are NOT "
81 "re-projected",
82 msg->z_reset_counter, static_cast<double>(msg->delta_z));
83 resetDetected = true;
84 }
85 }
86 xyResetCounter_ = msg->xy_reset_counter;
87 zResetCounter_ = msg->z_reset_counter;
88 firstMessage_ = false;
89 }
90
91 if (resetDetected)
92 {
94 }
96}
97
99{
100 std::lock_guard<std::mutex> lock(mutex_);
101 return x_;
102}
103
105{
106 std::lock_guard<std::mutex> lock(mutex_);
107 return y_;
108}
109
111{
112 std::lock_guard<std::mutex> lock(mutex_);
113 return z_;
114}
115
117{
118 std::lock_guard<std::mutex> lock(mutex_);
119 return heading_;
120}
121
123{
124 std::lock_guard<std::mutex> lock(mutex_);
125 return valid_;
126}
127
129{
130 std::lock_guard<std::mutex> lock(mutex_);
132}
133
135{
136 std::lock_guard<std::mutex> lock(mutex_);
137 return refLat_;
138}
139
141{
142 std::lock_guard<std::mutex> lock(mutex_);
143 return refLon_;
144}
145
147{
148 std::lock_guard<std::mutex> lock(mutex_);
149 return refAlt_;
150}
151
153{
154 std::lock_guard<std::mutex> lock(mutex_);
155 return refTimestamp_;
156}
157
159{
160 std::lock_guard<std::mutex> lock(mutex_);
161 return xyResetCounter_;
162}
163
165{
166 std::lock_guard<std::mutex> lock(mutex_);
167 return zResetCounter_;
168}
169
170bool CpVehicleLocalPosition::projectToNed(double lat, double lon, float & x, float & y) const
171{
172 std::lock_guard<std::mutex> lock(mutex_);
174 {
175 return false;
176 }
177 projection_.project(lat, lon, x, y);
178 return true;
179}
180
181bool CpVehicleLocalPosition::reprojectFromNed(float x, float y, double & lat, double & lon) const
182{
183 std::lock_guard<std::mutex> lock(mutex_);
185 {
186 return false;
187 }
188 projection_.reproject(x, y, lat, lon);
189 return true;
190}
191
192} // namespace cl_px4_mr
bool reprojectFromNed(float x, float y, double &lat, double &lon) const
smacc2::SmaccSignal< void()> onPositionReceived_
void onPositionMessage(const px4_msgs::msg::VehicleLocalPosition::SharedPtr msg)
smacc2::SmaccSignal< void()> onLocalPositionReset_
rclcpp::Subscription< px4_msgs::msg::VehicleLocalPosition >::SharedPtr subscriber_
bool projectToNed(double lat, double lon, float &x, float &y) const
void initReference(double lat0, double lon0, uint64_t timestamp=0)
Definition geo_utils.hpp:79
bool isInitialized() const
Definition geo_utils.hpp:89
void reproject(float x, float y, double &lat, double &lon) const
void project(double lat, double lon, float &x, float &y) const
Definition geo_utils.hpp:95
rclcpp::Logger getLogger() const
rclcpp::Node::SharedPtr getNode()