Skip to content

Commit 204e3cd

Browse files
daviduhmmartins-mozeiko
authored andcommitted
Implement controllable functionalities
1 parent 90ba386 commit 204e3cd

File tree

5 files changed

+115
-4
lines changed

5 files changed

+115
-4
lines changed

lgsvl/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
from .simulator import Simulator, RaycastHit, WeatherState
99
from .sensor import Sensor, CameraSensor, LidarSensor, ImuSensor
1010
from .agent import AgentType, VehicleControl, AgentState, Vehicle, EgoVehicle, NpcVehicle, Pedestrian, DriveWaypoint, WalkWaypoint, NPCControl
11+
from .controllable import Controllable

lgsvl/agent.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ def state(self):
106106
@state.setter
107107
@accepts(AgentState)
108108
def state(self, state):
109-
j = state.to_json()
110109
self.remote.command("agent/state/set", {
111110
"uid": self.uid,
112111
"state": state.to_json()

lgsvl/controllable.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#
2+
# Copyright (c) 2019 LG Electronics, Inc.
3+
#
4+
# This software contains code licensed as described in LICENSE.
5+
#
6+
7+
from .geometry import Transform
8+
from .utils import accepts
9+
10+
class Controllable:
11+
def __init__(self, remote, j):
12+
self.remote = remote
13+
self.uid = j["uid"]
14+
self.type = j["type"]
15+
self.transform = Transform.from_json(j)
16+
self.valid_actions = j["valid_actions"]
17+
self.default_control_policy = j["default_control_policy"]
18+
19+
@property
20+
def current_state(self):
21+
j = self.remote.command("controllable/current_state/get", {"uid": self.uid})
22+
return j["state"]
23+
24+
@property
25+
def control_policy(self):
26+
j = self.remote.command("controllable/control_policy/get", {"uid": self.uid})
27+
return j["control_policy"]
28+
29+
@accepts(str)
30+
def control(self, control_policy):
31+
self.remote.command("controllable/control_policy/set", {
32+
"uid": self.uid,
33+
"control_policy": control_policy,
34+
})
35+
36+
def __eq__(self, other):
37+
return self.uid == other.uid
38+
39+
def __hash__(self):
40+
return hash(self.uid)
41+
42+
def __repr__(self):
43+
return str({
44+
"type": str(self.type),
45+
"transform": str(self.transform),
46+
"valid_actions": str(self.valid_actions),
47+
"default_control_policy": str(self.default_control_policy),
48+
})

lgsvl/simulator.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .sensor import GpsData
1010
from .geometry import Vector, Transform
1111
from .utils import accepts
12+
from .controllable import Controllable
1213

1314
from collections import namedtuple
1415

@@ -194,6 +195,6 @@ def raycast(self, origin, direction, layer_mask = -1, max_distance = float("inf"
194195
return None
195196
return RaycastHit(hit["distance"], Vector.from_json(hit["point"]), Vector.from_json(hit["normal"]))
196197

197-
# @accepts(bool)
198-
# def set_physics(self, isPhysicsSimple):
199-
# self.remote.command("vehicle/set_npc_physics", {"isPhysicsSimple": isPhysicsSimple})
198+
def get_controllables(self):
199+
j = self.remote.command("controllable/get/all")
200+
return [Controllable(self.remote, controllable) for controllable in j]
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) 2019 LG Electronics, Inc.
4+
#
5+
# This software contains code licensed as described in LICENSE.
6+
#
7+
8+
import os
9+
import lgsvl
10+
11+
sim = lgsvl.Simulator(os.environ.get("SIMULATOR_HOST", "127.0.0.1"), 8181)
12+
13+
scene_name = "BorregasAve"
14+
15+
if sim.current_scene == scene_name:
16+
sim.reset()
17+
else:
18+
sim.load(scene_name, 42)
19+
20+
spawns = sim.get_spawn()
21+
22+
state = lgsvl.AgentState()
23+
state.transform = spawns[0]
24+
state.velocity = lgsvl.Vector(0, 0, 20)
25+
ego = sim.add_agent("Jaguar2015XE (Apollo 5.0)", lgsvl.AgentType.EGO, state)
26+
27+
print("Python API Quickstart #26: How to Control Traffic Light")
28+
29+
# Get a list of controllable objects
30+
controllables = sim.get_controllables()
31+
print("\n# List of controllable objects in {} scene:".format(scene_name))
32+
for c in controllables:
33+
print(c)
34+
35+
# Pick a traffic light of intrest
36+
signal = controllables[2]
37+
38+
# Get current controllable states
39+
print("\n# Current control policy:")
40+
print(signal.control_policy)
41+
42+
# Create a new control policy
43+
control_policy = "trigger=50;green=1;yellow=1.5;red=2;green=5;loop"
44+
45+
# Control this traffic light with a new control policy
46+
signal.control(control_policy)
47+
48+
print("\n# Updated control policy:")
49+
print(signal.control_policy)
50+
51+
# Get current state of signal
52+
print("\n# Current signal state before simulation:")
53+
print(signal.current_state)
54+
55+
seconds = 10
56+
input("\nPress Enter to run simulation for {} seconds".format(seconds))
57+
print("\nRunning simulation for {} seconds...".format(seconds))
58+
sim.run(seconds)
59+
60+
print("\n# Current signal state after simulation:")
61+
print(signal.current_state)
62+
print("\nDone!")

0 commit comments

Comments
 (0)