| #include "tlbmc/thermal/controller/algorithm/eat.h" |
| |
| #include <algorithm> |
| #include <chrono> // NOLINT |
| #include <cmath> |
| #include <optional> |
| |
| #include "absl/time/time.h" |
| |
| namespace milotic_tlbmc { |
| namespace thermal { |
| |
| std::optional<double> EatThermalLoop::ExecuteEatLoop( |
| double temperature, std::chrono::steady_clock::time_point now) { |
| sum_ambient_temperature_ += temperature; |
| ++recorded_temperature_count_; |
| |
| if (absl::FromChrono(now - last_calculation_time_) >= |
| absl::Hours(eat_params_.calculation_interval_hour()) - |
| tolerable_interval_epsilon_ && |
| recorded_temperature_count_ > 0) { |
| double average_eat = sum_ambient_temperature_ / recorded_temperature_count_; |
| |
| Reset(now); |
| |
| return CalculateAndSetReferenceEat(average_eat); |
| } |
| |
| return std::nullopt; |
| } |
| |
| double EatThermalLoop::CalculateAndSetReferenceEat(double average_eat) { |
| double delta = std::round(average_eat - eat_params_.reference_eat()); |
| double offset = |
| std::clamp(delta, eat_params_.slew_neg(), eat_params_.slew_pos()); |
| |
| eat_params_.set_reference_eat(eat_params_.reference_eat() + offset); |
| |
| return offset; |
| } |
| |
| void EatThermalLoop::Reset(std::chrono::steady_clock::time_point now) { |
| sum_ambient_temperature_ = 0.0; |
| recorded_temperature_count_ = 0; |
| last_calculation_time_ = now; |
| } |
| |
| } // namespace thermal |
| } // namespace milotic_tlbmc |