James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 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" |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 20 | #include "errors/exception.hpp" |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 21 | #include "util.hpp" |
| 22 | #include "zone.hpp" |
| 23 | |
| 24 | #include <algorithm> |
| 25 | #include <chrono> |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 26 | #include <cmath> |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 27 | #include <iostream> |
| 28 | #include <map> |
| 29 | #include <memory> |
| 30 | #include <thread> |
| 31 | #include <vector> |
| 32 | |
| 33 | void StepwiseController::process(void) |
| 34 | { |
| 35 | // Get input value |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 36 | double input = inputProc(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 37 | |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 38 | ec::StepwiseInfo info = get_stepwise_info(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 39 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 40 | double output = lastOutput; |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 41 | |
| 42 | // Calculate new output if hysteresis allows |
| 43 | if (std::isnan(output)) |
| 44 | { |
| 45 | output = ec::stepwise(info, input); |
| 46 | lastInput = input; |
| 47 | } |
| 48 | else if ((input - lastInput) > info.positiveHysteresis) |
| 49 | { |
| 50 | output = ec::stepwise(info, input); |
| 51 | lastInput = input; |
| 52 | } |
| 53 | else if ((lastInput - input) > info.negativeHysteresis) |
| 54 | { |
| 55 | output = ec::stepwise(info, input); |
| 56 | lastInput = input; |
| 57 | } |
| 58 | |
| 59 | lastOutput = output; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 60 | // Output new value |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 61 | outputProc(output); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 62 | |
| 63 | return; |
| 64 | } |
| 65 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 66 | std::unique_ptr<Controller> StepwiseController::createStepwiseController( |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 67 | ZoneInterface* owner, const std::string& id, |
| 68 | const std::vector<std::string>& inputs, const ec::StepwiseInfo& initial) |
| 69 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 70 | // StepwiseController requires at least 1 input |
| 71 | if (inputs.empty()) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 72 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 73 | throw ControllerBuildException("Stepwise controller missing inputs"); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 74 | return nullptr; |
| 75 | } |
| 76 | |
| 77 | auto thermal = std::make_unique<StepwiseController>(id, inputs, owner); |
| 78 | |
| 79 | ec::StepwiseInfo& info = thermal->get_stepwise_info(); |
| 80 | |
| 81 | info = initial; |
| 82 | |
| 83 | return thermal; |
| 84 | } |
| 85 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 86 | double StepwiseController::inputProc(void) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 87 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 88 | double value = std::numeric_limits<double>::lowest(); |
| 89 | for (const auto& in : _inputs) |
| 90 | { |
| 91 | value = std::max(value, _owner->getCachedValue(in)); |
| 92 | } |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 93 | return value; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 96 | void StepwiseController::outputProc(double value) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 97 | { |
James Feist | dca2d48 | 2019-02-15 16:14:31 -0800 | [diff] [blame] | 98 | // values are 10 for 10% |
| 99 | value *= 100; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 100 | _owner->addRPMSetPoint(value); |
| 101 | |
| 102 | return; |
| 103 | } |