You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This PR implements ERG mode tuning and improvements developed in collaboration with Marc Roy. The changes focus on refining PID control, improving power table lookup logic, and adding proper type conversions for sensor data.
Key Changes:
Refactored ERG mode calculation to return position values instead of directly updating, enabling better control flow
Added proper rounding for float-to-int conversions in sensor data (cadence and power)
Improved ERG mode calculations with new PID gain adjustments, better time-to-target calculations, and negative result validation for homed systems
// I.E. If the difference between ERG target and Current watts were 30, and the Shift step is defined as 600 steps,
78
78
// and ERG_Sensitivity were 1.0, ERG mode would move the stepper motor 600 steps to compensate. With an ERG_Sensitivity of 2.0, the stepper
79
79
// would move 1200 steps to compensate, however ERG_Sensitivity values much different than 1.0 imply shiftStep has been improperly configured.
80
-
#defineERG_SENSITIVITY2.0f
80
+
#defineERG_SENSITIVITY3.0f
81
81
82
82
// Number of watts per shift expected by ERG mode for it's calculation. The user should target this number by adjusting Shift Step until WATTS_PER_SHIFT
83
83
// is obtained as closely as possible during each shift.
@@ -276,8 +276,8 @@ constexpr const char* ANY = "any";
276
276
// Uncomment to use guardrails for ERG mode in the stepper loop.
277
277
#defineERG_GUARDRAILS
278
278
279
-
//Uncomment to enable the use of the power table for ERG mode.
280
-
#defineERG_MODE_USE_POWER_TABLE
279
+
//Uncomment to enable the use of the power table for ERG mode.
280
+
//#define ERG_MODE_USE_POWER_TABLE
281
281
282
282
// Uncomment to use the PID controller for ERG mode.
// It's better to undershoot increasing watts and overshoot decreasing watts, so lets set the lookup target to the nearest side of POWERTABLE_WATT_INCREMENT
double Kp = userConfig->getERGSensitivity(); // Proportional gain based on user sensitivity
217
219
218
220
// retrieves the current Watt output
219
221
int watts = newWatts.getValue();
220
222
// retrieves target Watt output
221
223
int target = newWatts.getTarget();
222
224
// subtracting target from current watts
223
-
float error = target - watts;
225
+
int error = target - watts;
226
+
227
+
// modifying gains based on error
228
+
if (abs(error) < 10) {
229
+
Kp = Kp * 0.25; // decrease further for tiny errors
230
+
} elseif (abs(error) < 50) {
231
+
Kp = Kp * 0.75; // Moderate for medium errors
232
+
} elseif (abs(error) > 100) {
233
+
Kp = Kp * 1.25; // Aggressive for large errors
234
+
}
224
235
225
236
// Defining proportional term
226
-
float proportional = Kp * error;
237
+
double proportional = Kp * error;
227
238
if (newWatts.getValue() < userConfig->getMinWatts()) {
228
239
proportional = proportional * userConfig->getERGSensitivity(); // increase proportional term when at very low watts. Prevents Zwift from timeout on initial interval.
0 commit comments