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" |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 21 | #include "tuning.hpp" |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 22 | #include "util.hpp" |
| 23 | #include "zone.hpp" |
| 24 | |
| 25 | #include <algorithm> |
| 26 | #include <chrono> |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 27 | #include <cmath> |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 28 | #include <iostream> |
| 29 | #include <map> |
| 30 | #include <memory> |
| 31 | #include <thread> |
| 32 | #include <vector> |
| 33 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 34 | namespace pid_control |
| 35 | { |
| 36 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 37 | void StepwiseController::process(void) |
| 38 | { |
| 39 | // Get input value |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 40 | double input = inputProc(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 41 | |
Patrick Venture | eb42820 | 2020-08-18 09:50:46 -0700 | [diff] [blame] | 42 | ec::StepwiseInfo info = getStepwiseInfo(); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 43 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 44 | double output = lastOutput; |
James Feist | 3dfaafd | 2018-09-20 15:46:58 -0700 | [diff] [blame] | 45 | |
| 46 | // Calculate new output if hysteresis allows |
| 47 | if (std::isnan(output)) |
| 48 | { |
| 49 | output = ec::stepwise(info, input); |
| 50 | lastInput = input; |
| 51 | } |
| 52 | else if ((input - lastInput) > info.positiveHysteresis) |
| 53 | { |
| 54 | output = ec::stepwise(info, input); |
| 55 | lastInput = input; |
| 56 | } |
| 57 | else if ((lastInput - input) > info.negativeHysteresis) |
| 58 | { |
| 59 | output = ec::stepwise(info, input); |
| 60 | lastInput = input; |
| 61 | } |
| 62 | |
| 63 | lastOutput = output; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 64 | // Output new value |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 65 | outputProc(output); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 66 | |
| 67 | return; |
| 68 | } |
| 69 | |
Patrick Venture | 563a356 | 2018-10-30 09:31:26 -0700 | [diff] [blame] | 70 | std::unique_ptr<Controller> StepwiseController::createStepwiseController( |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 71 | ZoneInterface* owner, const std::string& id, |
| 72 | const std::vector<std::string>& inputs, const ec::StepwiseInfo& initial) |
| 73 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 74 | // StepwiseController requires at least 1 input |
| 75 | if (inputs.empty()) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 76 | { |
James Feist | 734f953 | 2018-11-15 12:13:18 -0800 | [diff] [blame] | 77 | throw ControllerBuildException("Stepwise controller missing inputs"); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | auto thermal = std::make_unique<StepwiseController>(id, inputs, owner); |
Patrick Venture | a5cf208 | 2020-08-13 09:38:17 -0700 | [diff] [blame] | 81 | thermal->setStepwiseInfo(initial); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 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 | } |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 93 | |
| 94 | if (debugEnabled) |
| 95 | { |
| 96 | std::cerr << getID() |
| 97 | << " choose the maximum temperature value: " << value << "\n"; |
| 98 | } |
| 99 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 100 | return value; |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 103 | void StepwiseController::outputProc(double value) |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 104 | { |
Patrick Venture | eb42820 | 2020-08-18 09:50:46 -0700 | [diff] [blame] | 105 | if (getStepwiseInfo().isCeiling) |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 106 | { |
| 107 | _owner->addRPMCeiling(value); |
| 108 | } |
| 109 | else |
| 110 | { |
Nirav Shah | ccc8bb6 | 2022-02-17 21:06:51 -0800 | [diff] [blame] | 111 | _owner->addSetPoint(value, _id); |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 112 | if (debugEnabled) |
| 113 | { |
| 114 | std::cerr << getID() << " stepwise output pwm: " << value << "\n"; |
| 115 | } |
James Feist | 608304d | 2019-02-25 10:01:42 -0800 | [diff] [blame] | 116 | } |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 119 | |
| 120 | } // namespace pid_control |