message: Improve braking logic
diff: @@ -592,24 +592,20 @@ void update_brake_current() {
  }
  // Don't start braking until -Ibus > regen_current_allowed
  - float brake_current = std::max(-Ibus_sum - board_config.max_regen_current, 0.0f);
  - float brake_duty = std::max(brake_current * std::abs(board_config.brake_resistance) / vbus_voltage, 0.0f);
  + float brake_current = -Ibus_sum - board_config.max_regen_current;
  + float brake_duty = brake_current * std::abs(board_config.brake_resistance) / vbus_voltage;
  brake_duty = std::max((vbus_voltage - board_config.nominal_voltage) / (VBUS_OVERVOLTAGE_LEVEL / 0.9f - board_config.nominal_voltage), brake_duty);
  // Clamp the duty cycle
  - brake_duty = brake_duty < 0.0f ? 0.0f : (brake_duty > 0.9f ? 0.9f : brake_duty);
  + brake_duty = std::clamp(brake_duty, 0.0f, 0.9f);
  // Duty limit at 90% to allow bootstrap caps to charge
  // If brake_duty is NaN, this expression will also evaluate to false
  - if ((brake_duty >= 0.0f) && (brake_duty <= 0.9f)) {
  int high_on = static_cast<int>(TIM_APB1_PERIOD_CLOCKS * (1.0f - brake_duty));
  int low_off = high_on - TIM_APB1_DEADTIME_CLOCKS;
  if (low_off < 0) low_off = 0;
  safety_critical_apply_brake_resistor_timings(low_off, high_on);
  - } else {
  - //shuts off all motors AND brake resistor, sets error code on all motors.
  - low_level_fault(Motor::ERROR_BRAKE_CURRENT_OUT_OF_RANGE);
  - }
  +
  }
  