xref: /openbmc/phosphor-pid-control/util.cpp (revision 3f0f7bc3)
139199b4dSPatrick Venture /*
239199b4dSPatrick Venture // Copyright (c) 2018 Intel Corporation
339199b4dSPatrick Venture //
439199b4dSPatrick Venture // Licensed under the Apache License, Version 2.0 (the "License");
539199b4dSPatrick Venture // you may not use this file except in compliance with the License.
639199b4dSPatrick Venture // You may obtain a copy of the License at
739199b4dSPatrick Venture //
839199b4dSPatrick Venture //      http://www.apache.org/licenses/LICENSE-2.0
939199b4dSPatrick Venture //
1039199b4dSPatrick Venture // Unless required by applicable law or agreed to in writing, software
1139199b4dSPatrick Venture // distributed under the License is distributed on an "AS IS" BASIS,
1239199b4dSPatrick Venture // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1339199b4dSPatrick Venture // See the License for the specific language governing permissions and
1439199b4dSPatrick Venture // limitations under the License.
1539199b4dSPatrick Venture */
1639199b4dSPatrick Venture #include "util.hpp"
1739199b4dSPatrick Venture 
1839199b4dSPatrick Venture #include "conf.hpp"
1939199b4dSPatrick Venture 
2039199b4dSPatrick Venture #include <cstdint>
2139199b4dSPatrick Venture #include <iostream>
2239199b4dSPatrick Venture #include <map>
2339199b4dSPatrick Venture #include <string>
2439199b4dSPatrick Venture 
2539199b4dSPatrick Venture namespace pid_control
2639199b4dSPatrick Venture {
2739199b4dSPatrick Venture 
debugPrint(const std::map<std::string,conf::SensorConfig> & sensorConfig,const std::map<int64_t,conf::PIDConf> & zoneConfig,const std::map<int64_t,conf::ZoneConfig> & zoneDetailsConfig)2839199b4dSPatrick Venture void debugPrint(const std::map<std::string, conf::SensorConfig>& sensorConfig,
2939199b4dSPatrick Venture                 const std::map<int64_t, conf::PIDConf>& zoneConfig,
3039199b4dSPatrick Venture                 const std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig)
3139199b4dSPatrick Venture {
3239199b4dSPatrick Venture     if constexpr (!conf::DEBUG)
3339199b4dSPatrick Venture     {
3439199b4dSPatrick Venture         return;
3539199b4dSPatrick Venture     }
3639199b4dSPatrick Venture     // print sensor config
3739199b4dSPatrick Venture     std::cout << "sensor config:\n";
3839199b4dSPatrick Venture     std::cout << "{\n";
3939199b4dSPatrick Venture     for (const auto& pair : sensorConfig)
4039199b4dSPatrick Venture     {
4139199b4dSPatrick Venture         std::cout << "\t{" << pair.first << ",\n\t\t{";
4239199b4dSPatrick Venture         std::cout << pair.second.type << ", ";
4339199b4dSPatrick Venture         std::cout << pair.second.readPath << ", ";
4439199b4dSPatrick Venture         std::cout << pair.second.writePath << ", ";
4539199b4dSPatrick Venture         std::cout << pair.second.min << ", ";
4639199b4dSPatrick Venture         std::cout << pair.second.max << ", ";
478f73ad76SAlex.Song         std::cout << pair.second.timeout << ", ";
488f73ad76SAlex.Song         std::cout << pair.second.unavailableAsFailed << "},\n\t},\n";
4939199b4dSPatrick Venture     }
5039199b4dSPatrick Venture     std::cout << "}\n\n";
5139199b4dSPatrick Venture     std::cout << "ZoneDetailsConfig\n";
5239199b4dSPatrick Venture     std::cout << "{\n";
5339199b4dSPatrick Venture     for (const auto& zone : zoneDetailsConfig)
5439199b4dSPatrick Venture     {
5539199b4dSPatrick Venture         std::cout << "\t{" << zone.first << ",\n";
5639199b4dSPatrick Venture         std::cout << "\t\t{" << zone.second.minThermalOutput << ", ";
5739199b4dSPatrick Venture         std::cout << zone.second.failsafePercent << "}\n\t},\n";
5839199b4dSPatrick Venture     }
5939199b4dSPatrick Venture     std::cout << "}\n\n";
6039199b4dSPatrick Venture     std::cout << "ZoneConfig\n";
6139199b4dSPatrick Venture     std::cout << "{\n";
6239199b4dSPatrick Venture     for (const auto& zone : zoneConfig)
6339199b4dSPatrick Venture     {
6439199b4dSPatrick Venture         std::cout << "\t{" << zone.first << "\n";
6539199b4dSPatrick Venture         for (const auto& pidconf : zone.second)
6639199b4dSPatrick Venture         {
6739199b4dSPatrick Venture             std::cout << "\t\t{" << pidconf.first << ",\n";
6839199b4dSPatrick Venture             std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
6939199b4dSPatrick Venture             std::cout << "\t\t\t{";
7039199b4dSPatrick Venture             for (const auto& input : pidconf.second.inputs)
7139199b4dSPatrick Venture             {
7231058fd3SJosh Lehan                 std::cout << "\n\t\t\t" << input.name;
7331058fd3SJosh Lehan                 if (input.convertTempToMargin)
7431058fd3SJosh Lehan                 {
7531058fd3SJosh Lehan                     std::cout << "[" << input.convertMarginZero << "]";
7631058fd3SJosh Lehan                 }
7731058fd3SJosh Lehan                 std::cout << ",\n";
7839199b4dSPatrick Venture             }
7939199b4dSPatrick Venture             std::cout << "\t\t\t}\n";
8039199b4dSPatrick Venture             std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
8139199b4dSPatrick Venture             std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n";
8239199b4dSPatrick Venture             std::cout << "\t\t\t" << pidconf.second.pidInfo.proportionalCoeff
8339199b4dSPatrick Venture                       << ",\n";
8439199b4dSPatrick Venture             std::cout << "\t\t\t" << pidconf.second.pidInfo.integralCoeff
8539199b4dSPatrick Venture                       << ",\n";
8639199b4dSPatrick Venture             std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdOffset
8739199b4dSPatrick Venture                       << ",\n";
8839199b4dSPatrick Venture             std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdGain
8939199b4dSPatrick Venture                       << ",\n";
9039199b4dSPatrick Venture             std::cout << "\t\t\t{" << pidconf.second.pidInfo.integralLimit.min
9139199b4dSPatrick Venture                       << "," << pidconf.second.pidInfo.integralLimit.max
9239199b4dSPatrick Venture                       << "},\n";
9339199b4dSPatrick Venture             std::cout << "\t\t\t{" << pidconf.second.pidInfo.outLim.min << ","
9439199b4dSPatrick Venture                       << pidconf.second.pidInfo.outLim.max << "},\n";
9539199b4dSPatrick Venture             std::cout << "\t\t\t" << pidconf.second.pidInfo.slewNeg << ",\n";
9639199b4dSPatrick Venture             std::cout << "\t\t\t" << pidconf.second.pidInfo.slewPos << ",\n";
9739199b4dSPatrick Venture             std::cout << "\t\t\t}\n\t\t}\n";
9839199b4dSPatrick Venture         }
9939199b4dSPatrick Venture         std::cout << "\t},\n";
10039199b4dSPatrick Venture     }
10139199b4dSPatrick Venture     std::cout << "}\n\n";
10239199b4dSPatrick Venture }
10339199b4dSPatrick Venture 
10431058fd3SJosh Lehan std::vector<conf::SensorInput>
spliceInputs(const std::vector<std::string> & inputNames,const std::vector<double> & inputTempToMargin,const std::vector<std::string> & missingAcceptableNames)10531058fd3SJosh Lehan     spliceInputs(const std::vector<std::string>& inputNames,
106*3f0f7bc3SJosh Lehan                  const std::vector<double>& inputTempToMargin,
107*3f0f7bc3SJosh Lehan                  const std::vector<std::string>& missingAcceptableNames)
10831058fd3SJosh Lehan {
10931058fd3SJosh Lehan     std::vector<conf::SensorInput> results;
11031058fd3SJosh Lehan 
111*3f0f7bc3SJosh Lehan     // Default to TempToMargin and MissingIsAcceptable disabled
11231058fd3SJosh Lehan     for (const auto& inputName : inputNames)
11331058fd3SJosh Lehan     {
11431058fd3SJosh Lehan         conf::SensorInput newInput{
115*3f0f7bc3SJosh Lehan             inputName, std::numeric_limits<double>::quiet_NaN(), false, false};
11631058fd3SJosh Lehan 
11731058fd3SJosh Lehan         results.emplace_back(newInput);
11831058fd3SJosh Lehan     }
11931058fd3SJosh Lehan 
12031058fd3SJosh Lehan     size_t resultSize = results.size();
12131058fd3SJosh Lehan     size_t marginSize = inputTempToMargin.size();
12231058fd3SJosh Lehan 
12331058fd3SJosh Lehan     for (size_t index = 0; index < resultSize; ++index)
12431058fd3SJosh Lehan     {
12531058fd3SJosh Lehan         // If fewer doubles than strings, and vice versa, ignore remainder
12631058fd3SJosh Lehan         if (index >= marginSize)
12731058fd3SJosh Lehan         {
12831058fd3SJosh Lehan             break;
12931058fd3SJosh Lehan         }
13031058fd3SJosh Lehan 
13131058fd3SJosh Lehan         // Both vectors have this index, combine both into SensorInput
13231058fd3SJosh Lehan         results[index].convertMarginZero = inputTempToMargin[index];
13331058fd3SJosh Lehan         results[index].convertTempToMargin = true;
13431058fd3SJosh Lehan     }
13531058fd3SJosh Lehan 
136*3f0f7bc3SJosh Lehan     std::set<std::string> acceptableSet;
137*3f0f7bc3SJosh Lehan 
138*3f0f7bc3SJosh Lehan     // Copy vector to set, to avoid O(n^2) runtime below
139*3f0f7bc3SJosh Lehan     for (const auto& name : missingAcceptableNames)
140*3f0f7bc3SJosh Lehan     {
141*3f0f7bc3SJosh Lehan         acceptableSet.emplace(name);
142*3f0f7bc3SJosh Lehan     }
143*3f0f7bc3SJosh Lehan 
144*3f0f7bc3SJosh Lehan     // Flag missingIsAcceptable true if name found in that set
145*3f0f7bc3SJosh Lehan     for (auto& result : results)
146*3f0f7bc3SJosh Lehan     {
147*3f0f7bc3SJosh Lehan         if (acceptableSet.find(result.name) != acceptableSet.end())
148*3f0f7bc3SJosh Lehan         {
149*3f0f7bc3SJosh Lehan             result.missingIsAcceptable = true;
150*3f0f7bc3SJosh Lehan         }
151*3f0f7bc3SJosh Lehan     }
152*3f0f7bc3SJosh Lehan 
15331058fd3SJosh Lehan     return results;
15431058fd3SJosh Lehan }
15531058fd3SJosh Lehan 
15631058fd3SJosh Lehan std::vector<std::string>
splitNames(const std::vector<conf::SensorInput> & sensorInputs)15731058fd3SJosh Lehan     splitNames(const std::vector<conf::SensorInput>& sensorInputs)
15831058fd3SJosh Lehan {
15931058fd3SJosh Lehan     std::vector<std::string> results;
16031058fd3SJosh Lehan 
16131058fd3SJosh Lehan     for (const auto& sensorInput : sensorInputs)
16231058fd3SJosh Lehan     {
16331058fd3SJosh Lehan         results.emplace_back(sensorInput.name);
16431058fd3SJosh Lehan     }
16531058fd3SJosh Lehan 
16631058fd3SJosh Lehan     return results;
16731058fd3SJosh Lehan }
16831058fd3SJosh Lehan 
16939199b4dSPatrick Venture } // namespace pid_control
170