blob: 61f8d4a2bc7110d951bb6ff84bdee953cd5475db [file] [log] [blame]
Patrick Venture39199b42020-10-08 14:40:29 -07001/*
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
Ed Tanousf8b6e552025-06-27 13:27:50 -070020#include <cstddef>
Patrick Venture39199b42020-10-08 14:40:29 -070021#include <cstdint>
22#include <iostream>
Ed Tanousf8b6e552025-06-27 13:27:50 -070023#include <limits>
Patrick Venture39199b42020-10-08 14:40:29 -070024#include <map>
Ed Tanousf8b6e552025-06-27 13:27:50 -070025#include <set>
Patrick Venture39199b42020-10-08 14:40:29 -070026#include <string>
Ed Tanousf8b6e552025-06-27 13:27:50 -070027#include <vector>
Patrick Venture39199b42020-10-08 14:40:29 -070028
29namespace pid_control
30{
31
32void 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 {
Patrick Venture39199b42020-10-08 14:40:29 -070045 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 << ", ";
Alex.Song8f73ad72021-10-07 00:18:27 +080051 std::cout << pair.second.timeout << ", ";
Potin Laie1fa8592025-08-29 15:27:08 +080052 std::cout << pair.second.unavailableAsFailed << ", ";
53 std::cout << pair.second.ignoreFailIfHostOff << "},\n\t},\n";
Patrick Venture39199b42020-10-08 14:40:29 -070054 }
55 std::cout << "}\n\n";
56 std::cout << "ZoneDetailsConfig\n";
57 std::cout << "{\n";
58 for (const auto& zone : zoneDetailsConfig)
59 {
60 std::cout << "\t{" << zone.first << ",\n";
61 std::cout << "\t\t{" << zone.second.minThermalOutput << ", ";
62 std::cout << zone.second.failsafePercent << "}\n\t},\n";
63 }
64 std::cout << "}\n\n";
65 std::cout << "ZoneConfig\n";
66 std::cout << "{\n";
67 for (const auto& zone : zoneConfig)
68 {
69 std::cout << "\t{" << zone.first << "\n";
70 for (const auto& pidconf : zone.second)
71 {
72 std::cout << "\t\t{" << pidconf.first << ",\n";
73 std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
74 std::cout << "\t\t\t{";
75 for (const auto& input : pidconf.second.inputs)
76 {
Josh Lehan31058fd2023-01-13 11:06:16 -080077 std::cout << "\n\t\t\t" << input.name;
78 if (input.convertTempToMargin)
79 {
80 std::cout << "[" << input.convertMarginZero << "]";
81 }
82 std::cout << ",\n";
Patrick Venture39199b42020-10-08 14:40:29 -070083 }
84 std::cout << "\t\t\t}\n";
85 std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
86 std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n";
87 std::cout << "\t\t\t" << pidconf.second.pidInfo.proportionalCoeff
88 << ",\n";
89 std::cout << "\t\t\t" << pidconf.second.pidInfo.integralCoeff
90 << ",\n";
91 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdOffset
92 << ",\n";
93 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdGain
94 << ",\n";
95 std::cout << "\t\t\t{" << pidconf.second.pidInfo.integralLimit.min
96 << "," << pidconf.second.pidInfo.integralLimit.max
97 << "},\n";
98 std::cout << "\t\t\t{" << pidconf.second.pidInfo.outLim.min << ","
99 << pidconf.second.pidInfo.outLim.max << "},\n";
100 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewNeg << ",\n";
101 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewPos << ",\n";
102 std::cout << "\t\t\t}\n\t\t}\n";
103 }
104 std::cout << "\t},\n";
105 }
106 std::cout << "}\n\n";
107}
108
Patrick Williams19300272025-02-01 08:22:48 -0500109std::vector<conf::SensorInput> spliceInputs(
110 const std::vector<std::string>& inputNames,
111 const std::vector<double>& inputTempToMargin,
112 const std::vector<std::string>& missingAcceptableNames)
Josh Lehan31058fd2023-01-13 11:06:16 -0800113{
114 std::vector<conf::SensorInput> results;
115
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800116 // Default to TempToMargin and MissingIsAcceptable disabled
Josh Lehan31058fd2023-01-13 11:06:16 -0800117 for (const auto& inputName : inputNames)
118 {
119 conf::SensorInput newInput{
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800120 inputName, std::numeric_limits<double>::quiet_NaN(), false, false};
Josh Lehan31058fd2023-01-13 11:06:16 -0800121
122 results.emplace_back(newInput);
123 }
124
125 size_t resultSize = results.size();
126 size_t marginSize = inputTempToMargin.size();
127
128 for (size_t index = 0; index < resultSize; ++index)
129 {
130 // If fewer doubles than strings, and vice versa, ignore remainder
131 if (index >= marginSize)
132 {
133 break;
134 }
135
136 // Both vectors have this index, combine both into SensorInput
137 results[index].convertMarginZero = inputTempToMargin[index];
138 results[index].convertTempToMargin = true;
139 }
140
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800141 std::set<std::string> acceptableSet;
142
143 // Copy vector to set, to avoid O(n^2) runtime below
144 for (const auto& name : missingAcceptableNames)
145 {
146 acceptableSet.emplace(name);
147 }
148
149 // Flag missingIsAcceptable true if name found in that set
150 for (auto& result : results)
151 {
152 if (acceptableSet.find(result.name) != acceptableSet.end())
153 {
154 result.missingIsAcceptable = true;
155 }
156 }
157
Josh Lehan31058fd2023-01-13 11:06:16 -0800158 return results;
159}
160
Patrick Williams19300272025-02-01 08:22:48 -0500161std::vector<std::string> splitNames(
162 const std::vector<conf::SensorInput>& sensorInputs)
Josh Lehan31058fd2023-01-13 11:06:16 -0800163{
164 std::vector<std::string> results;
165
Ed Tanousd2768c52025-06-26 11:42:57 -0700166 results.reserve(sensorInputs.size());
Josh Lehan31058fd2023-01-13 11:06:16 -0800167 for (const auto& sensorInput : sensorInputs)
168 {
169 results.emplace_back(sensorInput.name);
170 }
171
172 return results;
173}
174
Patrick Venture39199b42020-10-08 14:40:29 -0700175} // namespace pid_control