1 /*
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"
20 #include "errors/exception.hpp"
21 #include "tuning.hpp"
22 #include "util.hpp"
23 #include "zone.hpp"
24
25 #include <algorithm>
26 #include <chrono>
27 #include <cmath>
28 #include <iostream>
29 #include <map>
30 #include <memory>
31 #include <vector>
32
33 namespace pid_control
34 {
35
process(void)36 void StepwiseController::process(void)
37 {
38 // Get input value
39 double input = inputProc();
40
41 ec::StepwiseInfo info = getStepwiseInfo();
42
43 double output = lastOutput;
44
45 // Calculate new output if hysteresis allows
46 if (std::isnan(output))
47 {
48 output = ec::stepwise(info, input);
49 lastInput = input;
50 }
51 else if ((input - lastInput) > info.positiveHysteresis)
52 {
53 output = ec::stepwise(info, input);
54 lastInput = input;
55 }
56 else if ((lastInput - input) > info.negativeHysteresis)
57 {
58 output = ec::stepwise(info, input);
59 lastInput = input;
60 }
61
62 lastOutput = output;
63 // Output new value
64 outputProc(output);
65
66 return;
67 }
68
createStepwiseController(ZoneInterface * owner,const std::string & id,const std::vector<std::string> & inputs,const ec::StepwiseInfo & initial)69 std::unique_ptr<Controller> StepwiseController::createStepwiseController(
70 ZoneInterface* owner, const std::string& id,
71 const std::vector<std::string>& inputs, const ec::StepwiseInfo& initial)
72 {
73 // StepwiseController requires at least 1 input
74 if (inputs.empty())
75 {
76 throw ControllerBuildException("Stepwise controller missing inputs");
77 }
78
79 auto thermal = std::make_unique<StepwiseController>(id, inputs, owner);
80 thermal->setStepwiseInfo(initial);
81
82 return thermal;
83 }
84
inputProc(void)85 double StepwiseController::inputProc(void)
86 {
87 double value = std::numeric_limits<double>::lowest();
88 for (const auto& in : _inputs)
89 {
90 value = std::max(value, _owner->getCachedValue(in));
91 }
92
93 if (debugEnabled)
94 {
95 std::cerr << getID()
96 << " choose the maximum temperature value: " << value << "\n";
97 }
98
99 return value;
100 }
101
outputProc(double value)102 void StepwiseController::outputProc(double value)
103 {
104 if (getStepwiseInfo().isCeiling)
105 {
106 _owner->addRPMCeiling(value);
107 }
108 else
109 {
110 _owner->addSetPoint(value, _id);
111 if (debugEnabled)
112 {
113 std::cerr << getID() << " stepwise output pwm: " << value << "\n";
114 }
115 }
116 return;
117 }
118
119 } // namespace pid_control
120