-
-
Notifications
You must be signed in to change notification settings - Fork 207
Expand file tree
/
Copy pathergtable.h
More file actions
329 lines (272 loc) · 10.6 KB
/
Copy pathergtable.h
File metadata and controls
329 lines (272 loc) · 10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#ifndef ERGTABLE_H
#define ERGTABLE_H
#include <QList>
#include <QSettings>
#include <QObject>
#include <QDebug>
#include <QDateTime>
#include <QMap>
#include <algorithm>
#include "qzsettings.h"
struct ergDataPoint {
uint16_t cadence = 0;
uint16_t wattage = 0;
uint16_t resistance = 0;
ergDataPoint() = default;
ergDataPoint(uint16_t c, uint16_t w, uint16_t r) : cadence(c), wattage(w), resistance(r) {}
};
Q_DECLARE_METATYPE(ergDataPoint)
struct CadenceResistancePair {
uint16_t cadence;
uint16_t resistance;
bool operator<(const CadenceResistancePair& other) const {
if (resistance != other.resistance) return resistance < other.resistance;
return cadence < other.cadence;
}
};
class WattageStats {
public:
static const int MAX_SAMPLES = 100;
static const int MIN_SAMPLES_REQUIRED = 10;
void addSample(uint16_t wattage) {
samples.append(wattage);
if (samples.size() > MAX_SAMPLES) {
samples.removeFirst();
}
medianNeedsUpdate = true;
}
uint16_t getMedian() {
if (!medianNeedsUpdate) return cachedMedian;
if (samples.isEmpty()) return 0;
QList<uint16_t> sortedSamples = samples;
std::sort(sortedSamples.begin(), sortedSamples.end());
int middle = sortedSamples.size() / 2;
if (sortedSamples.size() % 2 == 0) {
cachedMedian = (sortedSamples[middle-1] + sortedSamples[middle]) / 2;
} else {
cachedMedian = sortedSamples[middle];
}
medianNeedsUpdate = false;
return cachedMedian;
}
int sampleCount() const {
return samples.size();
}
void clear() {
samples.clear();
cachedMedian = 0;
medianNeedsUpdate = true;
}
private:
QList<uint16_t> samples;
uint16_t cachedMedian = 0;
bool medianNeedsUpdate = true;
};
class ergTable : public QObject {
Q_OBJECT
public:
ergTable(QObject *parent = nullptr) : QObject(parent) {
loadSettings();
}
~ergTable() {
saveSettings();
}
void reset() {
wattageData.clear();
consolidatedData.clear();
lastResistanceValue = 0xFFFF;
lastResistanceTime = QDateTime::currentDateTime();
// Clear the settings completely
QSettings settings;
settings.remove(QZSettings::ergDataPoints);
settings.sync();
}
void collectData(uint16_t cadence, uint16_t wattage, uint16_t resistance, bool ignoreResistanceTiming = false) {
if (resistance != lastResistanceValue) {
qDebug() << "resistance changed";
lastResistanceTime = QDateTime::currentDateTime();
lastResistanceValue = resistance;
}
if (lastResistanceTime.msecsTo(QDateTime::currentDateTime()) < 1000 && !ignoreResistanceTiming) {
qDebug() << "skipping collecting data due to resistance changing too fast";
return;
}
if (wattage > 0 && cadence > 0) {
CadenceResistancePair pair{cadence, resistance};
wattageData[pair].addSample(wattage);
if (wattageData[pair].sampleCount() >= WattageStats::MIN_SAMPLES_REQUIRED) {
updateDataTable(pair);
}
}
}
double estimateWattage(uint16_t givenCadence, uint16_t givenResistance) {
if (consolidatedData.isEmpty()) return 0;
// Get all points with matching resistance
QList<ergDataPoint> sameResPoints;
for (const auto& point : consolidatedData) {
if (point.resistance == givenResistance) {
sameResPoints.append(point);
}
}
// If no exact resistance match, find closest resistance
if (sameResPoints.isEmpty()) {
uint16_t minResDiff = UINT16_MAX;
uint16_t closestRes = 0;
for (const auto& point : consolidatedData) {
uint16_t resDiff = abs(int(point.resistance) - int(givenResistance));
if (resDiff < minResDiff) {
minResDiff = resDiff;
closestRes = point.resistance;
}
}
for (const auto& point : consolidatedData) {
if (point.resistance == closestRes) {
sameResPoints.append(point);
}
}
}
// Find points for interpolation
double lowerWatts = 0, upperWatts = 0;
uint16_t lowerCadence = 0, upperCadence = 0;
for (const auto& point : sameResPoints) {
if (point.cadence <= givenCadence && point.cadence > lowerCadence) {
lowerWatts = point.wattage;
lowerCadence = point.cadence;
}
if (point.cadence >= givenCadence && (upperCadence == 0 || point.cadence < upperCadence)) {
upperWatts = point.wattage;
upperCadence = point.cadence;
}
}
// Interpolate or use closest value
if (lowerCadence != 0 && upperCadence != 0 && lowerCadence != upperCadence) {
double ratio = (givenCadence - lowerCadence) / double(upperCadence - lowerCadence);
return lowerWatts + ratio * (upperWatts - lowerWatts);
} else if (lowerCadence != 0) {
return lowerWatts;
} else if (upperCadence != 0) {
return upperWatts;
}
// Fallback to closest point
return sameResPoints.first().wattage;
}
void setCadenceResistanceBandStep(uint16_t step) {
cadenceResistanceBandStep = step;
}
uint16_t resistanceFromPowerRequest(uint16_t power, uint16_t cadence, uint16_t maxResistance) {
if (cadenceResistanceBandStep > 1) {
cadence = (cadence / cadenceResistanceBandStep) * cadenceResistanceBandStep;
}
qDebug() << QStringLiteral("resistanceFromPowerRequest") << cadence;
if (cadence == 0)
return 1;
uint16_t best_resistance_match = 1;
int min_watt_difference = 1000;
for (uint16_t i = 1; i < maxResistance; i++) {
uint16_t current_watts = estimateWattage(cadence, i);
uint16_t next_watts = estimateWattage(cadence, i + 1);
if (current_watts <= power && next_watts >= power) {
qDebug() << current_watts << next_watts << power;
return i;
}
int diff = abs(current_watts - power);
if (diff < min_watt_difference) {
min_watt_difference = diff;
best_resistance_match = i;
qDebug() << QStringLiteral("best match") << best_resistance_match << "with watts" << current_watts << "diff" << diff;
}
}
qDebug() << "Bracketing not found, best match:" << best_resistance_match;
return best_resistance_match;
}
QList<ergDataPoint> getConsolidatedData() const {
return consolidatedData;
}
QMap<CadenceResistancePair, WattageStats> getWattageData() const {
return wattageData;
}
uint16_t getMaxResistance() const {
if (consolidatedData.isEmpty()) return 0;
uint16_t maxRes = 0;
for (const auto& point : consolidatedData) {
if (point.resistance > maxRes) {
maxRes = point.resistance;
}
}
return maxRes;
}
/**
* @brief Load default calibration data for a specific bike model.
* Only populates if the table is currently empty (won't overwrite user's learned data).
* Data format: "cadence|wattage|resistance;cadence|wattage|resistance;..."
*/
void loadDefaultData(const QString& defaultDataString) {
if (!consolidatedData.isEmpty()) {
qDebug() << "ergTable: skipping defaults, user data already exists ("
<< consolidatedData.size() << "points)";
return;
}
QStringList dataList = defaultDataString.split(";", Qt::SkipEmptyParts);
for (const QString& triple : dataList) {
QStringList fields = triple.split("|");
if (fields.size() == 3) {
uint16_t cadence = fields[0].toUInt();
uint16_t wattage = fields[1].toUInt();
uint16_t resistance = fields[2].toUInt();
consolidatedData.append(ergDataPoint(cadence, wattage, resistance));
}
}
qDebug() << "ergTable: loaded" << consolidatedData.size() << "default data points";
saveSettings();
}
private:
QMap<CadenceResistancePair, WattageStats> wattageData;
QList<ergDataPoint> consolidatedData;
uint16_t lastResistanceValue = 0xFFFF;
uint16_t cadenceResistanceBandStep = 0;
QDateTime lastResistanceTime = QDateTime::currentDateTime();
void updateDataTable(const CadenceResistancePair& pair) {
uint16_t medianWattage = wattageData[pair].getMedian();
// Remove existing point if it exists
for (int i = consolidatedData.size() - 1; i >= 0; --i) {
if (consolidatedData[i].cadence == pair.cadence &&
consolidatedData[i].resistance == pair.resistance) {
consolidatedData.removeAt(i);
break;
}
}
// Add new point
consolidatedData.append(ergDataPoint(pair.cadence, medianWattage, pair.resistance));
qDebug() << "Added/Updated point:"
<< "C:" << pair.cadence
<< "W:" << medianWattage
<< "R:" << pair.resistance;
saveSettings();
}
void loadSettings() {
QSettings settings;
QString data = settings.value(QZSettings::ergDataPoints,
QZSettings::default_ergDataPoints).toString();
QStringList dataList = data.split(";", Qt::SkipEmptyParts);
for (const QString& triple : dataList) {
QStringList fields = triple.split("|");
if (fields.size() == 3) {
uint16_t cadence = fields[0].toUInt();
uint16_t wattage = fields[1].toUInt();
uint16_t resistance = fields[2].toUInt();
consolidatedData.append(ergDataPoint(cadence, wattage, resistance));
}
}
}
void saveSettings() {
QSettings settings;
QStringList dataStrings;
for (const ergDataPoint& point : consolidatedData) {
dataStrings.append(QString("%1|%2|%3").arg(point.cadence)
.arg(point.wattage)
.arg(point.resistance));
}
settings.setValue(QZSettings::ergDataPoints, dataStrings.join(";"));
}
};
#endif // ERGTABLE_H