Skip to content

Commit 0d3f326

Browse files
committed
apply first order lag to acceleration
Signed-off-by: Autumn60 <harada.akiro@gmail.com>
1 parent 2082917 commit 0d3f326

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

Assets/AWSIM/Scripts/Vehicles/Vehicle.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,14 @@ public float SidewaySlipMultipler
189189
/// In the plane, output the force that will result in this acceleration.
190190
/// On a slope, it is affected by the slope resistance, so it does not match the input.
191191
/// </summary>
192-
// TODO: Compute first order lag
193-
public float AccelerationInput;
192+
public float AccelerationInput
193+
{
194+
get => acceleration.DesiredValue;
195+
set => acceleration.DesiredValue = Mathf.Clamp(value, -MaxAccelerationInput, MaxAccelerationInput);
196+
}
197+
private FirstOrderLaggedFloat acceleration;
198+
[SerializeField, Min(0.0f), Tooltip("Set 0 to disable the lag")]
199+
private float accelerationTimeConstant = 0.0f;
194200

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

224+
/// <summary>
225+
/// Vehicle acceleration (m/s^2)
226+
/// </summary>
227+
public float Acceleration => acceleration.Value;
228+
218229
/// <summary>
219230
/// Vehicle speed (m/s)
220231
/// </summary>
@@ -303,6 +314,9 @@ void Awake()
303314
ForwardSlipMultipler = 1f;
304315
SidewaySlipMultipler = 1f;
305316

317+
// Initialize acceleration
318+
acceleration = new FirstOrderLaggedFloat(accelerationTimeConstant, 0.0f);
319+
306320
// Initialize steer angle
307321
steerAngle = new FirstOrderLaggedFloat(steerAngleTimeConstant, 0.0f);
308322
}
@@ -331,9 +345,6 @@ private void OnTriggerExit(Collider other)
331345

332346
void FixedUpdate()
333347
{
334-
// Clamp input values.
335-
AccelerationInput = Mathf.Clamp(AccelerationInput, -MaxAccelerationInput, MaxAccelerationInput);
336-
337348
// Compute vehicle infomation.
338349
ComputeVehicleState();
339350

@@ -347,8 +358,7 @@ void FixedUpdate()
347358
if (sleep == false)
348359
{
349360
// Update wheel force.
350-
var acceleration = AccelerationInput;
351-
UpdateWheelsForce(acceleration);
361+
UpdateWheelsForce(Acceleration);
352362
}
353363

354364
// cache value for next frame.

0 commit comments

Comments
 (0)