Skip to content

Commit 030dfba

Browse files
committed
Get controllable object by position and type
1 parent a76234c commit 030dfba

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

lgsvl/simulator.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,17 @@ def raycast(self, origin, direction, layer_mask = -1, max_distance = float("inf"
195195
return None
196196
return RaycastHit(hit["distance"], Vector.from_json(hit["point"]), Vector.from_json(hit["normal"]))
197197

198-
def get_controllables(self):
199-
j = self.remote.command("controllable/get/all")
200-
return [Controllable(self.remote, controllable) for controllable in j]
198+
@accepts(str)
199+
def get_controllables(self, control_type = None):
200+
j = self.remote.command("controllable/get/all", {
201+
"type": control_type,
202+
})
203+
return [Controllable(self.remote, controllable) for controllable in j]
204+
205+
@accepts(Vector, str)
206+
def get_controllable(self, position, control_type = None):
207+
j = self.remote.command("controllable/get", {
208+
"position": position.to_json(),
209+
"type": control_type,
210+
})
211+
return Controllable(self.remote, j)

quickstart/27-control-traffic-lights.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,28 @@
2121

2222
state = lgsvl.AgentState()
2323
state.transform = spawns[0]
24-
state.velocity = lgsvl.Vector(0, 0, 20)
24+
state.transform.position.z += 20
25+
# state.velocity = lgsvl.Vector(0, 0, 20)
2526
ego = sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state)
2627

2728
print("Python API Quickstart #27: How to Control Traffic Light")
2829

29-
# Get a list of controllable objects
30-
controllables = sim.get_controllables()
30+
# # Get a list of controllable objects
31+
controllables = sim.get_controllables("signal")
3132
print("\n# List of controllable objects in {} scene:".format(scene_name))
3233
for c in controllables:
3334
print(c)
3435

35-
# Pick a traffic light of intrest
36-
signal = controllables[2]
36+
signal = sim.get_controllable(lgsvl.Vector(19, 5, 21), "signal")
37+
print("\n# Signal of interest:")
38+
print(signal)
3739

3840
# Get current controllable states
3941
print("\n# Current control policy:")
4042
print(signal.control_policy)
4143

4244
# Create a new control policy
43-
control_policy = "trigger=50;green=1;yellow=1.5;red=2;green=5;loop"
45+
control_policy = "trigger=50;green=3;yellow=2;red=1;loop"
4446

4547
# Control this traffic light with a new control policy
4648
signal.control(control_policy)
@@ -52,7 +54,7 @@
5254
print("\n# Current signal state before simulation:")
5355
print(signal.current_state)
5456

55-
seconds = 10
57+
seconds = 18
5658
input("\nPress Enter to run simulation for {} seconds".format(seconds))
5759
print("\nRunning simulation for {} seconds...".format(seconds))
5860
sim.run(seconds)

0 commit comments

Comments
 (0)