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 "drive.hpp"
18
19 #include "interfaces.hpp"
20 #include "sensor.hpp"
21 #include "sensors/pluggable.hpp"
22 #include "sysfs/sysfsread.hpp"
23 #include "sysfs/sysfswrite.hpp"
24
25 #include <cerrno>
26 #include <chrono>
27 #include <cstdint>
28 #include <iostream>
29 #include <memory>
30 #include <string>
31 #include <tuple>
32 #include <utility>
33 #include <vector>
34
35 namespace pid_control
36 {
37
38 using 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
Create(const std::string & readpath,const std::string & writepath)45 static std::unique_ptr<Sensor> Create(const std::string& readpath,
46 const std::string& writepath)
47 {
48 return std::make_unique<PluggableSensor>(
49 readpath, 0, /* default the timeout to disabled */
50 std::make_unique<SysFsRead>(readpath),
51 std::make_unique<SysFsWrite>(writepath, 0, MAX_PWM));
52 }
53
getAverage(std::tuple<tstamp,int64_t,int64_t> & values)54 int64_t getAverage(std::tuple<tstamp, int64_t, int64_t>& values)
55 {
56 return (std::get<1>(values) + std::get<2>(values)) / 2;
57 }
58
valueClose(int64_t value,int64_t goal)59 bool 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
driveGoal(int64_t & seriesCnt,int64_t setPwm,int64_t goal,std::vector<std::tuple<tstamp,int64_t,int64_t>> & series,std::vector<std::unique_ptr<Sensor>> & fanSensors)79 static 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)
82 {
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 {
93 bool check;
94 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
101 series.emplace_back(t1, n0, n1);
102 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
driveTime(int64_t & seriesCnt,int64_t setPwm,int64_t goal,std::vector<std::tuple<tstamp,int64_t,int64_t>> & series,std::vector<std::unique_ptr<Sensor>> & fanSensors)140 static void driveTime([[maybe_unused]] int64_t& seriesCnt, int64_t setPwm,
141 [[maybe_unused]] int64_t goal,
142 std::vector<std::tuple<tstamp, int64_t, int64_t>>& series,
143 std::vector<std::unique_ptr<Sensor>>& fanSensors)
144 {
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
166 series.emplace_back(t1, n0, n1);
167
168 auto duration =
169 std::chrono::duration_cast<std::chrono::microseconds>(t1 - t0)
170 .count();
171 if (duration >= (20000000us).count())
172 {
173 reading = false;
174 }
175 }
176
177 return;
178 }
179
driveMain(void)180 int driveMain(void)
181 {
182 /* Time series of the data, the timestamp after both are read and the
183 * values. */
184 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 */
195 std::vector<std::string> fans = {"/sys/class/hwmon/hwmon0/fan0_input",
196 "/sys/class/hwmon/hwmon0/fan4_input"};
197
198 std::vector<std::string> pwms = {"/sys/class/hwmon/hwmon0/pwm0",
199 "/sys/class/hwmon/hwmon0/pwm4"};
200
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
232 series.emplace_back(t1, fan0_start, fan1_start);
233 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. */
257 for (const auto& t : series)
258 {
259 tstamp ts = std::get<0>(t);
260 int64_t n0 = std::get<1>(t);
261 int64_t n1 = std::get<2>(t);
262
263 auto duration =
264 std::chrono::duration_cast<std::chrono::microseconds>(ts - tp)
265 .count();
266 std::cout << duration << "us, " << n0 << ", " << n1 << "\n";
267
268 tp = ts;
269 }
270
271 return 0;
272 }
273
274 } // namespace pid_control
275