Skip to content

Commit

Permalink
apply first order lag to acceleration
Browse files Browse the repository at this point in the history
Signed-off-by: Autumn60 <[email protected]>
  • Loading branch information
Autumn60 committed Nov 25, 2024
1 parent 2082917 commit 0d3f326
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions Assets/AWSIM/Scripts/Vehicles/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,14 @@ public float SidewaySlipMultipler
/// In the plane, output the force that will result in this acceleration.
/// On a slope, it is affected by the slope resistance, so it does not match the input.
/// </summary>
// TODO: Compute first order lag
public float AccelerationInput;
public float AccelerationInput
{
get => acceleration.DesiredValue;
set => acceleration.DesiredValue = Mathf.Clamp(value, -MaxAccelerationInput, MaxAccelerationInput);
}
private FirstOrderLaggedFloat acceleration;
[SerializeField, Min(0.0f), Tooltip("Set 0 to disable the lag")]
private float accelerationTimeConstant = 0.0f;

/// <summary>
/// Vehicle steering input. Tire angle (degree)
Expand All @@ -215,6 +221,11 @@ public float SteerAngleInput
/// </summary>
public Vector3 LocalAcceleration { get; private set; }

/// <summary>
/// Vehicle acceleration (m/s^2)
/// </summary>
public float Acceleration => acceleration.Value;

/// <summary>
/// Vehicle speed (m/s)
/// </summary>
Expand Down Expand Up @@ -303,6 +314,9 @@ void Awake()
ForwardSlipMultipler = 1f;
SidewaySlipMultipler = 1f;

// Initialize acceleration
acceleration = new FirstOrderLaggedFloat(accelerationTimeConstant, 0.0f);

// Initialize steer angle
steerAngle = new FirstOrderLaggedFloat(steerAngleTimeConstant, 0.0f);
}
Expand Down Expand Up @@ -331,9 +345,6 @@ private void OnTriggerExit(Collider other)

void FixedUpdate()
{
// Clamp input values.
AccelerationInput = Mathf.Clamp(AccelerationInput, -MaxAccelerationInput, MaxAccelerationInput);

// Compute vehicle infomation.
ComputeVehicleState();

Expand All @@ -347,8 +358,7 @@ void FixedUpdate()
if (sleep == false)
{
// Update wheel force.
var acceleration = AccelerationInput;
UpdateWheelsForce(acceleration);
UpdateWheelsForce(Acceleration);
}

// cache value for next frame.
Expand Down

0 comments on commit 0d3f326

Please sign in to comment.