xref: /openbmc/phosphor-pid-control/util.cpp (revision f8b6e55147148c3cfb42327ff267197a460b411c)
1 /*
2 // Copyright (c) 2018 Intel Corporation
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 #include "util.hpp"
17 
18 #include "conf.hpp"
19 
20 #include <cstddef>
21 #include <cstdint>
22 #include <iostream>
23 #include <limits>
24 #include <map>
25 #include <set>
26 #include <string>
27 #include <vector>
28 
29 namespace pid_control
30 {
31 
32 void debugPrint(const std::map<std::string, conf::SensorConfig>& sensorConfig,
33                 const std::map<int64_t, conf::PIDConf>& zoneConfig,
34                 const std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig)
35 {
36     if constexpr (!conf::DEBUG)
37     {
38         return;
39     }
40     // print sensor config
41     std::cout << "sensor config:\n";
42     std::cout << "{\n";
43     for (const auto& pair : sensorConfig)
44     {
45         std::cout << "\t{" << pair.first << ",\n\t\t{";
46         std::cout << pair.second.type << ", ";
47         std::cout << pair.second.readPath << ", ";
48         std::cout << pair.second.writePath << ", ";
49         std::cout << pair.second.min << ", ";
50         std::cout << pair.second.max << ", ";
51         std::cout << pair.second.timeout << ", ";
52         std::cout << pair.second.unavailableAsFailed << "},\n\t},\n";
53     }
54     std::cout << "}\n\n";
55     std::cout << "ZoneDetailsConfig\n";
56     std::cout << "{\n";
57     for (const auto& zone : zoneDetailsConfig)
58     {
59         std::cout << "\t{" << zone.first << ",\n";
60         std::cout << "\t\t{" << zone.second.minThermalOutput << ", ";
61         std::cout << zone.second.failsafePercent << "}\n\t},\n";
62     }
63     std::cout << "}\n\n";
64     std::cout << "ZoneConfig\n";
65     std::cout << "{\n";
66     for (const auto& zone : zoneConfig)
67     {
68         std::cout << "\t{" << zone.first << "\n";
69         for (const auto& pidconf : zone.second)
70         {
71             std::cout << "\t\t{" << pidconf.first << ",\n";
72             std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
73             std::cout << "\t\t\t{";
74             for (const auto& input : pidconf.second.inputs)
75             {
76                 std::cout << "\n\t\t\t" << input.name;
77                 if (input.convertTempToMargin)
78                 {
79                     std::cout << "[" << input.convertMarginZero << "]";
80                 }
81                 std::cout << ",\n";
82             }
83             std::cout << "\t\t\t}\n";
84             std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
85             std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n";
86             std::cout << "\t\t\t" << pidconf.second.pidInfo.proportionalCoeff
87                       << ",\n";
88             std::cout << "\t\t\t" << pidconf.second.pidInfo.integralCoeff
89                       << ",\n";
90             std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdOffset
91                       << ",\n";
92             std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdGain
93                       << ",\n";
94             std::cout << "\t\t\t{" << pidconf.second.pidInfo.integralLimit.min
95                       << "," << pidconf.second.pidInfo.integralLimit.max
96                       << "},\n";
97             std::cout << "\t\t\t{" << pidconf.second.pidInfo.outLim.min << ","
98                       << pidconf.second.pidInfo.outLim.max << "},\n";
99             std::cout << "\t\t\t" << pidconf.second.pidInfo.slewNeg << ",\n";
100             std::cout << "\t\t\t" << pidconf.second.pidInfo.slewPos << ",\n";
101             std::cout << "\t\t\t}\n\t\t}\n";
102         }
103         std::cout << "\t},\n";
104     }
105     std::cout << "}\n\n";
106 }
107 
108 std::vector<conf::SensorInput> spliceInputs(
109     const std::vector<std::string>& inputNames,
110     const std::vector<double>& inputTempToMargin,
111     const std::vector<std::string>& missingAcceptableNames)
112 {
113     std::vector<conf::SensorInput> results;
114 
115     // Default to TempToMargin and MissingIsAcceptable disabled
116     for (const auto& inputName : inputNames)
117     {
118         conf::SensorInput newInput{
119             inputName, std::numeric_limits<double>::quiet_NaN(), false, false};
120 
121         results.emplace_back(newInput);
122     }
123 
124     size_t resultSize = results.size();
125     size_t marginSize = inputTempToMargin.size();
126 
127     for (size_t index = 0; index < resultSize; ++index)
128     {
129         // If fewer doubles than strings, and vice versa, ignore remainder
130         if (index >= marginSize)
131         {
132             break;
133         }
134 
135         // Both vectors have this index, combine both into SensorInput
136         results[index].convertMarginZero = inputTempToMargin[index];
137         results[index].convertTempToMargin = true;
138     }
139 
140     std::set<std::string> acceptableSet;
141 
142     // Copy vector to set, to avoid O(n^2) runtime below
143     for (const auto& name : missingAcceptableNames)
144     {
145         acceptableSet.emplace(name);
146     }
147 
148     // Flag missingIsAcceptable true if name found in that set
149     for (auto& result : results)
150     {
151         if (acceptableSet.find(result.name) != acceptableSet.end())
152         {
153             result.missingIsAcceptable = true;
154         }
155     }
156 
157     return results;
158 }
159 
160 std::vector<std::string> splitNames(
161     const std::vector<conf::SensorInput>& sensorInputs)
162 {
163     std::vector<std::string> results;
164 
165     results.reserve(sensorInputs.size());
166     for (const auto& sensorInput : sensorInputs)
167     {
168         results.emplace_back(sensorInput.name);
169     }
170 
171     return results;
172 }
173 
174 } // namespace pid_control
175