Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2017 Google Inc. |
| 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 | |
| 17 | #include "pid/builderconfig.hpp" |
| 18 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 19 | #include "conf.hpp" |
| 20 | #include "pid/builder.hpp" |
| 21 | |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 22 | #include <fstream> |
| 23 | #include <iostream> |
| 24 | #include <libconfig.h++> |
| 25 | #include <memory> |
| 26 | #include <sdbusplus/bus.hpp> |
| 27 | #include <string> |
| 28 | #include <unordered_map> |
| 29 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 30 | std::unordered_map<int64_t, std::unique_ptr<PIDZone>> |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 31 | buildZonesFromConfig(const std::string& path, SensorManager& mgr, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 32 | sdbusplus::bus::bus& modeControlBus) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 33 | { |
| 34 | using namespace libconfig; |
| 35 | // zone -> pids |
| 36 | std::map<int64_t, PIDConf> pidConfig; |
| 37 | // zone -> configs |
Patrick Venture | f325231 | 2018-10-30 08:42:53 -0700 | [diff] [blame] | 38 | std::map<int64_t, struct ZoneConfig> zoneConfig; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 39 | |
| 40 | std::cerr << "entered BuildZonesFromConfig\n"; |
| 41 | |
| 42 | Config cfg; |
| 43 | |
| 44 | /* The load was modeled after the example source provided. */ |
| 45 | try |
| 46 | { |
| 47 | cfg.readFile(path.c_str()); |
| 48 | } |
| 49 | catch (const FileIOException& fioex) |
| 50 | { |
| 51 | std::cerr << "I/O error while reading file: " << fioex.what() |
| 52 | << std::endl; |
| 53 | throw; |
| 54 | } |
| 55 | catch (const ParseException& pex) |
| 56 | { |
| 57 | std::cerr << "Parse error at " << pex.getFile() << ":" << pex.getLine() |
| 58 | << " - " << pex.getError() << std::endl; |
| 59 | throw; |
| 60 | } |
| 61 | |
| 62 | try |
| 63 | { |
| 64 | const Setting& root = cfg.getRoot(); |
| 65 | const Setting& zones = root["zones"]; |
| 66 | int count = zones.getLength(); |
| 67 | |
| 68 | /* For each zone. */ |
| 69 | for (int i = 0; i < count; ++i) |
| 70 | { |
| 71 | const Setting& zoneSettings = zones[i]; |
| 72 | |
| 73 | int id; |
| 74 | PIDConf thisZone; |
Patrick Venture | f325231 | 2018-10-30 08:42:53 -0700 | [diff] [blame] | 75 | struct ZoneConfig thisZoneConfig; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 76 | |
| 77 | zoneSettings.lookupValue("id", id); |
| 78 | |
Patrick Venture | 8e2fdb3 | 2019-02-11 09:39:59 -0800 | [diff] [blame] | 79 | thisZoneConfig.minThermalRpm = zoneSettings.lookup("minThermalRpm"); |
| 80 | thisZoneConfig.failsafePercent = |
| 81 | zoneSettings.lookup("failsafePercent"); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 82 | |
| 83 | const Setting& pids = zoneSettings["pids"]; |
| 84 | int pidCount = pids.getLength(); |
| 85 | |
| 86 | for (int j = 0; j < pidCount; ++j) |
| 87 | { |
| 88 | const Setting& pid = pids[j]; |
| 89 | |
| 90 | std::string name; |
Patrick Venture | f325231 | 2018-10-30 08:42:53 -0700 | [diff] [blame] | 91 | struct ControllerInfo info; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 92 | |
| 93 | /* |
| 94 | * Mysteriously if you use lookupValue on these, and the type |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 95 | * is double. It won't work right. |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 96 | * |
| 97 | * If the configuration file value doesn't look explicitly like |
Patrick Venture | 5f59c0f | 2018-11-11 12:55:14 -0800 | [diff] [blame] | 98 | * a double it won't let you assign it to one. |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 99 | */ |
| 100 | name = pid.lookup("name").c_str(); |
| 101 | info.type = pid.lookup("type").c_str(); |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 102 | /* setpoint is only required to be set for thermal. */ |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 103 | /* TODO(venture): Verify this works optionally here. */ |
Patrick Venture | 7280e27 | 2019-02-11 10:45:32 -0800 | [diff] [blame] | 104 | info.setpoint = pid.lookup("setpoint"); |
Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 105 | info.pidInfo.ts = pid.lookup("pid.samplePeriod"); |
| 106 | info.pidInfo.proportionalCoeff = |
| 107 | pid.lookup("pid.proportionalCoeff"); |
| 108 | info.pidInfo.integralCoeff = pid.lookup("pid.integralCoeff"); |
| 109 | info.pidInfo.feedFwdOffset = |
| 110 | pid.lookup("pid.feedFwdOffOffsetCoeff"); |
| 111 | info.pidInfo.feedFwdGain = pid.lookup("pid.feedFwdGainCoeff"); |
| 112 | info.pidInfo.integralLimit.min = |
| 113 | pid.lookup("pid.integralCoeff.min"); |
| 114 | info.pidInfo.integralLimit.max = |
| 115 | pid.lookup("pid.integralCoeff.max"); |
| 116 | info.pidInfo.outLim.min = pid.lookup("pid.outLimit.min"); |
| 117 | info.pidInfo.outLim.max = pid.lookup("pid.outLimit.max"); |
| 118 | info.pidInfo.slewNeg = pid.lookup("pid.slewNeg"); |
| 119 | info.pidInfo.slewPos = pid.lookup("pid.slewPos"); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 120 | |
Patrick Venture | 7442c37 | 2019-02-11 10:21:05 -0800 | [diff] [blame] | 121 | std::cerr << "outLim.min: " << info.pidInfo.outLim.min << "\n"; |
| 122 | std::cerr << "outLim.max: " << info.pidInfo.outLim.max << "\n"; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 123 | |
| 124 | const Setting& inputs = pid["inputs"]; |
| 125 | int icount = inputs.getLength(); |
| 126 | |
| 127 | for (int z = 0; z < icount; ++z) |
| 128 | { |
| 129 | std::string v; |
| 130 | v = pid["inputs"][z].c_str(); |
| 131 | info.inputs.push_back(v); |
| 132 | } |
| 133 | |
| 134 | thisZone[name] = info; |
| 135 | } |
| 136 | |
| 137 | pidConfig[static_cast<int64_t>(id)] = thisZone; |
| 138 | zoneConfig[static_cast<int64_t>(id)] = thisZoneConfig; |
| 139 | } |
| 140 | } |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 141 | catch (const SettingTypeException& setex) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 142 | { |
| 143 | std::cerr << "Setting '" << setex.getPath() << "' type exception!" |
| 144 | << std::endl; |
| 145 | throw; |
| 146 | } |
| 147 | catch (const SettingNotFoundException& snex) |
| 148 | { |
| 149 | std::cerr << "Setting '" << snex.getPath() << "' not found!" |
| 150 | << std::endl; |
| 151 | throw; |
| 152 | } |
| 153 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 154 | return buildZones(pidConfig, zoneConfig, mgr, modeControlBus); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 155 | } |