blob: 5fdbcf48881cd84fc4762d9e4ec1435a26013290 [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
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700133static void driveTime(int64_t& seriesCnt, int64_t setPwm, int64_t goal,
134 std::vector<std::tuple<tstamp, int64_t, int64_t>>& series,
135 std::vector<std::unique_ptr<Sensor>>& fanSensors)
Patrick Venturee6206562018-03-08 15:36:53 -0800136{
137 using namespace std::literals::chrono_literals;
138
139 bool reading = true;
140
141 auto& fan0 = fanSensors.at(0);
142 auto& fan1 = fanSensors.at(1);
143
144 auto& s0 = series.at(0);
145 tstamp t0 = std::get<0>(s0);
146
147 fan0->write(setPwm);
148 fan1->write(setPwm);
149
150 while (reading)
151 {
152 ReadReturn r0 = fan0->read();
153 ReadReturn r1 = fan1->read();
154 int64_t n0 = static_cast<int64_t>(r0.value);
155 int64_t n1 = static_cast<int64_t>(r1.value);
156 tstamp t1 = std::chrono::high_resolution_clock::now();
157
158 series.push_back(std::make_tuple(t1, n0, n1));
159
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700160 auto duration =
161 std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0)
162 .count();
Patrick Venturee6206562018-03-08 15:36:53 -0800163 if (duration >= (20000000us).count())
164 {
165 reading = false;
166 }
167 }
168
169 return;
170}
171
172int driveMain(void)
173{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700174 /* Time series of the data, the timestamp after both are read and the
175 * values. */
Patrick Venturee6206562018-03-08 15:36:53 -0800176 std::vector<std::tuple<tstamp, int64_t, int64_t>> series;
177 int64_t seriesCnt = 0; /* in case vector count isn't constant time */
178 int drive = DRIVE;
179
180 /*
181 * The fan map:
182 * --> 0 | 4
183 * --> 1 | 5
184 * --> 2 | 6
185 * --> 3 | 7
186 */
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700187 std::vector<std::string> fans = {"/sys/class/hwmon/hwmon0/fan0_input",
188 "/sys/class/hwmon/hwmon0/fan4_input"};
Patrick Venturee6206562018-03-08 15:36:53 -0800189
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700190 std::vector<std::string> pwms = {"/sys/class/hwmon/hwmon0/pwm0",
191 "/sys/class/hwmon/hwmon0/pwm4"};
Patrick Venturee6206562018-03-08 15:36:53 -0800192
193 std::vector<std::unique_ptr<Sensor>> fanSensors;
194
195 auto fan0 = Create(fans[0], pwms[0]);
196 auto fan1 = Create(fans[1], pwms[1]);
197
198 ReadReturn r0 = fan0->read();
199 ReadReturn r1 = fan1->read();
200 int64_t pwm0_value = static_cast<int64_t>(r0.value);
201 int64_t pwm1_value = static_cast<int64_t>(r1.value);
202
203 if (MAX_PWM != pwm0_value || MAX_PWM != pwm1_value)
204 {
205 std::cerr << "bad PWM starting point.\n";
206 return -EINVAL;
207 }
208
209 r0 = fan0->read();
210 r1 = fan1->read();
211 int64_t fan0_start = r0.value;
212 int64_t fan1_start = r1.value;
213 tstamp t1 = std::chrono::high_resolution_clock::now();
214
215 /*
216 * I've done experiments, and seen 9080,10243 as a starting point
217 * which leads to a 50% goal of 4830.5, which is higher than the
218 * average that they reach, 4668. -- i guess i could try to figure out
219 * a good increase from one to the other, but how fast they're going
220 * actually influences how much they influence, so at slower speeds the
221 * improvement is less.
222 */
223
224 series.push_back(std::make_tuple(t1, fan0_start, fan1_start));
225 seriesCnt += 1;
226
227 int64_t average = (fan0_start + fan1_start) / 2;
228 int64_t goal = 0.5 * average;
229
230 std::cerr << "goal: " << goal << "\n";
231
232 // fan0 @ 128: 4691
233 // fan4 @ 128: 4707
234
235 fanSensors.push_back(std::move(fan0));
236 fanSensors.push_back(std::move(fan1));
237
238 if (DRIVE_TIME == drive)
239 {
240 driveTime(seriesCnt, 128, goal, series, fanSensors);
241 }
242 else if (DRIVE_GOAL == drive)
243 {
244 driveGoal(seriesCnt, 128, goal, series, fanSensors);
245 }
246 tstamp tp = t1;
247
248 /* Output the values and the timepoints as a time series for review. */
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700249 for (const auto& t : series)
Patrick Venturee6206562018-03-08 15:36:53 -0800250 {
251 tstamp ts = std::get<0>(t);
252 int64_t n0 = std::get<1>(t);
253 int64_t n1 = std::get<2>(t);
254
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700255 auto duration =
256 std::chrono::duration_cast<std::chrono::microseconds>(ts - tp)
257 .count();
Patrick Venturee6206562018-03-08 15:36:53 -0800258 std::cout << duration << "us, " << n0 << ", " << n1 << "\n";
259
260 tp = ts;
261 }
262
263 return 0;
264}
Patrick Venturea0764872020-08-08 07:48:43 -0700265
266} // namespace pid_control