SMACC2
Loading...
Searching...
No Matches
cp_tf_broadcaster.hpp
Go to the documentation of this file.
1// Copyright 2026 RobosoftAI 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/*****************************************************************************************************************
16 *
17 * Authors: Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
22
23#include <chrono>
24#include <memory>
25#include <string>
26
27#include <tf2_ros/static_transform_broadcaster.h>
28#include <tf2_ros/transform_broadcaster.h>
29#include <nav_msgs/msg/path.hpp>
30#include <px4_msgs/msg/vehicle_attitude.hpp>
31#include <rclcpp/rclcpp.hpp>
32#include <smacc2/smacc.hpp>
33
34namespace cl_px4_mr
35{
36
37class CpVehicleLocalPosition;
38
40{
41 std::string mapFrame = "map";
42 std::string baseFrame = "base_link";
43 // same translation as baseFrame, identity rotation: an RViz orbit camera
44 // targeting it follows the vehicle without spinning with its yaw
45 std::string followFrame = "base_link_follow";
46 std::string sensorFrame = "lidar_link";
47 bool publishSensorFrame = true;
48 // sensor mount in the body FLU frame (metres / radians)
49 double sensorXyz[3] = {0.12, 0.0, 0.315};
50 double sensorRpy[3] = {0.0, 0.0, 0.0};
51 double maxRateHz = 50.0; // vehicle_attitude arrives at 100-250 Hz
52 // flown trail as a nav_msgs/Path in mapFrame (for RViz); a pose is appended
53 // every trailMinStepM of travel, the path republished at <= trailRateHz
54 bool publishTrail = true;
55 std::string trailTopic = "/px4/trail";
56 double trailMinStepM = 0.25;
57 double trailRateHz = 2.0;
58 size_t trailMaxPoses = 20000;
59};
60
61// Publishes the PX4 state estimate as a ROS TF tree: map -> base_link from
62// /fmu/out/vehicle_attitude (rotation) and CpVehicleLocalPosition (translation),
63// converted from PX4's NED / FRD conventions to ROS ENU / FLU, plus a
64// translation-only map -> base_link_follow and a static base_link -> sensor
65// frame. Optionally also publishes the flown trail as a nav_msgs/Path. `map` is the FMU's local NED origin (ref_lat / ref_lon), expressed
66// ENU: x east, y north, z up. EKF origin resets are not compensated.
67//
68// Opt-in: not created by ClPx4Mr; an orthogonal that wants TF adds it with
69// createClient<ClPx4Mr>()->createComponent<CpTfBroadcaster>(params);
70// Threading: onAttitude runs on the ROS executor and touches no state machine
71// APIs.
73{
74public:
76 virtual ~CpTfBroadcaster();
77
78 void onInitialize() override;
79
80 const TfBroadcasterParams & params() const { return params_; }
81
82private:
83 void onAttitude(const px4_msgs::msg::VehicleAttitude::SharedPtr msg);
84
87 rclcpp::Subscription<px4_msgs::msg::VehicleAttitude>::SharedPtr attitudeSub_;
88 std::shared_ptr<tf2_ros::TransformBroadcaster> broadcaster_;
89 std::shared_ptr<tf2_ros::StaticTransformBroadcaster> staticBroadcaster_;
90 std::chrono::steady_clock::time_point lastSent_;
91 bool firstSent_ = false;
92 rclcpp::Publisher<nav_msgs::msg::Path>::SharedPtr trailPub_;
93 nav_msgs::msg::Path trail_;
94 std::chrono::steady_clock::time_point lastTrailSent_;
95};
96
97} // namespace cl_px4_mr
std::chrono::steady_clock::time_point lastTrailSent_
std::chrono::steady_clock::time_point lastSent_
const TfBroadcasterParams & params() const
rclcpp::Subscription< px4_msgs::msg::VehicleAttitude >::SharedPtr attitudeSub_
rclcpp::Publisher< nav_msgs::msg::Path >::SharedPtr trailPub_
std::shared_ptr< tf2_ros::StaticTransformBroadcaster > staticBroadcaster_
void onAttitude(const px4_msgs::msg::VehicleAttitude::SharedPtr msg)
std::shared_ptr< tf2_ros::TransformBroadcaster > broadcaster_
CpVehicleLocalPosition * localPosition_
CpTfBroadcaster(TfBroadcasterParams params={})