SMACC2
Loading...
Searching...
No Matches
cp_vehicle_local_position.hpp
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
15#pragma once
16
17#include <cstdint>
18#include <mutex>
19#include <px4_msgs/msg/vehicle_local_position.hpp>
20#include <rclcpp/rclcpp.hpp>
21#include <smacc2/smacc.hpp>
22
24
25namespace cl_px4_mr
26{
27
28// Caches the fused local NED position from /fmu/out/vehicle_local_position and
29// the global reference of that frame (ref_lat/ref_lon/ref_alt = the WGS84
30// position of the NED origin). Owns a MapProjection about that reference so
31// callers can convert mission lat/lon into the FMU's own local frame.
32//
33// EKF origin resets (xy_reset_counter / z_reset_counter) are logged and
34// signalled but NOT compensated: paths computed before a reset are stale by
35// delta_xy / delta_z.
37{
38public:
41
42 void onInitialize() override;
43
44 float getX() const;
45 float getY() const;
46 float getZ() const;
47 float getHeading() const;
48 bool isValid() const;
49
50 // --- global reference of the local frame ---
51 bool globalRefValid() const; // xy_global and a reference has been received
52 double getRefLat() const; // degrees
53 double getRefLon() const; // degrees
54 float getRefAlt() const; // metres AMSL
55 uint64_t getRefTimestamp() const;
56 uint8_t getXyResetCounter() const;
57 uint8_t getZResetCounter() const;
58
59 // lat/lon (degrees) -> local NED x (north), y (east) in metres.
60 // Returns false (outputs untouched) while the global reference is not valid.
61 bool projectToNed(double lat, double lon, float & x, float & y) const;
62 bool reprojectFromNed(float x, float y, double & lat, double & lon) const;
63
65 smacc2::SmaccSignal<void()> onLocalPositionReset_; // xy or z reset counter changed
66
67private:
68 void onPositionMessage(const px4_msgs::msg::VehicleLocalPosition::SharedPtr msg);
69
70 rclcpp::Subscription<px4_msgs::msg::VehicleLocalPosition>::SharedPtr subscriber_;
71 float x_ = 0.0f;
72 float y_ = 0.0f;
73 float z_ = 0.0f;
74 float heading_ = 0.0f;
75 bool valid_ = false;
76
77 bool xyGlobal_ = false;
78 bool zGlobal_ = false;
79 double refLat_ = 0.0;
80 double refLon_ = 0.0;
81 float refAlt_ = 0.0f;
82 uint64_t refTimestamp_ = 0;
83 uint8_t xyResetCounter_ = 0;
84 uint8_t zResetCounter_ = 0;
85 bool firstMessage_ = true;
87
88 mutable std::mutex mutex_;
89};
90
91} // 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