2016-04-16 07:26:43 -03:00
// -*- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*-
# include "Plane.h"
2016-05-08 02:57:22 -03:00
/*
the vehicle class has its own var table for TUNE_PARAM so it can
have separate parameter docs for the list of available parameters
*/
const AP_Param : : GroupInfo AP_Tuning_Plane : : var_info [ ] = {
// @Param: PARAM
// @DisplayName: Transmitter tuning parameter or set of parameters
// @Description: This sets which parameter or set of parameters will be tuned. Values greater than 100 indicate a set of parameters rather than a single parameter. Parameters less than 50 are for QuadPlane vertical lift motors only.
2016-05-08 05:45:42 -03:00
// @Values: 0:None,1:RateRollPI,2:RateRollP,3:RateRollI,4:RateRollD,5:RatePitchPI,6:RatePitchP,7:RatePitchI,8:RatePitchD,9:RateYawPI,10:RateYawP,11:RateYawI,12:RateYawD,13:AngleRollP,14:AnglePitchP,15:AngleYawP,16:PosXYP,17:PosZP,18:VelXYP,19:VelXYI,20:VelZP,21:AccelZP,22:AccelZI,23:AccelZD,50:FixedWingRollP,51:FixedWingRollI,52:FixedWingRollD,53:FixedWingRollFF,54:FixedWingPitchP,55:FixedWingPitchI,56:FixedWingPitchD,57:FixedWingPitchFF,101:Set_RateRollPitch,102:Set_RateRoll,103:Set_RatePitch,104:Set_RateYaw,105:Set_AngleRollPitch,106:Set_VelXY,107:Set_AccelZ
2016-05-08 02:57:22 -03:00
// @User: Standard
AP_GROUPINFO ( " PARAM " , 1 , AP_Tuning_Plane , parmset , 0 ) ,
// the rest of the parameters are from AP_Tuning
AP_NESTEDGROUPINFO ( AP_Tuning , 0 ) ,
AP_GROUPEND
} ;
2016-05-06 01:43:39 -03:00
/*
tables of tuning sets
*/
2016-05-08 01:35:53 -03:00
const uint8_t AP_Tuning_Plane : : tuning_set_rate_roll_pitch [ ] = { TUNING_RATE_ROLL_D , TUNING_RATE_ROLL_PI ,
TUNING_RATE_PITCH_D , TUNING_RATE_PITCH_PI } ;
const uint8_t AP_Tuning_Plane : : tuning_set_rate_roll [ ] = { TUNING_RATE_ROLL_D , TUNING_RATE_ROLL_PI } ;
const uint8_t AP_Tuning_Plane : : tuning_set_rate_pitch [ ] = { TUNING_RATE_PITCH_D , TUNING_RATE_PITCH_PI } ;
2016-05-08 05:45:42 -03:00
const uint8_t AP_Tuning_Plane : : tuning_set_rate_yaw [ ] = { TUNING_RATE_YAW_P , TUNING_RATE_YAW_I , TUNING_RATE_YAW_D } ;
const uint8_t AP_Tuning_Plane : : tuning_set_ang_roll_pitch [ ] = { TUNING_ANG_ROLL_P , TUNING_ANG_PITCH_P } ;
const uint8_t AP_Tuning_Plane : : tuning_set_vxy [ ] = { TUNING_VXY_P , TUNING_VXY_I } ;
const uint8_t AP_Tuning_Plane : : tuning_set_az [ ] = { TUNING_AZ_P , TUNING_AZ_I , TUNING_AZ_D } ;
2016-05-06 01:43:39 -03:00
// macro to prevent getting the array length wrong
# define TUNING_ARRAY(v) ARRAY_SIZE(v), v
// list of tuning sets
const AP_Tuning_Plane : : tuning_set AP_Tuning_Plane : : tuning_sets [ ] = {
2016-05-08 01:35:53 -03:00
{ TUNING_SET_RATE_ROLL_PITCH , TUNING_ARRAY ( tuning_set_rate_roll_pitch ) } ,
{ TUNING_SET_RATE_ROLL , TUNING_ARRAY ( tuning_set_rate_roll ) } ,
{ TUNING_SET_RATE_PITCH , TUNING_ARRAY ( tuning_set_rate_pitch ) } ,
2016-06-04 06:20:31 -03:00
{ TUNING_SET_RATE_YAW , TUNING_ARRAY ( tuning_set_rate_yaw ) } ,
{ TUNING_SET_ANG_ROLL_PITCH , TUNING_ARRAY ( tuning_set_ang_roll_pitch ) } ,
{ TUNING_SET_VXY , TUNING_ARRAY ( tuning_set_vxy ) } ,
{ TUNING_SET_AZ , TUNING_ARRAY ( tuning_set_az ) } ,
2016-05-08 01:35:53 -03:00
{ 0 , 0 , nullptr }
2016-04-16 07:26:43 -03:00
} ;
/*
2016-05-06 01:43:39 -03:00
table of tuning names
2016-04-16 07:26:43 -03:00
*/
2016-05-06 01:43:39 -03:00
const AP_Tuning_Plane : : tuning_name AP_Tuning_Plane : : tuning_names [ ] = {
2016-05-08 01:35:53 -03:00
{ TUNING_RATE_ROLL_PI , " RateRollPI " } ,
{ TUNING_RATE_ROLL_P , " RateRollP " } ,
{ TUNING_RATE_ROLL_I , " RateRollI " } ,
{ TUNING_RATE_ROLL_D , " RateRollD " } ,
{ TUNING_RATE_PITCH_PI , " RatePitchPI " } ,
{ TUNING_RATE_PITCH_P , " RatePitchP " } ,
{ TUNING_RATE_PITCH_I , " RatePitchI " } ,
{ TUNING_RATE_PITCH_D , " RatePitchD " } ,
{ TUNING_RATE_YAW_PI , " RateYawPI " } ,
{ TUNING_RATE_YAW_P , " RateYawP " } ,
{ TUNING_RATE_YAW_I , " RateYawI " } ,
{ TUNING_RATE_YAW_D , " RateYawD " } ,
{ TUNING_ANG_ROLL_P , " AngRollP " } ,
{ TUNING_ANG_PITCH_P , " AngPitchP " } ,
{ TUNING_ANG_YAW_P , " AngYawP " } ,
{ TUNING_PXY_P , " PXY_P " } ,
{ TUNING_PZ_P , " PZ_P " } ,
{ TUNING_VXY_P , " VXY_P " } ,
{ TUNING_VXY_I , " VXY_I " } ,
{ TUNING_VZ_P , " VZ_P " } ,
{ TUNING_AZ_P , " RateAZ_P " } ,
{ TUNING_AZ_I , " RateAZ_I " } ,
{ TUNING_AZ_D , " RateAZ_D " } ,
{ TUNING_RLL_P , " RollP " } ,
{ TUNING_RLL_I , " RollI " } ,
{ TUNING_RLL_D , " RollD " } ,
{ TUNING_RLL_FF , " RollFF " } ,
{ TUNING_PIT_P , " PitchP " } ,
{ TUNING_PIT_I , " PitchI " } ,
{ TUNING_PIT_D , " PitchD " } ,
{ TUNING_PIT_FF , " PitchFF " } ,
2016-05-06 01:43:39 -03:00
{ TUNING_NONE , nullptr }
} ;
2016-04-16 07:26:43 -03:00
/*
2016-05-06 01:43:39 -03:00
get a pointer to an AP_Float for a parameter , or NULL on fail
2016-04-16 07:26:43 -03:00
*/
2016-05-06 01:43:39 -03:00
AP_Float * AP_Tuning_Plane : : get_param_pointer ( uint8_t parm )
2016-04-16 07:26:43 -03:00
{
2016-05-08 01:35:53 -03:00
if ( parm < TUNING_FIXED_WING_BASE & & ! plane . quadplane . available ( ) ) {
2016-04-16 07:26:43 -03:00
// quadplane tuning options not available
2016-05-06 01:43:39 -03:00
return nullptr ;
2016-04-16 07:26:43 -03:00
}
2016-04-16 07:38:00 -03:00
2016-05-06 01:43:39 -03:00
switch ( parm ) {
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_ROLL_PI :
2016-05-06 01:43:39 -03:00
// use P for initial value when tuning PI
return & plane . quadplane . attitude_control - > get_rate_roll_pid ( ) . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_ROLL_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_roll_pid ( ) . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_ROLL_I :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_roll_pid ( ) . kI ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_ROLL_D :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_roll_pid ( ) . kD ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_PITCH_PI :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_pitch_pid ( ) . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_PITCH_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_pitch_pid ( ) . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_PITCH_I :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_pitch_pid ( ) . kI ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_PITCH_D :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_pitch_pid ( ) . kD ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_YAW_PI :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_yaw_pid ( ) . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_YAW_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_yaw_pid ( ) . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_YAW_I :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_yaw_pid ( ) . kI ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_RATE_YAW_D :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_rate_yaw_pid ( ) . kD ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_ANG_ROLL_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_angle_roll_p ( ) . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_ANG_PITCH_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_angle_pitch_p ( ) . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_ANG_YAW_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . attitude_control - > get_angle_yaw_p ( ) . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_PXY_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . p_pos_xy . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_PZ_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . p_alt_hold . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_VXY_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . pi_vel_xy . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_VXY_I :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . pi_vel_xy . kI ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_VZ_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . p_vel_z . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_AZ_P :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . pid_accel_z . kP ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_AZ_I :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . pid_accel_z . kI ( ) ;
2016-04-16 07:26:43 -03:00
2016-05-08 01:35:53 -03:00
case TUNING_AZ_D :
2016-05-06 01:43:39 -03:00
return & plane . quadplane . pid_accel_z . kD ( ) ;
2016-05-08 01:35:53 -03:00
// fixed wing tuning parameters
case TUNING_RLL_P :
return & plane . rollController . kP ( ) ;
case TUNING_RLL_I :
return & plane . rollController . kI ( ) ;
case TUNING_RLL_D :
return & plane . rollController . kD ( ) ;
case TUNING_RLL_FF :
return & plane . rollController . kFF ( ) ;
case TUNING_PIT_P :
return & plane . pitchController . kP ( ) ;
case TUNING_PIT_I :
return & plane . pitchController . kI ( ) ;
case TUNING_PIT_D :
return & plane . pitchController . kD ( ) ;
case TUNING_PIT_FF :
return & plane . pitchController . kFF ( ) ;
2016-04-16 07:26:43 -03:00
}
2016-05-06 01:43:39 -03:00
return nullptr ;
2016-04-16 07:26:43 -03:00
}
2016-05-06 01:43:39 -03:00
2016-04-16 07:26:43 -03:00
/*
2016-05-06 01:43:39 -03:00
save a parameter
2016-04-16 07:26:43 -03:00
*/
2016-05-06 01:43:39 -03:00
void AP_Tuning_Plane : : save_value ( uint8_t parm )
2016-04-16 07:26:43 -03:00
{
2016-05-06 01:43:39 -03:00
switch ( parm ) {
// special handling of dual-parameters
2016-05-08 01:35:53 -03:00
case TUNING_RATE_ROLL_PI :
save_value ( TUNING_RATE_ROLL_P ) ;
save_value ( TUNING_RATE_ROLL_I ) ;
2016-05-06 01:43:39 -03:00
break ;
2016-05-08 01:35:53 -03:00
case TUNING_RATE_PITCH_PI :
save_value ( TUNING_RATE_PITCH_P ) ;
save_value ( TUNING_RATE_PITCH_I ) ;
2016-05-06 01:43:39 -03:00
break ;
default :
AP_Float * f = get_param_pointer ( parm ) ;
if ( f ! = nullptr ) {
f - > save ( ) ;
}
break ;
}
}
/*
set a parameter
*/
void AP_Tuning_Plane : : set_value ( uint8_t parm , float value )
{
switch ( parm ) {
// special handling of dual-parameters
2016-05-08 01:35:53 -03:00
case TUNING_RATE_ROLL_PI :
set_value ( TUNING_RATE_ROLL_P , value ) ;
set_value ( TUNING_RATE_ROLL_I , value ) ;
2016-05-06 01:43:39 -03:00
break ;
2016-05-08 01:35:53 -03:00
case TUNING_RATE_PITCH_PI :
set_value ( TUNING_RATE_PITCH_P , value ) ;
set_value ( TUNING_RATE_PITCH_I , value ) ;
2016-05-06 01:43:39 -03:00
break ;
default :
AP_Float * f = get_param_pointer ( parm ) ;
if ( f ! = nullptr ) {
2016-10-13 08:08:13 -03:00
uint64_t param_bit = ( 1ULL < < parm ) ;
if ( ! ( param_bit & have_set ) ) {
// first time this param has been set by tuning. We
// need to see if a reversion value is available in
// FRAM, and if not then save one
float current_value = f - > get ( ) ;
if ( ! f - > load ( ) ) {
// there is no value in FRAM, set one
f - > set_and_save ( current_value ) ;
}
have_set | = param_bit ;
}
2016-05-06 01:43:39 -03:00
f - > set_and_notify ( value ) ;
}
break ;
}
}
/*
reload a parameter
*/
void AP_Tuning_Plane : : reload_value ( uint8_t parm )
{
switch ( parm ) {
// special handling of dual-parameters
2016-05-08 01:35:53 -03:00
case TUNING_RATE_ROLL_PI :
reload_value ( TUNING_RATE_ROLL_P ) ;
reload_value ( TUNING_RATE_ROLL_I ) ;
2016-05-06 01:43:39 -03:00
break ;
2016-05-08 01:35:53 -03:00
case TUNING_RATE_PITCH_PI :
reload_value ( TUNING_RATE_PITCH_P ) ;
reload_value ( TUNING_RATE_PITCH_I ) ;
2016-05-06 01:43:39 -03:00
break ;
default :
AP_Float * f = get_param_pointer ( parm ) ;
if ( f ! = nullptr ) {
2016-10-13 18:34:54 -03:00
uint64_t param_bit = ( 1ULL < < parm ) ;
// only reload if we have set this parameter at some point
if ( param_bit & have_set ) {
f - > load ( ) ;
}
2016-05-06 01:43:39 -03:00
}
break ;
}
2016-04-16 07:26:43 -03:00
}
2016-05-27 01:16:44 -03:00
/*
return current controller error
*/
float AP_Tuning_Plane : : controller_error ( uint8_t parm )
{
2016-06-01 04:19:33 -03:00
if ( ! plane . quadplane . available ( ) ) {
return 0 ;
}
2016-06-09 03:31:00 -03:00
// in general a good tune will have rmsP significantly greater
// than rmsD. Otherwise it is too easy to push D too high while
// tuning a quadplane and end up with D dominating
const float max_P_D_ratio = 3.0f ;
2016-07-06 01:41:27 -03:00
if ( plane . quadplane . motors - > get_throttle ( ) < 0.1f ) {
// don't report stale errors if not running VTOL motors
return 0 ;
}
2016-05-27 01:16:44 -03:00
switch ( parm ) {
// special handling of dual-parameters
case TUNING_RATE_ROLL_PI :
case TUNING_RATE_ROLL_P :
case TUNING_RATE_ROLL_I :
2016-06-01 04:19:33 -03:00
return plane . quadplane . attitude_control - > control_monitor_rms_output_roll ( ) ;
2016-06-09 03:31:00 -03:00
case TUNING_RATE_ROLL_D : {
// special case for D term to keep it well below P
float rms_P = plane . quadplane . attitude_control - > control_monitor_rms_output_roll_P ( ) ;
float rms_D = plane . quadplane . attitude_control - > control_monitor_rms_output_roll_D ( ) ;
if ( rms_P < rms_D * max_P_D_ratio ) {
return max_P_D_ratio ;
}
return rms_P + rms_D ;
}
2016-06-01 04:19:33 -03:00
2016-05-27 01:16:44 -03:00
case TUNING_RATE_PITCH_PI :
case TUNING_RATE_PITCH_P :
case TUNING_RATE_PITCH_I :
2016-06-01 04:19:33 -03:00
return plane . quadplane . attitude_control - > control_monitor_rms_output_pitch ( ) ;
2016-06-09 03:31:00 -03:00
case TUNING_RATE_PITCH_D : {
// special case for D term to keep it well below P
float rms_P = plane . quadplane . attitude_control - > control_monitor_rms_output_pitch_P ( ) ;
float rms_D = plane . quadplane . attitude_control - > control_monitor_rms_output_pitch_D ( ) ;
if ( rms_P < rms_D * max_P_D_ratio ) {
return max_P_D_ratio ;
}
return rms_P + rms_D ;
}
2016-05-27 01:16:44 -03:00
case TUNING_RATE_YAW_PI :
case TUNING_RATE_YAW_P :
case TUNING_RATE_YAW_I :
case TUNING_RATE_YAW_D :
2016-06-01 04:19:33 -03:00
return plane . quadplane . attitude_control - > control_monitor_rms_output_yaw ( ) ;
2016-05-27 01:16:44 -03:00
default :
// no special handler
return 0 ;
}
}