Skip to content

Commit 56a25ba

Browse files
shalinmehtalgsvlmartins-mozeiko
authored andcommitted
Generalize unit tests
1 parent ef7b1df commit 56a25ba

File tree

9 files changed

+185
-136
lines changed

9 files changed

+185
-136
lines changed

lgsvl/agent.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from .utils import accepts
1010

1111
from enum import Enum
12-
from collections import namedtuple
1312
from collections.abc import Iterable, Callable
1413
import math
1514

@@ -221,7 +220,7 @@ def follow(self, waypoints, loop = False):
221220
Parameters
222221
----------
223222
waypoints : list of DriveWaypoints
224-
DriveWaypoint : tuple (position, speed, angle, idle, deactivate, trigger_distance)
223+
DriveWaypoint : Class (position, speed, angle, idle, trigger_distance)
225224
226225
position : lgsvl.Vector()
227226
Unity coordinates of waypoint
@@ -309,7 +308,7 @@ def follow(self, waypoints, loop = False):
309308
Parameters
310309
----------
311310
waypoints : list of WalkWaypoints
312-
WalkWaypoint : tuple (position, idle, trigger_distance)
311+
WalkWaypoint : Class (position, idle, trigger_distance)
313312
314313
position : lgsvl.Vector()
315314
Unity coordinates of waypoint

lgsvl/geometry.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#
66
from math import sqrt
77

8+
import math
9+
810
class Vector:
911
def __init__(self, x = 0.0, y = 0.0, z = 0.0):
1012
self.x = x
@@ -48,6 +50,9 @@ def __mul__(self, v):
4850
def __rmul__(self, v):
4951
return self * v
5052

53+
def __neg__(self):
54+
return Vector(-self.x, -self.y, -self.z)
55+
5156
def magnitude(self):
5257
return sqrt(self.x**2 + self.y**2 + self.z**2)
5358

quickstart/03-raycast.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
state = lgsvl.AgentState()
2121
state.transform = spawns[0]
22+
forward = lgsvl.utils.transform_to_forward(state.transform)
23+
right = lgsvl.utils.transform_to_right(state.transform)
24+
up = lgsvl.utils.transform_to_up(state.transform)
2225
sim.add_agent("Lincoln2017MKZ (Apollo 5.0)", lgsvl.AgentType.EGO, state)
2326

2427
# This is the point from which the rays will originate from. It is raised 1m from the ground
@@ -39,26 +42,26 @@
3942

4043
# raycast returns None if the ray doesn't collide with anything
4144
# hit also has the point property which is the Unity position vector of where the ray collided with something
42-
hit = sim.raycast(p, lgsvl.Vector(1,0,0), layer_mask)
45+
hit = sim.raycast(p, right, layer_mask)
4346
if hit:
4447
print("Distance right:", hit.distance)
4548

46-
hit = sim.raycast(p, lgsvl.Vector(-1,0,0), layer_mask)
49+
hit = sim.raycast(p, -right, layer_mask)
4750
if hit:
4851
print("Distance left:", hit.distance)
4952

50-
hit = sim.raycast(p, lgsvl.Vector(0,0,-1), layer_mask)
53+
hit = sim.raycast(p, -forward, layer_mask)
5154
if hit:
5255
print("Distance back:", hit.distance)
5356

54-
hit = sim.raycast(p, lgsvl.Vector(0,0,1), layer_mask)
57+
hit = sim.raycast(p, forward, layer_mask)
5558
if hit:
5659
print("Distance forward:", hit.distance)
5760

58-
hit = sim.raycast(p, lgsvl.Vector(0,1,0), layer_mask)
61+
hit = sim.raycast(p, up, layer_mask)
5962
if hit:
6063
print("Distance up:", hit.distance)
6164

62-
hit = sim.raycast(p, lgsvl.Vector(0,-1,0), layer_mask)
65+
hit = sim.raycast(p, -up, layer_mask)
6366
if hit:
6467
print("Distance down:", hit.distance)

tests/test_EGO.py

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,41 +40,48 @@ def test_agent_velocity(self): # Check EGO velocity
4040
cmEqual(self, agent.state.velocity, state.velocity, "0 Velocity")
4141

4242
sim.reset()
43-
state.velocity = lgsvl.Vector(-50, 0, 0)
43+
right = lgsvl.utils.transform_to_right(state.transform)
44+
state.velocity = -50 * right
4445
agent = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, state)
4546

4647
cmEqual(self, agent.state.velocity, state.velocity, "50 Velocity")
4748

