Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI/Simulator: Drive a loop in metadrive #32308

Merged
merged 55 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
da95fb5
finish failure on crossing any line
bongbui321 Apr 28, 2024
8dc4894
update
bongbui321 Apr 28, 2024
88de374
standardize queue messages
bongbui321 Apr 28, 2024
f10158c
update control_command_gen
bongbui321 Apr 28, 2024
6bfefc4
fix
bongbui321 Apr 28, 2024
96cae21
fix logic
bongbui321 Apr 28, 2024
e7c509d
Merge branch 'queue_message' into metadrive_loop
bongbui321 Apr 28, 2024
abf54b6
update closing type
bongbui321 Apr 28, 2024
75abd6d
Merge branch 'queue_message' into metadrive_loop
bongbui321 Apr 28, 2024
fe19f3b
update test
bongbui321 Apr 29, 2024
ff0e769
update logic
bongbui321 Apr 29, 2024
1cd0edb
update test
bongbui321 Apr 29, 2024
6c605dc
merge master
bongbui321 May 6, 2024
866054a
Merge branch 'master' of https://github.com/commaai/openpilot into me…
bongbui321 May 7, 2024
270e7a5
add out of lane to local
bongbui321 May 7, 2024
a808cba
ci arrive_dest
bongbui321 May 15, 2024
8ecc989
Merge branch 'master' of https://github.com/commaai/openpilot into me…
bongbui321 May 15, 2024
46fda0b
pytest integration
bongbui321 May 18, 2024
0a8b331
merge master
bongbui321 May 18, 2024
9f905fa
update ci_config
bongbui321 May 18, 2024
ff67267
fix ruff
bongbui321 May 18, 2024
caa5e8e
move test termination to time
bongbui321 May 18, 2024
b0479e4
better
bongbui321 May 18, 2024
dd0d851
better order
bongbui321 May 18, 2024
58a305d
merge master
bongbui321 May 18, 2024
4898dcd
curve_len
bongbui321 May 19, 2024
f0ef637
add buffer
bongbui321 May 19, 2024
7ff5d22
cleanup
bongbui321 May 19, 2024
affc13e
cleanup
bongbui321 May 19, 2024
77bab20
cleanup
bongbui321 May 19, 2024
93ee681
cleanup
bongbui321 May 19, 2024
306d2d7
out_of_lane
bongbui321 May 19, 2024
bb5f893
cleanup
bongbui321 May 19, 2024
7076a12
merge tests
bongbui321 May 19, 2024
f9ded06
run 90s
bongbui321 May 19, 2024
858d617
change test name
bongbui321 May 19, 2024
dfc7bd3
local out of lane detect
bongbui321 May 19, 2024
c4ae510
out_of_lane
bongbui321 May 19, 2024
febab14
static anal
bongbui321 May 19, 2024
05b9b49
cleanup
bongbui321 May 20, 2024
4579458
test_duration
bongbui321 May 20, 2024
6717bac
change setup_class -> setup_create_bridge
bongbui321 May 20, 2024
3b644dd
no print state during test
bongbui321 May 20, 2024
d22133b
merge master
bongbui321 May 20, 2024
d70e9f8
new out_of_lane detect
bongbui321 May 21, 2024
80fe90c
cleanup print in common.py
bongbui321 May 21, 2024
7b19b9b
fix
bongbui321 May 21, 2024
9f052e6
fix
bongbui321 May 21, 2024
8eb15a0
Merge branch 'master' of https://github.com/commaai/openpilot into me…
bongbui321 May 21, 2024
5ba473e
check distance vs time
bongbui321 May 22, 2024
dfc14ec
cleanup
bongbui321 May 22, 2024
6479e33
cleanup increase check time
bongbui321 May 22, 2024
71ba53d
minimum bridge test time
bongbui321 May 22, 2024
8c0d071
wording
bongbui321 May 22, 2024
2b43631
cleanup
bongbui321 May 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tools_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ jobs:
${{ env.RUN }} "export MAPBOX_TOKEN='pk.eyJ1Ijoiam5ld2IiLCJhIjoiY2xxNW8zZXprMGw1ZzJwbzZneHd2NHljbSJ9.gV7VPRfbXFetD-1OVF0XZg' && \
source selfdrive/test/setup_xvfb.sh && \
source selfdrive/test/setup_vsound.sh && \
CI=1 pytest tools/sim/tests/test_metadrive_bridge.py -W ignore::pyopencl.CompilerWarning"
CI=1 pytest tools/sim/tests/test_metadrive_bridge.py --time_done=90 -W ignore::pyopencl.CompilerWarning"
bongbui321 marked this conversation as resolved.
Show resolved Hide resolved

