PX4 Firmware
PX4 Autopilot Software http://px4.io
vtol_type.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2015 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 * @file vtol_type.h
36 *
37 * @author Roman Bapst <bapstroman@gmail.com>
38 * @author Sander Smeets <sander@droneslab.com>
39 * @author Andreas Antener <andreas@uaventure.com>
40 *
41 */
42 
43 #ifndef VTOL_TYPE_H
44 #define VTOL_TYPE_H
45 
46 #include <lib/mathlib/mathlib.h>
47 #include <drivers/drv_hrt.h>
48 #include <drivers/drv_pwm_output.h>
49 
50 struct Params {
51  int32_t idle_pwm_mc; // pwm value for idle in mc mode
52  int32_t vtol_motor_id;
53  int32_t vtol_type;
54  bool elevons_mc_lock; // lock elevons in multicopter mode
55  float fw_min_alt; // minimum relative altitude for FW mode (QuadChute)
56  float fw_alt_err; // maximum negative altitude error for FW mode (Adaptive QuadChute)
57  float fw_qc_max_pitch; // maximum pitch angle FW mode (QuadChute)
58  float fw_qc_max_roll; // maximum roll angle FW mode (QuadChute)
70  int32_t fw_motors_off; /**< bitmask of all motors that should be off in fixed wing mode */
71  int32_t diff_thrust;
73 };
74 
75 // Has to match 1:1 msg/vtol_vehicle_status.msg
76 enum class mode {
77  TRANSITION_TO_FW = 1,
78  TRANSITION_TO_MC = 2,
79  ROTARY_WING = 3,
80  FIXED_WING = 4
81 };
82 
83 enum class vtol_type {
84  TAILSITTER = 0,
85  TILTROTOR,
86  STANDARD
87 };
88 
89 // these are states that can be applied to a selection of multirotor motors.
90 // e.g. if we need to shut off some motors after transitioning to fixed wing mode
91 // we can individually disable them while others might still need to be enabled to produce thrust.
92 // we can select the target motors via VT_FW_MOT_OFFID
93 enum class motor_state {
94  ENABLED = 0, // motor max pwm will be set to the standard max pwm value
95  DISABLED, // motor max pwm will be set to a value that shuts the motor off
96  IDLE, // motor max pwm will be set to VT_IDLE_PWM_MC
97  VALUE // motor max pwm will be set to a specific value provided, see set_motor_state()
98 };
99 
100 /**
101  * @brief Used to specify if min or max pwm values should be altered
102  */
103 enum class pwm_limit_type {
104  TYPE_MINIMUM = 0,
106 };
107 
108 class VtolAttitudeControl;
109 
110 class VtolType
111 {
112 public:
113 
114  VtolType(VtolAttitudeControl *att_controller);
115  VtolType(const VtolType &) = delete;
116  VtolType &operator=(const VtolType &) = delete;
117 
118  virtual ~VtolType() = default;
119 
120  /**
121  * Initialise.
122  */
123  bool init();
124 
125  /**
126  * Update vtol state.
127  */
128  virtual void update_vtol_state() = 0;
129 
130  /**
131  * Update transition state.
132  */
133  virtual void update_transition_state() = 0;
134 
135  /**
136  * Update multicopter state.
137  */
138  virtual void update_mc_state();
139 
140  /**
141  * Update fixed wing state.
142  */
143  virtual void update_fw_state();
144 
145  /**
146  * Write control values to actuator output topics.
147  */
148  virtual void fill_actuator_outputs() = 0;
149 
150  /**
151  * Special handling opportunity for the time right after transition to FW
152  * before TECS is running.
153  */
154  virtual void waiting_on_tecs() {}
155 
156  /**
157  * Checks for fixed-wing failsafe condition and issues abort request if needed.
158  */
159  void check_quadchute_condition();
160 
161  /**
162  * Returns true if we're allowed to do a mode transition on the ground.
163  */
164  bool can_transition_on_ground();
165 
166 
167 
168  mode get_mode() {return _vtol_mode;}
169 
170  virtual void parameters_update() = 0;
171 
172 protected:
175 
176  static constexpr const int num_outputs_max = 8;
177 
178  struct vehicle_attitude_s *_v_att; //vehicle attitude
179  struct vehicle_attitude_setpoint_s *_v_att_sp; //vehicle attitude setpoint
180  struct vehicle_attitude_setpoint_s *_mc_virtual_att_sp; // virtual mc attitude setpoint
181  struct vehicle_attitude_setpoint_s *_fw_virtual_att_sp; // virtual fw attitude setpoint
182  struct vehicle_control_mode_s *_v_control_mode; //vehicle control mode
184  struct actuator_controls_s *_actuators_out_0; //actuator controls going to the mc mixer
185  struct actuator_controls_s *_actuators_out_1; //actuator controls going to the fw mixer (used for elevons)
186  struct actuator_controls_s *_actuators_mc_in; //actuator controls from mc_rate_control
187  struct actuator_controls_s *_actuators_fw_in; //actuator controls from fw_att_control
193 
194  struct Params *_params;
195 
196  bool flag_idle_mc = false; //false = "idle is set for fixed wing mode"; true = "idle is set for multicopter mode"
197 
198  bool _pusher_active = false;
199  float _mc_roll_weight = 1.0f; // weight for multicopter attitude controller roll output
200  float _mc_pitch_weight = 1.0f; // weight for multicopter attitude controller pitch output
201  float _mc_yaw_weight = 1.0f; // weight for multicopter attitude controller yaw output
202  float _mc_throttle_weight = 1.0f; // weight for multicopter throttle command. Used to avoid
203 
204  // motors spinning up or cutting too fast when doing transitions.
205  float _thrust_transition = 0.0f; // thrust value applied during a front transition (tailsitter & tiltrotor only)
206 
207  float _ra_hrate = 0.0f; // rolling average on height rate for quadchute condition
208  float _ra_hrate_sp = 0.0f; // rolling average on height rate setpoint for quadchute condition
209 
210  bool _flag_was_in_trans_mode = false; // true if mode has just switched to transition
211 
212  hrt_abstime _trans_finished_ts = 0;
213 
214  bool _tecs_running = false;
215  hrt_abstime _tecs_running_ts = 0;
216 
218 
219 
220 
221  /**
222  * @brief Sets mc motor minimum pwm to VT_IDLE_PWM_MC which ensures
223  * that they are spinning in mc mode.
224  *
225  * @return true on success
226  */
227  bool set_idle_mc();
228 
229  /**
230  * @brief Sets mc motor minimum pwm to PWM_MIN which ensures that the
231  * motors stop spinning on zero throttle in fw mode.
232  *
233  * @return true on success
234  */
235  bool set_idle_fw();
236 
237 
238  /**
239  * @brief Sets state of a selection of motors, see struct motor_state
240  *
241  * @param[in] current_state The current motor state
242  * @param[in] next_state The next state
243  * @param[in] value Desired pwm value if next_state =
244  * motor_state::VALUE
245  *
246  * @return next_state if succesfull, otherwise current_state
247  */
248  motor_state set_motor_state(const motor_state current_state, const motor_state next_state, const int value = 0);
249 
250 private:
251 
252 
253  /**
254  * @brief Stores the max pwm values given by the system.
255  */
259 
260  /**
261  * @brief Adjust minimum/maximum pwm values for the output channels.
262  *
263  * @param pwm_output_values Struct containing the limit values for each channel
264  * @param[in] type Specifies if min or max limits are adjusted.
265  *
266  * @return True on success.
267  */
268  bool apply_pwm_limits(struct pwm_output_values &pwm_values, pwm_limit_type type);
269 
270  /**
271  * @brief Determines if channel is set in target.
272  *
273  * @param[in] channel The channel
274  * @param[in] target The target to check on.
275  *
276  * @return True if motor off channel, False otherwise.
277  */
278  bool is_channel_set(const int channel, const int target);
279 
280 };
281 
282 #endif
float front_trans_timeout
Definition: vtol_type.h:68
struct vehicle_local_position_setpoint_s * _local_pos_sp
Definition: vtol_type.h:189
float fw_qc_max_pitch
Definition: vtol_type.h:57
int32_t diff_thrust
Definition: vtol_type.h:71
float fw_alt_err
Definition: vtol_type.h:56
float airspeed_blend
Definition: vtol_type.h:66
int32_t fw_motors_off
bitmask of all motors that should be off in fixed wing mode
Definition: vtol_type.h:70
float mpc_xy_cruise
Definition: vtol_type.h:69
struct actuator_controls_s * _actuators_mc_in
Definition: vtol_type.h:186
struct tecs_status_s * _tecs_status
Definition: vtol_type.h:191
struct vehicle_attitude_s * _v_att
Definition: vtol_type.h:178
float fw_min_alt
Definition: vtol_type.h:55
mode _vtol_mode
Definition: vtol_type.h:174
vtol_type
Definition: vtol_type.h:83
High-resolution timer with callouts and timekeeping.
VtolAttitudeControl * _attc
Definition: vtol_type.h:173
struct Params * _params
Definition: vtol_type.h:194
motor_state
Definition: vtol_type.h:93
struct actuator_controls_s * _actuators_out_0
Definition: vtol_type.h:184
static void parameters_update()
update all parameters
void init()
Activates/configures the hardware registers.
struct vehicle_local_position_s * _local_pos
Definition: vtol_type.h:188
float diff_thrust_scale
Definition: vtol_type.h:72
struct actuator_controls_s * _actuators_fw_in
Definition: vtol_type.h:187
struct vehicle_attitude_setpoint_s * _v_att_sp
Definition: vtol_type.h:179
pwm_limit_type
Used to specify if min or max pwm values should be altered.
Definition: vtol_type.h:103
struct vehicle_attitude_setpoint_s * _mc_virtual_att_sp
Definition: vtol_type.h:180
struct vehicle_attitude_setpoint_s * _fw_virtual_att_sp
Definition: vtol_type.h:181
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
Definition: drv_hrt.h:58
float back_trans_duration
Definition: vtol_type.h:62
struct vehicle_control_mode_s * _v_control_mode
Definition: vtol_type.h:182
float transition_airspeed
Definition: vtol_type.h:63
float front_trans_time_openloop
Definition: vtol_type.h:59
float back_trans_throttle
Definition: vtol_type.h:65
struct airspeed_validated_s * _airspeed_validated
Definition: vtol_type.h:190
float fw_qc_max_roll
Definition: vtol_type.h:58
bool elevons_mc_lock
Definition: vtol_type.h:54
int32_t vtol_type
Definition: vtol_type.h:53
struct actuator_controls_s * _actuators_out_1
Definition: vtol_type.h:185
bool airspeed_disabled
Definition: vtol_type.h:67
Stores the max pwm values given by the system.
Definition: vtol_type.h:256
struct vehicle_land_detected_s * _land_detected
Definition: vtol_type.h:192
float front_trans_time_min
Definition: vtol_type.h:60
mode
Definition: vtol_type.h:76
float front_trans_duration
Definition: vtol_type.h:61
struct vtol_vehicle_status_s * _vtol_vehicle_status
Definition: vtol_type.h:183
int32_t vtol_motor_id
Definition: vtol_type.h:52
mode get_mode()
Definition: vtol_type.h:168
int32_t idle_pwm_mc
Definition: vtol_type.h:51
float front_trans_throttle
Definition: vtol_type.h:64
virtual void waiting_on_tecs()
Special handling opportunity for the time right after transition to FW before TECS is running...
Definition: vtol_type.h:154