Skip to content

Commit a039629

Browse files
daviduhmEricBoiseLGSVL
authored andcommitted
[AUTO-7325] [AUTO-7340] Add python API commands for Destination sensor
1 parent e58fd86 commit a039629

14 files changed

+342
-2
lines changed

Assets/Scripts/Api/ApiManager.cs

+20
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ private enum MessageType
7474
public HashSet<GameObject> Waypoints = new HashSet<GameObject>();
7575
public HashSet<GameObject> StopLine = new HashSet<GameObject>();
7676
public HashSet<GameObject> LaneChange = new HashSet<GameObject>();
77+
public HashSet<GameObject> Destinations = new HashSet<GameObject>();
7778
public List<JSONObject> Events = new List<JSONObject>();
7879

7980
public string Key { get; } = "ApiManager";
@@ -316,6 +317,7 @@ public async Task Reset()
316317
Waypoints.Clear();
317318
StopLine.Clear();
318319
LaneChange.Clear();
320+
Destinations.Clear();
319321

320322
StopAllCoroutines();
321323

@@ -412,6 +414,24 @@ public void AddWaypointReached(GameObject obj, int index)
412414
}
413415
}
414416

417+
public void AddDestinationReached(GameObject obj)
418+
{
419+
if (!Destinations.Contains(obj))
420+
{
421+
return;
422+
}
423+
424+
string uid;
425+
if (AgentUID.TryGetValue(obj, out uid))
426+
{
427+
var j = new JSONObject();
428+
j.Add("type", new JSONString("destination_reached"));
429+
j.Add("agent", new JSONString(uid));
430+
431+
Events.Add(j);
432+
}
433+
}
434+
415435
public void RegisterAgentWithWaypoints(GameObject obj)
416436
{
417437
if (AgentFollowingWaypoints.Contains(obj))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Copyright (c) 2021 LG Electronics, Inc.
3+
*
4+
* This software contains code licensed as described in LICENSE.
5+
*
6+
*/
7+
8+
using SimpleJSON;
9+
using UnityEngine;
10+
11+
namespace Simulator.Api.Commands
12+
{
13+
class AgentOnDestinationReached : ICommand
14+
{
15+
public string Name => "agent/on_destination_reached";
16+
17+
public void Execute(JSONNode args)
18+
{
19+
var api = ApiManager.Instance;
20+
var uid = args["uid"].Value;
21+
22+
if (api.Agents.TryGetValue(uid, out GameObject obj))
23+
{
24+
api.Destinations.Add(obj);
25+
api.SendResult(this);
26+
}
27+
else
28+
{
29+
api.SendError(this, $"Agent '{uid}' not found");
30+
}
31+
}
32+
}
33+
}

Assets/Scripts/Api/Commands/AgentOnDestinationReached.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Copyright (c) 2021 LG Electronics, Inc.
3+
*
4+
* This software contains code licensed as described in LICENSE.
5+
*
6+
*/
7+
8+
using SimpleJSON;
9+
using UnityEngine;
10+
using Simulator.Map;
11+
12+
namespace Simulator.Api.Commands
13+
{
14+
class MapFromNav : ICommand
15+
{
16+
public string Name => "map/from_nav";
17+
18+
public void Execute(JSONNode args)
19+
{
20+
var api = ApiManager.Instance;
21+
22+
var position = args["position"].ReadVector3();
23+
var orientation = args["orientation"].ReadQuaternion();
24+
25+
var nav = NavOrigin.Find();
26+
if (nav == null)
27+
{
28+
api.SendError(this, "NavOrigin not found");
29+
return;
30+
}
31+
32+
var point = nav.FromNavPose(position, orientation);
33+
34+
var res = new JSONObject();
35+
res.Add("position", point.position);
36+
res.Add("rotation", point.rotation.eulerAngles);
37+
38+
api.SendResult(this, res);
39+
}
40+
}
41+
}

Assets/Scripts/Api/Commands/MapFromNav.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright (c) 2021 LG Electronics, Inc.
3+
*
4+
* This software contains code licensed as described in LICENSE.
5+
*
6+
*/
7+
8+
using SimpleJSON;
9+
using UnityEngine;
10+
using System.Linq;
11+
using System.Reflection;
12+
using Simulator.Utilities;
13+
using Simulator.Sensors;
14+
15+
namespace Simulator.Api.Commands
16+
{
17+
class VehicleSetDestination : ICommand
18+
{
19+
public string Name => "vehicle/set_destination";
20+
21+
public void Execute(JSONNode args)
22+
{
23+
var uid = args["uid"].Value;
24+
var position = args["transform"]["position"].ReadVector3();
25+
var rotation = args["transform"]["rotation"].ReadVector3();
26+
27+
var api = ApiManager.Instance;
28+
if (api.Agents.TryGetValue(uid, out GameObject obj))
29+
{
30+
var sensors = obj.GetComponentsInChildren<SensorBase>();
31+
var destination_sensor = sensors.FirstOrDefault(s => s.GetType().GetCustomAttribute<SensorType>().Name == "Destination");
32+
33+
if (destination_sensor == null)
34+
{
35+
api.SendError(this, $"Agent '{uid}' does not have Destination Sensor");
36+
return;
37+
}
38+
39+
destination_sensor.GetType().GetMethod("SetDestination").Invoke(destination_sensor, new object[] { position, rotation });
40+
api.SendResult(this);
41+
}
42+
else
43+
{
44+
api.SendError(this, $"Agent '{uid}' not found");
45+
}
46+
}
47+
}
48+
}

Assets/Scripts/Api/Commands/VehicleSetDestination.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Copyright (c) 2021 LG Electronics, Inc.
3+
*
4+
* This software contains code licensed as described in LICENSE.
5+
*
6+
*/
7+
8+
using SimpleJSON;
9+
using UnityEngine;
10+
using System.Linq;
11+
using System.Reflection;
12+
using Simulator.Utilities;
13+
using Simulator.Sensors;
14+
15+
namespace Simulator.Api.Commands
16+
{
17+
class VehicleSetInitialPose : ICommand
18+
{
19+
public string Name => "vehicle/set_initial_pose";
20+
21+
public void Execute(JSONNode args)
22+
{
23+
var uid = args["uid"].Value;
24+
25+
var api = ApiManager.Instance;
26+
if (api.Agents.TryGetValue(uid, out GameObject obj))
27+
{
28+
var sensors = obj.GetComponentsInChildren<SensorBase>();
29+
var destination_sensor = sensors.FirstOrDefault(s => s.GetType().GetCustomAttribute<SensorType>().Name == "Destination");
30+
31+
if (destination_sensor == null)
32+
{
33+
api.SendError(this, $"Agent '{uid}' does not have Destination Sensor");
34+
return;
35+
}
36+
37+
destination_sensor.GetType().GetMethod("SetInitialPose").Invoke(destination_sensor, new object[] {});
38+
api.SendResult(this);
39+
}
40+
else
41+
{
42+
api.SendError(this, $"Agent '{uid}' not found");
43+
}
44+
}
45+
}
46+
}

Assets/Scripts/Api/Commands/VehicleSetInitialPose.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/Bridge/Data/Ros.cs

+15
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,28 @@ public struct Pose
214214
public Quaternion orientation;
215215
}
216216

