1 /** 2 * Copyright 2017 Google Inc. 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 "pidloop.hpp" 18 19 #include "pid/pidcontroller.hpp" 20 #include "pid/tuning.hpp" 21 #include "sensors/sensor.hpp" 22 23 #include <boost/asio/steady_timer.hpp> 24 25 #include <chrono> 26 #include <map> 27 #include <memory> 28 #include <thread> 29 #include <vector> 30 31 namespace pid_control 32 { 33 34 static void processThermals(DbusPidZone* zone) 35 { 36 // Get the latest margins. 37 zone->updateSensors(); 38 // Zero out the set point goals. 39 zone->clearSetPoints(); 40 zone->clearRPMCeilings(); 41 // Run the margin PIDs. 42 zone->processThermals(); 43 // Get the maximum RPM setpoint. 44 zone->determineMaxSetPointRequest(); 45 } 46 47 void pidControlLoop(DbusPidZone* zone, boost::asio::steady_timer& timer, 48 bool first, int ms100cnt) 49 { 50 if (first) 51 { 52 if (loggingEnabled) 53 { 54 zone->initializeLog(); 55 } 56 57 zone->initializeCache(); 58 processThermals(zone); 59 } 60 61 timer.expires_after(std::chrono::milliseconds(100)); 62 timer.async_wait( 63 [zone, &timer, ms100cnt](const boost::system::error_code& ec) mutable { 64 if (ec == boost::asio::error::operation_aborted) 65 { 66 return; // timer being canceled, stop loop 67 } 68 69 /* 70 * This should sleep on the conditional wait for the listen thread 71 * to tell us it's in sync. But then we also need a timeout option 72 * in case phosphor-hwmon is down, we can go into some weird failure 73 * more. 74 * 75 * Another approach would be to start all sensors in worst-case 76 * values, and fail-safe mode and then clear out of fail-safe mode 77 * once we start getting values. Which I think it is a solid 78 * approach. 79 * 80 * For now this runs before it necessarily has any sensor values. 81 * For the host sensors they start out in fail-safe mode. For the 82 * fans, they start out as 0 as input and then are adjusted once 83 * they have values. 84 * 85 * If a fan has failed, it's value will be whatever we're told or 86 * however we retrieve it. This program disregards fan values of 0, 87 * so any code providing a fan speed can set to 0 on failure and 88 * that fan value will be effectively ignored. The PID algorithm 89 * will be unhappy but nothing bad will happen. 90 * 91 * TODO(venture): If the fan value is 0 should that loop just be 92 * skipped? Right now, a 0 value is ignored in 93 * FanController::inputProc() 94 */ 95 96 // Check if we should just go back to sleep. 97 if (zone->getManualMode()) 98 { 99 pidControlLoop(zone, timer, false, ms100cnt); 100 return; 101 } 102 103 // Get the latest fan speeds. 104 zone->updateFanTelemetry(); 105 106 if (10 <= ms100cnt) 107 { 108 ms100cnt = 0; 109 110 processThermals(zone); 111 } 112 113 // Run the fan PIDs every iteration. 114 zone->processFans(); 115 116 if (loggingEnabled) 117 { 118 zone->getLogHandle() << "," << zone->getFailSafeMode(); 119 zone->getLogHandle() << std::endl; 120 } 121 122 ms100cnt += 1; 123 124 pidControlLoop(zone, timer, false, ms100cnt); 125 }); 126 } 127 128 } // namespace pid_control 129