SMACC2
Loading...
Searching...
No Matches
cp_trajectory_setpoint.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
17
18namespace cl_px4_mr
19{
20
22
24
26{
27 auto node = this->getNode();
28 publisher_ = node->create_publisher<px4_msgs::msg::TrajectorySetpoint>(
29 "/fmu/in/trajectory_setpoint", rclcpp::QoS(1).best_effort());
30
32
33 // Initialize lastSetpoint_ with NaNs
34 auto nan = std::numeric_limits<float>::quiet_NaN();
35 lastSetpoint_.position = {nan, nan, nan};
36 lastSetpoint_.velocity = {nan, nan, nan};
37 lastSetpoint_.acceleration = {nan, nan, nan};
38 lastSetpoint_.jerk = {nan, nan, nan};
39 lastSetpoint_.yaw = nan;
40 lastSetpoint_.yawspeed = nan;
41
42 RCLCPP_INFO(
43 getLogger(), "CpTrajectorySetpoint: publisher created on /fmu/in/trajectory_setpoint");
44}
45
46void CpTrajectorySetpoint::setPositionNED(float x, float y, float z, float yaw)
47{
48 auto node = this->getNode();
49 auto nan = std::numeric_limits<float>::quiet_NaN();
50
51 px4_msgs::msg::TrajectorySetpoint msg;
52 msg.timestamp = node->get_clock()->now().nanoseconds() / 1000;
53 msg.position = {x, y, z};
54 msg.velocity = {nan, nan, nan};
55 msg.acceleration = {nan, nan, nan};
56 msg.jerk = {nan, nan, nan};
57 msg.yaw = yaw;
58 msg.yawspeed = nan;
59
60 publisher_->publish(msg);
61
62 {
63 std::lock_guard<std::mutex> lock(mutex_);
64 lastSetpoint_ = msg;
65 hasPublished_ = true;
66 }
67
68 RCLCPP_INFO(
69 getLogger(), "CpTrajectorySetpoint: position NED [%.2f, %.2f, %.2f] yaw=%.2f", x, y, z, yaw);
70}
71
73{
75 {
76 RCLCPP_WARN(getLogger(), "CpTrajectorySetpoint::hold() - position not yet valid, skipping");
77 return;
78 }
79
83}
84
86{
87 std::lock_guard<std::mutex> lock(mutex_);
88 if (!hasPublished_) return;
89
90 auto node = this->getNode();
91 lastSetpoint_.timestamp = node->get_clock()->now().nanoseconds() / 1000;
92 publisher_->publish(lastSetpoint_);
93}
94
95px4_msgs::msg::TrajectorySetpoint CpTrajectorySetpoint::getLastSetpoint() const
96{
97 std::lock_guard<std::mutex> lock(mutex_);
98 return lastSetpoint_;
99}
100
101} // namespace cl_px4_mr
px4_msgs::msg::TrajectorySetpoint getLastSetpoint() const
px4_msgs::msg::TrajectorySetpoint lastSetpoint_
void setPositionNED(float x, float y, float z, float yaw=std::numeric_limits< float >::quiet_NaN())
rclcpp::Publisher< px4_msgs::msg::TrajectorySetpoint >::SharedPtr publisher_
rclcpp::Logger getLogger() const
void requiresComponent(TComponent *&requiredComponentStorage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
rclcpp::Node::SharedPtr getNode()