Skip to content

Plug back in the speed calculator#8

Merged
simonech merged 3 commits intomasterfrom
copilot/plug-speed-calculator-back-in
Feb 19, 2026
Merged

Plug back in the speed calculator#8
simonech merged 3 commits intomasterfrom
copilot/plug-speed-calculator-back-in

Conversation

Copy link
Contributor

Copilot AI commented Feb 19, 2026

  • Create Model/RideSegment.cs with duration, speed, power, and route segment reference
  • Create Services/RideCalculator.cs to calculate ride segments using physics formula
  • Update Program.cs to accept --power argument, use RideCalculator, output Total Time + Avg Speed
  • Update .github/workflows/build.yml to pass --power 200 and verify new output
  • Refactor: add CalculateVelocity(double power, double gradient) overload — gradient passed as decimal, no mutation of Parameters
  • Remove ClimbGrade from Parameters; gradient now flows as an explicit method argument
  • Harmonize gradient representation: always decimal throughout (no / 100 conversions)
Original prompt

This section details on the original issue you should resolve

<issue_title>Plug back in the speed calculator</issue_title>
<issue_description>Now that we have the segments, let's put back the speed estimate.

Create a "ride" model which is a list of ride segments.
Each ride segment has

  • time/duration
  • speed
  • power
  • and a reference to the route segment

Use the parameter object already in the repo as well as the physics formula to calculate a ride from all the route segments.

One small caveat: the current formula assumes grade as one of the parameters. Obviously now it must be taken from the grade of the segment.

Power before came from the original tcx, now it needs to be an argument of the CLI.
If power is not passed the app should fail.

Update the cli output by also adding the time taken, and avg speed.

Also update the test part of the workflow to include the new parameter. Use 200W for the test</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…flow

Co-authored-by: simonech <61557+simonech@users.noreply.github.com>
Copilot AI changed the title [WIP] Add speed calculator back to ride model with CLI updates Plug back in the speed calculator Feb 19, 2026
Copilot AI requested a review from simonech February 19, 2026 16:08
Comment on lines +22 to +24
_parameters.ClimbGrade = routeSegment.AverageGradient * 100;

double speedKmh = calculator.CalculateVelocity(powerWatts);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that parameters are mutated.
Can you refactor it into

double CalculateVelocity(double power, double gradient)

Also remove the climb grade parameter from the parameter object.

And while we are here:
The old parameter was a percentage, so it was divided by 100 before calculating the Atan.
The grade from the segment is already a decimal. So harmonise by always using a decimal without converting to and from percentage.

@copilot

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 929c1de:

  • ClimbGrade removed from Parameters
  • Added CalculateVelocity(double power, double gradient) (and a matching CalculatePower overload + private CalculateForces taking gradient directly)
  • Gradient is now always a decimal throughout — Math.Atan(gradient) with no / 100 anywhere
  • RideCalculator passes routeSegment.AverageGradient directly to the new overload; no more mutation

…from Parameters

Co-authored-by: simonech <61557+simonech@users.noreply.github.com>
Copilot AI requested a review from simonech February 19, 2026 16:31
@simonech simonech marked this pull request as ready for review February 19, 2026 16:37
Copilot AI review requested due to automatic review settings February 19, 2026 16:37
@simonech simonech merged commit 7b392b6 into master Feb 19, 2026
5 checks passed
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements a speed and time calculation feature for cycling routes by integrating physics-based velocity calculations with route segment data. It addresses issue #7 by creating a ride model that combines route segments with calculated speed and duration based on rider power input.