217+
[MessageType("geometry_msgs/PoseStamped")]
218+
public struct PoseStamped
219+
{
220+
public Header header;
221+
public Pose pose;
222+
}
223+
217224
[MessageType("geometry_msgs/PoseWithCovariance")]
218225
public struct PoseWithCovariance
219226
{
220227
public Pose pose;
221228
public double[] covariance; // float64[36] covariance
222229
}
223230

231+
[MessageType("geometry_msgs/PoseWithCovarianceStamped")]
232+
public struct PoseWithCovarianceStamped
233+
{
234+
public Header header;
235+
public Pose pose;
236+
public double[] covariance; // float64[36] covariance
237+
}
238+
224239
[MessageType("geometry_msgs/Quaternion")]
225240
public struct Quaternion
226241
{

Assets/Scripts/Map/NavOrigin.cs

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright (c) 2021 LG Electronics, Inc.
3+
*
4+
* This software contains code licensed as described in LICENSE.
5+
*
6+
*/
7+
8+
using UnityEngine;
9+
using System.Collections.Generic;
10+
11+
namespace Simulator.Map
12+
{
13+
public struct NavPose
14+
{
15+
// Unity: Right/Up/Forward
16+
// ROS: Forward/Left/Up
17+
18+
public Vector3 position;
19+
public Quaternion orientation;
20+
}
21+
22+
public class NavOrigin : MonoBehaviour
23+
{
24+
public float OriginX;
25+
public float OriginY;
26+
public float Rotation; // Not supported yet
27+
28+
public static NavOrigin Find()
29+
{
30+
var origin = FindObjectOfType<NavOrigin>();
31+
if (origin == null)
32+
{
33+
Debug.LogWarning("Map is missing NavOrigin component! Adding temporary NavOrigin. Please add to scene and set origin for Navigation2 stack");
34+
origin = new GameObject("NavOrigin").AddComponent<NavOrigin>();
35+
origin.transform.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
36+
origin.OriginX = 0;
37+
origin.OriginY = 0;
38+
origin.Rotation = 0;
39+
}
40+
41+
return origin;
42+
}
43+
44+
public NavPose GetNavPose(Transform pose)
45+
{
46+
var nav_pose = new NavPose();
47+
48+
var position = transform.InverseTransformPoint(pose.position);
49+
position.Set(position.z, -position.x, position.y);
50+
position.x += OriginX;
51+
position.y += OriginY;
52+
nav_pose.position = position;
53+
54+
var heading = Vector3.SignedAngle(transform.forward, pose.transform.forward, -transform.up);
55+
Quaternion orientation = Quaternion.Euler(0, 0, heading);
56+
nav_pose.orientation = orientation;
57+
58+
return nav_pose;
59+
}
60+
61+
public Transform FromNavPose(Vector3 position, Quaternion orientation)
62+
{
63+
var point = new GameObject();
64+
65+
position.x -= OriginX;
66+
position.y -= OriginY;
67+
position.Set(-position.y, position.z, position.x);
68+
point.transform.position = transform.TransformPoint(position);
69+
70+
orientation.Set(orientation.y, -orientation.z, -orientation.x, orientation.w);
71+
var heading = orientation.eulerAngles.y - transform.rotation.eulerAngles.y;
72+
point.transform.rotation = Quaternion.Euler(0, heading, 0);
73+
74+
var pose = point.transform;
75+
Destroy(point);
76+
77+
return pose;
78+
}
79+
}
80+
}

Assets/Scripts/Map/NavOrigin.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)