2017-02-11 01:50:03 -04:00
/*
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
/*
control code for tailsitters . Enabled by setting Q_FRAME_CLASS = 10
2019-01-17 13:54:15 -04:00
or by setting Q_TAILSIT_MOTMX nonzero and Q_FRAME_CLASS and Q_FRAME_TYPE
to a configuration supported by AP_MotorsMatrix
2017-02-11 01:50:03 -04:00
*/
2021-07-14 17:15:25 -03:00
# include "tailsitter.h"
2017-02-11 01:50:03 -04:00
# include "Plane.h"
2021-09-10 03:28:21 -03:00
# if HAL_QUADPLANE_ENABLED
2021-07-14 17:15:25 -03:00
const AP_Param : : GroupInfo Tailsitter : : var_info [ ] = {
// @Param: ENABLE
// @DisplayName: Enable Tailsitter
2021-08-31 19:37:58 -03:00
// @Values: 0:Disable, 1:Enable, 2:Enable Always
2023-10-11 04:41:54 -03:00
// @Description: This enables Tailsitter functionality. A value of 2 forces Qassist active and always stabilize in forward flight with airmode for stabilisation at 0 throttle, for use on vehicles with no control surfaces, vehicle will not arm in forward flight modes, see also Q_OPTIONS "Mtrs_Only_Qassist"
2021-07-14 17:15:25 -03:00
// @User: Standard
// @RebootRequired: True
AP_GROUPINFO_FLAGS ( " ENABLE " , 1 , Tailsitter , enable , 0 , AP_PARAM_FLAG_ENABLE ) ,
// @Param: ANGLE
// @DisplayName: Tailsitter fixed wing transition angle
// @Description: This is the pitch angle at which tailsitter aircraft will change from VTOL control to fixed wing control.
// @Units: deg
// @Range: 5 80
AP_GROUPINFO ( " ANGLE " , 2 , Tailsitter , transition_angle_fw , 45 ) ,
// @Param: ANG_VT
// @DisplayName: Tailsitter VTOL transition angle
// @Description: This is the pitch angle at which tailsitter aircraft will change from fixed wing control to VTOL control, if zero Q_TAILSIT_ANGLE will be used
// @Units: deg
// @Range: 5 80
AP_GROUPINFO ( " ANG_VT " , 3 , Tailsitter , transition_angle_vtol , 0 ) ,
// @Param: INPUT
// @DisplayName: Tailsitter input type bitmask
// @Description: This controls whether stick input when hovering as a tailsitter follows the conventions for fixed wing hovering or multicopter hovering. When PlaneMode is not enabled (bit0 = 0) the roll stick will roll the aircraft in earth frame and yaw stick will yaw in earth frame. When PlaneMode input is enabled, the roll and yaw sticks are swapped so that the roll stick controls earth-frame yaw and rudder controls earth-frame roll. When body-frame roll is enabled (bit1 = 1), the yaw stick controls earth-frame yaw rate and the roll stick controls roll in the tailsitter's body frame when flying level.
// @Bitmask: 0:PlaneMode,1:BodyFrameRoll
AP_GROUPINFO ( " INPUT " , 4 , Tailsitter , input_type , 0 ) ,
2021-08-23 20:22:34 -03:00
// 5 was MASK
// 6 was MASKCH
2021-07-14 17:15:25 -03:00
// @Param: VFGAIN
// @DisplayName: Tailsitter vector thrust gain in forward flight
// @Description: This controls the amount of vectored thrust control used in forward flight for a vectored tailsitter
// @Range: 0 1
// @Increment: 0.01
AP_GROUPINFO ( " VFGAIN " , 7 , Tailsitter , vectored_forward_gain , 0 ) ,
// @Param: VHGAIN
// @DisplayName: Tailsitter vector thrust gain in hover
// @Description: This controls the amount of vectored thrust control used in hover for a vectored tailsitter
// @Range: 0 1
// @Increment: 0.01
AP_GROUPINFO ( " VHGAIN " , 8 , Tailsitter , vectored_hover_gain , 0.5 ) ,
// @Param: VHPOW
// @DisplayName: Tailsitter vector thrust gain power
// @Description: This controls the amount of extra pitch given to the vectored control when at high pitch errors
// @Range: 0 4
// @Increment: 0.1
AP_GROUPINFO ( " VHPOW " , 9 , Tailsitter , vectored_hover_power , 2.5 ) ,
// @Param: GSCMAX
// @DisplayName: Maximum tailsitter gain scaling
// @Description: Maximum gain scaling for tailsitter Q_TAILSIT_GSCMSK options
// @Range: 1 5
// @User: Standard
AP_GROUPINFO ( " GSCMAX " , 10 , Tailsitter , throttle_scale_max , 2 ) ,
// @Param: RLL_MX
// @DisplayName: Maximum Roll angle
// @Description: Maximum Allowed roll angle for tailsitters. If this is zero then Q_ANGLE_MAX is used.
// @Units: deg
// @Range: 0 80
// @User: Standard
AP_GROUPINFO ( " RLL_MX " , 11 , Tailsitter , max_roll_angle , 0 ) ,
// @Param: MOTMX
// @DisplayName: Tailsitter motor mask
// @Description: Bitmask of motors to remain active in forward flight for a 'Copter' tailsitter. Non-zero indicates airframe is a Copter tailsitter and uses copter style motor layouts determined by Q_FRAME_CLASS and Q_FRAME_TYPE. This should be zero for non-Copter tailsitters.
// @User: Standard
2022-11-25 14:40:50 -04:00
// @Bitmask: 0:Motor 1, 1:Motor 2, 2:Motor 3, 3:Motor 4, 4:Motor 5, 5:Motor 6, 6:Motor 7, 7:Motor 8, 8:Motor 9, 9:Motor 10, 10:Motor 11, 11:Motor 12
2021-07-14 17:15:25 -03:00
AP_GROUPINFO ( " MOTMX " , 12 , Tailsitter , motor_mask , 0 ) ,
// @Param: GSCMSK
// @DisplayName: Tailsitter gain scaling mask
// @Description: Bitmask of gain scaling methods to be applied: Throttle: scale gains with throttle, ATT_THR: reduce gain at high throttle/tilt, 2:Disk theory velocity calculation, requires Q_TAILSIT_DSKLD to be set, ATT_THR must not be set, 3:Altitude correction, scale with air density
// @User: Standard
// @Bitmask: 0:Throttle,1:ATT_THR,2:Disk Theory,3:Altitude correction
AP_GROUPINFO ( " GSCMSK " , 13 , Tailsitter , gain_scaling_mask , TAILSITTER_GSCL_THROTTLE ) ,
// @Param: GSCMIN
// @DisplayName: Minimum tailsitter gain scaling
// @Description: Minimum gain scaling for tailsitter Q_TAILSIT_GSCMSK options
// @Range: 0.1 1
// @User: Standard
AP_GROUPINFO ( " GSCMIN " , 14 , Tailsitter , gain_scaling_min , 0.4 ) ,
// @Param: DSKLD
// @DisplayName: Tailsitter disk loading
// @Description: This is the vehicle weight in kg divided by the total disk area of all propellers in m^2. Only used with Q_TAILSIT_GSCMSK = 4
// @Units: kg/m/m
// @Range: 0 50
// @User: Standard
AP_GROUPINFO ( " DSKLD " , 15 , Tailsitter , disk_loading , 0 ) ,
// @Param: RAT_FW
// @DisplayName: Tailsitter VTOL to forward flight transition rate
// @Description: The pitch rate at which tailsitter aircraft will pitch down in the transition from VTOL to forward flight
// @Units: deg/s
// @Range: 10 500
AP_GROUPINFO ( " RAT_FW " , 16 , Tailsitter , transition_rate_fw , 50 ) ,
// @Param: RAT_VT
// @DisplayName: Tailsitter forward flight to VTOL transition rate
// @Description: The pitch rate at which tailsitter aircraft will pitch up in the transition from forward flight to VTOL
// @Units: deg/s
// @Range: 10 500
AP_GROUPINFO ( " RAT_VT " , 17 , Tailsitter , transition_rate_vtol , 50 ) ,
2021-08-21 14:01:57 -03:00
// @Param: THR_VT
// @DisplayName: Tailsitter forward flight to VTOL transition throttle
// @Description: Throttle used during FW->VTOL transition, -1 uses hover throttle
// @Units: %
// @Range: -1 100
AP_GROUPINFO ( " THR_VT " , 18 , Tailsitter , transition_throttle_vtol , - 1 ) ,
2021-12-08 16:16:06 -04:00
// @Param: VT_R_P
// @DisplayName: Tailsitter VTOL control surface roll gain
// @Description: Scale from PID output to control surface, for use where a single axis is actuated by both motors and Tilt/control surface on a copter style tailsitter, increase to favor control surfaces and reduce motor output by reducing gains
// @Range: 0 2
AP_GROUPINFO ( " VT_R_P " , 19 , Tailsitter , VTOL_roll_scale , 1 ) ,
// @Param: VT_P_P
// @DisplayName: Tailsitter VTOL control surface pitch gain
// @Description: Scale from PID output to control surface, for use where a single axis is actuated by both motors and Tilt/control surface on a copter style tailsitter, increase to favor control surfaces and reduce motor output by reducing gains
// @Range: 0 2
AP_GROUPINFO ( " VT_P_P " , 20 , Tailsitter , VTOL_pitch_scale , 1 ) ,
// @Param: VT_Y_P
// @DisplayName: Tailsitter VTOL control surface yaw gain
// @Description: Scale from PID output to control surface, for use where a single axis is actuated by both motors and Tilt/control surface on a copter style tailsitter, increase to favor control surfaces and reduce motor output by reducing gains
// @Range: 0 2
AP_GROUPINFO ( " VT_Y_P " , 21 , Tailsitter , VTOL_yaw_scale , 1 ) ,
2021-11-14 15:15:35 -04:00
// @Param: MIN_VO
// @DisplayName: Tailsitter Disk loading minimum outflow speed
2023-10-11 04:41:54 -03:00
// @Description: Use in conjunction with disk theory gain scaling and Q_TAILSIT_DSKLD to specify minumum airspeed over control surfaces, this will be used to boost throttle, when descending for example, 0 disables
2021-11-14 15:15:35 -04:00
// @Range: 0 15
AP_GROUPINFO ( " MIN_VO " , 22 , Tailsitter , disk_loading_min_outflow , 0 ) ,
2021-07-14 17:15:25 -03:00
AP_GROUPEND
} ;
2021-08-22 06:25:41 -03:00
/*
defaults for tailsitters
*/
static const struct AP_Param : : defaults_table_struct defaults_table_tailsitter [ ] = {
{ " KFF_RDDRMIX " , 0.02 } ,
{ " Q_A_RAT_PIT_FF " , 0.2 } ,
{ " Q_A_RAT_YAW_FF " , 0.2 } ,
{ " Q_A_RAT_YAW_I " , 0.18 } ,
{ " Q_A_ANGLE_BOOST " , 0 } ,
2024-01-18 21:50:36 -04:00
{ " PTCH_LIM_MAX_DEG " , 30 } ,
{ " PTCH_LIM_MIN_DEG " , - 30 } ,
2021-08-22 06:25:41 -03:00
{ " MIXING_GAIN " , 1.0 } ,
{ " RUDD_DT_GAIN " , 10 } ,
{ " Q_TRANSITION_MS " , 2000 } ,
{ " Q_TRANS_DECEL " , 6 } ,
2021-09-02 17:59:03 -03:00
{ " Q_A_ACCEL_P_MAX " , 30000 } ,
{ " Q_A_ACCEL_R_MAX " , 30000 } ,
{ " Q_P_POSXY_P " , 0.5 } ,
{ " Q_P_VELXY_P " , 1.0 } ,
{ " Q_P_VELXY_I " , 0.5 } ,
{ " Q_P_VELXY_D " , 0.25 } ,
2021-08-22 06:25:41 -03:00
} ;
2021-07-14 17:15:25 -03:00
Tailsitter : : Tailsitter ( QuadPlane & _quadplane , AP_MotorsMulticopter * & _motors ) : quadplane ( _quadplane ) , motors ( _motors )
2017-02-11 01:50:03 -04:00
{
2021-07-14 17:15:25 -03:00
AP_Param : : setup_object_defaults ( this , var_info ) ;
2017-02-11 01:50:03 -04:00
}
2021-08-22 06:25:41 -03:00
void Tailsitter : : setup ( )
{
// Set tailsitter enable flag based on old heuristics
2021-09-04 19:55:25 -03:00
if ( ! enable . configured ( ) & & ( ( ( quadplane . frame_class = = AP_Motors : : MOTOR_FRAME_TAILSITTER ) | | ( motor_mask ! = 0 ) ) & & ( quadplane . tiltrotor . type ! = Tiltrotor : : TILT_TYPE_BICOPTER ) ) ) {
2021-08-22 06:25:41 -03:00
enable . set_and_save ( 1 ) ;
}
2021-09-18 16:48:51 -03:00
if ( enable < = 0 ) {
2021-08-22 06:25:41 -03:00
return ;
}
2024-08-01 11:58:11 -03:00
quadplane . thrust_type = QuadPlane : : ThrustType : : TAILSITTER ;
2021-08-22 06:25:41 -03:00
// Set tailsitter transition rate to match old calculation
if ( ! transition_rate_fw . configured ( ) ) {
transition_rate_fw . set_and_save ( transition_angle_fw / ( quadplane . transition_time_ms / 2000.0f ) ) ;
}
// TODO: update this if servo function assignments change
// used by relax_attitude_control() to control special behavior for vectored tailsitters
_is_vectored = ( quadplane . frame_class = = AP_Motors : : MOTOR_FRAME_TAILSITTER ) & &
( ! is_zero ( vectored_hover_gain ) & &
( SRV_Channels : : function_assigned ( SRV_Channel : : k_tiltMotorLeft ) | |
SRV_Channels : : function_assigned ( SRV_Channel : : k_tiltMotorRight ) ) ) ;
2021-11-14 15:22:55 -04:00
_have_elevator = SRV_Channels : : function_assigned ( SRV_Channel : : k_elevator ) ;
_have_aileron = SRV_Channels : : function_assigned ( SRV_Channel : : k_aileron ) ;
_have_rudder = SRV_Channels : : function_assigned ( SRV_Channel : : k_rudder ) ;
2023-01-12 20:09:21 -04:00
_have_elevon = SRV_Channels : : function_assigned ( SRV_Channel : : k_elevon_left ) | | SRV_Channels : : function_assigned ( SRV_Channel : : k_elevon_right ) ;
_have_v_tail = SRV_Channels : : function_assigned ( SRV_Channel : : k_vtail_left ) | | SRV_Channels : : function_assigned ( SRV_Channel : : k_vtail_right ) ;
2021-11-14 15:22:55 -04:00
2021-08-22 06:25:41 -03:00
// set defaults for dual/single motor tailsitter
if ( quadplane . frame_class = = AP_Motors : : MOTOR_FRAME_TAILSITTER ) {
AP_Param : : set_defaults_from_table ( defaults_table_tailsitter , ARRAY_SIZE ( defaults_table_tailsitter ) ) ;
}
2021-08-28 14:52:09 -03:00
// Setup for control surface less operation
if ( enable = = 2 ) {
2024-03-07 19:20:36 -04:00
quadplane . assist . set_state ( VTOL_Assist : : STATE : : FORCE_ENABLED ) ;
2021-08-28 14:52:09 -03:00
quadplane . air_mode = AirMode : : ASSISTED_FLIGHT_ONLY ;
// Do not allow arming in forward flight modes
// motors will become active due to assisted flight airmode, the vehicle will try very hard to get level
2022-08-05 12:08:59 -03:00
quadplane . options . set ( quadplane . options . get ( ) | int32_t ( QuadPlane : : OPTION : : ONLY_ARM_IN_QMODE_OR_AUTO ) ) ;
2021-08-28 14:52:09 -03:00
}
2024-05-26 22:24:16 -03:00
transition = NEW_NOTHROW Tailsitter_Transition ( quadplane , motors , * this ) ;
2021-09-17 20:27:51 -03:00
if ( ! transition ) {
2021-09-18 16:48:51 -03:00
AP_BoardConfig : : allocation_error ( " tailsitter transition " ) ;
2021-09-17 20:27:51 -03:00
}
quadplane . transition = transition ;
setup_complete = true ;
2021-08-22 06:25:41 -03:00
}
2020-01-02 15:07:09 -04:00
/*
2020-09-02 23:45:52 -03:00
return true when flying a control surface only tailsitter
2020-01-02 15:07:09 -04:00
*/
2021-07-14 17:15:25 -03:00
bool Tailsitter : : is_control_surface_tailsitter ( void ) const
2020-01-02 15:07:09 -04:00
{
2021-07-14 17:15:25 -03:00
return quadplane . frame_class = = AP_Motors : : MOTOR_FRAME_TAILSITTER
& & ( is_zero ( vectored_hover_gain ) | | ! SRV_Channels : : function_assigned ( SRV_Channel : : k_tiltMotorLeft ) ) ;
2020-01-02 15:07:09 -04:00
}
2017-02-11 04:12:56 -04:00
/*
check if we are flying as a tailsitter
*/
2021-07-14 17:15:25 -03:00
bool Tailsitter : : active ( void )
2017-02-11 04:12:56 -04:00
{
2021-07-14 17:15:25 -03:00
if ( ! enabled ( ) ) {
2017-10-29 18:47:04 -03:00
return false ;
}
2021-07-14 17:15:25 -03:00
if ( quadplane . in_vtol_mode ( ) ) {
2017-10-29 18:47:04 -03:00
return true ;
}
2017-10-30 01:19:38 -03:00
// check if we are in ANGLE_WAIT fixed wing transition
2021-09-17 20:27:51 -03:00
if ( transition - > transition_state = = Tailsitter_Transition : : TRANSITION_ANGLE_WAIT_FW ) {
2017-10-29 18:47:04 -03:00
return true ;
}
return false ;
2017-02-11 04:12:56 -04:00
}
2017-02-11 01:50:03 -04:00
/*
run output for tailsitters
*/
2021-07-14 17:15:25 -03:00
void Tailsitter : : output ( void )
2017-02-11 01:50:03 -04:00
{
2021-07-14 17:15:25 -03:00
if ( ! enabled ( ) | | quadplane . motor_test . running | | ! quadplane . initialised ) {
2020-01-19 12:46:00 -04:00
// if motor test is running we don't want to overwrite it with output_motor_mask or motors_output
2017-04-22 22:35:25 -03:00
return ;
}
2018-10-21 14:53:09 -03:00
float tilt_left = 0.0f ;
float tilt_right = 0.0f ;
2021-09-20 13:56:32 -03:00
// throttle 0 to 1
float throttle = SRV_Channels : : get_output_scaled ( SRV_Channel : : k_throttle ) * 0.01 ;
2020-02-17 20:17:50 -04:00
// handle forward flight modes and transition to VTOL modes
2021-07-14 17:15:25 -03:00
if ( ! active ( ) | | in_vtol_transition ( ) ) {
2019-01-17 13:54:15 -04:00
// get FW controller throttle demand and mask of motors enabled during forward flight
2023-02-10 22:36:33 -04:00
if ( plane . arming . is_armed_and_safety_off ( ) & & in_vtol_transition ( ) & & ! quadplane . throttle_wait ) {
2020-02-17 20:17:50 -04:00
/*
2021-09-20 13:56:32 -03:00
during transitions to vtol mode set the throttle to hover thrust , center the rudder
2020-02-17 20:17:50 -04:00
*/
2021-08-21 14:01:57 -03:00
if ( ! is_negative ( transition_throttle_vtol ) ) {
// Q_TAILSIT_THR_VT is positive use it until transition is complete
2023-01-30 21:55:04 -04:00
throttle = motors - > thr_lin . actuator_to_thrust ( MIN ( transition_throttle_vtol * 0.01 , 1.0 ) ) ;
2021-08-21 14:01:57 -03:00
} else {
throttle = motors - > get_throttle_hover ( ) ;
// work out equivelent motors throttle level for cruise
2023-01-30 21:55:04 -04:00
throttle = MAX ( throttle , motors - > thr_lin . actuator_to_thrust ( plane . aparm . throttle_cruise . get ( ) * 0.01 ) ) ;
2021-08-21 14:01:57 -03:00
}
2020-11-13 20:28:42 -04:00
2021-09-18 15:02:12 -03:00
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_rudder , 0.0 ) ;
2021-09-20 13:56:32 -03:00
plane . rudder_dt = 0 ;
// in assisted flight this is done in the normal motor output path
if ( ! quadplane . assisted_flight ) {
2022-03-08 10:02:14 -04:00
// keep attitude control throttle level upto date, this value should never be output to motors
// it is used to re-set the accel Z integrator term allowing for a smooth transfer of control
quadplane . attitude_control - > set_throttle_out ( throttle , false , 0 ) ;
2021-09-20 13:56:32 -03:00
// convert the hover throttle to the same output that would result if used via AP_Motors
// apply expo, battery scaling and SPIN min/max.
2023-01-30 21:55:04 -04:00
throttle = motors - > thr_lin . thrust_to_actuator ( throttle ) ;
2020-02-17 20:17:50 -04:00
2021-09-20 13:56:32 -03:00
// override AP_MotorsTailsitter throttles during back transition
2020-11-13 20:28:42 -04:00
2021-09-20 13:56:32 -03:00
// apply PWM min and MAX to throttle left and right, just as via AP_Motors
uint16_t throttle_pwm = motors - > get_pwm_output_min ( ) + ( motors - > get_pwm_output_max ( ) - motors - > get_pwm_output_min ( ) ) * throttle ;
SRV_Channels : : set_output_pwm ( SRV_Channel : : k_throttleLeft , throttle_pwm ) ;
SRV_Channels : : set_output_pwm ( SRV_Channel : : k_throttleRight , throttle_pwm ) ;
2020-11-13 20:28:42 -04:00
2023-10-11 04:41:54 -03:00
// throttle output is not used by AP_Motors so might have different PWM range, set scaled
2021-09-20 13:56:32 -03:00
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_throttle , throttle * 100.0 ) ;
}
2020-02-17 20:17:50 -04:00
}
2021-07-14 17:15:25 -03:00
if ( ! quadplane . assisted_flight ) {
2020-02-17 20:17:50 -04:00
// set AP_MotorsMatrix throttles for forward flight
2021-09-20 13:56:32 -03:00
motors - > output_motor_mask ( throttle , motor_mask , plane . rudder_dt ) ;
2020-02-17 20:17:50 -04:00
// in forward flight: set motor tilt servos and throttles using FW controller
2021-07-14 17:15:25 -03:00
if ( vectored_forward_gain > 0 ) {
2021-01-03 16:43:53 -04:00
// remove scaling from surface speed scaling and apply throttle scaling
2021-09-04 19:55:25 -03:00
const float scaler = plane . control_mode = = & plane . mode_manual ? 1 : ( quadplane . FW_vector_throttle_scaling ( ) / plane . get_speed_scaler ( ) ) ;
2020-02-17 20:17:50 -04:00
// thrust vectoring in fixed wing flight
float aileron = SRV_Channels : : get_output_scaled ( SRV_Channel : : k_aileron ) ;
float elevator = SRV_Channels : : get_output_scaled ( SRV_Channel : : k_elevator ) ;
2021-07-14 17:15:25 -03:00
tilt_left = ( elevator + aileron ) * vectored_forward_gain * scaler ;
tilt_right = ( elevator - aileron ) * vectored_forward_gain * scaler ;
2019-01-17 13:54:15 -04:00
}
2020-02-17 20:17:50 -04:00
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_tiltMotorLeft , tilt_left ) ;
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_tiltMotorRight , tilt_right ) ;
return ;
2017-10-30 01:19:38 -03:00
}
2017-02-24 03:02:34 -04:00
}
2019-03-10 18:11:07 -03:00
2020-05-26 13:43:05 -03:00
// handle Copter controller
2019-04-25 16:39:27 -03:00
// the MultiCopter rate controller has already been run in an earlier call
2020-01-02 15:07:09 -04:00
// to motors_output() from quadplane.update(), unless we are in assisted flight
2020-11-16 13:28:31 -04:00
// tailsitter in TRANSITION_ANGLE_WAIT_FW is not really in assisted flight, its still in a VTOL mode
2021-09-17 20:27:51 -03:00
if ( quadplane . assisted_flight & & ( transition - > transition_state ! = Tailsitter_Transition : : TRANSITION_ANGLE_WAIT_FW ) ) {
2021-09-20 13:56:32 -03:00
quadplane . hold_stabilize ( throttle ) ;
2021-07-14 17:15:25 -03:00
quadplane . motors_output ( true ) ;
2020-05-26 13:43:05 -03:00
2022-08-05 12:08:59 -03:00
if ( quadplane . option_is_set ( QuadPlane : : OPTION : : TAILSIT_Q_ASSIST_MOTORS_ONLY ) ) {
2023-01-12 20:09:21 -04:00
// only use motors for Q assist, control surfaces remain under plane control. Zero copter I terms and use plane.
// Smoothly relax to zero so there is no step change in output, must also set limit flags so integrator cannot build faster than the relax.
2023-09-07 10:52:05 -03:00
// Assume there is always roll control surfaces, otherwise motors only assist should not be set.
2023-01-12 20:09:21 -04:00
const float dt = quadplane . attitude_control - > get_dt ( ) ;
2023-09-07 10:52:05 -03:00
// VTOL yaw / FW roll
2023-01-12 20:09:21 -04:00
quadplane . attitude_control - > get_rate_yaw_pid ( ) . relax_integrator ( 0.0 , dt , AC_ATTITUDE_RATE_RELAX_TC ) ;
motors - > limit . yaw = true ;
2023-09-07 10:52:05 -03:00
// VTOL and FW pitch
if ( _have_elevator | | _have_elevon | | _have_v_tail ) {
// have pitch control surfaces, use them
quadplane . attitude_control - > get_rate_pitch_pid ( ) . relax_integrator ( 0.0 , dt , AC_ATTITUDE_RATE_RELAX_TC ) ;
motors - > limit . pitch = true ;
} else {
// no pitch control surfaces, zero plane I terms and use motors
// We skip the outputting to surfaces for this axis from the copter controller but there are none setup
plane . pitchController . reset_I ( ) ;
}
// VTOL roll / FW yaw
2023-01-12 20:09:21 -04:00
if ( _have_rudder | | _have_v_tail ) {
// there are yaw control surfaces, zero motor I term
quadplane . attitude_control - > get_rate_roll_pid ( ) . relax_integrator ( 0.0 , dt , AC_ATTITUDE_RATE_RELAX_TC ) ;
motors - > limit . roll = true ;
} else {
// no yaw control surfaces, zero plane I terms and use motors
2023-09-07 10:52:05 -03:00
// We skip the outputting to surfaces for this axis from the copter controller but there are none setup
2023-01-12 20:09:21 -04:00
plane . yawController . reset_I ( ) ;
}
2020-05-26 13:43:05 -03:00
// output tilt motors
2020-11-09 20:43:07 -04:00
tilt_left = 0.0f ;
tilt_right = 0.0f ;
2021-07-14 17:15:25 -03:00
if ( vectored_hover_gain > 0 ) {
2020-11-06 14:41:02 -04:00
const float hover_throttle = motors - > get_throttle_hover ( ) ;
2021-09-20 13:56:32 -03:00
const float output_throttle = motors - > get_throttle ( ) ;
2021-07-14 17:15:25 -03:00
float throttle_scaler = throttle_scale_max ;
2021-09-20 13:56:32 -03:00
if ( is_positive ( output_throttle ) ) {
throttle_scaler = constrain_float ( hover_throttle / output_throttle , gain_scaling_min , throttle_scale_max ) ;
2020-11-06 14:41:02 -04:00
}
2021-07-14 17:15:25 -03:00
tilt_left = SRV_Channels : : get_output_scaled ( SRV_Channel : : k_tiltMotorLeft ) * vectored_hover_gain * throttle_scaler ;
tilt_right = SRV_Channels : : get_output_scaled ( SRV_Channel : : k_tiltMotorRight ) * vectored_hover_gain * throttle_scaler ;
2020-05-26 13:43:05 -03:00
}
2020-11-09 20:43:07 -04:00
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_tiltMotorLeft , tilt_left ) ;
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_tiltMotorRight , tilt_right ) ;
2020-05-26 13:43:05 -03:00
// skip remainder of the function that overwrites plane control surface outputs with copter
return ;
}
2020-01-02 15:07:09 -04:00
} else {
2021-07-14 17:15:25 -03:00
quadplane . motors_output ( false ) ;
2020-01-02 15:07:09 -04:00
}
2021-08-21 20:13:09 -03:00
// In full Q assist it is better to use copter I and zero plane
2017-02-24 03:02:34 -04:00
plane . pitchController . reset_I ( ) ;
plane . rollController . reset_I ( ) ;
2021-08-21 20:13:09 -03:00
plane . yawController . reset_I ( ) ;
2017-02-24 03:02:34 -04:00
2019-01-31 16:40:19 -04:00
// pull in copter control outputs
2021-12-08 16:16:06 -04:00
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_aileron , ( motors - > get_yaw ( ) + motors - > get_yaw_ff ( ) ) * - SERVO_MAX * VTOL_yaw_scale ) ;
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_elevator , ( motors - > get_pitch ( ) + motors - > get_pitch_ff ( ) ) * SERVO_MAX * VTOL_pitch_scale ) ;
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_rudder , ( motors - > get_roll ( ) + motors - > get_roll_ff ( ) ) * SERVO_MAX * VTOL_roll_scale ) ;
2019-01-31 16:40:19 -04:00
2023-02-10 22:36:33 -04:00
if ( plane . arming . is_armed_and_safety_off ( ) ) {
2017-11-05 02:05:08 -04:00
// scale surfaces for throttle
2021-07-14 17:15:25 -03:00
speed_scaling ( ) ;
2021-11-14 15:15:35 -04:00
} else if ( tailsitter_motors ! = nullptr ) {
tailsitter_motors - > set_min_throttle ( 0.0 ) ;
2017-11-05 02:05:08 -04:00
}
2020-11-09 20:43:07 -04:00
tilt_left = 0.0f ;
tilt_right = 0.0f ;
2021-07-14 17:15:25 -03:00
if ( vectored_hover_gain > 0 ) {
2017-04-09 21:01:58 -03:00
// thrust vectoring VTOL modes
2018-10-21 14:53:09 -03:00
tilt_left = SRV_Channels : : get_output_scaled ( SRV_Channel : : k_tiltMotorLeft ) ;
tilt_right = SRV_Channels : : get_output_scaled ( SRV_Channel : : k_tiltMotorRight ) ;
2017-05-04 18:34:27 -03:00
/*
apply extra elevator when at high pitch errors , using a
power law . This allows the motors to point straight up for
takeoff without integrator windup
*/
2021-07-14 17:15:25 -03:00
float des_pitch_cd = quadplane . attitude_control - > get_att_target_euler_cd ( ) . y ;
int32_t pitch_error_cd = ( des_pitch_cd - quadplane . ahrs_view - > pitch_sensor ) * 0.5 ;
2019-01-31 16:40:19 -04:00
float extra_pitch = constrain_float ( pitch_error_cd , - SERVO_MAX , SERVO_MAX ) / SERVO_MAX ;
2017-05-04 18:34:27 -03:00
float extra_sign = extra_pitch > 0 ? 1 : - 1 ;
2019-04-19 12:12:00 -03:00
float extra_elevator = 0 ;
2021-07-14 17:15:25 -03:00
if ( ! is_zero ( extra_pitch ) & & quadplane . in_vtol_mode ( ) ) {
extra_elevator = extra_sign * powf ( fabsf ( extra_pitch ) , vectored_hover_power ) * SERVO_MAX ;
2019-04-19 12:12:00 -03:00
}
2021-07-14 17:15:25 -03:00
tilt_left = extra_elevator + tilt_left * vectored_hover_gain ;
tilt_right = extra_elevator + tilt_right * vectored_hover_gain ;
2017-04-09 21:01:58 -03:00
}
2020-11-09 20:43:07 -04:00
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_tiltMotorLeft , tilt_left ) ;
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_tiltMotorRight , tilt_right ) ;
2018-10-21 14:53:09 -03:00
2020-11-05 15:02:57 -04:00
// Check for saturated limits
2021-09-18 15:02:12 -03:00
bool tilt_lim = _is_vectored & & ( ( fabsf ( SRV_Channels : : get_output_scaled ( SRV_Channel : : Aux_servo_function_t : : k_tiltMotorLeft ) ) > = SERVO_MAX ) | | ( fabsf ( SRV_Channels : : get_output_scaled ( SRV_Channel : : Aux_servo_function_t : : k_tiltMotorRight ) ) > = SERVO_MAX ) ) ;
2021-11-14 15:22:55 -04:00
bool roll_lim = _have_rudder & & ( fabsf ( SRV_Channels : : get_output_scaled ( SRV_Channel : : Aux_servo_function_t : : k_rudder ) ) > = SERVO_MAX ) ;
bool pitch_lim = _have_elevator & & ( fabsf ( SRV_Channels : : get_output_scaled ( SRV_Channel : : Aux_servo_function_t : : k_elevator ) ) > = SERVO_MAX ) ;
bool yaw_lim = _have_aileron & & ( fabsf ( SRV_Channels : : get_output_scaled ( SRV_Channel : : Aux_servo_function_t : : k_aileron ) ) > = SERVO_MAX ) ;
2020-11-05 15:02:57 -04:00
2021-11-13 19:57:05 -04:00
// Mix elevons and V-tail, always giving full priority to pitch
float elevator_mix = SRV_Channels : : get_output_scaled ( SRV_Channel : : k_elevator ) * ( 100.0 - plane . g . mixing_offset ) * 0.01 * plane . g . mixing_gain ;
float aileron_mix = SRV_Channels : : get_output_scaled ( SRV_Channel : : k_aileron ) * ( 100.0 + plane . g . mixing_offset ) * 0.01 * plane . g . mixing_gain ;
float rudder_mix = SRV_Channels : : get_output_scaled ( SRV_Channel : : k_rudder ) * ( 100.0 + plane . g . mixing_offset ) * 0.01 * plane . g . mixing_gain ;
const float headroom = SERVO_MAX - fabsf ( elevator_mix ) ;
if ( is_positive ( headroom ) ) {
if ( fabsf ( aileron_mix ) > headroom ) {
aileron_mix * = headroom / fabsf ( aileron_mix ) ;
2023-01-12 20:09:21 -04:00
yaw_lim | = _have_elevon ;
2021-11-13 19:57:05 -04:00
}
if ( fabsf ( rudder_mix ) > headroom ) {
rudder_mix * = headroom / fabsf ( rudder_mix ) ;
2023-01-12 20:09:21 -04:00
roll_lim | = _have_v_tail ;
2021-11-13 19:57:05 -04:00
}
} else {
aileron_mix = 0.0 ;
rudder_mix = 0.0 ;
2023-01-12 20:09:21 -04:00
yaw_lim | = _have_elevon ;
pitch_lim | = _have_elevon | | _have_v_tail ;
roll_lim | = _have_v_tail ;
2021-11-13 19:57:05 -04:00
}
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_elevon_left , elevator_mix - aileron_mix ) ;
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_elevon_right , elevator_mix + aileron_mix ) ;
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_vtail_right , elevator_mix - rudder_mix ) ;
SRV_Channels : : set_output_scaled ( SRV_Channel : : k_vtail_left , elevator_mix + rudder_mix ) ;
2020-11-05 15:02:57 -04:00
if ( roll_lim ) {
motors - > limit . roll = true ;
}
if ( pitch_lim | | tilt_lim ) {
motors - > limit . pitch = true ;
}
if ( yaw_lim | | tilt_lim ) {
motors - > limit . yaw = true ;
}
2018-10-21 14:53:09 -03:00
2017-02-11 01:50:03 -04:00
}
2017-02-11 04:12:56 -04:00
2017-02-12 05:58:00 -04:00
/*
return true when we have completed enough of a transition to switch to fixed wing control
*/
2021-07-14 17:15:25 -03:00
bool Tailsitter : : transition_fw_complete ( void )
2017-02-12 05:58:00 -04:00
{
2023-02-10 22:36:33 -04:00
if ( ! plane . arming . is_armed_and_safety_off ( ) ) {
2023-10-11 04:41:54 -03:00
// instant transition when disarmed, no message
2021-03-26 21:00:11 -03:00
return true ;
}
2021-07-14 17:15:25 -03:00
if ( labs ( quadplane . ahrs_view - > pitch_sensor ) > transition_angle_fw * 100 ) {
2021-03-25 17:32:31 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Transition FW done " ) ;
return true ;
}
2021-07-14 17:15:25 -03:00
if ( labs ( quadplane . ahrs_view - > roll_sensor ) > MAX ( 4500 , plane . roll_limit_cd + 500 ) ) {
2021-06-05 08:30:04 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Transition FW done, roll error " ) ;
2021-03-25 17:32:31 -03:00
return true ;
}
2021-11-05 21:12:47 -03:00
if ( AP_HAL : : millis ( ) - transition - > fw_transition_start_ms > ( ( transition_angle_fw + ( transition - > fw_transition_initial_pitch * 0.01f ) ) / transition_rate_fw ) * 1500 ) {
2021-06-05 08:30:04 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Transition FW done, timeout " ) ;
2017-10-30 01:19:38 -03:00
return true ;
}
// still waiting
return false ;
}
/*
return true when we have completed enough of a transition to switch to VTOL control
*/
2021-07-14 17:15:25 -03:00
bool Tailsitter : : transition_vtol_complete ( void ) const
2017-10-30 01:19:38 -03:00
{
2023-02-10 22:36:33 -04:00
if ( ! plane . arming . is_armed_and_safety_off ( ) ) {
2023-10-11 04:41:54 -03:00
// instant transition when disarmed, no message
2021-03-26 21:00:11 -03:00
return true ;
}
2020-12-01 11:10:40 -04:00
// for vectored tailsitters at zero pilot throttle
2021-07-14 17:15:25 -03:00
if ( ( quadplane . get_pilot_throttle ( ) < .05f ) & & _is_vectored ) {
2020-12-01 11:10:40 -04:00
// if we are not moving (hence on the ground?) or don't know
// transition immediately to tilt motors up and prevent prop strikes
2021-07-14 17:15:25 -03:00
if ( quadplane . ahrs . groundspeed ( ) < 1.0f ) {
2021-03-25 17:32:31 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Transition VTOL done, zero throttle " ) ;
2020-12-01 11:10:40 -04:00
return true ;
}
}
2021-07-14 17:15:25 -03:00
const float trans_angle = get_transition_angle_vtol ( ) ;
2021-03-25 17:32:31 -03:00
if ( labs ( plane . ahrs . pitch_sensor ) > trans_angle * 100 ) {
gcs ( ) . send_text ( MAV_SEVERITY_INFO , " Transition VTOL done " ) ;
return true ;
}
2021-04-07 12:22:19 -03:00
int32_t roll_cd = labs ( plane . ahrs . roll_sensor ) ;
if ( plane . fly_inverted ( ) ) {
roll_cd = 18000 - roll_cd ;
}
if ( roll_cd > MAX ( 4500 , plane . roll_limit_cd + 500 ) ) {
2021-06-05 08:30:04 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Transition VTOL done, roll error " ) ;
2021-03-25 17:32:31 -03:00
return true ;
}
2021-11-05 21:12:47 -03:00
if ( AP_HAL : : millis ( ) - transition - > vtol_transition_start_ms > ( ( trans_angle - ( transition - > vtol_transition_initial_pitch * 0.01f ) ) / transition_rate_vtol ) * 1500 ) {
2021-06-05 08:30:04 -03:00
gcs ( ) . send_text ( MAV_SEVERITY_WARNING , " Transition VTOL done, timeout " ) ;
2017-02-12 05:58:00 -04:00
return true ;
}
return false ;
}
2017-02-24 01:47:09 -04:00
// handle different tailsitter input types
2021-07-14 17:15:25 -03:00
void Tailsitter : : check_input ( void )
2017-02-24 01:47:09 -04:00
{
2021-07-14 17:15:25 -03:00
if ( active ( ) & & ( input_type & TAILSITTER_INPUT_PLANE ) ) {
2017-02-24 01:47:09 -04:00
// the user has asked for body frame controls when tailsitter
// is active. We switch around the control_in value for the
// channels to do this, as that ensures the value is
// consistent throughout the code
int16_t roll_in = plane . channel_roll - > get_control_in ( ) ;
int16_t yaw_in = plane . channel_rudder - > get_control_in ( ) ;
plane . channel_roll - > set_control_in ( yaw_in ) ;
plane . channel_rudder - > set_control_in ( - roll_in ) ;
}
}
2017-10-30 01:19:38 -03:00
/*
2019-02-26 00:14:04 -04:00
return true if we are a tailsitter transitioning to VTOL flight
2017-10-30 01:19:38 -03:00
*/
2021-07-14 17:15:25 -03:00
bool Tailsitter : : in_vtol_transition ( uint32_t now ) const
2017-10-30 01:19:38 -03:00
{
2021-07-14 17:15:25 -03:00
if ( ! enabled ( ) | | ! quadplane . in_vtol_mode ( ) ) {
2020-10-30 20:26:00 -03:00
return false ;
}
2021-09-17 20:27:51 -03:00
if ( transition - > transition_state = = Tailsitter_Transition : : TRANSITION_ANGLE_WAIT_VTOL ) {
2020-10-30 20:26:00 -03:00
return true ;
}
2021-09-17 20:27:51 -03:00
if ( ( now ! = 0 ) & & ( ( now - transition - > last_vtol_mode_ms ) > 1000 ) ) {
2020-10-30 20:26:00 -03:00
// only just come out of forward flight
return true ;
}
return false ;
2017-10-30 01:19:38 -03:00
}
2017-11-05 02:05:08 -04:00
2020-10-13 13:59:30 -03:00
/*
return true if we are a tailsitter in FW flight
*/
2021-07-14 17:15:25 -03:00
bool Tailsitter : : is_in_fw_flight ( void ) const
2020-10-13 13:59:30 -03:00
{
2021-09-17 20:27:51 -03:00
return enabled ( ) & & ! quadplane . in_vtol_mode ( ) & & transition - > transition_state = = Tailsitter_Transition : : TRANSITION_DONE ;
2020-10-13 13:59:30 -03:00
}
2021-03-25 17:32:31 -03:00
/*
return the tailsitter . transition_angle_vtol value if non zero , otherwise returns the tailsitter . transition_angle_fw value .
*/
2021-07-14 17:15:25 -03:00
int8_t Tailsitter : : get_transition_angle_vtol ( ) const
2021-03-25 17:32:31 -03:00
{
2021-07-14 17:15:25 -03:00
if ( transition_angle_vtol = = 0 ) {
return transition_angle_fw ;
2021-03-25 17:32:31 -03:00
}
2021-07-14 17:15:25 -03:00
return transition_angle_vtol ;
2021-03-25 17:32:31 -03:00
}
2017-11-05 02:05:08 -04:00
/*
2019-02-26 00:14:04 -04:00
account for speed scaling of control surfaces in VTOL modes
2017-11-05 02:05:08 -04:00
*/
2021-07-14 17:15:25 -03:00
void Tailsitter : : speed_scaling ( void )
2017-11-05 02:05:08 -04:00
{
const float hover_throttle = motors - > get_throttle_hover ( ) ;
2021-11-14 15:15:35 -04:00
const float throttle = motors - > get_throttle_out ( ) ;
2019-04-25 16:39:27 -03:00
float spd_scaler = 1.0f ;
2021-11-14 15:15:35 -04:00
float disk_loading_min_throttle = 0.0 ;
2018-04-15 08:31:53 -03:00
2023-10-11 04:41:54 -03:00
// Scaling with throttle
2021-07-14 17:15:25 -03:00
float throttle_scaler = throttle_scale_max ;
2020-11-03 19:52:08 -04:00
if ( is_positive ( throttle ) ) {
2021-07-14 17:15:25 -03:00
throttle_scaler = constrain_float ( hover_throttle / throttle , gain_scaling_min , throttle_scale_max ) ;
2020-11-03 19:52:08 -04:00
}
2021-07-14 17:15:25 -03:00
if ( ( gain_scaling_mask & TAILSITTER_GSCL_ATT_THR ) ! = 0 ) {
2019-02-26 00:14:04 -04:00
// reduce gains when flying at high speed in Q modes:
// critical parameter: violent oscillations if too high
// sudden loss of attitude control if too low
2021-07-14 17:15:25 -03:00
const float min_scale = gain_scaling_min ;
2019-02-26 00:14:04 -04:00
float tthr = 1.25f * hover_throttle ;
2019-04-25 16:39:27 -03:00
// reduce control surface throws at large tilt
// angles (assuming high airspeed)
// ramp down from 1 to max_atten at tilt angles over trans_angle
// (angles here are represented by their cosines)
// Note that the cosf call will be necessary if trans_angle becomes a parameter
// but the C language spec does not guarantee that trig functions can be used
// in constant expressions, even though gcc currently allows it.
constexpr float c_trans_angle = 0.9238795 ; // cosf(.125f * M_PI)
// alpha = (1 - max_atten) / (c_trans_angle - cosf(radians(90)));
const float alpha = ( 1 - min_scale ) / c_trans_angle ;
const float beta = 1 - alpha * c_trans_angle ;
2021-07-14 17:15:25 -03:00
const float c_tilt = quadplane . ahrs_view - > get_rotation_body_to_ned ( ) . c . z ;
2019-04-25 16:39:27 -03:00
if ( c_tilt < c_trans_angle ) {
spd_scaler = constrain_float ( beta + alpha * c_tilt , min_scale , 1.0f ) ;
// reduce throttle attenuation threshold too
tthr = 0.5f * hover_throttle ;
2019-02-26 00:14:04 -04:00
}
// if throttle is above hover thrust, apply additional attenuation
if ( throttle > tthr ) {
const float throttle_atten = 1 - ( throttle - tthr ) / ( 1 - tthr ) ;
spd_scaler * = throttle_atten ;
2019-04-25 16:39:27 -03:00
spd_scaler = constrain_float ( spd_scaler , min_scale , 1.0f ) ;
2019-02-26 00:14:04 -04:00
}
2019-04-25 16:39:27 -03:00
// limit positive and negative slew rates of applied speed scaling
constexpr float posTC = 2.0f ; // seconds
constexpr float negTC = 1.0f ; // seconds
const float posdelta = plane . G_Dt / posTC ;
const float negdelta = plane . G_Dt / negTC ;
spd_scaler = constrain_float ( spd_scaler , last_spd_scaler - negdelta , last_spd_scaler + posdelta ) ;
last_spd_scaler = spd_scaler ;
2020-11-03 19:52:08 -04:00
// also apply throttle scaling if enabled
2021-07-14 17:15:25 -03:00
if ( ( spd_scaler > = 1.0f ) & & ( ( gain_scaling_mask & TAILSITTER_GSCL_THROTTLE ) ! = 0 ) ) {
2020-11-03 19:52:08 -04:00
spd_scaler = MAX ( throttle_scaler , 1.0f ) ;
}
2021-07-14 17:15:25 -03:00
} else if ( ( ( gain_scaling_mask & TAILSITTER_GSCL_DISK_THEORY ) ! = 0 ) & & is_positive ( disk_loading . get ( ) ) ) {
2020-11-03 19:52:08 -04:00
// Use disk theory to estimate the velocity over the control surfaces
// https://web.mit.edu/16.unified/www/FALL/thermodynamics/notes/node86.html
float airspeed ;
2021-07-14 17:15:25 -03:00
if ( ! quadplane . ahrs . airspeed_estimate ( airspeed ) ) {
2020-11-03 19:52:08 -04:00
// No airspeed estimate, use throttle scaling
spd_scaler = throttle_scaler ;
2019-02-26 00:14:04 -04:00
} else {
2020-11-03 19:52:08 -04:00
// use the equation: T = 0.5 * rho * A (Ue^2 - U0^2) solved for Ue^2:
// Ue^2 = (T / (0.5 * rho *A)) + U0^2
// We don't know thrust or disk area, use T = (throttle/throttle_hover) * weight
// ((t / t_h ) * weight) / (0.5 * rho * A) = ((t / t_h) * mass * 9.81) / (0.5 * rho * A)
// (mass / A) is disk loading DL so:
// Ue^2 = (((t / t_h) * DL * 9.81)/(0.5 * rho)) + U0^2
2024-05-04 00:44:16 -03:00
const float rho = SSL_AIR_DENSITY * quadplane . ahrs . get_air_density_ratio ( ) ;
2020-11-03 19:52:08 -04:00
float hover_rho = rho ;
2021-07-14 17:15:25 -03:00
if ( ( gain_scaling_mask & TAILSITTER_GSCL_ALTITUDE ) ! = 0 ) {
2020-11-03 19:52:08 -04:00
// if applying altitude correction use sea level density for hover case
hover_rho = SSL_AIR_DENSITY ;
}
// hover case: (t / t_h) = 1 and U0 = 0
2021-07-14 17:15:25 -03:00
const float sq_hover_outflow = ( disk_loading . get ( ) * GRAVITY_MSS ) / ( 0.5f * hover_rho ) ;
2020-11-03 19:52:08 -04:00
// calculate the true outflow speed
2021-07-14 17:15:25 -03:00
const float sq_outflow = ( ( ( throttle / hover_throttle ) * disk_loading . get ( ) * GRAVITY_MSS ) / ( 0.5f * rho ) ) + sq ( MAX ( airspeed , 0 ) ) ;
2020-11-03 19:52:08 -04:00
// Scale by the ratio of squared hover outflow velocity to squared actual outflow velocity
2021-07-14 17:15:25 -03:00
spd_scaler = throttle_scale_max ;
2020-11-03 19:52:08 -04:00
if ( is_positive ( sq_outflow ) ) {
2021-07-14 17:15:25 -03:00
spd_scaler = constrain_float ( sq_hover_outflow / sq_outflow , gain_scaling_min . get ( ) , throttle_scale_max . get ( ) ) ;
2020-11-03 19:52:08 -04:00
}
2021-11-14 15:15:35 -04:00
if ( is_positive ( disk_loading_min_outflow ) ) {
// calculate throttle required to give minimum outflow speed over control surfaces
if ( is_positive ( airspeed ) ) {
disk_loading_min_throttle = ( ( ( sq ( disk_loading_min_outflow ) - sq ( airspeed ) ) * ( 0.5 * rho ) ) / ( disk_loading . get ( ) * GRAVITY_MSS ) ) * hover_throttle ;
} else {
// estimate backwards airspeed
float reverse_airspeed = 0.0 ;
Vector3f vel ;
if ( quadplane . ahrs . get_velocity_NED ( vel ) ) {
reverse_airspeed = quadplane . ahrs . earth_to_body ( vel - quadplane . ahrs . wind_estimate ( ) ) . x ;
}
// make sure actually negative
reverse_airspeed = MIN ( reverse_airspeed , 0.0 ) ;
disk_loading_min_throttle = ( ( ( sq ( disk_loading_min_outflow ) + sq ( reverse_airspeed ) ) * ( 0.5 * rho ) ) / ( disk_loading . get ( ) * GRAVITY_MSS ) ) * hover_throttle ;
}
disk_loading_min_throttle = MAX ( disk_loading_min_throttle , 0.0 ) ;
}
2019-02-26 00:14:04 -04:00
}
2020-11-03 19:52:08 -04:00
2021-07-14 17:15:25 -03:00
} else if ( ( gain_scaling_mask & TAILSITTER_GSCL_THROTTLE ) ! = 0 ) {
2020-11-03 19:52:08 -04:00
spd_scaler = throttle_scaler ;
}
2021-07-14 17:15:25 -03:00
if ( ( gain_scaling_mask & TAILSITTER_GSCL_ALTITUDE ) ! = 0 ) {
2020-11-03 19:52:08 -04:00
// air density correction
2024-05-04 00:44:16 -03:00
spd_scaler / = quadplane . ahrs . get_air_density_ratio ( ) ;
2017-11-05 02:05:08 -04:00
}
2018-04-15 08:31:53 -03:00
2019-04-25 16:39:27 -03:00
const SRV_Channel : : Aux_servo_function_t functions [ ] = {
2017-11-05 02:05:08 -04:00
SRV_Channel : : Aux_servo_function_t : : k_aileron ,
2018-10-21 14:53:09 -03:00
SRV_Channel : : Aux_servo_function_t : : k_elevator ,
2020-11-03 19:52:08 -04:00
SRV_Channel : : Aux_servo_function_t : : k_rudder ,
SRV_Channel : : Aux_servo_function_t : : k_tiltMotorLeft ,
SRV_Channel : : Aux_servo_function_t : : k_tiltMotorRight } ;
2017-11-05 02:05:08 -04:00
for ( uint8_t i = 0 ; i < ARRAY_SIZE ( functions ) ; i + + ) {
2021-09-18 15:02:12 -03:00
float v = SRV_Channels : : get_output_scaled ( functions [ i ] ) ;
2020-11-03 19:52:08 -04:00
if ( ( functions [ i ] = = SRV_Channel : : Aux_servo_function_t : : k_tiltMotorLeft ) | | ( functions [ i ] = = SRV_Channel : : Aux_servo_function_t : : k_tiltMotorRight ) ) {
// always apply throttle scaling to tilts
v * = throttle_scaler ;
} else {
v * = spd_scaler ;
}
2017-11-05 02:05:08 -04:00
SRV_Channels : : set_output_scaled ( functions [ i ] , v ) ;
}
2021-11-14 15:15:35 -04:00
if ( tailsitter_motors ! = nullptr ) {
tailsitter_motors - > set_min_throttle ( disk_loading_min_throttle ) ;
}
2023-12-14 21:37:11 -04:00
// Record for log
log_data . throttle_scaler = throttle_scaler ;
log_data . speed_scaler = spd_scaler ;
log_data . min_throttle = disk_loading_min_throttle ;
}
2023-07-13 21:58:09 -03:00
# if HAL_LOGGING_ENABLED
2023-12-14 21:37:11 -04:00
// Write tailsitter specific log
void Tailsitter : : write_log ( )
{
if ( ! enabled ( ) ) {
return ;
}
struct log_tailsitter pkt = {
LOG_PACKET_HEADER_INIT ( LOG_TSIT_MSG ) ,
time_us : AP_HAL : : micros64 ( ) ,
throttle_scaler : log_data . throttle_scaler ,
speed_scaler : log_data . speed_scaler ,
min_throttle : log_data . min_throttle ,
} ;
plane . logger . WriteBlock ( & pkt , sizeof ( pkt ) ) ;
2017-11-05 02:05:08 -04:00
}
2023-07-13 21:58:09 -03:00
# endif // HAL_LOGGING_ENABLED
2021-09-10 03:28:21 -03:00
2021-11-09 22:16:51 -04:00
// return true if pitch control should be relaxed
// on vectored belly sitters the pitch control is not relaxed in order to keep motors pointing and avoid risk of props hitting the ground
// always relax after a transition
bool Tailsitter : : relax_pitch ( )
{
return ! enabled ( ) | | ! _is_vectored | | ( transition - > vtol_limit_start_ms ! = 0 ) ;
}
2021-09-17 20:27:51 -03:00
/*
update for transition from quadplane to fixed wing mode
*/
void Tailsitter_Transition : : update ( )
{
const uint32_t now = millis ( ) ;
float aspeed ;
bool have_airspeed = quadplane . ahrs . airspeed_estimate ( aspeed ) ;
2024-02-24 19:18:19 -04:00
quadplane . assisted_flight = quadplane . assist . should_assist ( aspeed , have_airspeed ) ;
2021-09-17 20:27:51 -03:00
if ( transition_state < TRANSITION_DONE ) {
// during transition we ask TECS to use a synthetic
// airspeed. Otherwise the pitch limits will throw off the
// throttle calculation which is driven by pitch
plane . TECS_controller . use_synthetic_airspeed ( ) ;
}
switch ( transition_state ) {
case TRANSITION_ANGLE_WAIT_FW : {
2021-09-18 16:48:51 -03:00
if ( tailsitter . transition_fw_complete ( ) ) {
transition_state = TRANSITION_DONE ;
2023-02-10 22:36:33 -04:00
if ( plane . arming . is_armed_and_safety_off ( ) ) {
2022-01-23 12:24:02 -04:00
fw_limit_start_ms = now ;
fw_limit_initial_pitch = constrain_float ( quadplane . ahrs . pitch_sensor , - 8500 , 8500 ) ;
plane . nav_pitch_cd = fw_limit_initial_pitch ;
plane . nav_roll_cd = 0 ;
}
2021-09-18 16:48:51 -03:00
break ;
}
2021-09-17 20:27:51 -03:00
quadplane . set_desired_spool_state ( AP_Motors : : DesiredSpoolState : : THROTTLE_UNLIMITED ) ;
quadplane . assisted_flight = true ;
2021-11-05 21:12:47 -03:00
uint32_t dt = now - fw_transition_start_ms ;
2021-09-17 20:27:51 -03:00
// multiply by 0.1 to convert (degrees/second * milliseconds) to centi degrees
2021-11-05 21:12:47 -03:00
plane . nav_pitch_cd = constrain_float ( fw_transition_initial_pitch - ( quadplane . tailsitter . transition_rate_fw * dt ) * 0.1f * ( plane . fly_inverted ( ) ? - 1.0f : 1.0f ) , - 8500 , 8500 ) ;
2021-09-17 20:27:51 -03:00
plane . nav_roll_cd = 0 ;
2022-07-03 19:39:05 -03:00
quadplane . disable_yaw_rate_time_constant ( ) ;
2021-09-17 20:27:51 -03:00
quadplane . attitude_control - > input_euler_angle_roll_pitch_euler_rate_yaw ( plane . nav_roll_cd ,
plane . nav_pitch_cd ,
0 ) ;
// set throttle at either hover throttle or current throttle, whichever is higher, through the transition
quadplane . attitude_control - > set_throttle_out ( MAX ( motors - > get_throttle_hover ( ) , quadplane . attitude_control - > get_throttle_in ( ) ) , true , 0 ) ;
2021-09-18 16:48:51 -03:00
quadplane . motors_output ( ) ;
2021-09-17 20:27:51 -03:00
break ;
}
case TRANSITION_ANGLE_WAIT_VTOL :
// nothing to do, this is handled in the fixed wing attitude controller
2021-09-18 16:48:51 -03:00
break ;
2021-09-17 20:27:51 -03:00
case TRANSITION_DONE :
2021-09-18 16:48:51 -03:00
break ;
2021-09-17 20:27:51 -03:00
}
}
void Tailsitter_Transition : : VTOL_update ( )
{
2021-09-18 16:48:51 -03:00
const uint32_t now = AP_HAL : : millis ( ) ;
2021-09-17 20:27:51 -03:00
2021-09-18 16:48:51 -03:00
if ( ( now - last_vtol_mode_ms ) > 1000 ) {
2021-09-17 20:27:51 -03:00
/*
we are just entering a VTOL mode as a tailsitter , set
our transition state so the fixed wing controller brings
the nose up before we start trying to fly as a
multicopter
*/
transition_state = TRANSITION_ANGLE_WAIT_VTOL ;
}
2021-09-18 16:48:51 -03:00
last_vtol_mode_ms = now ;
if ( transition_state = = TRANSITION_ANGLE_WAIT_VTOL ) {
2021-09-17 20:27:51 -03:00
float aspeed ;
bool have_airspeed = quadplane . ahrs . airspeed_estimate ( aspeed ) ;
2022-06-29 09:35:55 -03:00
// provide assistance in forward flight portion of tailsitter transition
2024-02-24 19:18:19 -04:00
quadplane . assisted_flight = quadplane . assist . should_assist ( aspeed , have_airspeed ) ;
2021-09-18 16:48:51 -03:00
if ( ! quadplane . tailsitter . transition_vtol_complete ( ) ) {
return ;
2021-09-17 20:27:51 -03:00
}
2021-11-09 19:29:17 -04:00
// transition to VTOL complete, if armed set vtol rate limit starting point
2023-02-10 22:36:33 -04:00
if ( plane . arming . is_armed_and_safety_off ( ) ) {
2021-11-09 19:29:17 -04:00
vtol_limit_start_ms = now ;
vtol_limit_initial_pitch = quadplane . ahrs_view - > pitch_sensor ;
}
2024-02-24 19:35:12 -04:00
} else {
// Keep assistance reset while not checking
quadplane . assist . reset ( ) ;
2021-09-17 20:27:51 -03:00
}
2021-09-18 16:48:51 -03:00
restart ( ) ;
2021-09-17 20:27:51 -03:00
}
// return true if we should show VTOL view
bool Tailsitter_Transition : : show_vtol_view ( ) const
{
bool show_vtol = quadplane . in_vtol_mode ( ) ;
if ( show_vtol & & ( transition_state = = TRANSITION_ANGLE_WAIT_VTOL ) ) {
// in a vtol mode but still transitioning from forward flight
return false ;
}
if ( ! show_vtol & & ( transition_state = = TRANSITION_ANGLE_WAIT_FW ) ) {
// not in VTOL mode but still transitioning from VTOL
return true ;
}
return show_vtol ;
}
2023-04-15 21:18:05 -03:00
void Tailsitter_Transition : : set_FW_roll_pitch ( int32_t & nav_pitch_cd , int32_t & nav_roll_cd )
2021-09-17 20:27:51 -03:00
{
uint32_t now = AP_HAL : : millis ( ) ;
if ( tailsitter . in_vtol_transition ( now ) ) {
/*
during transition to vtol in a tailsitter try to raise the
nose while keeping the wings level
*/
2021-11-05 21:12:47 -03:00
uint32_t dt = now - vtol_transition_start_ms ;
2021-09-17 20:27:51 -03:00
// multiply by 0.1 to convert (degrees/second * milliseconds) to centi degrees
2021-11-05 21:12:47 -03:00
nav_pitch_cd = constrain_float ( vtol_transition_initial_pitch + ( tailsitter . transition_rate_vtol * dt ) * 0.1f , - 8500 , 8500 ) ;
2021-09-17 20:27:51 -03:00
nav_roll_cd = 0 ;
2021-11-05 21:12:47 -03:00
} else if ( transition_state = = TRANSITION_DONE ) {
// still in FW, reset transition starting point
2022-01-23 12:24:02 -04:00
vtol_transition_start_ms = now ;
vtol_transition_initial_pitch = constrain_float ( plane . nav_pitch_cd , - 8500 , 8500 ) ;
// rate limit initial pitch down
if ( fw_limit_start_ms ! = 0 ) {
const float pitch_limit_cd = fw_limit_initial_pitch - ( now - fw_limit_start_ms ) * tailsitter . transition_rate_fw * 0.1 ;
if ( ( pitch_limit_cd < = 0 ) | | ( nav_pitch_cd > = pitch_limit_cd ) ) {
// never limit past 0, never limit to a smaller pitch angle
fw_limit_start_ms = 0 ;
} else {
nav_pitch_cd = pitch_limit_cd ;
nav_roll_cd = 0 ;
}
}
2021-09-17 20:27:51 -03:00
}
}
2023-04-15 21:18:05 -03:00
bool Tailsitter_Transition : : allow_stick_mixing ( ) const
{
// Transitioning into VTOL flight, inital pitch up
if ( tailsitter . in_vtol_transition ( ) ) {
return false ;
}
2023-10-11 04:41:54 -03:00
// Transitioning into fixed wing flight, levelling off
2023-04-15 21:18:05 -03:00
if ( ( transition_state = = TRANSITION_DONE ) & & ( fw_limit_start_ms ! = 0 ) ) {
return false ;
}
return true ;
}
2021-11-09 19:29:17 -04:00
bool Tailsitter_Transition : : set_VTOL_roll_pitch_limit ( int32_t & nav_roll_cd , int32_t & nav_pitch_cd )
{
if ( vtol_limit_start_ms = = 0 ) {
return false ;
}
// prevent pitching towards 0 too quickly
const float pitch_change_cd = ( AP_HAL : : millis ( ) - vtol_limit_start_ms ) * tailsitter . transition_rate_vtol * 0.1 ;
if ( pitch_change_cd > fabsf ( vtol_limit_initial_pitch ) ) {
// limit has passed 0, nothing to do
vtol_limit_start_ms = 0 ;
return false ;
}
// continue limiting while limit angle is larger than desired angle
if ( is_negative ( vtol_limit_initial_pitch ) ) {
const float pitch_limit = vtol_limit_initial_pitch + pitch_change_cd ;
if ( nav_pitch_cd > pitch_limit ) {
nav_pitch_cd = pitch_limit ;
nav_roll_cd = 0 ;
return true ;
}
} else {
const float pitch_limit = vtol_limit_initial_pitch - pitch_change_cd ;
if ( nav_pitch_cd < pitch_limit ) {
nav_pitch_cd = pitch_limit ;
nav_roll_cd = 0 ;
return true ;
}
}
vtol_limit_start_ms = 0 ;
return false ;
}
2021-09-18 16:48:51 -03:00
// setup for the transition back to fixed wing
void Tailsitter_Transition : : restart ( )
{
transition_state = TRANSITION_ANGLE_WAIT_FW ;
2021-11-05 21:12:47 -03:00
fw_transition_start_ms = AP_HAL : : millis ( ) ;
fw_transition_initial_pitch = constrain_float ( quadplane . attitude_control - > get_attitude_target_quat ( ) . get_euler_pitch ( ) * degrees ( 100.0 ) , - 8500 , 8500 ) ;
}
// force state to FW and setup for the transition back to VTOL
2022-06-29 09:35:55 -03:00
void Tailsitter_Transition : : force_transition_complete ( )
2021-11-05 21:12:47 -03:00
{
transition_state = TRANSITION_DONE ;
vtol_transition_start_ms = AP_HAL : : millis ( ) ;
vtol_transition_initial_pitch = constrain_float ( plane . nav_pitch_cd , - 8500 , 8500 ) ;
2022-01-23 12:24:02 -04:00
fw_limit_start_ms = 0 ;
2024-02-24 19:35:12 -04:00
quadplane . assist . reset ( ) ;
2021-11-05 21:12:47 -03:00
}
2021-09-18 16:48:51 -03:00
2018-05-23 06:32:35 -03:00
MAV_VTOL_STATE Tailsitter_Transition : : get_mav_vtol_state ( ) const
{
switch ( transition_state ) {
case TRANSITION_ANGLE_WAIT_VTOL :
return MAV_VTOL_STATE_TRANSITION_TO_MC ;
case TRANSITION_DONE :
return MAV_VTOL_STATE_FW ;
case TRANSITION_ANGLE_WAIT_FW : {
if ( quadplane . in_vtol_mode ( ) ) {
return MAV_VTOL_STATE_MC ;
}
return MAV_VTOL_STATE_TRANSITION_TO_FW ;
}
}
return MAV_VTOL_STATE_UNDEFINED ;
}
2021-09-18 16:48:51 -03:00
2022-01-23 18:26:32 -04:00
// only allow to weathervane once transition is complete and desired pitch has been reached
bool Tailsitter_Transition : : allow_weathervane ( )
{
return ! tailsitter . in_vtol_transition ( ) & & ( vtol_limit_start_ms = = 0 ) ;
}
2021-09-10 03:28:21 -03:00
# endif // HAL_QUADPLANE_ENABLED