Changes:

  • Refactored VirtualSpeedCalculator to accept gradient as an explicit parameter instead of using a ClimbGrade property, harmonizing gradient representation as decimal values throughout
  • Added RideCalculator service to compute ride segments with speed, duration, and power for each route segment
  • Created RideSegment model to represent ride data (duration, speed, power) with a reference to the corresponding route segment
  • Enhanced CLI to require --power argument and display total time and average speed in addition to existing metrics
  • Updated CI workflow to pass --power 200 and verify new output fields

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
VirtualSpeed/VirtualSpeed/VirtualSpeedCalculator.cs Refactored to pass gradient as method parameter; removed ClimbGrade from Parameters class; added overloads for backward compatibility
VirtualSpeed/VirtualSpeed/Services/RideCalculator.cs New service that calculates ride segments by computing velocity for each route segment based on power and gradient
VirtualSpeed/VirtualSpeed/Model/RideSegment.cs New immutable record type containing duration, speed, power, and route segment reference
VirtualSpeed/VirtualSpeed/Program.cs Added --power argument parsing; integrated RideCalculator; added total time and average speed to output
.github/workflows/build.yml Updated test command to include --power 200 and verify Avg Speed appears in output

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +15 to 29
for (int i = 0; i < args.Length - 1; i++)
{
Console.WriteLine("Usage: app.exe --gpx <route.gpx>");
return;
if (args[i] == "--gpx")
filePath = args[i + 1];
else if (args[i] == "--power")
{
if (!double.TryParse(args[i + 1], System.Globalization.NumberStyles.Any,
System.Globalization.CultureInfo.InvariantCulture, out power))
{
Console.WriteLine("Error: --power must be a valid number.");
return;
}
hasPower = true;
}
}
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The argument parsing loop will skip the last argument if it's a flag. The condition i < args.Length - 1 means if someone passes --gpx file.gpx --power (without a value), the --power flag won't be checked. Additionally, if --power is the last argument with a value, it will work, but if --gpx is the last argument, it won't be processed. Consider changing the loop to check i < args.Length and then verify that i + 1 < args.Length before accessing args[i + 1] to provide better error handling.

Copilot uses AI. Check for mistakes.
Comment on lines 73 to 97
@@ -81,7 +91,7 @@ public double CalculateVelocity(double power)
lowervel = midvel;

midvel = (uppervel + lowervel) / 2.0;
midpow = CalculatePower(midvel);
midpow = CalculatePower(midvel, gradient);
} while (itcount++ < 100);

return midvel;
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The binary search for velocity starts with a range of -1000.0 to 1000.0 km/h, which could converge to a negative velocity for steep downhills with low power input. While the RideCalculator checks speedMs > 0 and sets duration to 0 in that case, this is a physically unrealistic scenario. Consider adding validation to ensure power is sufficient for the route, or clamping negative velocities to a minimum realistic speed, or providing a warning when this occurs.

Copilot uses AI. Check for mistakes.
{
Console.WriteLine("Error: --power must be a valid number.");
return;
}
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The power argument is parsed but not validated for positive values. A negative or zero power value would be physically meaningless and could cause issues in the velocity calculation. Consider adding validation to ensure power is a positive number (e.g., power <= 0) and providing a clear error message if it's not.

Suggested change
}
}
if (power <= 0)
{
Console.WriteLine("Error: --power must be a positive number (watts).");
return;
}

Copilot uses AI. Check for mistakes.
output=$(dotnet run --no-build --configuration Release -- --gpx routeWithDownhill.gpx --power 200)
echo "$output"
if echo "$output" | grep -q "Total Distance:"; then
if echo "$output" | grep -q "Total Distance:" && echo "$output" | grep -q "Avg Speed:"; then
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The workflow test checks for "Total Distance:" and "Avg Speed:" in the output, but doesn't verify "Total Time:" which is also a new output added in this PR. Consider adding a check for "Total Time:" to ensure all new outputs are being generated correctly.

Suggested change
if echo "$output" | grep -q "Total Distance:" && echo "$output" | grep -q "Avg Speed:"; then
if echo "$output" | grep -q "Total Distance:" && echo "$output" | grep -q "Avg Speed:" && echo "$output" | grep -q "Total Time:"; then

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Plug back in the speed calculator

3 participants