Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/TPA #5861

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
9a08b77
Rework TPA to have per-component setup and use more stable and intuit…
anton-matosov Nov 15, 2016
40a70f9
Add ZMR250 params
anton-matosov Nov 15, 2016
c4285a0
Remove debug code for RATE_I_MODE
anton-matosov Nov 15, 2016
195b618
Fix formatting
anton-matosov Nov 15, 2016
3a1c689
Remove code that should be in MC-Failsafe branch
anton-matosov Nov 16, 2016
26cf17f
Rename TPA_SLOPE into TPA_RATE and remove unused params
anton-matosov Nov 16, 2016
a4a795c
Merge latest 'master' into feature/TPA
anton-matosov Nov 16, 2016
e127e4a
Merge branch 'master' into feature/TPA
anton-matosov Nov 17, 2016
125cfb1
Merge commit 'b020be13f67838047d1c2532cf66a99ceb28cddf' into feature/TPA
anton-matosov Nov 30, 2016
fb7eb50
Merge branch 'master' into feature/TPA
anton-matosov Dec 1, 2016
7141b0f
Rename to solve conflict
anton-matosov Dec 2, 2016
4df4226
Update to the most recent config
anton-matosov Dec 2, 2016
50ff01d
Merge commit 'fee75c61a18d8c8d6a32406575ed5ede79d01446' into feature/TPA
anton-matosov Dec 2, 2016
2caf71e
Merge commit '64657900de1b306c509aeeb704ef897a00aa4183' into feature/TPA
anton-matosov Dec 3, 2016
3f57f08
Merge branch 'master' into feature/TPA
anton-matosov Dec 5, 2016
3adaf6a
Merge branch 'master' into feature/TPA
anton-matosov Dec 7, 2016
62a9f28
Merge branch 'master' into feature/TPA
anton-matosov Dec 7, 2016
c19e37d
Merge branch 'master' into feature/TPA
anton-matosov Dec 8, 2016
e280259
Merge branch 'master' into feature/TPA
anton-matosov Dec 9, 2016
983b745
Merge branch 'master' into feature/TPA
anton-matosov Dec 10, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ROMFS/px4fmu_common/init.d/4002_qavr5
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ then
param set MC_YAWRATE_I 0.1
param set MC_YAWRATE_D 0.0
param set MC_YAW_FF 0.5
param set MC_TPA_BREAK 0.7
param set MC_TPA_SLOPE 1.0
param set MC_TPA_BREAK_P 0.7
param set MC_TPA_RATE_P 0.3
param set PWM_MIN 1075
param set MPC_THR_MIN 0.06
param set MPC_MANTHR_MIN 0.06
Expand Down
4 changes: 2 additions & 2 deletions ROMFS/px4fmu_common/init.d/4009_qav250
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ then
param set MPC_MANTHR_MIN 0.06
param set CBRK_IO_SAFETY 22027
param set ATT_BIAS_MAX 0.0
param set MC_TPA_BREAK 0.5
param set MC_TPA_SLOPE 1.0
param set MC_TPA_BREAK_P 0.5
param set MC_TPA_RATE_P 0.5
fi
4 changes: 2 additions & 2 deletions ROMFS/px4fmu_common/init.d/4050_generic_250
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ then
param set MC_YAWRATE_MAX 400.0
param set MC_ACRO_R_MAX 360.0
param set MC_ACRO_P_MAX 360.0
param set MC_TPA_BREAK 0.5
param set MC_TPA_SLOPE 1.0
param set MC_TPA_BREAK_P 0.5
param set MC_TPA_RATE_P 0.5


param set PWM_MIN 1075
Expand Down
55 changes: 55 additions & 0 deletions ROMFS/px4fmu_common/init.d/4080_zmr250
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!nsh
#
# @name ZMR250 Racer
#
# @type Quadrotor x
#
# @output AUX1 feed-through of RC AUX1 channel
# @output AUX2 feed-through of RC AUX2 channel
#
# @maintainer Anton Matosov <anton.matosov@gmail.com>
#

sh /etc/init.d/rc.mc_defaults

set MIXER zmr250
set PWM_OUT 1234

if [ $AUTOCNF == yes ]
then
param set MC_ROLL_P 2.0
param set MC_ROLLRATE_P 0.05
param set MC_ROLLRATE_I 0.2
param set MC_ROLLRATE_D 0.0015
param set MC_ROLL_TC 0.18

param set MC_PITCH_P 2.0
param set MC_PITCHRATE_P 0.05
param set MC_PITCHRATE_I 0.2
param set MC_PITCHRATE_D 0.0015
param set MC_PITCH_TC 0.18

param set MC_YAW_P 1.0
param set MC_YAWRATE_P 0.15
param set MC_YAWRATE_I 0.2
param set MC_YAWRATE_D 0.0
param set MC_YAW_FF 0.5

