PX4 Firmware
PX4 Autopilot Software http://px4.io
FixedwingPositionControl.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2013-2019 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 
35 /**
36  * @file fw_pos_control_l1_main.hpp
37  * Implementation of a generic position controller based on the L1 norm. Outputs a bank / roll
38  * angle, equivalent to a lateral motion (for copters and rovers).
39  *
40  * The implementation for the controllers is in the ECL library. This class only
41  * interfaces to the library.
42  *
43  * @author Lorenz Meier <lorenz@px4.io>
44  * @author Thomas Gubler <thomasgubler@gmail.com>
45  * @author Andreas Antener <andreas@uaventure.com>
46  */
47 
48 #ifndef FIXEDWINGPOSITIONCONTROL_HPP_
49 #define FIXEDWINGPOSITIONCONTROL_HPP_
50 
53 
54 #include <float.h>
55 
56 #include <drivers/drv_hrt.h>
57 #include <lib/ecl/geo/geo.h>
59 #include <lib/ecl/tecs/tecs.h>
61 #include <lib/mathlib/mathlib.h>
62 #include <lib/perf/perf_counter.h>
63 #include <px4_platform_common/px4_config.h>
64 #include <px4_platform_common/defines.h>
65 #include <px4_platform_common/module.h>
66 #include <px4_platform_common/module_params.h>
67 #include <px4_platform_common/posix.h>
68 #include <px4_platform_common/px4_work_queue/WorkItem.hpp>
69 #include <uORB/Publication.hpp>
70 #include <uORB/Subscription.hpp>
90 #include <uORB/uORB.h>
92 
93 using math::constrain;
94 using math::max;
95 using math::min;
96 using math::radians;
97 
98 using matrix::Dcmf;
99 using matrix::Eulerf;
100 using matrix::Quatf;
101 using matrix::Vector2f;
102 using matrix::Vector3f;
103 using matrix::wrap_pi;
104 
106 
107 using namespace launchdetection;
108 using namespace runwaytakeoff;
109 using namespace time_literals;
110 
111 static constexpr float HDG_HOLD_DIST_NEXT =
112  3000.0f; // initial distance of waypoint in front of plane in heading hold mode
113 static constexpr float HDG_HOLD_REACHED_DIST =
114  1000.0f; // distance (plane to waypoint in front) at which waypoints are reset in heading hold mode
115 static constexpr float HDG_HOLD_SET_BACK_DIST = 100.0f; // distance by which previous waypoint is set behind the plane
116 static constexpr float HDG_HOLD_YAWRATE_THRESH = 0.15f; // max yawrate at which plane locks yaw for heading hold mode
117 static constexpr float HDG_HOLD_MAN_INPUT_THRESH =
118  0.01f; // max manual roll/yaw input from user which does not change the locked heading
119 
120 static constexpr hrt_abstime T_ALT_TIMEOUT = 1_s; // time after which we abort landing if terrain estimate is not valid
121 
122 static constexpr float THROTTLE_THRESH =
123  0.05f; ///< max throttle from user which will not lead to motors spinning up in altitude controlled modes
124 static constexpr float MANUAL_THROTTLE_CLIMBOUT_THRESH =
125  0.85f; ///< a throttle / pitch input above this value leads to the system switching to climbout mode
126 static constexpr float ALTHOLD_EPV_RESET_THRESH = 5.0f;
127 
128 class FixedwingPositionControl final : public ModuleBase<FixedwingPositionControl>, public ModuleParams,
129  public px4::WorkItem
130 {
131 public:
133  ~FixedwingPositionControl() override;
134 
135  /** @see ModuleBase */
136  static int task_spawn(int argc, char *argv[]);
137 
138  /** @see ModuleBase */
139  static int custom_command(int argc, char *argv[]);
140 
141  /** @see ModuleBase */
142  static int print_usage(const char *reason = nullptr);
143 
144  void Run() override;
145 
146  bool init();
147 
148  /** @see ModuleBase::print_status() */
149  int print_status() override;
150 
151 private:
152  orb_advert_t _mavlink_log_pub{nullptr};
153 
154  uORB::SubscriptionCallbackWorkItem _global_pos_sub{this, ORB_ID(vehicle_global_position)};
155 
156  uORB::Subscription _control_mode_sub{ORB_ID(vehicle_control_mode)}; ///< control mode subscription
157  uORB::Subscription _local_pos_sub{ORB_ID(vehicle_local_position)};
158  uORB::Subscription _manual_control_sub{ORB_ID(manual_control_setpoint)}; ///< notification of manual control updates
159  uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)}; ///< notification of parameter updates
160  uORB::Subscription _pos_sp_triplet_sub{ORB_ID(position_setpoint_triplet)};
161  uORB::Subscription _sensor_baro_sub{ORB_ID(sensor_baro)};
162  uORB::Subscription _vehicle_attitude_sub{ORB_ID(vehicle_attitude)}; ///< vehicle attitude subscription
163  uORB::Subscription _vehicle_command_sub{ORB_ID(vehicle_command)}; ///< vehicle command subscription
164  uORB::Subscription _vehicle_land_detected_sub{ORB_ID(vehicle_land_detected)}; ///< vehicle land detected subscription
165  uORB::Subscription _vehicle_status_sub{ORB_ID(vehicle_status)}; ///< vehicle status subscription
166  uORB::SubscriptionData<vehicle_angular_velocity_s> _vehicle_rates_sub{ORB_ID(vehicle_angular_velocity)};
167 
168  orb_advert_t _attitude_sp_pub{nullptr}; ///< attitude setpoint
169  orb_id_t _attitude_setpoint_id{nullptr};
170 
171  uORB::Publication<position_controller_status_s> _pos_ctrl_status_pub{ORB_ID(position_controller_status)}; ///< navigation capabilities publication
172  uORB::Publication<position_controller_landing_status_s> _pos_ctrl_landing_status_pub{ORB_ID(position_controller_landing_status)}; ///< landing status publication
173  uORB::Publication<tecs_status_s> _tecs_status_pub{ORB_ID(tecs_status)}; ///< TECS status publication
174 
175  manual_control_setpoint_s _manual {}; ///< r/c channel data
176  position_setpoint_triplet_s _pos_sp_triplet {}; ///< triplet of mission items
177  vehicle_attitude_s _att {}; ///< vehicle attitude setpoint
178  vehicle_attitude_setpoint_s _att_sp {}; ///< vehicle attitude setpoint
179  vehicle_command_s _vehicle_command {}; ///< vehicle commands
180  vehicle_control_mode_s _control_mode {}; ///< control mode
181  vehicle_global_position_s _global_pos {}; ///< global vehicle position
182  vehicle_local_position_s _local_pos {}; ///< vehicle local position
183  vehicle_land_detected_s _vehicle_land_detected {}; ///< vehicle land detected
184  vehicle_status_s _vehicle_status {}; ///< vehicle status
185 
186  SubscriptionData<airspeed_validated_s> _airspeed_validated_sub{ORB_ID(airspeed_validated)};
187  SubscriptionData<vehicle_acceleration_s> _vehicle_acceleration_sub{ORB_ID(vehicle_acceleration)};
188 
189  perf_counter_t _loop_perf; ///< loop performance counter
190 
191  float _hold_alt{0.0f}; ///< hold altitude for altitude mode
192  float _takeoff_ground_alt{0.0f}; ///< ground altitude at which plane was launched
193  float _hdg_hold_yaw{0.0f}; ///< hold heading for velocity mode
194  bool _hdg_hold_enabled{false}; ///< heading hold enabled
195  bool _yaw_lock_engaged{false}; ///< yaw is locked for heading hold
196  float _althold_epv{0.0f}; ///< the position estimate accuracy when engaging alt hold
197  bool _was_in_deadband{false}; ///< wether the last stick input was in althold deadband
198 
199  position_setpoint_s _hdg_hold_prev_wp {}; ///< position where heading hold started
200  position_setpoint_s _hdg_hold_curr_wp {}; ///< position to which heading hold flies
201 
202  hrt_abstime _control_position_last_called{0}; ///< last call of control_position
203 
204  /* Landing */
205  bool _land_noreturn_horizontal{false};
206  bool _land_noreturn_vertical{false};
207  bool _land_stayonground{false};
208  bool _land_motor_lim{false};
209  bool _land_onslope{false};
210  bool _land_abort{false};
211 
213 
214  hrt_abstime _time_started_landing{0}; ///< time at which landing started
215 
216  float _t_alt_prev_valid{0}; ///< last terrain estimate which was valid
217  hrt_abstime _time_last_t_alt{0}; ///< time at which we had last valid terrain alt
218 
219  float _flare_height{0.0f}; ///< estimated height to ground at which flare started
220  float _flare_pitch_sp{0.0f}; ///< Current forced (i.e. not determined using TECS) flare pitch setpoint
221  float _flare_curve_alt_rel_last{0.0f};
222  float _target_bearing{0.0f}; ///< estimated height to ground at which flare started
223 
224  bool _was_in_air{false}; ///< indicated wether the plane was in the air in the previous interation*/
225  hrt_abstime _time_went_in_air{0}; ///< time at which the plane went in the air
226 
227  /* Takeoff launch detection and runway */
230  hrt_abstime _launch_detection_notify{0};
231 
233 
234  bool _last_manual{false}; ///< true if the last iteration was in manual mode (used to determine when a reset is needed)
235 
236  /* throttle and airspeed states */
237  bool _airspeed_valid{false}; ///< flag if a valid airspeed estimate exists
238  hrt_abstime _airspeed_last_valid{0}; ///< last time airspeed was received. Used to detect timeouts.
239  float _airspeed{0.0f};
240  float _eas2tas{1.0f};
241 
242  float _groundspeed_undershoot{0.0f}; ///< ground speed error to min. speed in m/s
243 
244  Dcmf _R_nb; ///< current attitude
245  float _roll{0.0f};
246  float _pitch{0.0f};
247  float _yaw{0.0f};
248 
249  bool _reinitialize_tecs{true}; ///< indicates if the TECS states should be reinitialized (used for VTOL)
250  bool _is_tecs_running{false};
251  hrt_abstime _last_tecs_update{0};
252 
253  float _asp_after_transition{0.0f};
254  bool _was_in_transition{false};
255 
256  // estimator reset counters
257  uint8_t _pos_reset_counter{0}; ///< captures the number of times the estimator has reset the horizontal position
258  uint8_t _alt_reset_counter{0}; ///< captures the number of times the estimator has reset the altitude state
259 
262 
267  FW_POSCTRL_MODE_OTHER
268  } _control_mode_current{FW_POSCTRL_MODE_OTHER}; ///< used to check the mode in the last control loop iteration. Use to check if the last iteration was in the same mode.
269 
270  struct {
272 
277 
282 
285 
291 
296 
299 
307 
308  // VTOL
310  int32_t vtol_type;
311  } _parameters{}; ///< local copies of interesting parameters
312 
313  struct {
315 
320 
337 
343 
346 
353 
358 
360 
373 
375  } _parameter_handles {}; ///< handles for interesting parameters
376 
377  DEFINE_PARAMETERS(
378  (ParamFloat<px4::params::FW_GND_SPD_MIN>) _groundspeed_min
379  )
380 
381  // Update our local parameter cache.
382  int parameters_update();
383 
384  // Update subscriptions
385  void airspeed_poll();
386  void control_update();
387  void vehicle_attitude_poll();
388  void vehicle_command_poll();
389  void vehicle_control_mode_poll();
390  void vehicle_status_poll();
391 
392  void status_publish();
393  void landing_status_publish();
394  void tecs_status_publish();
395 
396  void abort_landing(bool abort);
397 
398  /**
399  * Get a new waypoint based on heading and distance from current position
400  *
401  * @param heading the heading to fly to
402  * @param distance the distance of the generated waypoint
403  * @param waypoint_prev the waypoint at the current position
404  * @param waypoint_next the waypoint in the heading direction
405  */
406  void get_waypoint_heading_distance(float heading, position_setpoint_s &waypoint_prev,
407  position_setpoint_s &waypoint_next, bool flag_init);
408 
409  /**
410  * Return the terrain estimate during takeoff or takeoff_alt if terrain estimate is not available
411  */
412  float get_terrain_altitude_takeoff(float takeoff_alt, const vehicle_global_position_s &global_pos);
413 
414  /**
415  * Check if we are in a takeoff situation
416  */
417  bool in_takeoff_situation();
418 
419  /**
420  * Do takeoff help when in altitude controlled modes
421  * @param hold_altitude altitude setpoint for controller
422  * @param pitch_limit_min minimum pitch allowed
423  */
424  void do_takeoff_help(float *hold_altitude, float *pitch_limit_min);
425 
426  /**
427  * Update desired altitude base on user pitch stick input
428  *
429  * @param dt Time step
430  * @return true if climbout mode was requested by user (climb with max rate and min airspeed)
431  */
432  bool update_desired_altitude(float dt);
433 
434  bool control_position(const Vector2f &curr_pos, const Vector2f &ground_speed, const position_setpoint_s &pos_sp_prev,
435  const position_setpoint_s &pos_sp_curr, const position_setpoint_s &pos_sp_next);
436  void control_takeoff(const Vector2f &curr_pos, const Vector2f &ground_speed, const position_setpoint_s &pos_sp_prev,
437  const position_setpoint_s &pos_sp_curr);
438  void control_landing(const Vector2f &curr_pos, const Vector2f &ground_speed, const position_setpoint_s &pos_sp_prev,
439  const position_setpoint_s &pos_sp_curr);
440 
441  float get_tecs_pitch();
442  float get_tecs_thrust();
443 
444  float get_demanded_airspeed();
445  float calculate_target_airspeed(float airspeed_demand, const Vector2f &ground_speed);
446 
447  /**
448  * Handle incoming vehicle commands
449  */
450  void handle_command();
451 
452  void reset_takeoff_state(bool force = false);
453  void reset_landing_state();
454 
455  /*
456  * Call TECS : a wrapper function to call the TECS implementation
457  */
458  void tecs_update_pitch_throttle(float alt_sp, float airspeed_sp,
459  float pitch_min_rad, float pitch_max_rad,
460  float throttle_min, float throttle_max, float throttle_cruise,
461  bool climbout_mode, float climbout_pitch_min_rad,
462  uint8_t mode = tecs_status_s::TECS_MODE_NORMAL);
463 
464 };
465 
466 #endif // FIXEDWINGPOSITIONCONTROL_HPP_
constexpr _Tp constrain(_Tp val, _Tp min_val, _Tp max_val)
Definition: Limits.hpp:66
struct uart_esc::@19 _parameter_handles
API for the uORB lightweight object broker.
Definition of geo / math functions to perform geodesic calculations.
L1 Nonlinear Guidance Logic.
static constexpr float THROTTLE_THRESH
max throttle from user which will not lead to motors spinning up in altitude controlled modes ...
Dcm< float > Dcmf
Definition: Dcm.hpp:185
No launch has been detected.
Definition: LaunchMethod.h:48
perf_counter_t _loop_perf
loop performance counter
static constexpr float HDG_HOLD_REACHED_DIST
void print_status()
Definition: Commander.cpp:517
static constexpr float MANUAL_THROTTLE_CLIMBOUT_THRESH
a throttle / pitch input above this value leads to the system switching to climbout mode ...
static void print_usage()
static constexpr float HDG_HOLD_SET_BACK_DIST
High-resolution timer with callouts and timekeeping.
Implementation of L1 position control.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
Header common to all counters.
static void parameters_update()
update all parameters
void init()
Activates/configures the hardware registers.
Runway takeoff handling for fixed-wing UAVs with steerable wheels.
static constexpr float ALTHOLD_EPV_RESET_THRESH
static constexpr float HDG_HOLD_YAWRATE_THRESH
constexpr T radians(T degrees)
Definition: Limits.hpp:85
Vector2< float > Vector2f
Definition: Vector2.hpp:69
Auto Detection for different launch methods (e.g.
Euler< float > Eulerf
Definition: Euler.hpp:156
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
constexpr _Tp min(_Tp a, _Tp b)
Definition: Limits.hpp:54
Definition: tecs.h:45
Type wrap_pi(Type x)
Wrap value in range [-π, π)
Vector3< float > Vector3f
Definition: Vector3.hpp:136
static constexpr float HDG_HOLD_DIST_NEXT
struct uart_esc::@18 _parameters
Object metadata.
Definition: uORB.h:50
constexpr _Tp max(_Tp a, _Tp b)
Definition: Limits.hpp:60
Quaternion< float > Quatf
Definition: Quaternion.hpp:544
float dt
Definition: px4io.c:73
static constexpr hrt_abstime T_ALT_TIMEOUT
static constexpr float HDG_HOLD_MAN_INPUT_THRESH
mode
Definition: vtol_type.h:76
uint32_t param_t
Parameter handle.
Definition: param.h:98
Performance measuring tools.