Merge pull request #1982 from betaflight/looptime_guard

Guard against too fast looptimes unsupported by motor pwm type
This commit is contained in:
borisbstyle 2017-01-03 23:29:25 +01:00 committed by GitHub
commit 4ed266bc5e
2 changed files with 39 additions and 1 deletions

View File

@ -18,6 +18,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "platform.h"
@ -1040,9 +1041,46 @@ void validateAndFixGyroConfig(void)
gyroConfig()->gyro_soft_notch_hz_2 = 0;
}
float samplingTime = 0.000125f;
if (gyroConfig()->gyro_lpf != GYRO_LPF_256HZ && gyroConfig()->gyro_lpf != GYRO_LPF_NONE) {
pidConfig()->pid_process_denom = 1; // When gyro set to 1khz always set pid speed 1:1 to sampling speed
gyroConfig()->gyro_sync_denom = 1;
samplingTime = 0.001f;
}
// check for looptime restrictions based on motor protocol. Motor times have safety margin
const float pidLooptime = samplingTime * gyroConfig()->gyro_sync_denom * pidConfig()->pid_process_denom;
float motorUpdateRestriction;
switch(motorConfig()->motorPwmProtocol) {
case (PWM_TYPE_STANDARD):
motorUpdateRestriction = 0.002f;
break;
case (PWM_TYPE_ONESHOT125):
motorUpdateRestriction = 0.0005f;
break;
case (PWM_TYPE_ONESHOT42):
motorUpdateRestriction = 0.0001f;
break;
case (PWM_TYPE_DSHOT150):
motorUpdateRestriction = 0.000125f;
break;
case (PWM_TYPE_DSHOT300):
motorUpdateRestriction = 0.0000625f;
break;
default:
motorUpdateRestriction = 0.00003125f;
}
if(pidLooptime < motorUpdateRestriction)
pidConfig()->pid_process_denom = motorUpdateRestriction / (samplingTime * gyroConfig()->gyro_sync_denom);
// Prevent overriding the max rate of motors
if(motorConfig()->useUnsyncedPwm) {
uint32_t maxEscRate = lrintf(1.0f / motorUpdateRestriction);
if(motorConfig()->motorPwmRate > maxEscRate)
motorConfig()->motorPwmRate = maxEscRate;
}
}

View File

@ -691,7 +691,7 @@ const clivalue_t valueTable[] = {
{ "accum_threshold", VAR_UINT16 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.rollPitchItermIgnoreRate, .config.minmax = {15, 1000 } },
{ "yaw_accum_threshold", VAR_UINT16 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.yawItermIgnoreRate, .config.minmax = {15, 1000 } },
{ "yaw_lowpass", VAR_UINT16 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.yaw_lpf_hz, .config.minmax = {0, 500 } },
{ "pid_process_denom", VAR_UINT8 | MASTER_VALUE, &pidConfig()->pid_process_denom, .config.minmax = { 1, 8 } },
{ "pid_process_denom", VAR_UINT8 | MASTER_VALUE, &pidConfig()->pid_process_denom, .config.minmax = { 1, 16 } },
{ "p_pitch", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.P8[PITCH], .config.minmax = { 0, 200 } },
{ "i_pitch", VAR_UINT8 | PROFILE_VALUE, &masterConfig.profile[0].pidProfile.I8[PITCH], .config.minmax = { 0, 200 } },