param set MC_ACRO_R_MAX 1080.0
param set MC_ACRO_P_MAX 1080.0
param set MC_ACRO_Y_MAX 1080.0

param set MC_TPA_BREAK_P 0.5
param set MC_TPA_BREAK_D 0.7
param set MC_TPA_RATE_P 0.5
param set MC_TPA_RATE_D 0.5

param set PWM_MIN 1075
param set PWM_RATE 400
param set PWM_DISARMED 900
param set FAILSAFE 100

# param set NAV_RCL_ACT 6 # Lockdown

param set CBRK_IO_SAFETY 22027
fi
10 changes: 10 additions & 0 deletions ROMFS/px4fmu_common/mixers/zmr250.main.mix
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# R: <geometry> <roll scale> <pitch scale> <yaw scale> <deadband>
R: 4x 7654 10000 10000 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anton-matosov I'm talking about the roll_scale parameter (7654) on this line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, never mind my previous post then.
Mixer config is the best way to accommodate custom geometry. ZMR 250 is asymmetric and mixer compensates this.
For ZMR250 mixing for cleanflight/betaflight please refer to https://oscarliang.com/custom-motor-output-mix-quadcopter/


M: 1
O: 10000 10000 0 -10000 10000
S: 0 4 10000 10000 0 -10000 10000

M: 1
O: 10000 10000 0 -10000 10000
S: 0 5 10000 10000 0 -10000 10000
72 changes: 58 additions & 14 deletions src/modules/mc_att_control/mc_att_control_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ extern "C" __EXPORT int mc_att_control_main(int argc, char *argv[]);
#define YAW_DEADZONE 0.05f
#define MIN_TAKEOFF_THRUST 0.2f
#define RATES_I_LIMIT 0.3f
#define TPA_RATE_LOWER_LIMIT 0.05f
#define MANUAL_THROTTLE_MAX_MULTICOPTER 0.9f
#define ATTITUDE_TC_DEFAULT 0.2f

Expand Down Expand Up @@ -188,8 +189,12 @@ class MulticopterAttitudeControl
param_t pitch_rate_i;
param_t pitch_rate_d;
param_t pitch_rate_ff;
param_t tpa_breakpoint;
param_t tpa_slope;
param_t tpa_breakpoint_p;
param_t tpa_breakpoint_i;
param_t tpa_breakpoint_d;
param_t tpa_rate_p;
param_t tpa_rate_i;
param_t tpa_rate_d;
param_t yaw_p;
param_t yaw_rate_p;
param_t yaw_rate_i;
Expand Down Expand Up @@ -224,8 +229,12 @@ class MulticopterAttitudeControl
math::Vector<3> rate_ff; /**< Feedforward gain for desired rates */
float yaw_ff; /**< yaw control feed-forward */

float tpa_breakpoint; /**< Throttle PID Attenuation breakpoint */
float tpa_slope; /**< Throttle PID Attenuation slope */
float tpa_breakpoint_p; /**< Throttle PID Attenuation breakpoint */
float tpa_breakpoint_i; /**< Throttle PID Attenuation breakpoint */
float tpa_breakpoint_d; /**< Throttle PID Attenuation breakpoint */
float tpa_rate_p; /**< Throttle PID Attenuation slope */
float tpa_rate_i; /**< Throttle PID Attenuation slope */
float tpa_rate_d; /**< Throttle PID Attenuation slope */

float roll_rate_max;
float pitch_rate_max;
Expand Down Expand Up @@ -289,6 +298,11 @@ class MulticopterAttitudeControl
*/
void control_attitude_rates(float dt);

/**
* Throttle PID attenuation.
*/
math::Vector<3> pid_attenuations(float tpa_breakpoint, float tpa_rate);

/**
* Check for vehicle status updates.
*/
Expand Down Expand Up @@ -398,8 +412,12 @@ MulticopterAttitudeControl::MulticopterAttitudeControl() :
_params_handles.pitch_rate_i = param_find("MC_PITCHRATE_I");
_params_handles.pitch_rate_d = param_find("MC_PITCHRATE_D");
_params_handles.pitch_rate_ff = param_find("MC_PITCHRATE_FF");
_params_handles.tpa_breakpoint = param_find("MC_TPA_BREAK");
_params_handles.tpa_slope = param_find("MC_TPA_SLOPE");
_params_handles.tpa_breakpoint_p = param_find("MC_TPA_BREAK_P");
_params_handles.tpa_breakpoint_i = param_find("MC_TPA_BREAK_I");
_params_handles.tpa_breakpoint_d = param_find("MC_TPA_BREAK_D");
_params_handles.tpa_rate_p = param_find("MC_TPA_RATE_P");
_params_handles.tpa_rate_i = param_find("MC_TPA_RATE_I");
_params_handles.tpa_rate_d = param_find("MC_TPA_RATE_D");
_params_handles.yaw_p = param_find("MC_YAW_P");
_params_handles.yaw_rate_p = param_find("MC_YAWRATE_P");
_params_handles.yaw_rate_i = param_find("MC_YAWRATE_I");
Expand Down Expand Up @@ -496,10 +514,12 @@ MulticopterAttitudeControl::parameters_update()
param_get(_params_handles.pitch_rate_ff, &v);
_params.rate_ff(1) = v;

