PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
Loading...
Searching...
No Matches
lateral_longitudinal.hpp
1/****************************************************************************
2 * Copyright (c) 2025 PX4 Development Team.
3 * SPDX-License-Identifier: BSD-3-Clause
4 ****************************************************************************/
5
6#pragma once
7
8#include <Eigen/Eigen>
9#include <optional>
10#include <px4_msgs/msg/fixed_wing_lateral_setpoint.hpp>
11#include <px4_msgs/msg/fixed_wing_longitudinal_setpoint.hpp>
12#include <px4_msgs/msg/lateral_control_configuration.hpp>
13#include <px4_msgs/msg/longitudinal_control_configuration.hpp>
14#include <px4_ros2/common/exception.hpp>
15#include <px4_ros2/common/setpoint_base.hpp>
16
17namespace px4_ros2 {
22struct FwLateralLongitudinalSetpoint;
23struct FwControlConfiguration;
24
29 public:
36 explicit FwLateralLongitudinalSetpointType(Context& context, bool is_position_optional = false);
37
38 ~FwLateralLongitudinalSetpointType() override = default;
39
40 SetpointType getSetpointType() override
41 {
42 return px4_msgs::msg::SetpointConfig::TYPE_FIXEDWING_LATERAL_LONGITUDINAL;
43 }
44
46 px4_msgs::msg::SetpointConfigReply& setpoint_config_reply) override;
47
67 void update(const FwLateralLongitudinalSetpoint& setpoint, const FwControlConfiguration& config);
68
84
93 void updateWithAltitude(float altitude_amsl_sp, float course_sp,
94 std::optional<float> equivalent_airspeed_sp = {},
95 std::optional<float> lateral_acceleration_sp = {});
96
105 void updateWithHeightRate(float height_rate_sp, float course_sp,
106 std::optional<float> equivalent_airspeed_sp = {},
107 std::optional<float> lateral_acceleration_sp = {});
108
109 float desiredUpdateRateHz() override { return 30.f; }
110
111 private:
112 void publishConfigurationIfNeeded();
113
114 rclcpp::Node& _node;
115 bool _local_position_is_optional;
116 rclcpp::Publisher<px4_msgs::msg::FixedWingLateralSetpoint>::SharedPtr _fw_lateral_sp_pub;
117 rclcpp::Publisher<px4_msgs::msg::FixedWingLongitudinalSetpoint>::SharedPtr
118 _fw_longitudinal_sp_pub;
119
120 rclcpp::Publisher<px4_msgs::msg::LateralControlConfiguration>::SharedPtr
121 _lateral_control_configuration_pub;
122 rclcpp::Publisher<px4_msgs::msg::LongitudinalControlConfiguration>::SharedPtr
123 _longitudinal_control_configuration_pub;
124
125 rclcpp::Time _last_config_published_time{};
126 std::optional<px4_msgs::msg::LateralControlConfiguration> _current_lateral_configuration;
127 std::optional<px4_msgs::msg::LongitudinalControlConfiguration>
128 _current_longitudinal_configuration;
129};
130
132 std::optional<float> course;
133 std::optional<float> airspeed_direction;
134 std::optional<float> lateral_acceleration;
135 std::optional<float> altitude_msl;
136 std::optional<float> height_rate;
137 std::optional<float> equivalent_airspeed;
138 std::optional<float> throttle_direct;
139
140 FwLateralLongitudinalSetpoint& withCourse(float course_sp)
141 {
142 course = course_sp;
143 return *this;
144 }
145
146 FwLateralLongitudinalSetpoint& withAirspeedDirection(float airspeed_direction_sp)
147 {
148 airspeed_direction = airspeed_direction_sp;
149 return *this;
150 }
151
152 FwLateralLongitudinalSetpoint& withLateralAcceleration(float lateral_acceleration_sp)
153 {
154 lateral_acceleration = lateral_acceleration_sp;
155 return *this;
156 }
157
158 FwLateralLongitudinalSetpoint& withAltitude(float altitude_sp)
159 {
160 altitude_msl = altitude_sp;
161 return *this;
162 }
163
164 FwLateralLongitudinalSetpoint& withHeightRate(float height_rate_sp)
165 {
166 height_rate = height_rate_sp;
167 return *this;
168 }
169
170 FwLateralLongitudinalSetpoint& withEquivalentAirspeed(float equivalent_airspeed_sp)
171 {
172 equivalent_airspeed = equivalent_airspeed_sp;
173 return *this;
174 }
175
176 FwLateralLongitudinalSetpoint& withThrottleDirect(float throttle_direct_sp)
177 {
178 throttle_direct = throttle_direct_sp;
179 return *this;
180 }
181};
182
184 std::optional<float> min_pitch;
185 std::optional<float> max_pitch;
186 std::optional<float> min_throttle;
187 std::optional<float> max_throttle;
188 std::optional<float> max_lateral_acceleration;
189 std::optional<float> target_climb_rate;
190 std::optional<float> target_sink_rate;
191 std::optional<float> speed_weight;
192
193 FwControlConfiguration& withPitchLimits(float min_pitch_sp, float max_pitch_sp)
194 {
195 min_pitch = min_pitch_sp;
196 max_pitch = max_pitch_sp;
197
198 return *this;
199 }
200
201 FwControlConfiguration& withThrottleLimits(float min_throttle_sp, float max_throttle_sp)
202 {
203 min_throttle = min_throttle_sp;
204 max_throttle = max_throttle_sp;
205
206 return *this;
207 }
208
209 FwControlConfiguration& withMaxAcceleration(float max_lateral_acceleration_sp)
210 {
211 max_lateral_acceleration = max_lateral_acceleration_sp;
212 return *this;
213 }
214
215 FwControlConfiguration& withTargetSinkRate(float target_sink_rate_sp)
216 {
217 target_sink_rate = target_sink_rate_sp;
218 return *this;
219 }
220
221 FwControlConfiguration& withTargetClimbRate(float target_climb_rate_sp)
222 {
223 target_climb_rate = target_climb_rate_sp;
224 return *this;
225 }
226
236 {
237 if (speed_weight_sp < 0.0 || speed_weight_sp > 2.0) {
238 throw Exception("Speed weight must be between 0 and 2.");
239 }
240 speed_weight = speed_weight_sp;
241 return *this;
242 }
243};
244
246} /* namespace px4_ros2 */
Definition context.hpp:18
Definition exception.hpp:14
Setpoint type for fixedwing control.
Definition lateral_longitudinal.hpp:28
FwLateralLongitudinalSetpointType(Context &context, bool is_position_optional=false)
void updateWithAltitude(float altitude_amsl_sp, float course_sp, std::optional< float > equivalent_airspeed_sp={}, std::optional< float > lateral_acceleration_sp={})
Update the setpoint with simplest set of inputs: Altitude and Course. Lateral acceleration setpoints ...
SetpointType getSetpointType() override
Definition lateral_longitudinal.hpp:40
void update(const FwLateralLongitudinalSetpoint &setpoint)
Update the setpoint with full flexibility by passing a FwLateralLongitudinalSetpoint.
void update(const FwLateralLongitudinalSetpoint &setpoint, const FwControlConfiguration &config)
Update the setpoint with full flexibility by passing a FwLateralLongitudinalSetpoint and FwControlCon...
void clearOptionalRequirements(px4_msgs::msg::SetpointConfigReply &setpoint_config_reply) override
void updateWithHeightRate(float height_rate_sp, float course_sp, std::optional< float > equivalent_airspeed_sp={}, std::optional< float > lateral_acceleration_sp={})
Update the setpoint with simplest set of inputs: Height rate and Course. Lateral acceleration setpoin...
Definition setpoint_base.hpp:20
Definition lateral_longitudinal.hpp:183
FwControlConfiguration & withSpeedWeight(float speed_weight_sp)
Configure the speed weight for TECS (Total Energy Control System). For more information on TECS,...
Definition lateral_longitudinal.hpp:235
Definition lateral_longitudinal.hpp:131