Racing Line Optimization - compare your best lap with iteratively generated optimal racing lines

So I want to start off by saying that this project for the time being is still very much under development and I’m not even sure how advanced I want it to get yet, or exactly how much more work I want to put in it.

I wrote some MATLAB code to calculate optimal lap times and optimize racing lines using the CasADi solver. It allows the import of RaceChrono telemetry data and comparing it directly against the simulated optimal line.

You can give it your vehicle parameters:

  • Lateral acceleration
  • Longitudinal acceleration (forward)
  • Longitudinal deceleration (braking)
  • Lateral jerk
  • Longitudinal jerk
  • Minimum turning radius

In the figure above the limits for these parameters were taken from the racechrono log, that way the optimization algorithm creates a perfect lap for similar vehicle dynamics. The logged lap is my 12.09 world record T-race lap for comparison.

After the constraints are set it generates the optimal racing line for the programmed track. Trying to keep the vehicle on one of the above mentioned limits all the time. At the moment I only have the T-race track implemented, as the reason why I even got started on this project is that I was wondering what the optimal lap of T-race actually is, and where I might be able to find more improvements.

You can find the project on github if you want to run it for yourself:

It is being developed in Matlab R2026a, don’t know whether it works in other versions.
You’ll need to download CasADi and add it to your matlab path for this to work.

It is possible to implement custom tracks, however only T-race is tested for now. Now with CasADi it can do ~500 nodes now and I also added interpolation between nodes if it’s needed for particularly large tracks.

10 Likes

Changed the underlying solver to CasADi so now it can do much more nodes per lap. Can also interpolate data between nodes now.

What if you tried adding ideal acceleration and deceleration zones or color the line like in race chrono. Guessing it’s way harder

1 Like

Implemented the colored lines

2 Likes

Damn that’s siiiick

1 Like

Now you know what you gotta do lol. Virtual Coach/Race Crew. Take all this data bridge RaceBox and VESC telemetry into an app that then helps you on pace, how off the line you are, getting on brakes and acceleration, etc talking to you through BT.

2 Likes

Would be very cool if I get there eventually. Once I get to the fully custom electronics part of my board it would make sense developing an app for it to have all the telemetry data conveniently accessible.

What I’m working on for now requires a PC with Matlab installed on it to check the data. Matlab has very powerful tools to work with data, and relatively easy to code as well, hence why I started here. I haven’t looked into it yet, but I have serious doubts about the optimal lap generation being viable on a phone, simply because of not having access to matlab’s vast toolbox. However I can certainly imagine using the matlab code to export the pregenerated ideal lap data and build just a comparison tool into the mobile app.

All that said, I am not a great programmer, so even if I end up making that app, don’t expect that to happen very soon.

1 Like

We just need tokens for programming lol

1 Like

Here is the current progress and the comparison to my best lap of today’s session. I also moved all the configuration settings to the first section and (to some degree) untangled the code for readability if anyone wants to download it. PS: you need matlab.

At this point the only implemented track is still T-race, but with some of the changes I made today it’s a lot more robust under the hood so it should work for other tracks as well without too many problems hopefully :crossed_fingers:

1 Like

Added speed traps like in racechrono (red is gps, green is optimal).

Also implemented speed dependent lateral jerk limits. In effect, it doesn’t simulate with a jerk of 4-5G/s in the middle of the straight now when starting the turn in (at speed, this would be way too scary), but still simulates with the high limit in the low speed corners, where it is actually being achieved according to the gps data. This slowed down the optimal line a bit, so it reflects reality better now.

1 Like

Added time delta ribbon. Green where I am gaining time, red where I am loosing time, transparent where I am keeping pace.

1 Like

Added a helper file for adding your own tracks relatively simply from a google maps satelite view screenshot:

And then to show the optimal line generation and the comparison features on these tracks: :smiley:

Overhauled how tracks are defined and now it can do any track. *elevation features are not implemented.

Also added top speed limit and max power limit so that high speed acceleration is reduced to realistic levels.

5 Likes

Could have fooled me… this looks amazing!

3 Likes

I’ve been working on a race coach, it’s working very well and I’m working on making it live in your headset

but it relies on having the data from somebody better than you to give advice.

It also is pretty basic and only measures entry / apex / exit speeds.

I’d love to try and implement your maths into it to improve the coaching and the data.

5 Likes

That’s amazing, especially the headset integration would be so good to have! We can take a look at the possibilities, I’ll DM you in a bit.

I can see two possibilities, one is to pregenerate many laps with various vehicle dynamics (probably easiest to implement) and match the closest. And then there’s the proper way which allows you to set your specific vehicle dynamics and generate laps for that - I’d prefer to take a look at the possibility of implementing it this way if it’s possible (have to check feasability), there’s a fair bit of computational requirement for this though and would most likely require a fair bit of work from both of us.

2 Likes