blob: 201c58b7d248affaf41864040feb40fb690e32db [file] [edit]
#include "tlbmc/thermal/controller/stepwise_controller.h"
#include <algorithm>
#include <cmath>
#include <limits>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "tlbmc/thermal/controller/algorithm/stepwise.h"
#include "tlbmc/thermal/zone_manager.h"
namespace milotic_tlbmc {
namespace thermal {
absl::StatusOr<std::unique_ptr<StepwiseController>> StepwiseController::Create(
StepwiseControllerParameters params) {
if (params.input_sensors.empty()) {
return absl::InvalidArgumentError(absl::StrFormat(
"No input sensor is assigned to stepwise controller `%s`!", params.id));
}
return absl::WrapUnique(new StepwiseController(
params.id, params.zone_manager, std::move(params.stepwise_loop),
std::move(params.input_sensors), params.is_ceiling));
}
void StepwiseController::ProcessThermalLoop() {
double input = ProcessInput();
ProcessOutput(GetStepwiseLoop()->ExecuteStepwiseLoop(input));
}
double StepwiseController::ProcessInput() {
double input = std::numeric_limits<double>::lowest();
for (const std::string& sensor_name : GetInputSensors()) {
double sensor_value =
zone_owner_->GetSensorReadings(sensor_name).scaled_value;
if (!std::isfinite(sensor_value)) {
continue;
}
input = std::max(input, sensor_value);
}
zone_owner_->TryLogThermalDebugMessage(absl::StrFormat(
"Stepwise controller %s chose the input value %lf\n", GetId(), input));
return input;
}
void StepwiseController::ProcessOutput(double output) {
if (GetIsCeiling()) {
zone_owner_->AddRpmCeiling(output);
zone_owner_->TryLogThermalDebugMessage(absl::StrFormat(
"Stepwise controller %s outputs rpm ceiling: %lf\n", GetId(), output));
return;
}
zone_owner_->AddSetpoint(GetId(), output);
zone_owner_->TryLogThermalDebugMessage(absl::StrFormat(
"Stepwise controller %s outputs setpoint: %lf\n", GetId(), output));
if (zone_owner_->GetDebugEnabled()) {
zone_owner_->AppendSampledData(absl::StrCat(output, ","));
}
}
} // namespace thermal
} // namespace milotic_tlbmc