PX4 ROS 2 Interface Library
Library to interface with PX4 from a companion computer using ROS 2
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 <px4_msgs/msg/fixed_wing_lateral_setpoint.hpp>
9 #include <px4_msgs/msg/fixed_wing_longitudinal_setpoint.hpp>
10 #include <px4_msgs/msg/lateral_control_configuration.hpp>
11 #include <px4_msgs/msg/longitudinal_control_configuration.hpp>
12 #include <Eigen/Eigen>
13 #include <optional>
14 
15 #include <px4_ros2/common/setpoint_base.hpp>
16 #include <px4_ros2/common/exception.hpp>
17 
18 namespace px4_ros2
19 {
24 struct FwLateralLongitudinalSetpoint;
25 struct FwControlConfiguration;
26 
31 {
32 public:
33  explicit FwLateralLongitudinalSetpointType(Context & context);
34 
35  ~FwLateralLongitudinalSetpointType() override = default;
36 
37  Configuration getConfiguration() override;
38 
56  void update(
57  const FwLateralLongitudinalSetpoint & setpoint,
58  const FwControlConfiguration & config);
59 
73  void update(const FwLateralLongitudinalSetpoint & setpoint);
74 
83  float altitude_amsl_sp, float course_sp,
84  std::optional<float> equivalent_airspeed_sp = {},
85  std::optional<float> lateral_acceleration_sp = {});
86 
95  float height_rate_sp, float course_sp,
96  std::optional<float> equivalent_airspeed_sp = {},
97  std::optional<float> lateral_acceleration_sp = {});
98 
99  float desiredUpdateRateHz() override {return 30.f;}
100 
101 private:
102  rclcpp::Node & _node;
103  rclcpp::Publisher<px4_msgs::msg::FixedWingLateralSetpoint>::SharedPtr
104  _fw_lateral_sp_pub;
105  rclcpp::Publisher<px4_msgs::msg::FixedWingLongitudinalSetpoint>::SharedPtr
106  _fw_longitudinal_sp_pub;
107 
108  rclcpp::Publisher<px4_msgs::msg::LateralControlConfiguration>::SharedPtr
109  _lateral_control_configuration_pub;
110  rclcpp::Publisher<px4_msgs::msg::LongitudinalControlConfiguration>::SharedPtr
111  _longitudinal_control_configuration_pub;
112 };
113 
115 {
116  std::optional<float> course;
117  std::optional<float> airspeed_direction;
118  std::optional<float> lateral_acceleration;
119  std::optional<float> altitude_msl;
120  std::optional<float> height_rate;
121  std::optional<float> equivalent_airspeed;
122  std::optional<float> throttle_direct;
123 
124  FwLateralLongitudinalSetpoint & withCourse(float course_sp)
125  {
126  course = course_sp;
127  return *this;
128  }
129 
130  FwLateralLongitudinalSetpoint & withAirspeedDirection(float airspeed_direction_sp)
131  {
132  airspeed_direction = airspeed_direction_sp;
133  return *this;
134  }
135 
136  FwLateralLongitudinalSetpoint & withLateralAcceleration(float lateral_acceleration_sp)
137  {
138  lateral_acceleration = lateral_acceleration_sp;
139  return *this;
140  }
141 
142  FwLateralLongitudinalSetpoint & withAltitude(float altitude_sp)
143  {
144  altitude_msl = altitude_sp;
145  return *this;
146  }
147 
148  FwLateralLongitudinalSetpoint & withHeightRate(float height_rate_sp)
149  {
150  height_rate = height_rate_sp;
151  return *this;
152  }
153 
154  FwLateralLongitudinalSetpoint & withEquivalentAirspeed(float equivalent_airspeed_sp)
155  {
156  equivalent_airspeed = equivalent_airspeed_sp;
157  return *this;
158  }
159 
160  FwLateralLongitudinalSetpoint & withThrottleDirect(float throttle_direct_sp)
161  {
162 
163  throttle_direct = throttle_direct_sp;
164  return *this;
165  }
166 };
167 
169 {
170  std::optional<float> min_pitch;
171  std::optional<float> max_pitch;
172  std::optional<float> min_throttle;
173  std::optional<float> max_throttle;
174  std::optional<float> max_lateral_acceleration;
175  std::optional<float> target_climb_rate;
176  std::optional<float> target_sink_rate;
177  std::optional<float> speed_weight;
178 
179  FwControlConfiguration & withPitchLimits(float min_pitch_sp, float max_pitch_sp)
180  {
181 
182  min_pitch = min_pitch_sp;
183  max_pitch = max_pitch_sp;
184 
185  return *this;
186  }
187 
188  FwControlConfiguration & withThrottleLimits(float min_throttle_sp, float max_throttle_sp)
189  {
190 
191  min_throttle = min_throttle_sp;
192  max_throttle = max_throttle_sp;
193 
194  return *this;
195  }
196 
197  FwControlConfiguration & withMaxAcceleration(float max_lateral_acceleration_sp)
198  {
199 
200  max_lateral_acceleration = max_lateral_acceleration_sp;
201  return *this;
202  }
203 
204  FwControlConfiguration & withTargetSinkRate(float target_sink_rate_sp)
205  {
206 
207  target_sink_rate = target_sink_rate_sp;
208  return *this;
209  }
210 
211  FwControlConfiguration & withTargetClimbRate(float target_climb_rate_sp)
212  {
213 
214  target_climb_rate = target_climb_rate_sp;
215  return *this;
216  }
217 
223  FwControlConfiguration & withSpeedWeight(float speed_weight_sp)
224  {
225  if (speed_weight_sp < 0.0 || speed_weight_sp > 2.0) {
226  throw Exception("Speed weight must be between 0 and 2.");
227  }
228  speed_weight = speed_weight_sp;
229  return *this;
230  }
231 };
232 
234 } /* namespace px4_ros2 */
Definition: context.hpp:20
Definition: exception.hpp:16
Setpoint type for fixedwing control.
Definition: lateral_longitudinal.hpp:31
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 ...
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 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:21
Definition: lateral_longitudinal.hpp:169
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:223
Definition: lateral_longitudinal.hpp:115
Definition: setpoint_base.hpp:26