Skip to content

Commit ea90981

Browse files
Driver Acceleration (#931)
2 parents 6ab9032 + e4eea8e commit ea90981

File tree

9 files changed

+385
-282
lines changed

9 files changed

+385
-282
lines changed

engine/Assets/Scripts/Drivers/LinearDriver.cs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,19 @@ public class LinearDriver : Driver {
1010

1111
public ConfigurableJoint JointA { get; private set; }
1212
public ConfigurableJoint JointB { get; private set; }
13-
private float _maxSpeed;
14-
13+
private float _lastVel = 0;
1514
public float _position = 0f;
1615
public float Position {
1716
get => _position;
1817
private
1918
set {
2019
var newPos = Mathf.Clamp(value, Limits.Lower, Limits.Upper);
2120
JointA.connectedAnchor = JointA.anchor + (JointA.axis * newPos);
22-
_position = newPos;
21+
if (newPos == Limits.Lower || newPos == Limits.Upper)
22+
_lastVel = 0;
23+
_position = newPos;
2324
}
2425
}
25-
2626
// Note: only used to save between sessions
2727
private JointMotor _motor;
2828
public JointMotor Motor {
@@ -68,18 +68,34 @@ public LinearDriver(string name, string[] inputs, string[] outputs, SimObject si
6868
_motor = motor.Value;
6969
} else {
7070
Motor = new JointMotor() {
71-
force = 2000,
71+
force = 0.1f,
7272
freeSpin = false,
73-
targetVelocity = 5,
73+
targetVelocity = 0.2f,
7474
};
7575
}
7676
}
7777

7878
public override void Update() {
79+
// TODO: Position
80+
81+
// VelocityControl
82+
7983
float value = (float) MainInput;
8084

81-
var velocity = value * _motor.targetVelocity;
82-
Position += Time.deltaTime * velocity;
85+
var tarVel = value * _motor.targetVelocity;
86+
87+
var delta = tarVel - _lastVel;
88+
var possibleDelta = _motor.force * Time.deltaTime; // Force = acceleration in M/S/S
89+
90+
if (Mathf.Abs(delta) > possibleDelta)
91+
delta = possibleDelta * Mathf.Sign(delta);
92+
93+
_lastVel += delta;
94+
95+
if (Mathf.Abs(_lastVel * Time.deltaTime) > _motor.targetVelocity)
96+
_lastVel = _motor.targetVelocity * Mathf.Sign(_lastVel);
97+
98+
Position += _lastVel;
8399
}
84100
}
85101
}

engine/Assets/Scripts/Drivers/RotationalDriver.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using UnityEngine;
99
using Synthesis.Util;
1010
using Synthesis.Physics;
11+
using Math = System.Math;
1112

1213
#nullable enable
1314

@@ -115,6 +116,7 @@ public Rigidbody RbB {
115116
}
116117
}
117118

119+
private float _lastVel = 0f;
118120
private float _fakedTheta = 0f;
119121
private float _fakedOmega = 0f;
120122

@@ -167,9 +169,9 @@ public RotationalDriver(string name, string[] inputs, string[] outputs, SimObjec
167169
} else {
168170
Motor = new JointMotor() {
169171
// Default Motor. Slow but powerful enough. Also uses Motor to save it
170-
force = 2000,
172+
force = 0.1f,
171173
freeSpin = false,
172-
targetVelocity = 5,
174+
targetVelocity = 0.2f,
173175
};
174176
}
175177

