blob: 1e6c3016f1766bacec2548aa7de8b1a6f13aef42 [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"
20#include "util.hpp"
21#include "zone.hpp"
22
23#include <algorithm>
24#include <chrono>
James Feist3dfaafd2018-09-20 15:46:58 -070025#include <cmath>
James Feist22c257a2018-08-31 14:07:12 -070026#include <iostream>
27#include <map>
28#include <memory>
29#include <thread>
30#include <vector>
31
32void StepwiseController::process(void)
33{
34 // Get input value
35 float input = input_proc();
36
James Feist3dfaafd2018-09-20 15:46:58 -070037 ec::StepwiseInfo info = get_stepwise_info();
James Feist22c257a2018-08-31 14:07:12 -070038
James Feist3dfaafd2018-09-20 15:46:58 -070039 float output = lastOutput;
40
41 // Calculate new output if hysteresis allows
42 if (std::isnan(output))
43 {
44 output = ec::stepwise(info, input);
45 lastInput = input;
46 }
47 else if ((input - lastInput) > info.positiveHysteresis)
48 {
49 output = ec::stepwise(info, input);
50 lastInput = input;
51 }
52 else if ((lastInput - input) > info.negativeHysteresis)
53 {
54 output = ec::stepwise(info, input);
55 lastInput = input;
56 }
57
58 lastOutput = output;
James Feist22c257a2018-08-31 14:07:12 -070059 // Output new value
60 output_proc(output);
61
62 return;
63}
64
65std::unique_ptr<Controller> StepwiseController::CreateStepwiseController(
66 ZoneInterface* owner, const std::string& id,
67 const std::vector<std::string>& inputs, const ec::StepwiseInfo& initial)
68{
69 // StepwiseController currently only supports precisely one input.
70 if (inputs.size() != 1)
71 {
72 return nullptr;
73 }
74
75 auto thermal = std::make_unique<StepwiseController>(id, inputs, owner);
76
77 ec::StepwiseInfo& info = thermal->get_stepwise_info();
78
79 info = initial;
80
81 return thermal;
82}
83
84float StepwiseController::input_proc(void)
85{
86 double value = _owner->getCachedValue(_inputs.at(0));
87 return static_cast<float>(value);
88}
89
90void StepwiseController::output_proc(float value)
91{
92 _owner->addRPMSetPoint(value);
93
94 return;
95}