-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApolloControlSensor.cs
232 lines (194 loc) · 8.39 KB
/
ApolloControlSensor.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/**
* Copyright (c) 2019-2021 LG Electronics, Inc.
*
* This software contains code licensed as described in LICENSE.
*
*/
using SimpleJSON;
using Simulator.Api;
using Simulator.Bridge;
using Simulator.Bridge.Data;
using Simulator.Utilities;
using UnityEngine;
using Simulator.Sensors.UI;
using System.Collections.Generic;
using System.Collections;
using Simulator.Analysis;
namespace Simulator.Sensors
{
[SensorType("Apollo Control", new[] { typeof(VehicleControlData) })]
public class ApolloControlSensor : 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;
[AnalysisMeasurement(MeasurementType.Misc)]
public bool IsControlReceived = false;
float ADAccelInput = 0f;
float ADSteerInput = 0f;
public AnimationCurve AccelerationInputCurve;
public AnimationCurve BrakeInputCurve;
private double LastTimeStamp = 0; // from Apollo
private 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)
{
var subscriber = BridgeMessageDispatcher.Instance.GetSynchronousSubscriber<VehicleControlData>(data =>
{
if (!IsControlReceived)
{
IsControlReceived = true;
if (ApiManager.Instance != null)
{
var jsonData = new JSONObject();
ApiManager.Instance.AddCustom(transform.parent.gameObject, "checkControl", jsonData);
}
}
controlData = data;
LastControlUpdate = SimulatorManager.Instance.CurrentTime;
if (double.IsInfinity(data.Acceleration.GetValueOrDefault()) || double.IsInfinity(data.Braking.GetValueOrDefault()) ||
double.IsNaN(data.Acceleration.GetValueOrDefault()) || double.IsNaN(data.Braking.GetValueOrDefault()))
{
return;
}
var timeStamp = data.TimeStampSec.GetValueOrDefault();
var dt = (float)(timeStamp - LastTimeStamp);
LastTimeStamp = timeStamp;
Debug.Assert(data.Acceleration.GetValueOrDefault() >= 0 && data.Acceleration.GetValueOrDefault() <= 1);
Debug.Assert(data.Braking.GetValueOrDefault() >= 0 && data.Braking.GetValueOrDefault() <= 1);
var linearAccel = AccelerationInputCurve.Evaluate(data.Acceleration.GetValueOrDefault()) - BrakeInputCurve.Evaluate(data.Braking.GetValueOrDefault());
var steeringTarget = -data.SteerTarget.GetValueOrDefault();
var steeringAngle = Controller.SteerInput;
var sgn = Mathf.Sign(steeringTarget - steeringAngle);
var steeringRate = data.SteerRate.GetValueOrDefault() * sgn;
steeringAngle += steeringRate * dt;
if (sgn != steeringTarget - steeringAngle) // prevent oversteering
{
steeringAngle = steeringTarget;
}
ADSteerInput = steeringAngle;
ADAccelInput = linearAccel;
if (data.CurrentGear == GearPosition.Reverse)
{
Dynamics.ShiftReverseAutoGearBox();
}
else if (data.CurrentGear == GearPosition.Drive)
{
Dynamics.ShiftFirstGear();
}
});
bridge.AddSubscriber(Topic, subscriber);
}
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("Time Stamp Sec", controlData.TimeStampSec.GetValueOrDefault());
graphData.Add("Steer Rate", controlData.SteerRate.GetValueOrDefault());
graphData.Add("Steer Target", controlData.SteerTarget.GetValueOrDefault());
graphData.Add("Gear", controlData.CurrentGear);
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);
}
}
}