param_get(_params_handles.tpa_breakpoint, &v);
_params.tpa_breakpoint = v;
param_get(_params_handles.tpa_slope, &v);
_params.tpa_slope = v;
param_get(_params_handles.tpa_breakpoint_p, &_params.tpa_breakpoint_p);
param_get(_params_handles.tpa_breakpoint_i, &_params.tpa_breakpoint_i);
param_get(_params_handles.tpa_breakpoint_d, &_params.tpa_breakpoint_d);
param_get(_params_handles.tpa_rate_p, &_params.tpa_rate_p);
param_get(_params_handles.tpa_rate_i, &_params.tpa_rate_i);
param_get(_params_handles.tpa_rate_d, &_params.tpa_rate_d);

/* yaw gains */
param_get(_params_handles.yaw_p, &v);
Expand Down Expand Up @@ -790,6 +810,27 @@ MulticopterAttitudeControl::control_attitude(float dt)
}
}

/*
* Throttle PID attenuation
* Function visualization available here https://www.desmos.com/calculator/gn4mfoddje
* Input: 'tpa_breakpoint', 'tpa_rate', '_thrust_sp'
* Output: 'pidAttenuationPerAxis' vector
*/
math::Vector<3>
MulticopterAttitudeControl::pid_attenuations(float tpa_breakpoint, float tpa_rate)
{
/* throttle pid attenuation factor */
float tpa = 1.0f - tpa_rate * (fabsf(_v_rates_sp.thrust) - tpa_breakpoint) / (1.0f - tpa_breakpoint);
tpa = fmaxf(TPA_RATE_LOWER_LIMIT, fminf(1.0f, tpa));

math::Vector<3> pidAttenuationPerAxis;
pidAttenuationPerAxis(AXIS_INDEX_ROLL) = tpa;
pidAttenuationPerAxis(AXIS_INDEX_PITCH) = tpa;
pidAttenuationPerAxis(AXIS_INDEX_YAW) = 1.0;

return pidAttenuationPerAxis;
}

/*
* Attitude rates controller.
* Input: '_rates_sp' vector, '_thrust_sp'
Expand All @@ -809,13 +850,16 @@ MulticopterAttitudeControl::control_attitude_rates(float dt)
rates(1) = _ctrl_state.pitch_rate;
rates(2) = _ctrl_state.yaw_rate;

/* throttle pid attenuation factor */
float tpa = fmaxf(0.0f, fminf(1.0f, 1.0f - _params.tpa_slope * (fabsf(_v_rates_sp.thrust) - _params.tpa_breakpoint)));
math::Vector<3> rates_p_scaled = _params.rate_p.emult(pid_attenuations(_params.tpa_breakpoint_p, _params.tpa_rate_p));
math::Vector<3> rates_i_scaled = _params.rate_i.emult(pid_attenuations(_params.tpa_breakpoint_i, _params.tpa_rate_i));
math::Vector<3> rates_d_scaled = _params.rate_d.emult(pid_attenuations(_params.tpa_breakpoint_d, _params.tpa_rate_d));

/* angular rates error */
math::Vector<3> rates_err = _rates_sp - rates;

_att_control = _params.rate_p.emult(rates_err * tpa) + _params.rate_d.emult(_rates_prev - rates) / dt + _rates_int +
_att_control = rates_p_scaled.emult(rates_err) +
_rates_int +
rates_d_scaled.emult(_rates_prev - rates) / dt +
_params.rate_ff.emult(_rates_sp);

