blob: d141f87ffe806eb47cf5cf387cc9a6e09dda90cc [file] [log] [blame]
James Feist22c257a2018-08-31 14:07:12 -07001/*
2// Copyright (c) 2018 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
16
17#include "stepwisecontroller.hpp"
18
19#include "ec/stepwise.hpp"
James Feist734f9532018-11-15 12:13:18 -080020#include "errors/exception.hpp"
Bonnie Loc51ba912022-10-12 14:07:22 +080021#include "tuning.hpp"
James Feist22c257a2018-08-31 14:07:12 -070022#include "util.hpp"
23#include "zone.hpp"
24
25#include <algorithm>
26#include <chrono>
James Feist3dfaafd2018-09-20 15:46:58 -070027#include <cmath>
James Feist22c257a2018-08-31 14:07:12 -070028#include <iostream>
29#include <map>
30#include <memory>
31#include <thread>
32#include <vector>
33
Patrick Venturea0764872020-08-08 07:48:43 -070034namespace pid_control
35{
36
James Feist22c257a2018-08-31 14:07:12 -070037void StepwiseController::process(void)
38{
39 // Get input value
Patrick Venture5f59c0f2018-11-11 12:55:14 -080040 double input = inputProc();
James Feist22c257a2018-08-31 14:07:12 -070041
Patrick Ventureeb428202020-08-18 09:50:46 -070042 ec::StepwiseInfo info = getStepwiseInfo();
James Feist22c257a2018-08-31 14:07:12 -070043
Patrick Venture5f59c0f2018-11-11 12:55:14 -080044 double output = lastOutput;
James Feist3dfaafd2018-09-20 15:46:58 -070045
46 // Calculate new output if hysteresis allows
47 if (std::isnan(output))
48 {
49 output = ec::stepwise(info, input);
50 lastInput = input;
51 }
52 else if ((input - lastInput) > info.positiveHysteresis)
53 {
54 output = ec::stepwise(info, input);
55 lastInput = input;
56 }
57 else if ((lastInput - input) > info.negativeHysteresis)
58 {
59 output = ec::stepwise(info, input);
60 lastInput = input;
61 }
62
63 lastOutput = output;
James Feist22c257a2018-08-31 14:07:12 -070064 // Output new value
Patrick Venture563a3562018-10-30 09:31:26 -070065 outputProc(output);
James Feist22c257a2018-08-31 14:07:12 -070066
67 return;
68}
69
Patrick Venture563a3562018-10-30 09:31:26 -070070std::unique_ptr<Controller> StepwiseController::createStepwiseController(
James Feist22c257a2018-08-31 14:07:12 -070071 ZoneInterface* owner, const std::string& id,
72 const std::vector<std::string>& inputs, const ec::StepwiseInfo& initial)
73{
James Feist734f9532018-11-15 12:13:18 -080074 // StepwiseController requires at least 1 input
75 if (inputs.empty())
James Feist22c257a2018-08-31 14:07:12 -070076 {
James Feist734f9532018-11-15 12:13:18 -080077 throw ControllerBuildException("Stepwise controller missing inputs");
James Feist22c257a2018-08-31 14:07:12 -070078 return nullptr;
79 }
80
81 auto thermal = std::make_unique<StepwiseController>(id, inputs, owner);
Patrick Venturea5cf2082020-08-13 09:38:17 -070082 thermal->setStepwiseInfo(initial);
James Feist22c257a2018-08-31 14:07:12 -070083
84 return thermal;
85}
86
Patrick Venture5f59c0f2018-11-11 12:55:14 -080087double StepwiseController::inputProc(void)
James Feist22c257a2018-08-31 14:07:12 -070088{
James Feist734f9532018-11-15 12:13:18 -080089 double value = std::numeric_limits<double>::lowest();
90 for (const auto& in : _inputs)
91 {
92 value = std::max(value, _owner->getCachedValue(in));
93 }
Bonnie Loc51ba912022-10-12 14:07:22 +080094
95 if (debugEnabled)
96 {
97 std::cerr << getID()
98 << " choose the maximum temperature value: " << value << "\n";
99 }
100
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800101 return value;
James Feist22c257a2018-08-31 14:07:12 -0700102}
103
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800104void StepwiseController::outputProc(double value)
James Feist22c257a2018-08-31 14:07:12 -0700105{
Patrick Ventureeb428202020-08-18 09:50:46 -0700106 if (getStepwiseInfo().isCeiling)
James Feist608304d2019-02-25 10:01:42 -0800107 {
108 _owner->addRPMCeiling(value);
109 }
110 else
111 {
Nirav Shahccc8bb62022-02-17 21:06:51 -0800112 _owner->addSetPoint(value, _id);
Bonnie Loc51ba912022-10-12 14:07:22 +0800113 if (debugEnabled)
114 {
115 std::cerr << getID() << " stepwise output pwm: " << value << "\n";
116 }
James Feist608304d2019-02-25 10:01:42 -0800117 }
James Feist22c257a2018-08-31 14:07:12 -0700118 return;
119}
Patrick Venturea0764872020-08-08 07:48:43 -0700120
121} // namespace pid_control