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