PX4 Firmware
PX4 Autopilot Software http://px4.io
FixedwingAttitudeControl.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 #include <drivers/drv_hrt.h>
39 #include <lib/ecl/geo/geo.h>
40 #include <lib/mathlib/mathlib.h>
41 #include <lib/parameters/param.h>
42 #include <lib/perf/perf_counter.h>
43 #include <matrix/math.hpp>
44 #include <px4_platform_common/px4_config.h>
45 #include <px4_platform_common/defines.h>
46 #include <px4_platform_common/module.h>
47 #include <px4_platform_common/module_params.h>
48 #include <px4_platform_common/posix.h>
49 #include <px4_platform_common/tasks.h>
50 #include <px4_platform_common/px4_work_queue/WorkItem.hpp>
51 #include <uORB/Publication.hpp>
53 #include <uORB/Subscription.hpp>
69 
70 using matrix::Eulerf;
71 using matrix::Quatf;
72 
74 
75 class FixedwingAttitudeControl final : public ModuleBase<FixedwingAttitudeControl>, public ModuleParams,
76  public px4::WorkItem
77 {
78 public:
80  ~FixedwingAttitudeControl() override;
81 
82  /** @see ModuleBase */
83  static int task_spawn(int argc, char *argv[]);
84 
85  /** @see ModuleBase */
86  static int custom_command(int argc, char *argv[]);
87 
88  /** @see ModuleBase */
89  static int print_usage(const char *reason = nullptr);
90 
91  /** @see ModuleBase::print_status() */
92  int print_status() override;
93 
94  void Run() override;
95 
96  bool init();
97 
98 private:
99 
100  uORB::SubscriptionCallbackWorkItem _att_sub{this, ORB_ID(vehicle_attitude)}; /**< vehicle attitude */
101 
102  uORB::Subscription _att_sp_sub{ORB_ID(vehicle_attitude_setpoint)}; /**< vehicle attitude setpoint */
103  uORB::Subscription _battery_status_sub{ORB_ID(battery_status)}; /**< battery status subscription */
104  uORB::Subscription _global_pos_sub{ORB_ID(vehicle_global_position)}; /**< global position subscription */
105  uORB::Subscription _manual_sub{ORB_ID(manual_control_setpoint)}; /**< notification of manual control updates */
106  uORB::Subscription _parameter_update_sub{ORB_ID(parameter_update)}; /**< notification of parameter updates */
107  uORB::Subscription _rates_sp_sub{ORB_ID(vehicle_rates_setpoint)}; /**< vehicle rates setpoint */
108  uORB::Subscription _vcontrol_mode_sub{ORB_ID(vehicle_control_mode)}; /**< vehicle status subscription */
109  uORB::Subscription _vehicle_land_detected_sub{ORB_ID(vehicle_land_detected)}; /**< vehicle land detected subscription */
110  uORB::Subscription _vehicle_status_sub{ORB_ID(vehicle_status)}; /**< vehicle status subscription */
111  uORB::Subscription _vehicle_rates_sub{ORB_ID(vehicle_angular_velocity)};
112 
114 
115  uORB::Publication<actuator_controls_s> _actuators_2_pub{ORB_ID(actuator_controls_2)}; /**< actuator control group 1 setpoint (Airframe) */
116  uORB::Publication<vehicle_rates_setpoint_s> _rate_sp_pub{ORB_ID(vehicle_rates_setpoint)}; /**< rate setpoint publication */
117  uORB::PublicationMulti<rate_ctrl_status_s> _rate_ctrl_status_pub{ORB_ID(rate_ctrl_status)}; /**< rate controller status publication */
118 
120  orb_advert_t _attitude_sp_pub{nullptr}; /**< attitude setpoint point */
121 
122  orb_id_t _actuators_id{nullptr}; /**< pointer to correct actuator controls0 uORB metadata structure */
123  orb_advert_t _actuators_0_pub{nullptr}; /**< actuator control group 0 setpoint */
124 
125  actuator_controls_s _actuators {}; /**< actuator control inputs */
126  actuator_controls_s _actuators_airframe {}; /**< actuator control inputs */
127  manual_control_setpoint_s _manual {}; /**< r/c channel data */
128  vehicle_attitude_s _att {}; /**< vehicle attitude setpoint */
129  vehicle_attitude_setpoint_s _att_sp {}; /**< vehicle attitude setpoint */
130  vehicle_control_mode_s _vcontrol_mode {}; /**< vehicle control mode */
131  vehicle_global_position_s _global_pos {}; /**< global position */
132  vehicle_rates_setpoint_s _rates_sp {}; /* attitude rates setpoint */
133  vehicle_status_s _vehicle_status {}; /**< vehicle status */
134 
135  perf_counter_t _loop_perf; /**< loop performance counter */
136 
137  float _flaps_applied{0.0f};
138  float _flaperons_applied{0.0f};
139 
140  float _airspeed_scaling{1.0f};
141 
142  bool _landed{true};
143 
144  float _battery_scale{1.0f};
145 
147 
148  bool _is_tailsitter{false};
149 
151  (ParamFloat<px4::params::FW_ACRO_X_MAX>) _param_fw_acro_x_max,
152  (ParamFloat<px4::params::FW_ACRO_Y_MAX>) _param_fw_acro_y_max,
153  (ParamFloat<px4::params::FW_ACRO_Z_MAX>) _param_fw_acro_z_max,
154 
155  (ParamFloat<px4::params::FW_AIRSPD_MAX>) _param_fw_airspd_max,
156  (ParamFloat<px4::params::FW_AIRSPD_MIN>) _param_fw_airspd_min,
157  (ParamFloat<px4::params::FW_AIRSPD_TRIM>) _param_fw_airspd_trim,
158  (ParamInt<px4::params::FW_ARSP_MODE>) _param_fw_arsp_mode,
159 
160  (ParamBool<px4::params::FW_BAT_SCALE_EN>) _param_fw_bat_scale_en,
161 
162  (ParamFloat<px4::params::FW_DTRIM_P_FLPS>) _param_fw_dtrim_p_flps,
163  (ParamFloat<px4::params::FW_DTRIM_P_VMAX>) _param_fw_dtrim_p_vmax,
164  (ParamFloat<px4::params::FW_DTRIM_P_VMIN>) _param_fw_dtrim_p_vmin,
165  (ParamFloat<px4::params::FW_DTRIM_R_FLPS>) _param_fw_dtrim_r_flps,
166  (ParamFloat<px4::params::FW_DTRIM_R_VMAX>) _param_fw_dtrim_r_vmax,
167  (ParamFloat<px4::params::FW_DTRIM_R_VMIN>) _param_fw_dtrim_r_vmin,
168  (ParamFloat<px4::params::FW_DTRIM_Y_VMAX>) _param_fw_dtrim_y_vmax,
169  (ParamFloat<px4::params::FW_DTRIM_Y_VMIN>) _param_fw_dtrim_y_vmin,
170 
171  (ParamFloat<px4::params::FW_FLAPERON_SCL>) _param_fw_flaperon_scl,
172  (ParamFloat<px4::params::FW_FLAPS_LND_SCL>) _param_fw_flaps_lnd_scl,
173  (ParamFloat<px4::params::FW_FLAPS_SCL>) _param_fw_flaps_scl,
174  (ParamFloat<px4::params::FW_FLAPS_TO_SCL>) _param_fw_flaps_to_scl,
175 
176  (ParamFloat<px4::params::FW_MAN_P_MAX>) _param_fw_man_p_max,
177  (ParamFloat<px4::params::FW_MAN_P_SC>) _param_fw_man_p_sc,
178  (ParamFloat<px4::params::FW_MAN_R_MAX>) _param_fw_man_r_max,
179  (ParamFloat<px4::params::FW_MAN_R_SC>) _param_fw_man_r_sc,
180  (ParamFloat<px4::params::FW_MAN_Y_SC>) _param_fw_man_y_sc,
181 
182  (ParamFloat<px4::params::FW_P_RMAX_NEG>) _param_fw_p_rmax_neg,
183  (ParamFloat<px4::params::FW_P_RMAX_POS>) _param_fw_p_rmax_pos,
184  (ParamFloat<px4::params::FW_P_TC>) _param_fw_p_tc,
185  (ParamFloat<px4::params::FW_PR_FF>) _param_fw_pr_ff,
186  (ParamFloat<px4::params::FW_PR_I>) _param_fw_pr_i,
187  (ParamFloat<px4::params::FW_PR_IMAX>) _param_fw_pr_imax,
188  (ParamFloat<px4::params::FW_PR_P>) _param_fw_pr_p,
189  (ParamFloat<px4::params::FW_PSP_OFF>) _param_fw_psp_off,
190 
191  (ParamFloat<px4::params::FW_RATT_TH>) _param_fw_ratt_th,
192 
193  (ParamFloat<px4::params::FW_R_RMAX>) _param_fw_r_rmax,
194  (ParamFloat<px4::params::FW_R_TC>) _param_fw_r_tc,
195  (ParamFloat<px4::params::FW_RLL_TO_YAW_FF>) _param_fw_rll_to_yaw_ff,
196  (ParamFloat<px4::params::FW_RR_FF>) _param_fw_rr_ff,
197  (ParamFloat<px4::params::FW_RR_I>) _param_fw_rr_i,
198  (ParamFloat<px4::params::FW_RR_IMAX>) _param_fw_rr_imax,
199  (ParamFloat<px4::params::FW_RR_P>) _param_fw_rr_p,
200  (ParamFloat<px4::params::FW_RSP_OFF>) _param_fw_rsp_off,
201 
202  (ParamBool<px4::params::FW_W_EN>) _param_fw_w_en,
203  (ParamFloat<px4::params::FW_W_RMAX>) _param_fw_w_rmax,
204  (ParamFloat<px4::params::FW_WR_FF>) _param_fw_wr_ff,
205  (ParamFloat<px4::params::FW_WR_I>) _param_fw_wr_i,
206  (ParamFloat<px4::params::FW_WR_IMAX>) _param_fw_wr_imax,
207  (ParamFloat<px4::params::FW_WR_P>) _param_fw_wr_p,
208 
209  (ParamFloat<px4::params::FW_Y_RMAX>) _param_fw_y_rmax,
210  (ParamFloat<px4::params::FW_YR_FF>) _param_fw_yr_ff,
211  (ParamFloat<px4::params::FW_YR_I>) _param_fw_yr_i,
212  (ParamFloat<px4::params::FW_YR_IMAX>) _param_fw_yr_imax,
213  (ParamFloat<px4::params::FW_YR_P>) _param_fw_yr_p,
214 
215  (ParamFloat<px4::params::TRIM_PITCH>) _param_trim_pitch,
216  (ParamFloat<px4::params::TRIM_ROLL>) _param_trim_roll,
217  (ParamFloat<px4::params::TRIM_YAW>) _param_trim_yaw
218  )
219 
220  ECL_RollController _roll_ctrl;
224 
225  void control_flaps(const float dt);
226 
227  /**
228  * Update our local parameter cache.
229  */
230  int parameters_update();
231 
233  void vehicle_manual_poll();
236  void vehicle_status_poll();
238 
240 };
static int task_spawn(int argc, char *argv[])
vehicle_attitude_s _att
vehicle attitude setpoint
uORB::Subscription _vehicle_status_sub
vehicle status subscription
Definition of geo / math functions to perform geodesic calculations.
orb_id_t _actuators_id
pointer to correct actuator controls0 uORB metadata structure
manual_control_setpoint_s _manual
r/c channel data
uORB::Subscription _battery_status_sub
battery status subscription
Definition of a simple orthogonal roll PID controller.
DEFINE_PARAMETERS((ParamFloat< px4::params::FW_ACRO_X_MAX >) _param_fw_acro_x_max,(ParamFloat< px4::params::FW_ACRO_Y_MAX >) _param_fw_acro_y_max,(ParamFloat< px4::params::FW_ACRO_Z_MAX >) _param_fw_acro_z_max,(ParamFloat< px4::params::FW_AIRSPD_MAX >) _param_fw_airspd_max,(ParamFloat< px4::params::FW_AIRSPD_MIN >) _param_fw_airspd_min,(ParamFloat< px4::params::FW_AIRSPD_TRIM >) _param_fw_airspd_trim,(ParamInt< px4::params::FW_ARSP_MODE >) _param_fw_arsp_mode,(ParamBool< px4::params::FW_BAT_SCALE_EN >) _param_fw_bat_scale_en,(ParamFloat< px4::params::FW_DTRIM_P_FLPS >) _param_fw_dtrim_p_flps,(ParamFloat< px4::params::FW_DTRIM_P_VMAX >) _param_fw_dtrim_p_vmax,(ParamFloat< px4::params::FW_DTRIM_P_VMIN >) _param_fw_dtrim_p_vmin,(ParamFloat< px4::params::FW_DTRIM_R_FLPS >) _param_fw_dtrim_r_flps,(ParamFloat< px4::params::FW_DTRIM_R_VMAX >) _param_fw_dtrim_r_vmax,(ParamFloat< px4::params::FW_DTRIM_R_VMIN >) _param_fw_dtrim_r_vmin,(ParamFloat< px4::params::FW_DTRIM_Y_VMAX >) _param_fw_dtrim_y_vmax,(ParamFloat< px4::params::FW_DTRIM_Y_VMIN >) _param_fw_dtrim_y_vmin,(ParamFloat< px4::params::FW_FLAPERON_SCL >) _param_fw_flaperon_scl,(ParamFloat< px4::params::FW_FLAPS_LND_SCL >) _param_fw_flaps_lnd_scl,(ParamFloat< px4::params::FW_FLAPS_SCL >) _param_fw_flaps_scl,(ParamFloat< px4::params::FW_FLAPS_TO_SCL >) _param_fw_flaps_to_scl,(ParamFloat< px4::params::FW_MAN_P_MAX >) _param_fw_man_p_max,(ParamFloat< px4::params::FW_MAN_P_SC >) _param_fw_man_p_sc,(ParamFloat< px4::params::FW_MAN_R_MAX >) _param_fw_man_r_max,(ParamFloat< px4::params::FW_MAN_R_SC >) _param_fw_man_r_sc,(ParamFloat< px4::params::FW_MAN_Y_SC >) _param_fw_man_y_sc,(ParamFloat< px4::params::FW_P_RMAX_NEG >) _param_fw_p_rmax_neg,(ParamFloat< px4::params::FW_P_RMAX_POS >) _param_fw_p_rmax_pos,(ParamFloat< px4::params::FW_P_TC >) _param_fw_p_tc,(ParamFloat< px4::params::FW_PR_FF >) _param_fw_pr_ff,(ParamFloat< px4::params::FW_PR_I >) _param_fw_pr_i,(ParamFloat< px4::params::FW_PR_IMAX >) _param_fw_pr_imax,(ParamFloat< px4::params::FW_PR_P >) _param_fw_pr_p,(ParamFloat< px4::params::FW_PSP_OFF >) _param_fw_psp_off,(ParamFloat< px4::params::FW_RATT_TH >) _param_fw_ratt_th,(ParamFloat< px4::params::FW_R_RMAX >) _param_fw_r_rmax,(ParamFloat< px4::params::FW_R_TC >) _param_fw_r_tc,(ParamFloat< px4::params::FW_RLL_TO_YAW_FF >) _param_fw_rll_to_yaw_ff,(ParamFloat< px4::params::FW_RR_FF >) _param_fw_rr_ff,(ParamFloat< px4::params::FW_RR_I >) _param_fw_rr_i,(ParamFloat< px4::params::FW_RR_IMAX >) _param_fw_rr_imax,(ParamFloat< px4::params::FW_RR_P >) _param_fw_rr_p,(ParamFloat< px4::params::FW_RSP_OFF >) _param_fw_rsp_off,(ParamBool< px4::params::FW_W_EN >) _param_fw_w_en,(ParamFloat< px4::params::FW_W_RMAX >) _param_fw_w_rmax,(ParamFloat< px4::params::FW_WR_FF >) _param_fw_wr_ff,(ParamFloat< px4::params::FW_WR_I >) _param_fw_wr_i,(ParamFloat< px4::params::FW_WR_IMAX >) _param_fw_wr_imax,(ParamFloat< px4::params::FW_WR_P >) _param_fw_wr_p,(ParamFloat< px4::params::FW_Y_RMAX >) _param_fw_y_rmax,(ParamFloat< px4::params::FW_YR_FF >) _param_fw_yr_ff,(ParamFloat< px4::params::FW_YR_I >) _param_fw_yr_i,(ParamFloat< px4::params::FW_YR_IMAX >) _param_fw_yr_imax,(ParamFloat< px4::params::FW_YR_P >) _param_fw_yr_p,(ParamFloat< px4::params::TRIM_PITCH >) _param_trim_pitch,(ParamFloat< px4::params::TRIM_ROLL >) _param_trim_roll,(ParamFloat< px4::params::TRIM_YAW >) _param_trim_yaw) ECL_RollController _roll_ctrl
actuator_controls_s _actuators
actuator control inputs
vehicle_control_mode_s _vcontrol_mode
vehicle control mode
perf_counter_t _loop_perf
loop performance counter
vehicle_attitude_setpoint_s _att_sp
vehicle attitude setpoint
uORB::SubscriptionData< airspeed_validated_s > _airspeed_validated_sub
static int custom_command(int argc, char *argv[])
uORB::Subscription _vcontrol_mode_sub
vehicle status subscription
High-resolution timer with callouts and timekeeping.
Definition of a simple orthogonal pitch PID controller.
Global flash based parameter store.
Definition of a simple orthogonal coordinated turn yaw PID controller.
uORB::PublicationMulti< rate_ctrl_status_s > _rate_ctrl_status_pub
rate controller status publication
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
uORB::Subscription _rates_sp_sub
vehicle rates setpoint
Header common to all counters.
uORB::Publication< actuator_controls_s > _actuators_2_pub
actuator control group 1 setpoint (Airframe)
uORB::Subscription _global_pos_sub
global position subscription
orb_advert_t _attitude_sp_pub
attitude setpoint point
Euler< float > Eulerf
Definition: Euler.hpp:156
orb_advert_t _actuators_0_pub
actuator control group 0 setpoint
vehicle_status_s _vehicle_status
vehicle status
uORB::Publication< vehicle_rates_setpoint_s > _rate_sp_pub
rate setpoint publication
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
Definition: uORB.h:134
uORB::SubscriptionCallbackWorkItem _att_sub
vehicle attitude
actuator_controls_s _actuators_airframe
actuator control inputs
Definition of a simple orthogonal coordinated turn yaw PID controller.
Object metadata.
Definition: uORB.h:50
int parameters_update()
Update our local parameter cache.
vehicle_global_position_s _global_pos
global position
Quaternion< float > Quatf
Definition: Quaternion.hpp:544
float dt
Definition: px4io.c:73
uORB::Subscription _vehicle_land_detected_sub
vehicle land detected subscription
uORB::Subscription _att_sp_sub
vehicle attitude setpoint
uORB::Subscription _manual_sub
notification of manual control updates
vehicle_rates_setpoint_s _rates_sp
static int print_usage(const char *reason=nullptr)
uORB::Subscription _parameter_update_sub
notification of parameter updates
Performance measuring tools.