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" 20 #include "util.hpp" 21 #include "zone.hpp" 22 23 #include <algorithm> 24 #include <chrono> 25 #include <cmath> 26 #include <iostream> 27 #include <map> 28 #include <memory> 29 #include <thread> 30 #include <vector> 31 32 void StepwiseController::process(void) 33 { 34 // Get input value 35 float input = inputProc(); 36 37 ec::StepwiseInfo info = get_stepwise_info(); 38 39 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; 59 // Output new value 60 outputProc(output); 61 62 return; 63 } 64 65 std::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 84 float StepwiseController::inputProc(void) 85 { 86 double value = _owner->getCachedValue(_inputs.at(0)); 87 return static_cast<float>(value); 88 } 89 90 void StepwiseController::outputProc(float value) 91 { 92 _owner->addRPMSetPoint(value); 93 94 return; 95 } 96