blob: 15ce05a507cbef0a2c9a83af8664f56346421d5b [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"
20#include "sensors/pluggable.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080021#include "sysfs/sysfsread.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070022#include "sysfs/sysfswrite.hpp"
23
24#include <iostream>
25#include <memory>
26#include <tuple>
Patrick Venturee6206562018-03-08 15:36:53 -080027
Patrick Venturea0764872020-08-08 07:48:43 -070028namespace pid_control
29{
30
Patrick Venturee6206562018-03-08 15:36:53 -080031using 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 Ventureda4a5dd2018-08-31 09:42:48 -070038static std::unique_ptr<Sensor> Create(std::string readpath,
39 std::string writepath)
Patrick Venturee6206562018-03-08 15:36:53 -080040{
41 return std::make_unique<PluggableSensor>(
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070042 readpath, 0, /* default the timeout to disabled */
43 std::make_unique<SysFsRead>(readpath),
44 std::make_unique<SysFsWrite>(writepath, 0, MAX_PWM));
Patrick Venturee6206562018-03-08 15:36:53 -080045}
46
47int64_t getAverage(std::tuple<tstamp, int64_t, int64_t>& values)
48{
49 return (std::get<1>(values) + std::get<2>(values)) / 2;
50}
51
52bool 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 Ventureda4a5dd2018-08-31 09:42:48 -070072static 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 Venturee6206562018-03-08 15:36:53 -080075{
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 Venture6d310492019-03-11 08:54:51 -070086 bool check;
Patrick Venturee6206562018-03-08 15:36:53 -080087 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.Wua1ae4fa2022-10-28 17:38:35 +0800133static void driveTime([[maybe_unused]] int64_t& seriesCnt, int64_t setPwm,
134 [[maybe_unused]] int64_t goal,
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700135 std::vector<std::tuple<tstamp, int64_t, int64_t>>& series,
136 std::vector<std::unique_ptr<Sensor>>& fanSensors)
Patrick Venturee6206562018-03-08 15:36:53 -0800137{
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 Ventureda4a5dd2018-08-31 09:42:48 -0700161 auto duration =
162 std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0)
163 .count();
Patrick Venturee6206562018-03-08 15:36:53 -0800164 if (duration >= (20000000us).count())
165 {
166 reading = false;
167 }
168 }
169
170 return;
171}
172
173int driveMain(void)
174{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700175 /* Time series of the data, the timestamp after both are read and the
176 * values. */
Patrick Venturee6206562018-03-08 15:36:53 -0800177 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 Ventureda4a5dd2018-08-31 09:42:48 -0700188 std::vector<std::string> fans = {"/sys/class/hwmon/hwmon0/fan0_input",
189 "/sys/class/hwmon/hwmon0/fan4_input"};
Patrick Venturee6206562018-03-08 15:36:53 -0800190
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700191 std::vector<std::string> pwms = {"/sys/class/hwmon/hwmon0/pwm0",
192 "/sys/class/hwmon/hwmon0/pwm4"};
Patrick Venturee6206562018-03-08 15:36:53 -0800193
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 Venture4a2dc4d2018-10-23 09:02:55 -0700250 for (const auto& t : series)
Patrick Venturee6206562018-03-08 15:36:53 -0800251 {
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 Ventureda4a5dd2018-08-31 09:42:48 -0700256 auto duration =
257 std::chrono::duration_cast<std::chrono::microseconds>(ts - tp)
258 .count();
Patrick Venturee6206562018-03-08 15:36:53 -0800259 std::cout << duration << "us, " << n0 << ", " << n1 << "\n";
260
261 tp = ts;
262 }
263
264 return 0;
265}
Patrick Venturea0764872020-08-08 07:48:43 -0700266
267} // namespace pid_control