Writing a lap time sim tool

10 views (last 30 days)
Nick Deeley
Nick Deeley on 22 Oct 2018
Commented: Guillaume on 6 Nov 2018
I'm trying to write a lap time sim tool for race tracks. I have it in excel, so, most of the maths is done, except for the parts where excel cant work stuff out (as far as I know) like braking and acceleration zones but that can be worked on at a later date.
The main problem is that I need to allow the user to input section lengths: corners and straights, and preferably in order.
But, as people who have very kindly helped so far have pointed out, I need to explain the whole problem :)
So, as my Matlab and programming knowledge is limited, I'd like to know what approaches I can do for this, what functions would I need, and how would I need to use them. While it would be great for code to be provided, I dont want to ask other people to do the work for me, and I wont learn anything, but, I do learn best from examples so any similar examples I can expand on would be great.
There could be upto 100 or so sections of the track, for the straights it's a straight forward length and calculation. For corners, I need to determine the radius of that corner, so, need to be able to identify which section is which.
After that, I should have a resulting value (the top speed) for each section which I need to use in further calculations.
Any help would be appreciated.
  4 Comments
Guillaume
Guillaume on 22 Oct 2018
Well, what inputs does your simulation need? You said the track details, that's one. What else?
Nick Deeley
Nick Deeley on 22 Oct 2018
Car details will be the others. At the moment, I have: Tyre co-efficient of friction, co-efficient of drag, frontal area and mass. Other details: Gravity and air density.
But, as I study more into vehicle dynamics and what other factors I could use, especially when it comes into braking and acceleration forces, I would need to add to these in at a later date reasonably easily.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 22 Oct 2018
Probably, the first thing you need to settle on is what form your inputs will take. If I understand your requirements, I would have three inputs to the simulation function:
  • track details. I would have that as a structure array with fields:
type: a scalar categorical with possible values: 'straight' or 'corner'
length: a scalar double
radius: a scalar double
inclination:? if that's needed, a scalar double
type may not even be needed since a corner with infinite radius is a straight.
  • car details, a scalar structure (unless you simulate more than one car, in which case it's another structure array), with fields
friction
drag
frontalarea
mass
all scalar doubles
  • environment, another scalar structure with fields
gravity
air density
humidity (?)
air temperature (?)
and the signature of your function would be:
function [someresult, maybesomeotherresult] = tracksimulation(track, car, environment)
%TRACKSIMULATION: Simulate the car racing along the specified track under given environment conditions
%with:
% track: and Nx1 structure with fields
% type: scalar categorical with possible values 'straight' or 'corner'
% ... you can write the rest
% first thing you should do when you start writing a function is document its inputs/outputs
You can even have a helper function to help you build that track structure:
function track = addsection(track, type, length, radius)
%ADDSECTION: add a section to the given track (or start the track)
%with:
% track: a previously constructed track structure or an empty array to start the track
% type: the type of section to add, char array with value: 'straight' (or 's') or 'corner' (or 'c')
% length: length of section to add, scalar double
% radius: radius of section to add, ignored and can be omitted if type is 'straight'.
if isempty(track)
track = structure('type', {}, 'length', {}, 'radius', {});
else
assert(isstruct(track) && all(ismember({'type', 'length', 'radius'}, fieldnames(track))), 'track input must be a structure with correct fields');
end
validateattributes(type, {'char', 'string'}, {'scalartext'}, 2)
if strcmp(type, 's'), type = 'straight'; elseif strcmp(type, 'c'), type = 'corner'; end
validateattributes(length, {'numeric'}, {'scalar', 'positive', 'finite'}, 3);
if nargin < 3
if strcmp(type, 'corner'), error('radius must be specified when type is ''corner''); end
radius = Inf;
else
validateattributes(radius, {'numeric'}, {'scalar', 'positive', 'finite'}, 4);
end
track = [track; struct('type', categorical({type}, {'straight', 'corner'}), 'length', length, 'radius', radius)];
end
  11 Comments
Nick Deeley
Nick Deeley on 5 Nov 2018
Ok, that all makes sense to me, I think.
How do I tell the code to do maths calculation on the numbers inside the TRACK(1).sections variable if it's a 'c' ??
That part is still confusing me.
If I try to display section or section.type or sectionidx, then they appear empty. No warnings are given, so, I assume the code is run, it's just that there's nothing been done by the code?
Guillaume
Guillaume on 6 Nov 2018
Again, I'm really not clear on which data model you're using So, please clarify. What are the fields of your structures (TRACK and sections)? What are the possible values of each field?

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!