Hi everyone,
Just been tinkering with an ABS script this afternoon but wanted to make it better.
Here is the code so far
So basically we define the starting braking values we want (same as car.ini values) for each wheel. That sets a target to get back to when the ABS is active.
Then we set the same value again just to get things going, but that value will change during operation, it just needs a starting value.
Then we define some variables, the ABS pulse speed, the start of slowing speed, and the rate it slows down by.
Then we define the optimum slip ratio for each wheel, this is based on preference but ideally you want a bit less at the rear to make it stable. Every car and it's tyres might need different values here.
Everything is linearly changed.
Then we check each wheel, if it's slipping over the optimum then we start to tweak the brake force down. We tweak the brake force down for slipping wheels again and again until they reach 0 brake force or stop slipping over the optimum slip ratio... at that point they start having their brake force increased again.
Then finally we set the braking forces to the car at the end of each physics step.
It seems to work nicely. I've factored plenty of variables, but it'd be nice to offer up a few modes to choose from, pairing both front wheels rather than each one, for both slip detection and for brake force adjustment.
I guess a function could be written that takes the main variables and applies it to each wheel, pairs of wheels, or all four wheels for example.
Then maybe it can make it into Onyx or Racer properly...
I know I've cheated by using slip ratio rather than wheel accelerations to detect slip, BUT, in the end the real ECU's will err towards this kind of response any way, that is the ideal response and thus they program towards it!
Anyone care to share ideas or improve the script further?
I'd really like to see basic 'car' features like this scripted by us guys and then we can maintain and improve them with time.
If Ruud then finds the time he can improve and/or implement them better or into Racer properly using our scripts as a guide!
Thanks
Dave
Just been tinkering with an ABS script this afternoon but wanted to make it better.
Here is the code so far
Code:
//Focus on the car used by the script owner
rcar $car=get scriptowner car
shared float $slipratio0
shared float $slipratio1
shared float $slipratio2
shared float $slipratio3
shared float $brake0stat = 4040
shared float $brake1stat = 4040
shared float $brake2stat = 1920
shared float $brake3stat = 1920
shared float $brake0 = 4040
shared float $brake1 = 4040
shared float $brake2 = 1920
shared float $brake3 = 1920
shared float $wheel0optimum = -0.3
shared float $wheel1optimum = -0.3
shared float $wheel2optimum = -0.2
shared float $wheel3optimum = -0.2
//Base frequency at speed for an 'off' cycle (total on/off cycle would be double this value in ms)
shared float $abs_base = 40
//At what speed (m/s) to start to reduce the frequency of the ABS
shared float $abs_slow = 10
//Factor that slows the ABS down linearly from the start of speed reduction to 0 (bigger = slower)
shared float $abs_slow_factor = 14
shared float $brake
shared float $speed
shared float $abs_speed
while 1
{
$brake = get $car brake
$speed = get $car tachovelocity
if $speed < $abs_slow
{
$abs_speed = $abs_base + (($abs_slow - $speed) * $abs_slow_factor)
}
$slipratio0 = get $car wheel 0 slipratio
$slipratio1 = get $car wheel 1 slipratio
$slipratio2 = get $car wheel 2 slipratio
$slipratio3 = get $car wheel 3 slipratio
//WHEEL0
if $slipratio0 < $wheel0optimum
{
if $brake0 > 0
{
$brake0 -= ($brake0stat / $abs_speed)
}
else
{
$brake0 = 0
}
}
else
{
if $brake0 < $brake0stat
{
$brake0 += ($brake0stat / $abs_speed)
}
else
{
$brake0 = $brake0stat
}
}
//WHEEL1
if $slipratio1 < $wheel1optimum
{
if $brake1 > 0
{
$brake1 -= ($brake1stat / $abs_speed)
}
else
{
$brake1 = 0
}
}
else
{
if $brake1 < $brake1stat
{
$brake1 += ($brake1stat / $abs_speed)
}
else
{
$brake1 = $brake1stat
}
}
//WHEEL2
if $slipratio2 < $wheel2optimum
{
if $brake2 > 0
{
$brake2 -= ($brake2stat / $abs_speed)
}
else
{
$brake2 = 0
}
}
else
{
if $brake2 < $brake2stat
{
$brake2 += ($brake2stat / $abs_speed)
}
else
{
$brake2 = $brake2stat
}
}
//WHEEL3
if $slipratio3 < $wheel3optimum
{
if $brake3 > 0
{
$brake3 -= ($brake3stat / $abs_speed)
}
else
{
$brake3 = 0
}
}
else
{
if $brake3 < $brake3stat
{
$brake3 += ($brake3stat / $abs_speed)
}
else
{
$brake3 = $brake3stat
}
}
set system "car0.wheel0.max_braking" to $brake0;
set system "car0.wheel1.max_braking" to $brake1;
set system "car0.wheel2.max_braking" to $brake2;
set system "car0.wheel3.max_braking" to $brake3;
interrupt
}
So basically we define the starting braking values we want (same as car.ini values) for each wheel. That sets a target to get back to when the ABS is active.
Then we set the same value again just to get things going, but that value will change during operation, it just needs a starting value.
Then we define some variables, the ABS pulse speed, the start of slowing speed, and the rate it slows down by.
Then we define the optimum slip ratio for each wheel, this is based on preference but ideally you want a bit less at the rear to make it stable. Every car and it's tyres might need different values here.
Everything is linearly changed.
Then we check each wheel, if it's slipping over the optimum then we start to tweak the brake force down. We tweak the brake force down for slipping wheels again and again until they reach 0 brake force or stop slipping over the optimum slip ratio... at that point they start having their brake force increased again.
Then finally we set the braking forces to the car at the end of each physics step.
It seems to work nicely. I've factored plenty of variables, but it'd be nice to offer up a few modes to choose from, pairing both front wheels rather than each one, for both slip detection and for brake force adjustment.
I guess a function could be written that takes the main variables and applies it to each wheel, pairs of wheels, or all four wheels for example.
Then maybe it can make it into Onyx or Racer properly...
I know I've cheated by using slip ratio rather than wheel accelerations to detect slip, BUT, in the end the real ECU's will err towards this kind of response any way, that is the ideal response and thus they program towards it!
Anyone care to share ideas or improve the script further?
I'd really like to see basic 'car' features like this scripted by us guys and then we can maintain and improve them with time.
If Ruud then finds the time he can improve and/or implement them better or into Racer properly using our scripts as a guide!
Thanks
Dave