0 votes
by
I am trying to implement a longitudinal controller for a vehicle using this asset. The controller will control the speed of the vehicle and in order to apply the required acceleration it has to know the throttle required to provide the needed torque. Is there a function I can use or way I can calculate the required throttle to provide a certain torque for a vehicle, ideally through the VPVehicleController?

If an engine map which shows the torque provided against RPM for different throttle outputs is available, would you be able to provide it for me please?

Thank you in advance.

1 Answer

0 votes
by
edited by
 
Best answer

First you need to get direct access to the engine block in the vehicle:

Engine engine = vehicle.GetInternalObject(typeof(Engine)) as Engine;

Then you can use the method Engine.CalculateTorque() to get the torque at the given rpm at both full throttle and no throttle:

float minTorque = engine.CalculateTorque(rpm, 0.0f);
float maxTorque = engine.CalculateTorque(rpm, 1.0f);

Now use Mathf.InverseLerp() to calculate the throttle required to produce the specified torque:

float throttle = Mathf.InverseLerp(minTorque, maxTorque, wantedTorque);

Notes:

  • Using minTorque is necessary because the engine will produce braking torque (negative) bellow some throttle value.
  • VehicleBase.GetInternalObject() is available in the Professional and Enterprise editions of VPP. This method returns null in the Community Edition.
  • Engine block scripting reference: https://vehiclephysics.com/blocks/engine/#scripting-reference
...