blob: 87c4debb5243d8dbdd7a1fa7c80485fe70ab31a9 [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
20#include <cstdint>
21#include <iostream>
22#include <map>
23#include <string>
24
25namespace pid_control
26{
27
28void debugPrint(const std::map<std::string, conf::SensorConfig>& sensorConfig,
29 const std::map<int64_t, conf::PIDConf>& zoneConfig,
30 const std::map<int64_t, conf::ZoneConfig>& zoneDetailsConfig)
31{
32 if constexpr (!conf::DEBUG)
33 {
34 return;
35 }
36 // print sensor config
37 std::cout << "sensor config:\n";
38 std::cout << "{\n";
39 for (const auto& pair : sensorConfig)
40 {
Patrick Venture39199b42020-10-08 14:40:29 -070041 std::cout << "\t{" << pair.first << ",\n\t\t{";
42 std::cout << pair.second.type << ", ";
43 std::cout << pair.second.readPath << ", ";
44 std::cout << pair.second.writePath << ", ";
45 std::cout << pair.second.min << ", ";
46 std::cout << pair.second.max << ", ";
Alex.Song8f73ad72021-10-07 00:18:27 +080047 std::cout << pair.second.timeout << ", ";
48 std::cout << pair.second.unavailableAsFailed << "},\n\t},\n";
Patrick Venture39199b42020-10-08 14:40:29 -070049 }
50 std::cout << "}\n\n";
51 std::cout << "ZoneDetailsConfig\n";
52 std::cout << "{\n";
53 for (const auto& zone : zoneDetailsConfig)
54 {
55 std::cout << "\t{" << zone.first << ",\n";
56 std::cout << "\t\t{" << zone.second.minThermalOutput << ", ";
57 std::cout << zone.second.failsafePercent << "}\n\t},\n";
58 }
59 std::cout << "}\n\n";
60 std::cout << "ZoneConfig\n";
61 std::cout << "{\n";
62 for (const auto& zone : zoneConfig)
63 {
64 std::cout << "\t{" << zone.first << "\n";
65 for (const auto& pidconf : zone.second)
66 {
67 std::cout << "\t\t{" << pidconf.first << ",\n";
68 std::cout << "\t\t\t{" << pidconf.second.type << ",\n";
69 std::cout << "\t\t\t{";
70 for (const auto& input : pidconf.second.inputs)
71 {
Josh Lehan31058fd2023-01-13 11:06:16 -080072 std::cout << "\n\t\t\t" << input.name;
73 if (input.convertTempToMargin)
74 {
75 std::cout << "[" << input.convertMarginZero << "]";
76 }
77 std::cout << ",\n";
Patrick Venture39199b42020-10-08 14:40:29 -070078 }
79 std::cout << "\t\t\t}\n";
80 std::cout << "\t\t\t" << pidconf.second.setpoint << ",\n";
81 std::cout << "\t\t\t{" << pidconf.second.pidInfo.ts << ",\n";
82 std::cout << "\t\t\t" << pidconf.second.pidInfo.proportionalCoeff
83 << ",\n";
84 std::cout << "\t\t\t" << pidconf.second.pidInfo.integralCoeff
85 << ",\n";
86 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdOffset
87 << ",\n";
88 std::cout << "\t\t\t" << pidconf.second.pidInfo.feedFwdGain
89 << ",\n";
90 std::cout << "\t\t\t{" << pidconf.second.pidInfo.integralLimit.min
91 << "," << pidconf.second.pidInfo.integralLimit.max
92 << "},\n";
93 std::cout << "\t\t\t{" << pidconf.second.pidInfo.outLim.min << ","
94 << pidconf.second.pidInfo.outLim.max << "},\n";
95 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewNeg << ",\n";
96 std::cout << "\t\t\t" << pidconf.second.pidInfo.slewPos << ",\n";
97 std::cout << "\t\t\t}\n\t\t}\n";
98 }
99 std::cout << "\t},\n";
100 }
101 std::cout << "}\n\n";
102}
103
Josh Lehan31058fd2023-01-13 11:06:16 -0800104std::vector<conf::SensorInput>
105 spliceInputs(const std::vector<std::string>& inputNames,
106 const std::vector<double>& inputTempToMargin)
107{
108 std::vector<conf::SensorInput> results;
109
110 // Default to the TempToMargin feature disabled
111 for (const auto& inputName : inputNames)
112 {
113 conf::SensorInput newInput{
114 inputName, std::numeric_limits<double>::quiet_NaN(), false};
115
116 results.emplace_back(newInput);
117 }
118
119 size_t resultSize = results.size();
120 size_t marginSize = inputTempToMargin.size();
121
122 for (size_t index = 0; index < resultSize; ++index)
123 {
124 // If fewer doubles than strings, and vice versa, ignore remainder
125 if (index >= marginSize)
126 {
127 break;
128 }
129
130 // Both vectors have this index, combine both into SensorInput
131 results[index].convertMarginZero = inputTempToMargin[index];
132 results[index].convertTempToMargin = true;
133 }
134
135 return results;
136}
137
138std::vector<std::string>
139 splitNames(const std::vector<conf::SensorInput>& sensorInputs)
140{
141 std::vector<std::string> results;
142
143 for (const auto& sensorInput : sensorInputs)
144 {
145 results.emplace_back(sensorInput.name);
146 }
147
148 return results;
149}
150
Patrick Venture39199b42020-10-08 14:40:29 -0700151} // namespace pid_control