Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [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 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 17 | #include "drive.hpp" |
| 18 | |
| 19 | #include "interfaces.hpp" |
| 20 | #include "sensors/pluggable.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 21 | #include "sysfs/sysfsread.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 22 | #include "sysfs/sysfswrite.hpp" |
| 23 | |
| 24 | #include <iostream> |
| 25 | #include <memory> |
| 26 | #include <tuple> |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 27 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 28 | namespace pid_control |
| 29 | { |
| 30 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 31 | using tstamp = std::chrono::high_resolution_clock::time_point; |
| 32 | |
| 33 | #define DRIVE_TIME 1 |
| 34 | #define DRIVE_GOAL 2 |
| 35 | #define DRIVE DRIVE_TIME |
| 36 | #define MAX_PWM 255 |
| 37 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 38 | static std::unique_ptr<Sensor> Create(std::string readpath, |
| 39 | std::string writepath) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 40 | { |
| 41 | return std::make_unique<PluggableSensor>( |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 42 | readpath, 0, /* default the timeout to disabled */ |
| 43 | std::make_unique<SysFsRead>(readpath), |
| 44 | std::make_unique<SysFsWrite>(writepath, 0, MAX_PWM)); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int64_t getAverage(std::tuple<tstamp, int64_t, int64_t>& values) |
| 48 | { |
| 49 | return (std::get<1>(values) + std::get<2>(values)) / 2; |
| 50 | } |
| 51 | |
| 52 | bool valueClose(int64_t value, int64_t goal) |
| 53 | { |
| 54 | #if 0 |
| 55 | int64_t delta = 100; /* within 100 */ |
| 56 | if (value < (goal + delta) && |
| 57 | value > (goal - delta)) |
| 58 | { |
| 59 | return true; |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | /* let's make sure it's below goal. */ |
| 64 | if (value < goal) |
| 65 | { |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | return false; |
| 70 | } |
| 71 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 72 | static void driveGoal(int64_t& seriesCnt, int64_t setPwm, int64_t goal, |
| 73 | std::vector<std::tuple<tstamp, int64_t, int64_t>>& series, |
| 74 | std::vector<std::unique_ptr<Sensor>>& fanSensors) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 75 | { |
| 76 | bool reading = true; |
| 77 | |
| 78 | auto& fan0 = fanSensors.at(0); |
| 79 | auto& fan1 = fanSensors.at(1); |
| 80 | |
| 81 | fan0->write(setPwm); |
| 82 | fan1->write(setPwm); |
| 83 | |
| 84 | while (reading) |
| 85 | { |
Patrick Venture | 6d31049 | 2019-03-11 08:54:51 -0700 | [diff] [blame] | 86 | bool check; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 87 | ReadReturn r0 = fan0->read(); |
| 88 | ReadReturn r1 = fan1->read(); |
| 89 | int64_t n0 = static_cast<int64_t>(r0.value); |
| 90 | int64_t n1 = static_cast<int64_t>(r1.value); |
| 91 | |
| 92 | tstamp t1 = std::chrono::high_resolution_clock::now(); |
| 93 | |
| 94 | series.push_back(std::make_tuple(t1, n0, n1)); |
| 95 | seriesCnt += 1; |
| 96 | |
| 97 | int64_t avgn = (n0 + n1) / 2; |
| 98 | /* check last three values against goal if this is close */ |
| 99 | check = valueClose(avgn, goal); |
| 100 | |
| 101 | /* We know the last entry is within range. */ |
| 102 | if (check && seriesCnt > 3) |
| 103 | { |
| 104 | /* n-2 values */ |
| 105 | std::tuple<tstamp, int64_t, int64_t> nm2 = series.at(seriesCnt - 3); |
| 106 | /* n-1 values */ |
| 107 | std::tuple<tstamp, int64_t, int64_t> nm1 = series.at(seriesCnt - 2); |
| 108 | |
| 109 | int64_t avgnm2 = getAverage(nm2); |
| 110 | int64_t avgnm1 = getAverage(nm1); |
| 111 | |
| 112 | int64_t together = (avgnm2 + avgnm1) / 2; |
| 113 | |
| 114 | reading = !valueClose(together, goal); |
| 115 | |
| 116 | if (!reading) |
| 117 | { |
| 118 | std::cerr << "finished reaching goal\n"; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* Early abort for testing. */ |
| 123 | if (seriesCnt > 150000) |
| 124 | { |
| 125 | std::cerr << "aborting after 150000 reads.\n"; |
| 126 | reading = false; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | return; |
| 131 | } |
| 132 | |
Harvey.Wu | a1ae4fa | 2022-10-28 17:38:35 +0800 | [diff] [blame] | 133 | static void driveTime([[maybe_unused]] int64_t& seriesCnt, int64_t setPwm, |
| 134 | [[maybe_unused]] int64_t goal, |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 135 | std::vector<std::tuple<tstamp, int64_t, int64_t>>& series, |
| 136 | std::vector<std::unique_ptr<Sensor>>& fanSensors) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 137 | { |
| 138 | using namespace std::literals::chrono_literals; |
| 139 | |
| 140 | bool reading = true; |
| 141 | |
| 142 | auto& fan0 = fanSensors.at(0); |
| 143 | auto& fan1 = fanSensors.at(1); |
| 144 | |
| 145 | auto& s0 = series.at(0); |
| 146 | tstamp t0 = std::get<0>(s0); |
| 147 | |
| 148 | fan0->write(setPwm); |
| 149 | fan1->write(setPwm); |
| 150 | |
| 151 | while (reading) |
| 152 | { |
| 153 | ReadReturn r0 = fan0->read(); |
| 154 | ReadReturn r1 = fan1->read(); |
| 155 | int64_t n0 = static_cast<int64_t>(r0.value); |
| 156 | int64_t n1 = static_cast<int64_t>(r1.value); |
| 157 | tstamp t1 = std::chrono::high_resolution_clock::now(); |
| 158 | |
| 159 | series.push_back(std::make_tuple(t1, n0, n1)); |
| 160 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 161 | auto duration = |
| 162 | std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0) |
| 163 | .count(); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 164 | if (duration >= (20000000us).count()) |
| 165 | { |
| 166 | reading = false; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | int driveMain(void) |
| 174 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 175 | /* Time series of the data, the timestamp after both are read and the |
| 176 | * values. */ |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 177 | std::vector<std::tuple<tstamp, int64_t, int64_t>> series; |
| 178 | int64_t seriesCnt = 0; /* in case vector count isn't constant time */ |
| 179 | int drive = DRIVE; |
| 180 | |
| 181 | /* |
| 182 | * The fan map: |
| 183 | * --> 0 | 4 |
| 184 | * --> 1 | 5 |
| 185 | * --> 2 | 6 |
| 186 | * --> 3 | 7 |
| 187 | */ |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 188 | std::vector<std::string> fans = {"/sys/class/hwmon/hwmon0/fan0_input", |
| 189 | "/sys/class/hwmon/hwmon0/fan4_input"}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 190 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 191 | std::vector<std::string> pwms = {"/sys/class/hwmon/hwmon0/pwm0", |
| 192 | "/sys/class/hwmon/hwmon0/pwm4"}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 193 | |
| 194 | std::vector<std::unique_ptr<Sensor>> fanSensors; |
| 195 | |
| 196 | auto fan0 = Create(fans[0], pwms[0]); |
| 197 | auto fan1 = Create(fans[1], pwms[1]); |
| 198 | |
| 199 | ReadReturn r0 = fan0->read(); |
| 200 | ReadReturn r1 = fan1->read(); |
| 201 | int64_t pwm0_value = static_cast<int64_t>(r0.value); |
| 202 | int64_t pwm1_value = static_cast<int64_t>(r1.value); |
| 203 | |
| 204 | if (MAX_PWM != pwm0_value || MAX_PWM != pwm1_value) |
| 205 | { |
| 206 | std::cerr << "bad PWM starting point.\n"; |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
| 210 | r0 = fan0->read(); |
| 211 | r1 = fan1->read(); |
| 212 | int64_t fan0_start = r0.value; |
| 213 | int64_t fan1_start = r1.value; |
| 214 | tstamp t1 = std::chrono::high_resolution_clock::now(); |
| 215 | |
| 216 | /* |
| 217 | * I've done experiments, and seen 9080,10243 as a starting point |
| 218 | * which leads to a 50% goal of 4830.5, which is higher than the |
| 219 | * average that they reach, 4668. -- i guess i could try to figure out |
| 220 | * a good increase from one to the other, but how fast they're going |
| 221 | * actually influences how much they influence, so at slower speeds the |
| 222 | * improvement is less. |
| 223 | */ |
| 224 | |
| 225 | series.push_back(std::make_tuple(t1, fan0_start, fan1_start)); |
| 226 | seriesCnt += 1; |
| 227 | |
| 228 | int64_t average = (fan0_start + fan1_start) / 2; |
| 229 | int64_t goal = 0.5 * average; |
| 230 | |
| 231 | std::cerr << "goal: " << goal << "\n"; |
| 232 | |
| 233 | // fan0 @ 128: 4691 |
| 234 | // fan4 @ 128: 4707 |
| 235 | |
| 236 | fanSensors.push_back(std::move(fan0)); |
| 237 | fanSensors.push_back(std::move(fan1)); |
| 238 | |
| 239 | if (DRIVE_TIME == drive) |
| 240 | { |
| 241 | driveTime(seriesCnt, 128, goal, series, fanSensors); |
| 242 | } |
| 243 | else if (DRIVE_GOAL == drive) |
| 244 | { |
| 245 | driveGoal(seriesCnt, 128, goal, series, fanSensors); |
| 246 | } |
| 247 | tstamp tp = t1; |
| 248 | |
| 249 | /* Output the values and the timepoints as a time series for review. */ |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 250 | for (const auto& t : series) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 251 | { |
| 252 | tstamp ts = std::get<0>(t); |
| 253 | int64_t n0 = std::get<1>(t); |
| 254 | int64_t n1 = std::get<2>(t); |
| 255 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 256 | auto duration = |
| 257 | std::chrono::duration_cast<std::chrono::microseconds>(ts - tp) |
| 258 | .count(); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 259 | std::cout << duration << "us, " << n0 << ", " << n1 << "\n"; |
| 260 | |
| 261 | tp = ts; |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 266 | |
| 267 | } // namespace pid_control |