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