122c257abSJames Feist /* 222c257abSJames Feist // Copyright (c) 2018 Intel Corporation 322c257abSJames Feist // 422c257abSJames Feist // Licensed under the Apache License, Version 2.0 (the "License"); 522c257abSJames Feist // you may not use this file except in compliance with the License. 622c257abSJames Feist // You may obtain a copy of the License at 722c257abSJames Feist // 822c257abSJames Feist // http://www.apache.org/licenses/LICENSE-2.0 922c257abSJames Feist // 1022c257abSJames Feist // Unless required by applicable law or agreed to in writing, software 1122c257abSJames Feist // distributed under the License is distributed on an "AS IS" BASIS, 1222c257abSJames Feist // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1322c257abSJames Feist // See the License for the specific language governing permissions and 1422c257abSJames Feist // limitations under the License. 1522c257abSJames Feist */ 1622c257abSJames Feist 1722c257abSJames Feist #include "stepwisecontroller.hpp" 1822c257abSJames Feist 1922c257abSJames Feist #include "ec/stepwise.hpp" 2022c257abSJames Feist #include "util.hpp" 2122c257abSJames Feist #include "zone.hpp" 2222c257abSJames Feist 2322c257abSJames Feist #include <algorithm> 2422c257abSJames Feist #include <chrono> 253dfaafdaSJames Feist #include <cmath> 2622c257abSJames Feist #include <iostream> 2722c257abSJames Feist #include <map> 2822c257abSJames Feist #include <memory> 2922c257abSJames Feist #include <thread> 3022c257abSJames Feist #include <vector> 3122c257abSJames Feist 3222c257abSJames Feist void StepwiseController::process(void) 3322c257abSJames Feist { 3422c257abSJames Feist // Get input value 35*5f59c0fdSPatrick Venture double input = inputProc(); 3622c257abSJames Feist 373dfaafdaSJames Feist ec::StepwiseInfo info = get_stepwise_info(); 3822c257abSJames Feist 39*5f59c0fdSPatrick Venture double output = lastOutput; 403dfaafdaSJames Feist 413dfaafdaSJames Feist // Calculate new output if hysteresis allows 423dfaafdaSJames Feist if (std::isnan(output)) 433dfaafdaSJames Feist { 443dfaafdaSJames Feist output = ec::stepwise(info, input); 453dfaafdaSJames Feist lastInput = input; 463dfaafdaSJames Feist } 473dfaafdaSJames Feist else if ((input - lastInput) > info.positiveHysteresis) 483dfaafdaSJames Feist { 493dfaafdaSJames Feist output = ec::stepwise(info, input); 503dfaafdaSJames Feist lastInput = input; 513dfaafdaSJames Feist } 523dfaafdaSJames Feist else if ((lastInput - input) > info.negativeHysteresis) 533dfaafdaSJames Feist { 543dfaafdaSJames Feist output = ec::stepwise(info, input); 553dfaafdaSJames Feist lastInput = input; 563dfaafdaSJames Feist } 573dfaafdaSJames Feist 583dfaafdaSJames Feist lastOutput = output; 5922c257abSJames Feist // Output new value 60563a356fSPatrick Venture outputProc(output); 6122c257abSJames Feist 6222c257abSJames Feist return; 6322c257abSJames Feist } 6422c257abSJames Feist 65563a356fSPatrick Venture std::unique_ptr<Controller> StepwiseController::createStepwiseController( 6622c257abSJames Feist ZoneInterface* owner, const std::string& id, 6722c257abSJames Feist const std::vector<std::string>& inputs, const ec::StepwiseInfo& initial) 6822c257abSJames Feist { 6922c257abSJames Feist // StepwiseController currently only supports precisely one input. 7022c257abSJames Feist if (inputs.size() != 1) 7122c257abSJames Feist { 7222c257abSJames Feist return nullptr; 7322c257abSJames Feist } 7422c257abSJames Feist 7522c257abSJames Feist auto thermal = std::make_unique<StepwiseController>(id, inputs, owner); 7622c257abSJames Feist 7722c257abSJames Feist ec::StepwiseInfo& info = thermal->get_stepwise_info(); 7822c257abSJames Feist 7922c257abSJames Feist info = initial; 8022c257abSJames Feist 8122c257abSJames Feist return thermal; 8222c257abSJames Feist } 8322c257abSJames Feist 84*5f59c0fdSPatrick Venture double StepwiseController::inputProc(void) 8522c257abSJames Feist { 8622c257abSJames Feist double value = _owner->getCachedValue(_inputs.at(0)); 87*5f59c0fdSPatrick Venture return value; 8822c257abSJames Feist } 8922c257abSJames Feist 90*5f59c0fdSPatrick Venture void StepwiseController::outputProc(double value) 9122c257abSJames Feist { 9222c257abSJames Feist _owner->addRPMSetPoint(value); 9322c257abSJames Feist 9422c257abSJames Feist return; 9522c257abSJames Feist } 96