4849
def test_ego_different_directions(self): # Check that the xyz velocities equate to xyz changes in position
4950
with SimConnection(40) as sim:
5051
state = spawnState(sim)
51-
state.velocity = lgsvl.Vector(-10,0,0)
52+
forward = lgsvl.utils.transform_to_forward(state.transform)
53+
up = lgsvl.utils.transform_to_up(state.transform)
54+
right = lgsvl.utils.transform_to_right(state.transform)
55+
56+
state.velocity = -10 * right
5257
ego = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, state)
5358
sim.run(.5)
54-
self.assertNotAlmostEqual(state.position.x, ego.state.position.x, places=1)
55-
self.assertAlmostEqual(state.position.y, ego.state.position.y, places=1)
56-
self.assertAlmostEqual(state.position.z, ego.state.position.z, places=1)
59+
target = state.position - 3 * right
60+
self.assertLess((ego.state.position - target).magnitude(), 1)
5761
sim.remove_agent(ego)
58-
state.velocity = lgsvl.Vector(0,10,0)
62+
63+
state.velocity = 10 * up
5964
ego = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, state)
6065
sim.run(.5)
61-
self.assertNotAlmostEqual(state.position.y, ego.state.position.y, places=1)
62-
self.assertAlmostEqual(state.position.x, ego.state.position.x, places=1)
63-
self.assertAlmostEqual(state.position.z, ego.state.position.z, places=1)
66+
target = state.position + 3 * up
67+
self.assertLess((ego.state.position - target).magnitude(), 1)
6468
sim.remove_agent(ego)
65-
state.velocity = lgsvl.Vector(0,0,10)
69+
70+
state.velocity = 10 * forward
6671
ego = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, state)
6772
sim.run(.5)
68-
self.assertNotAlmostEqual(state.position.z, ego.state.position.z, places=1)
69-
self.assertAlmostEqual(state.position.y, ego.state.position.y, delta=0.15)
70-
self.assertAlmostEqual(state.position.x, ego.state.position.x, places=1)
73+
target = state.position + 4 * forward
74+
self.assertLess((ego.state.position - target).magnitude(), 1)
7175

7276
def test_speed(self): # check that speed returns a reasonable number
7377
with SimConnection() as sim:
7478
state = spawnState(sim)
75-
state.velocity = lgsvl.Vector(-10,10,10)
79+
forward = lgsvl.utils.transform_to_forward(state.transform)
80+
up = lgsvl.utils.transform_to_up(state.transform)
81+
right = lgsvl.utils.transform_to_right(state.transform)
82+
state.velocity = -10 * right + 10 * up + 10 * forward
7683
ego = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, state)
77-
self.assertAlmostEqual(ego.state.speed, math.sqrt(300))
84+
self.assertAlmostEqual(ego.state.speed, math.sqrt(300), places=3)
7885

7986
@unittest.skip("No highway in currents maps")
8087
def test_rotation_on_highway_ramp(self): # Check that vehicle is rotated when spawned on the highway ramp
@@ -120,7 +127,7 @@ def test_ego_braking(self): # Check that a brake command can be given to an EGO
120127
control = lgsvl.VehicleControl()
121128
ego.apply_control(control,True)
122129
sim.run(3)
123-
noBrakePosition = ego.state.position.z
130+
noBrakeDistance = (ego.state.position - state.position).magnitude()
124131

125132
sim.reset()
126133
ego = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, state)
@@ -132,7 +139,8 @@ def test_ego_braking(self): # Check that a brake command can be given to an EGO
132139
control.braking = 1
133140
ego.apply_control(control, True)
134141
sim.run(3)
135-
self.assertLess(ego.state.position.z, noBrakePosition)
142+
brakeDistance = (ego.state.position - state.position).magnitude()
143+
self.assertLess(brakeDistance, noBrakeDistance)
136144

137145
def test_ego_handbrake(self): # Check that the handbrake can be enable on an EGO vehicle, and the car stops sooner than without brakes
138146
with SimConnection(60) as sim:
@@ -145,7 +153,7 @@ def test_ego_handbrake(self): # Check that the handbrake can be enable on an EGO
145153
control = lgsvl.VehicleControl()
146154
ego.apply_control(control,True)
147155
sim.run(3)
148-
noBrakePosition = ego.state.position.z
156+
noBrakeDistance = (ego.state.position - state.position).magnitude()
149157

150158
sim.reset()
151159
ego = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, state)
@@ -157,32 +165,41 @@ def test_ego_handbrake(self): # Check that the handbrake can be enable on an EGO
157165
control.handbrake = True
158166
ego.apply_control(control, True)
159167
sim.run(3)
160-
self.assertLess(ego.state.position.z, noBrakePosition)
168+
brakeDistance = (ego.state.position - state.position).magnitude()
169+
self.assertLess(brakeDistance, noBrakeDistance)
161170

162171
def test_ego_reverse(self): # Check that the gear can be changed in an EGO vehicle, and the car moves in reverse
163172
with SimConnection() as sim:
164-
ego = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, spawnState(sim))
173+
state = spawnState(sim)
174+
ego = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, state)
165175
control = lgsvl.VehicleControl()
166176
control.throttle = 0.5
167177
control.reverse = True
168178
ego.apply_control(control, True)
169179
sim.run(2)
170-
self.assertLess(ego.state.position.z, sim.get_spawn()[0].position.z)
180+
forward = lgsvl.utils.transform_to_forward(state.transform)
181+
target = state.position - 3.5 * forward
182+
diff = ego.state.position - target
183+
self.assertLess((diff).magnitude(), 1)
171184

172185
def test_not_sticky_control(self): # Check that the a non sticky control is removed
173186
with SimConnection(60) as sim:
174187
state = spawnState(sim)
175-
state.position.z += 20
176188
ego = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, state)
177189
control = lgsvl.VehicleControl()
178190
control.throttle = 1
179191
ego.apply_control(control, True)
180192
sim.run(3)
181193
stickySpeed = ego.state.speed
194+
195+
sim.reset()
196+
ego = sim.add_agent("Jaguar2015XE (Apollo 3.0)", lgsvl.AgentType.EGO, state)
182197
control = lgsvl.VehicleControl()
183198
control.throttle = 1
184-
ego.apply_control(control, False)
185-
sim.run(3)
199+
ego.apply_control(control, True)
200+
sim.run(1)
201+
ego.apply_control(control,False)
202+
sim.run(2)
186203
finalSpeed = ego.state.speed
187204
self.assertGreater(stickySpeed, finalSpeed)
188205

0 commit comments

Comments
 (0)