-
Notifications
You must be signed in to change notification settings - Fork 7
/
LGSVLControlSensor.cs
211 lines (177 loc) · 7.35 KB
/
LGSVLControlSensor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/**
* Copyright (c) 2019-2021 LG Electronics, Inc.
*
* This software contains code licensed as described in LICENSE.
*
*/
using Simulator.Bridge;
using Simulator.Bridge.Data;
using Simulator.Utilities;
using UnityEngine;
using Simulator.Sensors.UI;
using System.Collections.Generic;
using Simulator.Analysis;
using System.Collections;
namespace Simulator.Sensors
{
[SensorType("LGSVL Control", new[] { typeof(VehicleControlData) })]
public class LGSVLControlSensor : SensorBase, IVehicleInputs
{
VehicleControlData Data;
IAgentController Controller;
IVehicleDynamics Dynamics;
double LastControlUpdate = 0f;
float ActualLinVel = 0f;
float ActualAngVel = 0f;
public float SteerInput { get; private set; } = 0f;
public float AccelInput { get; private set; } = 0f;
public float BrakeInput { get; private set; } = 0f;
[AnalysisMeasurement(MeasurementType.Input)]
public float MaxSteer = 0f;
[AnalysisMeasurement(MeasurementType.Input)]
public float MaxAccel = 0f;
[AnalysisMeasurement(MeasurementType.Input)]
public float MaxBrake = 0f;
float ADAccelInput = 0f;
float ADSteerInput = 0f;
public AnimationCurve AccelerationInputCurve;
public AnimationCurve BrakeInputCurve;
VehicleControlData controlData;
[SensorParameter]
public float StuckTravelThreshold = 0.1f; // apollo autoware lgsvl sensor
[SensorParameter]
public float StuckTimeThreshold = 10.0f;
private float ThrottleCommand = 0f;
private float ThrottleCuttoff = 0.05f;
private Vector3 StuckStartPosition;
private float StuckTime;
private bool EgoIsStuck = false;
private void Awake()
{
LastControlUpdate = SimulatorManager.Instance.CurrentTime;
Controller = GetComponentInParent<IAgentController>();
Dynamics = GetComponentInParent<IVehicleDynamics>();
}
protected override void Initialize()
{
StuckStartPosition = transform.position;
}
protected override void Deinitialize()
{
}
private void Update()
{
// stuck analysis
ThrottleCommand = Dynamics.AccellInput;
if (!EgoIsStuck && ThrottleCommand > ThrottleCuttoff && Vector3.Distance(transform.position, StuckStartPosition) < StuckTravelThreshold)
{
StuckTime += Time.fixedDeltaTime;
if (StuckTime > StuckTimeThreshold)
{
StuckEvent(Controller.GTID);
EgoIsStuck = true;
}
}
else
{
StuckStartPosition = transform.position;
StuckTime = 0f;
}
var projectedLinVec = Vector3.Project(Dynamics.Velocity, transform.forward);
ActualLinVel = projectedLinVec.magnitude * (Vector3.Dot(Dynamics.Velocity, transform.forward) > 0 ? 1.0f : -1.0f);
var projectedAngVec = Vector3.Project(Dynamics.AngularVelocity, transform.up);
ActualAngVel = projectedAngVec.magnitude * (projectedAngVec.y > 0 ? -1.0f : 1.0f);
// LastControlUpdate and Time.Time come from Unity.
if (SimulatorManager.Instance.CurrentTime - LastControlUpdate >= 0.5) // > 500ms
{
ADAccelInput = ADSteerInput = AccelInput = SteerInput = 0f;
}
}
private void FixedUpdate()
{
if (SimulatorManager.Instance.CurrentTime - LastControlUpdate < 0.5f)
{
AccelInput = ADAccelInput;
SteerInput = ADSteerInput;
MaxSteer = Mathf.Max(MaxSteer, Mathf.Sign(SteerInput) * SteerInput);
MaxAccel = Mathf.Max(MaxAccel, Mathf.Sign(AccelInput) * AccelInput);
MaxBrake = Mathf.Max(MaxBrake, Mathf.Sign(BrakeInput) * BrakeInput);
}
}
public override void OnBridgeSetup(BridgeInstance bridge)
{
if (bridge.Plugin.GetBridgeNameAttribute().Type == "ROS")
{
bridge.AddSubscriber<VehicleControlData>(Topic, data =>
{
if (Time.timeScale == 0f)
return;
controlData = data;
LastControlUpdate = SimulatorManager.Instance.CurrentTime;
float wheelAngle = data.SteerAngle.GetValueOrDefault();
wheelAngle = UnityEngine.Mathf.Clamp(wheelAngle, -Dynamics.MaxSteeringAngle, Dynamics.MaxSteeringAngle);
var k = (float)(wheelAngle + Dynamics.MaxSteeringAngle) / (Dynamics.MaxSteeringAngle * 2);
ADSteerInput = UnityEngine.Mathf.Lerp(-1f, 1f, k);
ADAccelInput = data.Acceleration.GetValueOrDefault() - data.Braking.GetValueOrDefault();
});
}
else if (bridge.Plugin.GetBridgeNameAttribute().Type == "ROS2")
{
bridge.AddSubscriber<VehicleControlData>(Topic, data =>
{
if (Time.timeScale == 0f)
return;
controlData = data;
LastControlUpdate = SimulatorManager.Instance.CurrentTime;
float wheelAngle = data.SteerAngle.GetValueOrDefault();
ADSteerInput = wheelAngle;
ADAccelInput = data.Acceleration.GetValueOrDefault() - data.Braking.GetValueOrDefault();
if (data.TargetGear == GearPosition.Reverse)
{
Dynamics.ShiftReverseAutoGearBox();
}
else if (data.TargetGear == GearPosition.Drive)
{
Dynamics.ShiftFirstGear();
}
});
}
}
public override void OnVisualize(Visualizer visualizer)
{
Debug.Assert(visualizer != null);
var graphData = new Dictionary<string, object>()
{
{"AD Accel Input", ADAccelInput},
{"AD Steer Input", ADSteerInput},
{"Last Control Update", LastControlUpdate},
{"Actual Linear Velocity", ActualLinVel},
{"Actual Angular Velocity", ActualAngVel},
{ "EgoIsStuck", EgoIsStuck },
};
if (controlData == null)
{
return;
}
graphData.Add("Acceleration", controlData.Acceleration.GetValueOrDefault());
graphData.Add("Braking", controlData.Braking.GetValueOrDefault());
graphData.Add("Steer Angle", controlData.SteerAngle.GetValueOrDefault());
visualizer.UpdateGraphValues(graphData);
}
public override void OnVisualizeToggle(bool state)
{
//
}
private void StuckEvent(uint id)
{
Hashtable data = new Hashtable
{
{ "Id", id },
{ "Type", "Stuck" },
{ "Time", SimulatorManager.Instance.GetSessionElapsedTimeSpan().ToString() },
{ "Status", AnalysisManager.AnalysisStatusType.Failed },
};
SimulatorManager.Instance.AnalysisManager.AddEvent(data);
}
}
}