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 | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 79 | thisZoneConfig.minthermalrpm = zoneSettings.lookup("minthermalrpm"); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 80 | thisZoneConfig.failsafepercent = |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 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 |
| 95 | * is float. It won't work right. |
| 96 | * |
| 97 | * If the configuration file value doesn't look explicitly like |
| 98 | * a float it won't let you assign it to one. |
| 99 | */ |
| 100 | name = pid.lookup("name").c_str(); |
| 101 | info.type = pid.lookup("type").c_str(); |
| 102 | /* set-point is only required to be set for thermal. */ |
| 103 | /* TODO(venture): Verify this works optionally here. */ |
| 104 | info.setpoint = pid.lookup("set-point"); |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 105 | info.pidInfo.ts = pid.lookup("pid.sampleperiod"); |
| 106 | info.pidInfo.p_c = pid.lookup("pid.p_coefficient"); |
| 107 | info.pidInfo.i_c = pid.lookup("pid.i_coefficient"); |
| 108 | info.pidInfo.ff_off = pid.lookup("pid.ff_off_coefficient"); |
| 109 | info.pidInfo.ff_gain = pid.lookup("pid.ff_gain_coefficient"); |
| 110 | info.pidInfo.i_lim.min = pid.lookup("pid.i_limit.min"); |
| 111 | info.pidInfo.i_lim.max = pid.lookup("pid.i_limit.max"); |
| 112 | info.pidInfo.out_lim.min = pid.lookup("pid.out_limit.min"); |
| 113 | info.pidInfo.out_lim.max = pid.lookup("pid.out_limit.max"); |
| 114 | info.pidInfo.slew_neg = pid.lookup("pid.slew_neg"); |
| 115 | info.pidInfo.slew_pos = pid.lookup("pid.slew_pos"); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 116 | |
James Feist | 22c257a | 2018-08-31 14:07:12 -0700 | [diff] [blame] | 117 | std::cerr << "out_lim.min: " << info.pidInfo.out_lim.min |
| 118 | << "\n"; |
| 119 | std::cerr << "out_lim.max: " << info.pidInfo.out_lim.max |
| 120 | << "\n"; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 121 | |
| 122 | const Setting& inputs = pid["inputs"]; |
| 123 | int icount = inputs.getLength(); |
| 124 | |
| 125 | for (int z = 0; z < icount; ++z) |
| 126 | { |
| 127 | std::string v; |
| 128 | v = pid["inputs"][z].c_str(); |
| 129 | info.inputs.push_back(v); |
| 130 | } |
| 131 | |
| 132 | thisZone[name] = info; |
| 133 | } |
| 134 | |
| 135 | pidConfig[static_cast<int64_t>(id)] = thisZone; |
| 136 | zoneConfig[static_cast<int64_t>(id)] = thisZoneConfig; |
| 137 | } |
| 138 | } |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 139 | catch (const SettingTypeException& setex) |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 140 | { |
| 141 | std::cerr << "Setting '" << setex.getPath() << "' type exception!" |
| 142 | << std::endl; |
| 143 | throw; |
| 144 | } |
| 145 | catch (const SettingNotFoundException& snex) |
| 146 | { |
| 147 | std::cerr << "Setting '" << snex.getPath() << "' not found!" |
| 148 | << std::endl; |
| 149 | throw; |
| 150 | } |
| 151 | |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 152 | return buildZones(pidConfig, zoneConfig, mgr, modeControlBus); |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 153 | } |