devcontainer:
name: devcontainer
Expand Down
18 changes: 11 additions & 7 deletions tools/sim/bridge/metadrive/metadrive_bridge.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
from multiprocessing import Queue

from metadrive.component.sensors.base_camera import _cuda_enable
Expand Down Expand Up @@ -27,29 +28,32 @@ def curve_block(length, angle=45, direction=0):
}

def create_map(track_size=60):
curve_len = track_size * 2
return dict(
type=MapGenerateMethod.PG_MAP_FILE,
lane_num=2,
lane_width=3.5,
lane_width=4.5,
config=[
None,
straight_block(track_size),
curve_block(track_size*2, 90),
curve_block(curve_len, 90),
straight_block(track_size),
curve_block(track_size*2, 90),
curve_block(curve_len, 90),
straight_block(track_size),
curve_block(track_size*2, 90),
curve_block(curve_len, 90),
straight_block(track_size),
curve_block(track_size*2, 90),
curve_block(curve_len, 90),
]
)


class MetaDriveBridge(SimulatorBridge):
TICKS_PER_FRAME = 5

def __init__(self, dual_camera, high_quality):
def __init__(self, dual_camera, high_quality, time_done=math.inf, test_run=False):
self.should_render = False
self.test_run = test_run
self.time_done = time_done if self.test_run else math.inf

super().__init__(dual_camera, high_quality)

Expand Down Expand Up @@ -83,4 +87,4 @@ def spawn_world(self, queue: Queue):
preload_models=False
)

return MetaDriveWorld(queue, config, self.dual_camera)
return MetaDriveWorld(queue, config, self.time_done, self.test_run, self.dual_camera)
17 changes: 13 additions & 4 deletions tools/sim/bridge/metadrive/metadrive_process.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math
import time
import numpy as np

from collections import namedtuple
Expand Down Expand Up @@ -49,7 +50,7 @@ def arrive_destination_patch(self, *args, **kwargs):

def metadrive_process(dual_camera: bool, config: dict, camera_array, wide_camera_array, image_lock,
controls_recv: Connection, simulation_state_send: Connection, vehicle_state_send: Connection,
exit_event):
exit_event, start_time, time_done, test_run):
arrive_dest_done = config.pop("arrive_dest_done", True)
apply_metadrive_patches(arrive_dest_done)

Expand Down Expand Up @@ -111,10 +112,18 @@ def get_cam_as_rgb(cam):
reset()

if rk.frame % 5 == 0:
obs, _, terminated, _, info = env.step(vc)
_, _, terminated, _, _ = env.step(vc)
time_out = True if time.monotonic() - start_time >= time_done else False
out_of_lane = env.vehicle.on_broken_line or env.vehicle.on_yellow_continuous_line or env.vehicle.on_white_continuous_line or env.vehicle.crash_sidewalk
bongbui321 marked this conversation as resolved.
Show resolved Hide resolved

if terminated or ((out_of_lane or time_out) and test_run):
if terminated:
done_result = env.done_function("default_agent")
elif out_of_lane:
done_result = (True, {"out_of_lane" : True})
elif time_out:
done_result = (True, {"time_done" : True})

