blob: 687a36ce461aee29feda7920992343a9943b84a5 [file] [log] [blame]
Patrick Venturee6206562018-03-08 15:36:53 -08001/**
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 Venturee6206562018-03-08 15:36:53 -080017#include "drive.hpp"
18
19#include "interfaces.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070020#include "sensor.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080021#include "sensors/pluggable.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080022#include "sysfs/sysfsread.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023#include "sysfs/sysfswrite.hpp"
24
Ed Tanousf8b6e552025-06-27 13:27:50 -070025#include <cerrno>
26#include <chrono>
27#include <cstdint>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070028#include <iostream>
29#include <memory>
Ed Tanousf8b6e552025-06-27 13:27:50 -070030#include <string>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031#include <tuple>
Ed Tanousf8b6e552025-06-27 13:27:50 -070032#include <utility>
33#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080034
Patrick Venturea0764872020-08-08 07:48:43 -070035namespace pid_control
36{
37
Patrick Venturee6206562018-03-08 15:36:53 -080038using tstamp = std::chrono::high_resolution_clock::time_point;
39
40#define DRIVE_TIME 1
41#define DRIVE_GOAL 2
42#define DRIVE DRIVE_TIME
43#define MAX_PWM 255
44
Ed Tanousd2768c52025-06-26 11:42:57 -070045static std::unique_ptr<Sensor> Create(const std::string& readpath,
46 const std::string& writepath)
Patrick Venturee6206562018-03-08 15:36:53 -080047{
48 return std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049 readpath, 0, /* default the timeout to disabled */
50 std::make_unique<SysFsRead>(readpath),
51 std::make_unique<SysFsWrite>(writepath, 0, MAX_PWM));
Patrick Venturee6206562018-03-08 15:36:53 -080052}
53
54int64_t getAverage(std::tuple<tstamp, int64_t, int64_t>& values)
55{
56 return (std::get<1>(values) + std::get<2>(values)) / 2;
57}
58
59bool valueClose(int64_t value, int64_t goal)
60{
61#if 0
62 int64_t delta = 100; /* within 100 */
63 if (value < (goal + delta) &&
64 value > (goal - delta))
65 {
66 return true;
67 }
68#endif
69
70 /* let's make sure it's below goal. */
71 if (value < goal)
72 {
73 return true;
74 }
75
76 return false;
77}
78
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070079static void driveGoal(int64_t& seriesCnt, int64_t setPwm, int64_t goal,
80 std::vector<std::tuple<tstamp, int64_t, int64_t>>& series,
81 std::vector<std::unique_ptr<Sensor>>& fanSensors)
Patrick Venturee6206562018-03-08 15:36:53 -080082{
83 bool reading = true;
84
85 auto& fan0 = fanSensors.at(0);
86 auto& fan1 = fanSensors.at(1);
87
88 fan0->write(setPwm);
89 fan1->write(setPwm);
90
91 while (reading)
92 {
Patrick Venture6d310492019-03-11 08:54:51 -070093 bool check;
Patrick Venturee6206562018-03-08 15:36:53 -080094 ReadReturn r0 = fan0->read();
95 ReadReturn r1 = fan1->read();
96 int64_t n0 = static_cast<int64_t>(r0.value);
97 int64_t n1 = static_cast<int64_t>(r1.value);
98
99 tstamp t1 = std::chrono::high_resolution_clock::now();
100
Ed Tanousd2768c52025-06-26 11:42:57 -0700101 series.emplace_back(t1, n0, n1);
Patrick Venturee6206562018-03-08 15:36:53 -0800102 seriesCnt += 1;
103
104 int64_t avgn = (n0 + n1) / 2;
105 /* check last three values against goal if this is close */
106 check = valueClose(avgn, goal);
107
108 /* We know the last entry is within range. */
109 if (check && seriesCnt > 3)
110 {
111 /* n-2 values */
112 std::tuple<tstamp, int64_t, int64_t> nm2 = series.at(seriesCnt - 3);
113 /* n-1 values */
114 std::tuple<tstamp, int64_t, int64_t> nm1 = series.at(seriesCnt - 2);
115
116 int64_t avgnm2 = getAverage(nm2);
117 int64_t avgnm1 = getAverage(nm1);
118
119 int64_t together = (avgnm2 + avgnm1) / 2;
120
121 reading = !valueClose(together, goal);
122
123 if (!reading)
124 {
125 std::cerr << "finished reaching goal\n";
126 }
127 }
128
129 /* Early abort for testing. */
130 if (seriesCnt > 150000)
131 {
132 std::cerr << "aborting after 150000 reads.\n";
133 reading = false;
134 }
135 }
136
137 return;
138}
139
Harvey.Wua1ae4fa2022-10-28 17:38:35 +0800140static void driveTime([[maybe_unused]] int64_t& seriesCnt, int64_t setPwm,
141 [[maybe_unused]] int64_t goal,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700142 std::vector<std::tuple<tstamp, int64_t, int64_t>>& series,
143 std::vector<std::unique_ptr<Sensor>>& fanSensors)
Patrick Venturee6206562018-03-08 15:36:53 -0800144{
145 using namespace std::literals::chrono_literals;
146
147 bool reading = true;
148
149 auto& fan0 = fanSensors.at(0);
150 auto& fan1 = fanSensors.at(1);
151
152 auto& s0 = series.at(0);
153 tstamp t0 = std::get<0>(s0);
154
155 fan0->write(setPwm);
156 fan1->write(setPwm);
157
158 while (reading)
159 {
160 ReadReturn r0 = fan0->read();
161 ReadReturn r1 = fan1->read();
162 int64_t n0 = static_cast<int64_t>(r0.value);
163 int64_t n1 = static_cast<int64_t>(r1.value);
164 tstamp t1 = std::chrono::high_resolution_clock::now();
165
Ed Tanousd2768c52025-06-26 11:42:57 -0700166 series.emplace_back(t1, n0, n1);
Patrick Venturee6206562018-03-08 15:36:53 -0800167
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700168 auto duration =
169 std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0)
170 .count();
Patrick Venturee6206562018-03-08 15:36:53 -0800171 if (duration >= (20000000us).count())
172 {
173 reading = false;
174 }
175 }
176
177 return;
178}
179
180int driveMain(void)
181{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700182 /* Time series of the data, the timestamp after both are read and the
183 * values. */
Patrick Venturee6206562018-03-08 15:36:53 -0800184 std::vector<std::tuple<tstamp, int64_t, int64_t>> series;
185 int64_t seriesCnt = 0; /* in case vector count isn't constant time */
186 int drive = DRIVE;
187
188 /*
189 * The fan map:
190 * --> 0 | 4
191 * --> 1 | 5
192 * --> 2 | 6
193 * --> 3 | 7
194 */
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700195 std::vector<std::string> fans = {"/sys/class/hwmon/hwmon0/fan0_input",
196 "/sys/class/hwmon/hwmon0/fan4_input"};
Patrick Venturee6206562018-03-08 15:36:53 -0800197
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700198 std::vector<std::string> pwms = {"/sys/class/hwmon/hwmon0/pwm0",
199 "/sys/class/hwmon/hwmon0/pwm4"};
Patrick Venturee6206562018-03-08 15:36:53 -0800200
201 std::vector<std::unique_ptr<Sensor>> fanSensors;
202
203 auto fan0 = Create(fans[0], pwms[0]);
204 auto fan1 = Create(fans[1], pwms[1]);
205
206 ReadReturn r0 = fan0->read();
207 ReadReturn r1 = fan1->read();
208 int64_t pwm0_value = static_cast<int64_t>(r0.value);
209 int64_t pwm1_value = static_cast<int64_t>(r1.value);
210
211 if (MAX_PWM != pwm0_value || MAX_PWM != pwm1_value)
212 {
213 std::cerr << "bad PWM starting point.\n";
214 return -EINVAL;
215 }
216
217 r0 = fan0->read();
218 r1 = fan1->read();
219 int64_t fan0_start = r0.value;
220 int64_t fan1_start = r1.value;
221 tstamp t1 = std::chrono::high_resolution_clock::now();
222
223 /*
224 * I've done experiments, and seen 9080,10243 as a starting point
225 * which leads to a 50% goal of 4830.5, which is higher than the
226 * average that they reach, 4668. -- i guess i could try to figure out
227 * a good increase from one to the other, but how fast they're going
228 * actually influences how much they influence, so at slower speeds the
229 * improvement is less.
230 */
231
Ed Tanousd2768c52025-06-26 11:42:57 -0700232 series.emplace_back(t1, fan0_start, fan1_start);
Patrick Venturee6206562018-03-08 15:36:53 -0800233 seriesCnt += 1;
234
235 int64_t average = (fan0_start + fan1_start) / 2;
236 int64_t goal = 0.5 * average;
237
238 std::cerr << "goal: " << goal << "\n";
239
240 // fan0 @ 128: 4691
241 // fan4 @ 128: 4707
242
243 fanSensors.push_back(std::move(fan0));
244 fanSensors.push_back(std::move(fan1));
245
246 if (DRIVE_TIME == drive)
247 {
248 driveTime(seriesCnt, 128, goal, series, fanSensors);
249 }
250 else if (DRIVE_GOAL == drive)
251 {
252 driveGoal(seriesCnt, 128, goal, series, fanSensors);
253 }
254 tstamp tp = t1;
255
256 /* Output the values and the timepoints as a time series for review. */
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700257 for (const auto& t : series)
Patrick Venturee6206562018-03-08 15:36:53 -0800258 {
259 tstamp ts = std::get<0>(t);
260 int64_t n0 = std::get<1>(t);
261 int64_t n1 = std::get<2>(t);
262
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700263 auto duration =
264 std::chrono::duration_cast<std::chrono::microseconds>(ts - tp)
265 .count();
Patrick Venturee6206562018-03-08 15:36:53 -0800266 std::cout << duration << "us, " << n0 << ", " << n1 << "\n";
267
268 tp = ts;
269 }
270
271 return 0;
272}
Patrick Venturea0764872020-08-08 07:48:43 -0700273
274} // namespace pid_control