Skip to content

Commit 8a4779f

Browse files
committed
fix: initialize batt voltage mult to 1 to avoid divide by zero
1 parent 24d074a commit 8a4779f

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

src/mixer.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ void Mixer::param_change_callback(uint16_t param_id)
112112
} else switch (param_id) {
113113
case PARAM_PRIMARY_MIXER:
114114
case PARAM_SECONDARY_MIXER:
115+
case PARAM_USE_MOTOR_PARAMETERS:
116+
case PARAM_RC_F_AXIS:
115117
init_mixing();
116118
break;
117119
case PARAM_MOTOR_RESISTANCE: R_ = RF_.params_.get_param_float(PARAM_MOTOR_RESISTANCE); break;
@@ -454,7 +456,7 @@ float Mixer::mix_multirotor_without_motor_parameters(Controller::Output commands
454456
float Mixer::mix_multirotor_with_motor_parameters(Controller::Output commands)
455457
{
456458
// Mix the inputs
457-
float max_output = 1.0;
459+
float max_output{1.0};
458460

459461
float rho = RF_.sensors_.rho();
460462

@@ -467,17 +469,29 @@ float Mixer::mix_multirotor_with_motor_parameters(Controller::Output commands)
467469
commands.Qx * (*mixer_to_use_.Qx)[i] +
468470
commands.Qy * (*mixer_to_use_.Qy)[i] +
469471
commands.Qz * (*mixer_to_use_.Qz)[i];
470-
472+
471473
// Ensure that omega_squared is non-negative
472474
if (omega_squared < 0.0) { omega_squared = 0.0; }
473475

474476
// Ch. 4, setting equation for torque produced by a propeller equal to Eq. 4.19
475477
// Note that we assume constant advance ratio, leading to constant torque and thrust constants.
478+
if (K_Q_ < 0.0000001) {
479+
RF_.comm_manager_.log(CommLinkInterface::LogSeverity::LOG_ERROR,
480+
"Divide by K_Q, which is zero! Skipping");
481+
return max_output;
482+
}
476483
float V_in = rho * pow(D_, 5.0) / (4.0 * pow(M_PI, 2.0)) * omega_squared * C_Q_ * R_ / K_Q_
477484
+ R_ * i_0_ + K_V_ * sqrt(omega_squared);
478485

479486
// Convert desired V_in setting to a throttle setting
480487
BatteryStruct * batt = RF_.sensors_.get_battery();
488+
if (batt->voltage < 0.0001) {
489+
RF_.comm_manager_.log(CommLinkInterface::LogSeverity::LOG_ERROR,
490+
"Divide by battery voltage, which is near zero!");
491+
RF_.comm_manager_.log(CommLinkInterface::LogSeverity::LOG_ERROR,
492+
"Is battery multiplier set correctly?");
493+
return max_output;
494+
}
481495
outputs_[i] = V_in / batt->voltage;
482496

483497
// Save off the largest control output (for motors) if it is greater than 1.0 for future scaling
@@ -527,7 +541,7 @@ void Mixer::mix_multirotor()
527541
}
528542

529543
// Mix the outputs based on if a custom mixer (i.e. with motor parameters) is selected.
530-
float max_output;
544+
float max_output{0.0};
531545
if (RF_.params_.get_param_int(PARAM_USE_MOTOR_PARAMETERS)) {
532546
max_output = mix_multirotor_with_motor_parameters(commands);
533547
} else {

src/param.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ void Params::set_defaults(void)
426426
/*****************************/
427427
/*** BATTERY MONITOR SETUP ***/
428428
/*****************************/
429-
init_param_float(PARAM_BATTERY_VOLTAGE_MULTIPLIER, "BATT_VOLT_MULT", 0.0f); // Multiplier for the voltage sensor | 0 | inf
430-
init_param_float(PARAM_BATTERY_CURRENT_MULTIPLIER, "BATT_CURR_MULT", 0.0f); // Multiplier for the current sensor | 0 | inf
429+
init_param_float(PARAM_BATTERY_VOLTAGE_MULTIPLIER, "BATT_VOLT_MULT", 1.0f); // Multiplier for the voltage sensor | 0 | inf
430+
init_param_float(PARAM_BATTERY_CURRENT_MULTIPLIER, "BATT_CURR_MULT", 1.0f); // Multiplier for the current sensor | 0 | inf
431431
init_param_float(PARAM_BATTERY_VOLTAGE_ALPHA, "BATT_VOLT_ALPHA", 0.995f); // Alpha value for the low pass filter on the reported battery voltage | 0 | 1
432432
init_param_float(PARAM_BATTERY_CURRENT_ALPHA, "BATT_CURR_ALPHA", 0.995f); // Alpha value for the low pass filter on the reported battery current | 0 | 1
433433

test/parameters_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ TEST(Parameters, DefaultParameters)
217217
EXPECT_PARAM_EQ_FLOAT(PARAM_FC_ROLL, 0.0f); // roll angle (deg) of flight controller wrt aircraft body | 0 | 360
218218
EXPECT_PARAM_EQ_FLOAT(PARAM_FC_PITCH, 0.0f); // pitch angle (deg) of flight controller wrt aircraft body | 0 | 360
219219
EXPECT_PARAM_EQ_FLOAT(PARAM_FC_YAW, 0.0f); // yaw angle (deg) of flight controller wrt aircraft body | 0 | 360
220-
EXPECT_PARAM_EQ_FLOAT(PARAM_BATTERY_VOLTAGE_MULTIPLIER, 0.0f); // Multiplier for the voltage sensor | 0 | inf
221-
EXPECT_PARAM_EQ_FLOAT(PARAM_BATTERY_CURRENT_MULTIPLIER, 0.0f); // Multiplier for the current sensor | 0 | inf
220+
EXPECT_PARAM_EQ_FLOAT(PARAM_BATTERY_VOLTAGE_MULTIPLIER, 1.0f); // Multiplier for the voltage sensor | 0 | inf
221+
EXPECT_PARAM_EQ_FLOAT(PARAM_BATTERY_CURRENT_MULTIPLIER, 1.0f); // Multiplier for the current sensor | 0 | inf
222222
EXPECT_PARAM_EQ_INT(PARAM_OFFBOARD_TIMEOUT, 100); // Timeout in milliseconds for offboard commands, after which RC override is activated | 0 | 100000
223223
}

0 commit comments

Comments
 (0)