Here is a script to patch ebike_app.c in order to modify when assistance begin to decrease with speed:
WARNING: if you set 20% of speed max assistance decrease at 25*0.8=20 and end at 25*1.2=30km/h tho its illegal regard to european law on street (even in street mode)
if you dont want it dont write "+ margin" in script bellow!
ie
uint16_t speed_limit_high = target_speed + margin;
uint16_t speed_limit_high = target_speed
my problem was assistance in cadence mode decreasing near 23 24 i wanted to retard the decrease.
mbrusa code
defines a transition window around the configured maximum speed. For example, if the maximum is set to 25 km/h, the code calculates:
- speed_limit_low = (25 – 2) × 10 = 230 (which corresponds to 23.0 km/h)
- speed_limit_high = (25 + 2) × 10 = 270 (which corresponds to 27.0 km/h)
Here's what the code does in that range:
- Below 23 km/h: Full assistance is provided.
- Between 23 km/h and 27 km/h: The code uses a mapping function to progressively reduce the target battery current (and therefore the motor assistance) as the speed increases. This means that assistance ramps down gradually within this window.
- Above 27 km/h: The assistance is completely cut off.
This design creates a soft transition—ensuring that the motor assistance decreases smoothly rather than being abruptly cut at exactly 25 km/h to help accommodate transient speed variations and sensor noise.
In contrast, strict legal regulations for electric bicycles (often for comfort and safety reasons) may require assistance to stop exactly at 25 km/h. However, the original implementation intentionally uses a ±2 km/h margin, cutting off assistance completely only when the measured speed exceeds 27 km/h (for a configured 25 km/h maximum).
So, to answer your question: according to this code, the motor assistance does not cut off abruptly at 25 km/h—it instead begins to fade between 23 and 27 km/h in street mode.
What i do: i choose a % of max speed and assistance begin to fade at max speed-% to end at max speed+%
I choosed 2%: for me in legal mode assistance begin to fade at 24.5km/h and stop at 25.5 km/h.
legaly there's a small marge, not specified in norme but 2% should be ok.
here is patch code (powershell): apply&compile&flash
# Ask for the desired margin percentage (between 0 and 20)
$MARGIN_PERCENT = [int](Read-Host "Enter the desired margin percentage (between 0 and 20)")
if ($MARGIN_PERCENT -lt 0 -or $MARGIN_PERCENT -gt 20) {
Write-Host "Error: The margin must be between 0% and 20%."
exit
}
# Name of the source file to modify
$SOURCE_FILE = "ebike_app.c"
# Verify that the source file exists
if (!(Test-Path $SOURCE_FILE)) {
Write-Host "Error: The file $SOURCE_FILE does not exist!"
exit
}
# Read the source file as an array of lines
$lines = Get-Content $SOURCE_FILE
# Search for all lines containing speed limit related variables (original or already patched)
# We look for any lines with "speed_limit_low", "speed_limit_high", "target_speed", or "margin"
$indices = @()
for ($i = 0; $i -lt $lines.Length; $i++) {
if ($lines[$i] -match "uint16_t\s+(speed_limit_low|speed_limit_high|target_speed|margin)\s*=") {
$indices += $i
}
}
if ($indices.Count -eq 0) {
Write-Host "No occurrence of 'speed_limit_low', 'speed_limit_high', 'target_speed', or 'margin' was found in the file."
exit
}
# Assume that the block containing these definitions is grouped together.
# If the block spans more than 10 lines, abort modification to avoid misidentification.
$blockStart = ($indices | Measure-Object -Minimum).Minimum
$blockEnd = ($indices | Measure-Object -Maximum).Maximum
if (($blockEnd - $blockStart) -gt 10) {
Write-Host "The detected block seems too large ($($blockEnd - $blockStart + 1) lines). The script cannot reliably identify it."
exit
}
# Construct the new code block using the proportional margin approach.
# Here, the target speed (in deci-km/h) is computed and then the margin is given as:
# margin = target_speed * (MARGIN_PERCENT / 100)
# Finally, speed_limit_low and speed_limit_high are calculated accordingly.
$replacementBlock = @"
uint16_t target_speed = m_configuration_variables.ui8_wheel_speed_max * 10;
uint16_t margin = (uint16_t)(target_speed * $($MARGIN_PERCENT) / 100.0); // margin of $MARGIN_PERCENT%
uint16_t speed_limit_low = target_speed - margin;
uint16_t speed_limit_high = target_speed + margin;
// write this for strict high marge:
//uint16_t speed_limit_high = target_speed
"@
# Split the replacement block into lines
$newBlockLines = $replacementBlock.Trim().Split("`n")
# Replace the detected block (from blockStart to blockEnd) with the new block
if ($blockStart -gt 0) {
$before = $lines[0..($blockStart - 1)]
} else {
$before = @()
}
if ($blockEnd -lt ($lines.Length - 1)) {
$after = $lines[($blockEnd + 1)..($lines.Length - 1)]
} else {
$after = @()
}
$lines = $before + $newBlockLines + $after
# Write the modified content back to the source file
$lines | Set-Content $SOURCE_FILE
# Display success message
Write-Host "Patch applied successfully! The new speed margin of $MARGIN_PERCENT% has been set in $SOURCE_FILE."
Pause