blob: bced2c94e30ac4142457f0c5288ca6d13c120bde [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 << ", ";
52 std::cout << pair.second.unavailableAsFailed << "},\n\t},\n";
Patrick Venture39199b42020-10-08 14:40:29 -070053 }
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 {
Josh Lehan31058fd2023-01-13 11:06:16 -080076 std::cout << "\n\t\t\t" << input.name;
77 if (input.convertTempToMargin)
78 {
79 std::cout << "[" << input.convertMarginZero << "]";
80 }
81 std::cout << ",\n";
Patrick Venture39199b42020-10-08 14:40:29 -070082 }
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
Patrick Williams19300272025-02-01 08:22:48 -0500108std::vector<conf::SensorInput> spliceInputs(
109 const std::vector<std::string>& inputNames,
110 const std::vector<double>& inputTempToMargin,
111 const std::vector<std::string>& missingAcceptableNames)
Josh Lehan31058fd2023-01-13 11:06:16 -0800112{
113 std::vector<conf::SensorInput> results;
114
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800115 // Default to TempToMargin and MissingIsAcceptable disabled
Josh Lehan31058fd2023-01-13 11:06:16 -0800116 for (const auto& inputName : inputNames)
117 {
118 conf::SensorInput newInput{
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800119 inputName, std::numeric_limits<double>::quiet_NaN(), false, false};
Josh Lehan31058fd2023-01-13 11:06:16 -0800120
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
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800140 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
Josh Lehan31058fd2023-01-13 11:06:16 -0800157 return results;
158}
159
Patrick Williams19300272025-02-01 08:22:48 -0500160std::vector<std::string> splitNames(
161 const std::vector<conf::SensorInput>& sensorInputs)
Josh Lehan31058fd2023-01-13 11:06:16 -0800162{
163 std::vector<std::string> results;
164
Ed Tanousd2768c52025-06-26 11:42:57 -0700165 results.reserve(sensorInputs.size());
Josh Lehan31058fd2023-01-13 11:06:16 -0800166 for (const auto& sensorInput : sensorInputs)
167 {
168 results.emplace_back(sensorInput.name);
169 }
170
171 return results;
172}
173
Patrick Venture39199b42020-10-08 14:40:29 -0700174} // namespace pid_control