-
Notifications
You must be signed in to change notification settings - Fork 2
/
LaneFollowingSensor.cs
146 lines (119 loc) · 4.47 KB
/
LaneFollowingSensor.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
/**
* Copyright (c) 2020-2021 LG Electronics, Inc.
*
* This software contains code licensed as described in LICENSE.
*
*/
using UnityEngine;
using Simulator.Bridge;
using Simulator.Utilities;
using Simulator.Sensors.UI;
using System.Collections.Generic;
using Simulator.Bridge.Data;
namespace Simulator.Sensors
{
[SensorType("Lane Following", new [] { typeof(VehicleControlData) })]
public class LaneFollowingSensor : SensorBase, IVehicleInputs
{
[SensorParameter]
[Range(0f, 30f)]
public float CruiseSpeed = 0f;
[SensorParameter]
[Range(0f, 2f)]
public float SteeringSensitivity = 1f;
[AnalysisMeasurement(MeasurementType.Velocity)]
public float MaxSpeed = 0f;
[AnalysisMeasurement(MeasurementType.Count)]
public int ControlMessages = 0;
[AnalysisMeasurement(MeasurementType.Input)]
public float LargestSteerInput = 0f;
[AnalysisMeasurement(MeasurementType.Duration)]
public double AverageInferenceTime = 0f;
private IVehicleDynamics Dynamics;
private IAgentController Controller;
public float SteerInput { get; private set; } = 0f;
public float AccelInput { get; private set; } = 0f;
public float BrakeInput { get; private set; } = 0f;
private BridgeInstance Bridge;
private float ADSteerInput = 0f;
private float MaxSteerInput = 0f;
private double InferenceTime = 0f;
private double TotalInferenceTime = 0f;
private double LastControlUpdate = 0f;
private void Awake()
{
LastControlUpdate = SimulatorManager.Instance.CurrentTime;
Dynamics = GetComponentInParent<IVehicleDynamics>();
Controller = GetComponentInParent<IAgentController>();
}
protected override void Initialize()
{
}
protected override void Deinitialize()
{
}
public override void OnBridgeSetup(BridgeInstance bridge)
{
Bridge = bridge;
Bridge.AddSubscriber<VehicleControlData>(Topic, data =>
{
if (Time.timeScale == 0f)
return;
LastControlUpdate = SimulatorManager.Instance.CurrentTime;
ADSteerInput = data.SteerInput.GetValueOrDefault();
InferenceTime = data.TimeStampSec.GetValueOrDefault();
ControlMessages++;
TotalInferenceTime += InferenceTime;
AverageInferenceTime = TotalInferenceTime / (float)ControlMessages;
});
}
public void Update()
{
if (SimulatorManager.Instance.CurrentTime - LastControlUpdate >= 0.5)
{
ADSteerInput = SteerInput = 0f;
}
}
private void FixedUpdate()
{
if (Bridge != null && Bridge.Status == Status.Connected)
{
if (SimulatorManager.Instance.CurrentTime - LastControlUpdate < 0.5f)
{
if (MaxSteerInput < Mathf.Abs(ADSteerInput))
{
MaxSteerInput = Mathf.Abs(ADSteerInput);
LargestSteerInput = ADSteerInput;
}
SteerInput = ADSteerInput * SteeringSensitivity;
}
}
if (Controller.AccelInput >= 0)
{
AccelInput = Dynamics.Velocity.magnitude < CruiseSpeed ? 1f : 0f;
}
MaxSpeed = Mathf.Max(MaxSpeed, Dynamics.Velocity.magnitude);
}
public override void OnVisualize(Visualizer visualizer)
{
Debug.Assert(visualizer != null);
var graphData = new Dictionary<string, object>()
{
{"Cruise Speed", CruiseSpeed},
{"Speed", Dynamics.Velocity.magnitude},
{"AD Steer Input", ADSteerInput},
{"Largest AD Steer Input", LargestSteerInput},
{"Steering Sensitivity", SteeringSensitivity},
{"Number of Control Messages", ControlMessages},
{"Inference Time", InferenceTime},
{"Average Inference Time", AverageInferenceTime},
{"Last Control Update", LastControlUpdate},
};
visualizer.UpdateGraphValues(graphData);
}
public override void OnVisualizeToggle(bool state)
{
//
}
}
}