_rates_sp_prev = _rates_sp;
Expand All @@ -825,7 +869,7 @@ MulticopterAttitudeControl::control_attitude_rates(float dt)
if (_thrust_sp > MIN_TAKEOFF_THRUST && !_motor_limits.lower_limit && !_motor_limits.upper_limit) {
for (int i = AXIS_INDEX_ROLL; i < AXIS_COUNT; i++) {
if (fabsf(_att_control(i)) < _thrust_sp) {
float rate_i = _rates_int(i) + _params.rate_i(i) * rates_err(i) * dt;
float rate_i = _rates_int(i) + rates_i_scaled(i) * rates_err(i) * dt;

if (PX4_ISFINITE(rate_i) && rate_i > -RATES_I_LIMIT && rate_i < RATES_I_LIMIT &&
_att_control(i) > -RATES_I_LIMIT && _att_control(i) < RATES_I_LIMIT &&
Expand Down
78 changes: 70 additions & 8 deletions src/modules/mc_att_control/mc_att_control_params.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ PARAM_DEFINE_FLOAT(MC_PITCH_TC, 0.2f);
*
* Roll proportional gain, i.e. desired angular speed in rad/s for error 1 rad.
*
* @unit 1/s
* @min 0.0
* @max 8
* @decimal 2
Expand Down Expand Up @@ -138,7 +139,7 @@ PARAM_DEFINE_FLOAT(MC_ROLLRATE_FF, 0.0f);
* @min 0.0
* @max 10
* @decimal 2
* @increment 0.0005
* @increment 0.1
* @group Multicopter Attitude Control
*/
PARAM_DEFINE_FLOAT(MC_PITCH_P, 6.5f);
Expand Down Expand Up @@ -377,8 +378,9 @@ PARAM_DEFINE_FLOAT(MC_ACRO_Y_MAX, 360.0f);
PARAM_DEFINE_FLOAT(MC_RATT_TH, 1.0f);

/**
* Threshold for Throttle PID Attenuation (TPA)
* TPA P Breakpoint
*
* Throttle PID Attenuation (TPA)
* Magnitude of throttle setpoint at which to begin attenuating roll/pitch P gain
*
* @min 0.0
Expand All @@ -387,23 +389,83 @@ PARAM_DEFINE_FLOAT(MC_RATT_TH, 1.0f);
* @increment 0.1
* @group Multicopter Attitude Control
*/
PARAM_DEFINE_FLOAT(MC_TPA_BREAK, 1.0f);
PARAM_DEFINE_FLOAT(MC_TPA_BREAK_P, 1.0f);

/**
* TPA I Breakpoint
*
* Throttle PID Attenuation (TPA)
* Magnitude of throttle setpoint at which to begin attenuating roll/pitch I gain
*
* @min 0.0
* @max 1.0
* @decimal 2
* @increment 0.1
* @group Multicopter Attitude Control
*/
PARAM_DEFINE_FLOAT(MC_TPA_BREAK_I, 1.0f);

/**
* TPA D Breakpoint
*
* Throttle PID Attenuation (TPA)
* Magnitude of throttle setpoint at which to begin attenuating roll/pitch D gain
*
* @min 0.0
* @max 1.0
* @decimal 2
* @increment 0.1
* @group Multicopter Attitude Control
*/
PARAM_DEFINE_FLOAT(MC_TPA_BREAK_D, 1.0f);

/**
* Slope for Throttle PID Attenuation (TPA)
* TPA Rate P
*
* Throttle PID Attenuation (TPA)
* Rate at which to attenuate roll/pitch P gain
* Attenuation factor is 1.0 when throttle magnitude is below the setpoint
* Above the setpoint, the attenuation factor is (1 - slope*(abs(throttle)-breakpoint))
* Above the setpoint, the attenuation factor is (1 - rate * (throttle - breakpoint) / (1.0 - breakpoint))
*
* @min 0.0
* @max 2.0
* @max 1.0
* @decimal 2
* @increment 0.1
* @increment 0.05
* @group Multicopter Attitude Control
*/
PARAM_DEFINE_FLOAT(MC_TPA_RATE_P, 0.0f);

/**
* TPA Rate I
*
* Throttle PID Attenuation (TPA)
* Rate at which to attenuate roll/pitch I gain
* Attenuation factor is 1.0 when throttle magnitude is below the setpoint
* Above the setpoint, the attenuation factor is (1 - rate * (throttle - breakpoint) / (1.0 - breakpoint))
*
* @min 0.0
* @max 1.0
* @decimal 2
* @increment 0.05
* @group Multicopter Attitude Control
*/
PARAM_DEFINE_FLOAT(MC_TPA_SLOPE, 1.0f);
PARAM_DEFINE_FLOAT(MC_TPA_RATE_I, 0.0f);

/**
* TPA Rate D
*
* Throttle PID Attenuation (TPA)
* Rate at which to attenuate roll/pitch D gain
* Attenuation factor is 1.0 when throttle magnitude is below the setpoint
* Above the setpoint, the attenuation factor is (1 - rate * (throttle - breakpoint) / (1.0 - breakpoint))
*
* @min 0.0
* @max 1.0
* @decimal 2
* @increment 0.05
* @group Multicopter Attitude Control
*/
PARAM_DEFINE_FLOAT(MC_TPA_RATE_D, 0.0f);

/**
* Whether to scale outputs by battery power level
Expand Down