if terminated:
done_result = env.done_function("default_agent")
simulation_state = metadrive_simulation_state(
running=False,
done=done_result[0],
Expand Down
5 changes: 3 additions & 2 deletions tools/sim/bridge/metadrive/metadrive_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


class MetaDriveWorld(World):
def __init__(self, status_q, config, dual_camera = False):
def __init__(self, status_q, config, time_done, test_run, dual_camera=False):
super().__init__(dual_camera)
self.status_q = status_q
self.camera_array = Array(ctypes.c_uint8, W*H*3)
Expand All @@ -30,11 +30,12 @@ def __init__(self, status_q, config, dual_camera = False):

self.exit_event = multiprocessing.Event()

start_time = time.monotonic()
self.metadrive_process = multiprocessing.Process(name="metadrive process", target=
functools.partial(metadrive_process, dual_camera, config,
self.camera_array, self.wide_camera_array, self.image_lock,
self.controls_recv, self.simulation_state_send,
self.vehicle_state_send, self.exit_event))
self.vehicle_state_send, self.exit_event, start_time, time_done, test_run))

self.metadrive_process.start()
self.status_q.put(QueueMessage(QueueMessageType.START_STATUS, "starting"))
Expand Down
92 changes: 0 additions & 92 deletions tools/sim/scenarios/metadrive/stay_in_lane.py

This file was deleted.

8 changes: 8 additions & 0 deletions tools/sim/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import pytest

def pytest_addoption(parser):
parser.addoption("--time_done", action="store", default=60, type=int, help="Seconds to run metadrive drive")
bongbui321 marked this conversation as resolved.
Show resolved Hide resolved

@pytest.fixture
def time_done(request):
return request.config.getoption("--time_done")
7 changes: 6 additions & 1 deletion tools/sim/tests/test_metadrive_bridge.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
import pytest
import warnings

# Since metadrive depends on pkg_resources, and pkg_resources is deprecated as an API
warnings.filterwarnings("ignore", category=DeprecationWarning)

Expand All @@ -9,5 +10,9 @@

@pytest.mark.slow
class TestMetaDriveBridge(TestSimBridgeBase):
@pytest.fixture(autouse=True)
bongbui321 marked this conversation as resolved.
Show resolved Hide resolved
def setup_class(self, time_done):
self.time_done = time_done

def create_bridge(self):
return MetaDriveBridge(False, False)
return MetaDriveBridge(False, False, self.time_done, True)
15 changes: 14 additions & 1 deletion tools/sim/tests/test_sim_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from cereal import messaging
from openpilot.common.basedir import BASEDIR
from openpilot.tools.sim.bridge.common import QueueMessageType

SIM_DIR = os.path.join(BASEDIR, "tools/sim")

Expand All @@ -19,7 +20,7 @@ def setup_class(cls):
def setup_method(self):
self.processes = []

def test_engage(self):
def test_driving(self):
# Startup manager and bridge.py. Check processes are running, then engage and verify.
p_manager = subprocess.Popen("./launch_openpilot.sh", cwd=SIM_DIR)
self.processes.append(p_manager)
Expand Down Expand Up @@ -70,6 +71,18 @@ def test_engage(self):

assert min_counts_control_active == control_active, f"Simulator did not engage a minimal of {min_counts_control_active} steps was {control_active}"

failure_states = []
while bridge.started.value:
continue

while not q.empty():
state = q.get()
if state.type == QueueMessageType.TERMINATION_INFO:
done_info = state.info
failure_states = [done_state for done_state in done_info if done_state != "time_done" and done_info[done_state]]
break
assert len(failure_states) == 0, f"Simulator fails to finish a loop. Failure states: {failure_states}"

def teardown_method(self):
print("Test shutting down. CommIssues are acceptable")
for p in reversed(self.processes):
Expand Down
Loading