blob: 5c367783ee5af90f436f788a88bd38a20e691732 [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"
James Feist22c257a2018-08-31 14:07:12 -070021#include "util.hpp"
22#include "zone.hpp"
23
24#include <algorithm>
25#include <chrono>
James Feist3dfaafd2018-09-20 15:46:58 -070026#include <cmath>
James Feist22c257a2018-08-31 14:07:12 -070027#include <iostream>
28#include <map>
29#include <memory>
30#include <thread>
31#include <vector>
32
Patrick Venturea0764872020-08-08 07:48:43 -070033namespace pid_control
34{
35
James Feist22c257a2018-08-31 14:07:12 -070036void StepwiseController::process(void)
37{
38 // Get input value
Patrick Venture5f59c0f2018-11-11 12:55:14 -080039 double input = inputProc();
James Feist22c257a2018-08-31 14:07:12 -070040
Patrick Ventureeb428202020-08-18 09:50:46 -070041 ec::StepwiseInfo info = getStepwiseInfo();
James Feist22c257a2018-08-31 14:07:12 -070042
Patrick Venture5f59c0f2018-11-11 12:55:14 -080043 double output = lastOutput;
James Feist3dfaafd2018-09-20 15:46:58 -070044
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;
James Feist22c257a2018-08-31 14:07:12 -070063 // Output new value
Patrick Venture563a3562018-10-30 09:31:26 -070064 outputProc(output);
James Feist22c257a2018-08-31 14:07:12 -070065
66 return;
67}
68
Patrick Venture563a3562018-10-30 09:31:26 -070069std::unique_ptr<Controller> StepwiseController::createStepwiseController(
James Feist22c257a2018-08-31 14:07:12 -070070 ZoneInterface* owner, const std::string& id,
71 const std::vector<std::string>& inputs, const ec::StepwiseInfo& initial)
72{
James Feist734f9532018-11-15 12:13:18 -080073 // StepwiseController requires at least 1 input
74 if (inputs.empty())
James Feist22c257a2018-08-31 14:07:12 -070075 {
James Feist734f9532018-11-15 12:13:18 -080076 throw ControllerBuildException("Stepwise controller missing inputs");
James Feist22c257a2018-08-31 14:07:12 -070077 return nullptr;
78 }
79
80 auto thermal = std::make_unique<StepwiseController>(id, inputs, owner);
Patrick Venturea5cf2082020-08-13 09:38:17 -070081 thermal->setStepwiseInfo(initial);
James Feist22c257a2018-08-31 14:07:12 -070082
83 return thermal;
84}
85
Patrick Venture5f59c0f2018-11-11 12:55:14 -080086double StepwiseController::inputProc(void)
James Feist22c257a2018-08-31 14:07:12 -070087{
James Feist734f9532018-11-15 12:13:18 -080088 double value = std::numeric_limits<double>::lowest();
89 for (const auto& in : _inputs)
90 {
91 value = std::max(value, _owner->getCachedValue(in));
92 }
Patrick Venture5f59c0f2018-11-11 12:55:14 -080093 return value;
James Feist22c257a2018-08-31 14:07:12 -070094}
95
Patrick Venture5f59c0f2018-11-11 12:55:14 -080096void StepwiseController::outputProc(double value)
James Feist22c257a2018-08-31 14:07:12 -070097{
Patrick Ventureeb428202020-08-18 09:50:46 -070098 if (getStepwiseInfo().isCeiling)
James Feist608304d2019-02-25 10:01:42 -080099 {
100 _owner->addRPMCeiling(value);
101 }
102 else
103 {
Nirav Shahccc8bb62022-02-17 21:06:51 -0800104 _owner->addSetPoint(value, _id);
James Feist608304d2019-02-25 10:01:42 -0800105 }
James Feist22c257a2018-08-31 14:07:12 -0700106 return;
107}
Patrick Venturea0764872020-08-08 07:48:43 -0700108
109} // namespace pid_control