1ce6a3f36SJames Feist /** 2ce6a3f36SJames Feist * Copyright 2017 Google Inc. 3ce6a3f36SJames Feist * 4ce6a3f36SJames Feist * Licensed under the Apache License, Version 2.0 (the "License"); 5ce6a3f36SJames Feist * you may not use this file except in compliance with the License. 6ce6a3f36SJames Feist * You may obtain a copy of the License at 7ce6a3f36SJames Feist * 8ce6a3f36SJames Feist * http://www.apache.org/licenses/LICENSE-2.0 9ce6a3f36SJames Feist * 10ce6a3f36SJames Feist * Unless required by applicable law or agreed to in writing, software 11ce6a3f36SJames Feist * distributed under the License is distributed on an "AS IS" BASIS, 12ce6a3f36SJames Feist * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13ce6a3f36SJames Feist * See the License for the specific language governing permissions and 14ce6a3f36SJames Feist * limitations under the License. 15ce6a3f36SJames Feist */ 16ce6a3f36SJames Feist 17ce6a3f36SJames Feist #include "pidloop.hpp" 18ce6a3f36SJames Feist 19ce6a3f36SJames Feist #include "pid/pidcontroller.hpp" 20ce6a3f36SJames Feist #include "pid/tuning.hpp" 21ce6a3f36SJames Feist #include "sensors/sensor.hpp" 22ce6a3f36SJames Feist 23ce6a3f36SJames Feist #include <boost/asio/steady_timer.hpp> 24ce6a3f36SJames Feist #include <chrono> 25ce6a3f36SJames Feist #include <map> 26ce6a3f36SJames Feist #include <memory> 27ce6a3f36SJames Feist #include <thread> 28ce6a3f36SJames Feist #include <vector> 29ce6a3f36SJames Feist 30ce6a3f36SJames Feist static void processThermals(PIDZone* zone) 31ce6a3f36SJames Feist { 32ce6a3f36SJames Feist // Get the latest margins. 33ce6a3f36SJames Feist zone->updateSensors(); 34ce6a3f36SJames Feist // Zero out the RPM set point goals. 35ce6a3f36SJames Feist zone->clearRPMSetPoints(); 36ce6a3f36SJames Feist zone->clearRPMCeilings(); 37ce6a3f36SJames Feist // Run the margin PIDs. 38ce6a3f36SJames Feist zone->processThermals(); 39ce6a3f36SJames Feist // Get the maximum RPM setpoint. 40ce6a3f36SJames Feist zone->determineMaxRPMRequest(); 41ce6a3f36SJames Feist } 42ce6a3f36SJames Feist 43ce6a3f36SJames Feist void pidControlLoop(PIDZone* zone, boost::asio::steady_timer& timer, bool first, 44ce6a3f36SJames Feist int ms100cnt) 45ce6a3f36SJames Feist { 46ce6a3f36SJames Feist if (first) 47ce6a3f36SJames Feist { 48de79ee05SPatrick Venture if (loggingEnabled) 49ce6a3f36SJames Feist { 50ce6a3f36SJames Feist zone->initializeLog(); 51ce6a3f36SJames Feist } 52ce6a3f36SJames Feist 53ce6a3f36SJames Feist zone->initializeCache(); 54ce6a3f36SJames Feist processThermals(zone); 55ce6a3f36SJames Feist } 56ce6a3f36SJames Feist 57ce6a3f36SJames Feist timer.expires_after(std::chrono::milliseconds(100)); 58ce6a3f36SJames Feist timer.async_wait( 59ce6a3f36SJames Feist [zone, &timer, ms100cnt](const boost::system::error_code& ec) mutable { 60*1fe08952SJames Feist if (ec == boost::asio::error::operation_aborted) 61*1fe08952SJames Feist { 62*1fe08952SJames Feist return; // timer being canceled, stop loop 63*1fe08952SJames Feist } 64*1fe08952SJames Feist 65ce6a3f36SJames Feist /* 66ce6a3f36SJames Feist * This should sleep on the conditional wait for the listen thread 67ce6a3f36SJames Feist * to tell us it's in sync. But then we also need a timeout option 68ce6a3f36SJames Feist * in case phosphor-hwmon is down, we can go into some weird failure 69ce6a3f36SJames Feist * more. 70ce6a3f36SJames Feist * 71ce6a3f36SJames Feist * Another approach would be to start all sensors in worst-case 72ce6a3f36SJames Feist * values, and fail-safe mode and then clear out of fail-safe mode 73ce6a3f36SJames Feist * once we start getting values. Which I think it is a solid 74ce6a3f36SJames Feist * approach. 75ce6a3f36SJames Feist * 76ce6a3f36SJames Feist * For now this runs before it necessarily has any sensor values. 77ce6a3f36SJames Feist * For the host sensors they start out in fail-safe mode. For the 78ce6a3f36SJames Feist * fans, they start out as 0 as input and then are adjusted once 79ce6a3f36SJames Feist * they have values. 80ce6a3f36SJames Feist * 81ce6a3f36SJames Feist * If a fan has failed, it's value will be whatever we're told or 82ce6a3f36SJames Feist * however we retrieve it. This program disregards fan values of 0, 83ce6a3f36SJames Feist * so any code providing a fan speed can set to 0 on failure and 84ce6a3f36SJames Feist * that fan value will be effectively ignored. The PID algorithm 85ce6a3f36SJames Feist * will be unhappy but nothing bad will happen. 86ce6a3f36SJames Feist * 87ce6a3f36SJames Feist * TODO(venture): If the fan value is 0 should that loop just be 88ce6a3f36SJames Feist * skipped? Right now, a 0 value is ignored in 89ce6a3f36SJames Feist * FanController::inputProc() 90ce6a3f36SJames Feist */ 91ce6a3f36SJames Feist 92ce6a3f36SJames Feist // Check if we should just go back to sleep. 93ce6a3f36SJames Feist if (zone->getManualMode()) 94ce6a3f36SJames Feist { 95ce6a3f36SJames Feist pidControlLoop(zone, timer, false, ms100cnt); 96ce6a3f36SJames Feist return; 97ce6a3f36SJames Feist } 98ce6a3f36SJames Feist 99ce6a3f36SJames Feist // Get the latest fan speeds. 100ce6a3f36SJames Feist zone->updateFanTelemetry(); 101ce6a3f36SJames Feist 102ce6a3f36SJames Feist if (10 <= ms100cnt) 103ce6a3f36SJames Feist { 104ce6a3f36SJames Feist ms100cnt = 0; 105ce6a3f36SJames Feist 106ce6a3f36SJames Feist processThermals(zone); 107ce6a3f36SJames Feist } 108ce6a3f36SJames Feist 109ce6a3f36SJames Feist // Run the fan PIDs every iteration. 110ce6a3f36SJames Feist zone->processFans(); 111ce6a3f36SJames Feist 112de79ee05SPatrick Venture if (loggingEnabled) 113ce6a3f36SJames Feist { 114ce6a3f36SJames Feist zone->getLogHandle() << "," << zone->getFailSafeMode(); 115ce6a3f36SJames Feist zone->getLogHandle() << std::endl; 116ce6a3f36SJames Feist } 117ce6a3f36SJames Feist 118ce6a3f36SJames Feist ms100cnt += 1; 119ce6a3f36SJames Feist 120ce6a3f36SJames Feist pidControlLoop(zone, timer, false, ms100cnt); 121ce6a3f36SJames Feist }); 122ce6a3f36SJames Feist } 123