Skip to content

Commit 611202c

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

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/mixer.cpp

Lines changed: 24 additions & 8 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;
@@ -429,7 +431,7 @@ void Mixer::set_new_aux_command(aux_command_t new_aux_command)
429431
float Mixer::mix_multirotor_without_motor_parameters(Controller::Output commands)
430432
{
431433
// Mix the inputs
432-
float max_output = 1.0;
434+
float max_output{1.0};
433435

434436
for (uint8_t i = 0; i < NUM_MIXER_OUTPUTS; i++) {
435437
if ((*mixer_to_use_.output_type)[i] != AUX) {
@@ -454,8 +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;
458-
459+
float max_output{1.0};
459460
float rho = RF_.sensors_.rho();
460461

461462
for (uint8_t i = 0; i < NUM_MIXER_OUTPUTS; i++) {
@@ -467,17 +468,33 @@ float Mixer::mix_multirotor_with_motor_parameters(Controller::Output commands)
467468
commands.Qx * (*mixer_to_use_.Qx)[i] +
468469
commands.Qy * (*mixer_to_use_.Qy)[i] +
469470
commands.Qz * (*mixer_to_use_.Qz)[i];
470-
471+
471472
// Ensure that omega_squared is non-negative
472473
if (omega_squared < 0.0) { omega_squared = 0.0; }
473474

475+
if (K_Q_ < 0.0000001) {
476+
RF_.comm_manager_.log(CommLinkInterface::LogSeverity::LOG_ERROR,
477+
"Divide by K_V, which is zero! Skipping");
478+
outputs_[i] = 0.0;
479+
continue;
480+
}
481+
474482
// Ch. 4, setting equation for torque produced by a propeller equal to Eq. 4.19
475483
// Note that we assume constant advance ratio, leading to constant torque and thrust constants.
476484
float V_in = rho * pow(D_, 5.0) / (4.0 * pow(M_PI, 2.0)) * omega_squared * C_Q_ * R_ / K_Q_
477485
+ R_ * i_0_ + K_V_ * sqrt(omega_squared);
478486

479487
// Convert desired V_in setting to a throttle setting
480488
BatteryStruct * batt = RF_.sensors_.get_battery();
489+
if (batt->voltage < 0.0001) {
490+
RF_.comm_manager_.log(CommLinkInterface::LogSeverity::LOG_ERROR,
491+
"Divide by battery voltage, which is near zero!");
492+
RF_.comm_manager_.log(CommLinkInterface::LogSeverity::LOG_ERROR,
493+
"Is battery multiplier set correctly?");
494+
outputs_[i] = 0.0;
495+
continue;
496+
}
497+
481498
outputs_[i] = V_in / batt->voltage;
482499

483500
// Save off the largest control output (for motors) if it is greater than 1.0 for future scaling
@@ -502,7 +519,7 @@ void Mixer::mix_multirotor()
502519
Controller::Output commands = RF_.controller_.output();
503520

504521
// Check the throttle command based on the axis corresponding to the RC F channel
505-
float throttle_command = 0.0;
522+
float throttle_command{0.0};
506523
switch (static_cast<rc_f_axis_t>(RF_.params_.get_param_int(PARAM_RC_F_AXIS))) {
507524
case X_AXIS:
508525
throttle_command = commands.Fx;
@@ -527,7 +544,7 @@ void Mixer::mix_multirotor()
527544
}
528545

529546
// Mix the outputs based on if a custom mixer (i.e. with motor parameters) is selected.
530-
float max_output;
547+
float max_output{1.0};
531548
if (RF_.params_.get_param_int(PARAM_USE_MOTOR_PARAMETERS)) {
532549
max_output = mix_multirotor_with_motor_parameters(commands);
533550
} else {
@@ -544,7 +561,7 @@ void Mixer::mix_multirotor()
544561
// There is no relative scaling on the above equations. In other words, if the input F command is too
545562
// high, then it will "drown out" all other desired outputs. Therefore, we saturate motor outputs to
546563
// maintain controllability even during aggressive maneuvers.
547-
float scale_factor = 1.0;
564+
float scale_factor{1.0};
548565
if (max_output > 1.0) { scale_factor = 1.0 / max_output; }
549566

550567
// Perform Motor Output Scaling
@@ -617,7 +634,6 @@ void Mixer::mix_output()
617634
}
618635

619636
// Insert AUX Commands, and assemble combined_output_types array (Does not override mixer values)
620-
621637
// For the first NUM_MIXER_OUTPUTS channels, only write aux_command to channels the mixer is not
622638
// using
623639
for (uint8_t i = 0; i < NUM_MIXER_OUTPUTS; i++) {

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)