@@ -269,9 +271,21 @@ private void VelocityControl(float deltaT) {
269271
SynthesisUtil.GetInertiaAroundParallelAxis(_jointB.connectedBody, _jointA.anchor, _jointA.axis);
270272

271273
if (_useFakeMotion) {
272-
float alpha = val * _convertedMotorTargetVel;
274+
float tarVel = val == 0 ? 0 : Mathf.Sign(val) * _convertedMotorTargetVel;
273275

274-
_fakedTheta += alpha * deltaT;
276+
var delta = tarVel - _lastVel;
277+
var posDelta = _motor.force * Mathf.Rad2Deg * Time.deltaTime;
278+
279+
if (Mathf.Abs(delta) > posDelta)
280+
delta = posDelta * Mathf.Sign(delta);
281+
282+
_lastVel += delta;
283+
284+
if (Mathf.Abs(_lastVel) > _convertedMotorTargetVel)
285+
_lastVel = _convertedMotorTargetVel * Mathf.Sign(_lastVel);
286+
287+
float lastFakedTheta = _fakedTheta;
288+
_fakedTheta += _lastVel * deltaT;
275289

276290
if (_rotationalLimits.HasValue) {
277291
if (_fakedTheta > _rotationalLimits.Value.max) {
@@ -280,8 +294,13 @@ private void VelocityControl(float deltaT) {
280294
_fakedTheta = _rotationalLimits.Value.min;
281295
}
282296

297+
// Limit theta to specific range
283298
_fakedTheta = Mathf.Clamp(_fakedTheta, -180, 179);
284299

300+
// Check and see if we've hit a hard limit to zero out velocity
301+
if (Math.Abs(lastFakedTheta - _fakedTheta) < 0.001f)
302+
_lastVel = 0;
303+
285304
_jointA.limits =
286305
new JointLimits { bounceMinVelocity = _rotationalLimits.Value.bounceMinVelocity,
287306
bounciness = _rotationalLimits.Value.bounciness,

engine/Assets/Scripts/Drivers/WheelDriver.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public WheelDriver(string name, string[] inputs, string[] outputs, SimObject sim
138138
} else {
139139
Motor = new JointMotor() {
140140
// Default Motor. Slow but powerful enough. Also uses Motor to save it
141-
force = 2000,
141+
force = 1, // About a Neo 550. Max is Falcon 550 at 4.67
142142
freeSpin = false,
143143
targetVelocity = 30,
144144
};
@@ -184,26 +184,20 @@ private void VelocityControl() {
184184
if (!_useMotor)
185185
return;
186186

187-
var val = (float) MainInput;
188-
189-
_targetRotationalSpeed = val * _motor.targetVelocity;
190-
191-
var delta = _targetRotationalSpeed - _customWheel.RotationSpeed;
192-
var possibleDelta = (_motor.force * Time.deltaTime) / _customWheel.Inertia;
193-
if (Mathf.Abs(delta) > possibleDelta)
194-
delta = possibleDelta * Mathf.Sign(delta);
195-
196-
var lastRotSpeed = _customWheel.RotationSpeed;
197-
_customWheel.RotationSpeed += delta;
187+
var val = (float) MainInput;
188+
var lastRotSpeed = _customWheel.RotationSpeed;
189+
_customWheel.RotationSpeed = val * _motor.targetVelocity;
198190

199191
if (!float.IsNaN(_lastUpdate)) {
200192
var deltaT = Time.realtimeSinceStartup - _lastUpdate;
201193

202194
if (deltaT == 0f)
203195
return;
204196

205-
var alpha = (_customWheel.RotationSpeed - lastRotSpeed) / deltaT;
206-
_jointAngle += 0.5f * alpha * deltaT * deltaT + lastRotSpeed * deltaT;
197+
// Calculations:
198+
// var alpha = (_customWheel.RotationSpeed - lastRotSpeed) / deltaT;
199+
// 0.5f * alpha * deltaT * deltaT + lastRotSpeed * deltaT;
200+
_jointAngle += deltaT * (_customWheel.RotationSpeed - 0.5f * lastRotSpeed);
207201
}
208202
}
209203
}

engine/Assets/Scripts/Importer/Importer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ void WheelJoint() {
210210
mat.dynamicFriction = 0f;
211211
mat.staticFriction = 0f;
212212
mat.frictionCombine = PhysicMaterialCombine.Multiply;
213+
mat.bounceCombine = PhysicMaterialCombine.Multiply;
214+
mat.bounciness = 0f;
213215
});
214216

215217
var wheelA = gameObjectA.AddComponent<FixedJoint>();

engine/Assets/Scripts/UI/Dynamic/MainHUD.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ public static void SetUpConfig() {
526526
drawerPosition: DrawerPosition.Bottom, icon: SynthesisAssetCollection.GetSpriteByName("roborio"));
527527
AddItemToDrawer("Drivetrain", b => DynamicUIManager.CreateModal<ChangeDrivetrainModal>(),
528528
drawerPosition: DrawerPosition.Bottom, icon: SynthesisAssetCollection.GetSpriteByName("drivetrain"));
529-
AddItemToDrawer("Motors", b => { DynamicUIManager.CreateModal<ConfigMotorModal>(); },
529+
AddItemToDrawer("Joints", b => { DynamicUIManager.CreateModal<ConfigJointModal>(); },
530530
drawerPosition: DrawerPosition.Bottom, icon: SynthesisAssetCollection.GetSpriteByName("joint-icon"));
531531

532532
if (ModeManager.CurrentMode.GetType() == typeof(PracticeMode))

engine/Assets/Scripts/UI/Dynamic/Modals/Configuring/ChangeInputsModal.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ private void PopulateInputSelections() {
4646

4747
var inputScrollView =
4848
leftContent.CreateScrollView().SetHeight<ScrollView>(CONTENT_HEIGHT).ApplyTemplate(VerticalLayout);
49+
inputScrollView.SetBackgroundColor<ScrollView>(ColorManager.SynthesisColor.HighlightHover);
4950

5051
// make background transparent
5152
inputScrollView.RootGameObject.GetComponent<UnityEngine.UI.Image>().color = Color.clear;

0 commit comments

Comments
 (0)