xref: /openbmc/phosphor-pid-control/pid/stepwisecontroller.cpp (revision 22c257abd27fd76ed5280ef5c717eef1f28bfca9)
1*22c257abSJames Feist /*
2*22c257abSJames Feist // Copyright (c) 2018 Intel Corporation
3*22c257abSJames Feist //
4*22c257abSJames Feist // Licensed under the Apache License, Version 2.0 (the "License");
5*22c257abSJames Feist // you may not use this file except in compliance with the License.
6*22c257abSJames Feist // You may obtain a copy of the License at
7*22c257abSJames Feist //
8*22c257abSJames Feist //      http://www.apache.org/licenses/LICENSE-2.0
9*22c257abSJames Feist //
10*22c257abSJames Feist // Unless required by applicable law or agreed to in writing, software
11*22c257abSJames Feist // distributed under the License is distributed on an "AS IS" BASIS,
12*22c257abSJames Feist // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*22c257abSJames Feist // See the License for the specific language governing permissions and
14*22c257abSJames Feist // limitations under the License.
15*22c257abSJames Feist */
16*22c257abSJames Feist 
17*22c257abSJames Feist #include "stepwisecontroller.hpp"
18*22c257abSJames Feist 
19*22c257abSJames Feist #include "ec/stepwise.hpp"
20*22c257abSJames Feist #include "util.hpp"
21*22c257abSJames Feist #include "zone.hpp"
22*22c257abSJames Feist 
23*22c257abSJames Feist #include <algorithm>
24*22c257abSJames Feist #include <chrono>
25*22c257abSJames Feist #include <iostream>
26*22c257abSJames Feist #include <map>
27*22c257abSJames Feist #include <memory>
28*22c257abSJames Feist #include <thread>
29*22c257abSJames Feist #include <vector>
30*22c257abSJames Feist 
31*22c257abSJames Feist void StepwiseController::process(void)
32*22c257abSJames Feist {
33*22c257abSJames Feist     // Get input value
34*22c257abSJames Feist     float input = input_proc();
35*22c257abSJames Feist 
36*22c257abSJames Feist     // Calculate new output
37*22c257abSJames Feist     float output = ec::stepwise(get_stepwise_info(), input);
38*22c257abSJames Feist 
39*22c257abSJames Feist     // Output new value
40*22c257abSJames Feist     output_proc(output);
41*22c257abSJames Feist 
42*22c257abSJames Feist     return;
43*22c257abSJames Feist }
44*22c257abSJames Feist 
45*22c257abSJames Feist std::unique_ptr<Controller> StepwiseController::CreateStepwiseController(
46*22c257abSJames Feist     ZoneInterface* owner, const std::string& id,
47*22c257abSJames Feist     const std::vector<std::string>& inputs, const ec::StepwiseInfo& initial)
48*22c257abSJames Feist {
49*22c257abSJames Feist     // StepwiseController currently only supports precisely one input.
50*22c257abSJames Feist     if (inputs.size() != 1)
51*22c257abSJames Feist     {
52*22c257abSJames Feist         return nullptr;
53*22c257abSJames Feist     }
54*22c257abSJames Feist 
55*22c257abSJames Feist     auto thermal = std::make_unique<StepwiseController>(id, inputs, owner);
56*22c257abSJames Feist 
57*22c257abSJames Feist     ec::StepwiseInfo& info = thermal->get_stepwise_info();
58*22c257abSJames Feist 
59*22c257abSJames Feist     info = initial;
60*22c257abSJames Feist 
61*22c257abSJames Feist     return thermal;
62*22c257abSJames Feist }
63*22c257abSJames Feist 
64*22c257abSJames Feist float StepwiseController::input_proc(void)
65*22c257abSJames Feist {
66*22c257abSJames Feist     double value = _owner->getCachedValue(_inputs.at(0));
67*22c257abSJames Feist     return static_cast<float>(value);
68*22c257abSJames Feist }
69*22c257abSJames Feist 
70*22c257abSJames Feist void StepwiseController::output_proc(float value)
71*22c257abSJames Feist {
72*22c257abSJames Feist     _owner->addRPMSetPoint(value);
73*22c257abSJames Feist 
74*22c257abSJames Feist     return;
75*22c257abSJames Feist }
76