forked from gaia-platform/LGSVLVppWheelSpeedSensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLGSVLVppWheelSpeedSensor.cs
145 lines (128 loc) · 4.56 KB
/
LGSVLVppWheelSpeedSensor.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
/**
* Copyright (c) 2021-2022 Gaia Platform LLC
*
* 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;
using VehiclePhysics;
namespace Simulator.Sensors
{
public class LGSVLVppWheelSpeedData
{
public float x;
public float y;
public float width;
public float height;
}
public class LGSVLVppWheelSpeedDataBridgePlugin : ISensorBridgePlugin
{
public void Register(IBridgePlugin plugin)
{
if (plugin?.GetBridgeNameAttribute()?.Name == "ROS" || plugin?.GetBridgeNameAttribute()?.Type == "ROS2" ||
plugin?.GetBridgeNameAttribute()?.Type == "ROS2_Bridge_GAIA"
)
{
plugin.Factory.RegPublisher(plugin,
(LGSVLVppWheelSpeedData data) => new Simulator.Bridge.Data.Lgsvl.BoundingBox2D()
{
x = data.x,
y = data.y,
width = data.width,
height = data.height
}
);
}
}
}
[SensorType("LGSVL VPP Wheel Speed Sensor", new[] { typeof(LGSVLVppWheelSpeedData) })]
public class LGSVLVppWheelSpeedSensor : SensorBase
{
private VehicleVPPControllerInput VPP;
private VehicleSMI NonVPP_Alt = null;
private BridgeInstance Bridge;
private Publisher<LGSVLVppWheelSpeedData> Publish;
private float FLWheelSpeed = 0f;
private float FRWheelSpeed = 0f;
private float RLWheelSpeed = 0f;
private float RRWheelSpeed = 0f;
private void Awake()
{
VPP = GetComponentInParent<VehicleVPPControllerInput>();
if (VPP == null)
{
Debug.LogWarning("Could not find VehicleVPPControllerInput.");
NonVPP_Alt = GetComponentInParent<VehicleSMI>();
if (NonVPP_Alt == null){
Debug.LogWarning("Could not find supported vehicle dynamics model (VPP/VehicleSMI).");
}
}
}
protected override void Initialize()
{
}
protected override void Deinitialize()
{
}
private void Update()
{
}
private void FixedUpdate()
{
if (VPP == null)
{
FLWheelSpeed = NonVPP_Alt.GetWheelAngularVelocity(0);
FRWheelSpeed = NonVPP_Alt.GetWheelAngularVelocity(1);
RLWheelSpeed = NonVPP_Alt.GetWheelAngularVelocity(2);
RRWheelSpeed = NonVPP_Alt.GetWheelAngularVelocity(3);
} else {
FLWheelSpeed = VPP.GetWheelAngularVelocity(VPP.GetWheelIndex(0, VehicleBase.WheelPos.Left));
FRWheelSpeed = VPP.GetWheelAngularVelocity(VPP.GetWheelIndex(0, VehicleBase.WheelPos.Right));
RLWheelSpeed = VPP.GetWheelAngularVelocity(VPP.GetWheelIndex(1, VehicleBase.WheelPos.Left));
RRWheelSpeed = VPP.GetWheelAngularVelocity(VPP.GetWheelIndex(1, VehicleBase.WheelPos.Right));
}
if (Bridge?.Status == Status.Connected && Publish != null)
{
Publish(new LGSVLVppWheelSpeedData()
{
x = FLWheelSpeed,
y = FRWheelSpeed,
width = RLWheelSpeed,
height = RRWheelSpeed
});
}
}
public override void OnBridgeSetup(BridgeInstance bridge)
{
if (bridge?.Plugin?.GetBridgeNameAttribute()?.Type == "ROS2" ||
bridge?.Plugin?.GetBridgeNameAttribute()?.Type == "ROS2_Bridge_GAIA"
)
{
Bridge = bridge;
Publish = Bridge.AddPublisher<LGSVLVppWheelSpeedData>(Topic);
}
}
public override void OnVisualize(Visualizer visualizer)
{
Debug.Assert(visualizer != null);
var graphData = new Dictionary<string, object>()
{
{"FL Wheel Speed", FLWheelSpeed},
{"FR Wheel Speed", FRWheelSpeed},
{"RL Wheel Speed", RLWheelSpeed},
{"RR Wheel Speed", RRWheelSpeed},
};
visualizer.UpdateGraphValues(graphData);
}
public override void OnVisualizeToggle(bool state)
{
}
}
}