Skip to content

Commit 628871e

Browse files
committed
Use artefacts config toolkit to merge params
1 parent 935b2de commit 628871e

File tree

2 files changed

+7
-149
lines changed

2 files changed

+7
-149
lines changed

src/sam_bot_nav2_gz/test/test_reach_goal_pytest.py

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import pytest
33
import rclpy
4-
import json
54
from launch.substitutions import (
65
LaunchConfiguration,
76
)
@@ -21,65 +20,15 @@
2120
from sim_state import IgnitionSimStateUtil
2221
from sim_state.metric_value import Pose
2322
from artefacts_toolkit_testsuite.pytest import metrics_fixture
23+
from artefacts_toolkit_config import merge_ros_params_files
24+
#Currently requires https://github.com/art-e-fact/artefacts-toolkit-config/pull/8
2425

2526

2627
ARTEFACTS_PARAMS_FILE = os.environ.get(
2728
"ARTEFACTS_SCENARIO_PARAMS_FILE", "scenario_params.yaml"
2829
)
2930

3031

31-
def deep_merge_dicts(source, override):
32-
"""Recursively merge two dictionaries, with values from `override` taking precedence over `source`"""
33-
for key, value in override.items():
34-
if isinstance(value, dict) and key in source:
35-
source[key] = deep_merge_dicts(source[key], value)
36-
else:
37-
source[key] = value
38-
return source
39-
40-
def rosify_params(params: dict):
41-
"""
42-
Store `params` in `param_file` and convert to ros2 param file nested format,
43-
to be used by the launch file
44-
"""
45-
content = {}
46-
for k, v in params.items():
47-
try:
48-
node, pname = k.split("/")
49-
except Exception:
50-
print(
51-
localise(
52-
"Problem with parameter name. Please ensure params are in the format `node/param`"
53-
)
54-
)
55-
return
56-
if node not in content:
57-
content[node] = {"ros__parameters": {}}
58-
# handles nested keys for params in the form of dot notation
59-
current_level = content[node]["ros__parameters"]
60-
keys = pname.split(".")
61-
for key in keys[:-1]:
62-
if key not in current_level:
63-
current_level[key] = {}
64-
current_level = current_level[key]
65-
current_level[keys[-1]] = v
66-
return content
67-
68-
69-
def merge_ros_params_files(source, override, destination):
70-
"""Merge two ROS2 yaml parameter files into one, overriding the values in the first one with the values in `override`"""
71-
import yaml
72-
73-
with open(source, "r") as f:
74-
source_params = yaml.safe_load(f)
75-
76-
with open(override, "r") as f:
77-
override_param = yaml.safe_load(f)
78-
override_params_ros = rosify_params(override_param)
79-
80-
merged_params = deep_merge_dicts(source_params, override_params_ros)
81-
with open(destination, "w") as f:
82-
yaml.dump(merged_params, f)
8332

8433

8534
@pytest.fixture(scope="module")
@@ -106,7 +55,7 @@ def launch_description(reach_goal_proc, sim):
10655
new_params_file = "all_params.yaml"
10756
try:
10857
merge_ros_params_files(
109-
source_params_file, ARTEFACTS_PARAMS_FILE, new_params_file
58+
source_params_file, ARTEFACTS_PARAMS_FILE, new_params_file, rosify=True
11059
)
11160
except FileNotFoundError:
11261
pass

src/sam_bot_nav2_gz/test/test_waypoints_pytest.py

Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import os
22
import pytest
33
import rclpy
4-
import json
54
from launch.substitutions import (
65
LaunchConfiguration,
76
)
@@ -23,51 +22,14 @@
2322
from artefacts_toolkit_testsuite.pytest import metrics_fixture
2423
import yaml
2524
from datetime import datetime
25+
from artefacts_toolkit_config import merge_ros_params_files
26+
#Currently requires https://github.com/art-e-fact/artefacts-toolkit-config/pull/8
2627

2728

2829
ARTEFACTS_PARAMS_FILE = os.environ.get(
2930
"ARTEFACTS_SCENARIO_PARAMS_FILE", "scenario_params.yaml"
3031
)
3132

32-
waypoints = yaml.safe_load('''
33-
waypoints:
34-
- position:
35-
x: 0.8006443977355957
36-
y: 0.5491957664489746
37-
z: 0.0
38-
orientation:
39-
x: 0.0
40-
y: 0.0
41-
z: -0.0055409271259092485
42-
w: 0.9999846489454652
43-
- position:
44-
x: 1.8789787292480469
45-
y: 0.5389942526817322
46-
z: 0.0
47-
orientation:
48-
x: 0.0
49-
y: 0.0
50-
z: 0.010695864295550759
51-
w: 0.9999427976074288
52-
- position:
53-
x: 3.0792641639709473
54-
y: 0.6118782758712769
55-
z: 0.0
56-
orientation:
57-
x: 0.0
58-
y: 0.0
59-
z: 0.01899610435153287
60-
w: 0.9998195577300264
61-
- position:
62-
x: 3.8347740173339844
63-
y: 0.012513279914855957
64-
z: 0.0
65-
orientation:
66-
x: 0.0
67-
y: 0.0
68-
z: -0.7548200584119721
69-
w: 0.6559319167558071
70-
''')
7133
waypoints = yaml.safe_load('''
7234
waypoints:
7335
- position:
@@ -144,59 +106,6 @@
144106
w: 0.6960848684271199
145107
''')
146108

147-
def deep_merge_dicts(source, override):
148-
"""Recursively merge two dictionaries, with values from `override` taking precedence over `source`"""
149-
for key, value in override.items():
150-
if isinstance(value, dict) and key in source:
151-
source[key] = deep_merge_dicts(source[key], value)
152-
else:
153-
source[key] = value
154-
return source
155-
156-
def rosify_params(params: dict):
157-
"""
158-
Store `params` in `param_file` and convert to ros2 param file nested format,
159-
to be used by the launch file
160-
"""
161-
content = {}
162-
for k, v in params.items():
163-
try:
164-
node, pname = k.split("/")
165-
except Exception:
166-
print(
167-
localise(
168-
"Problem with parameter name. Please ensure params are in the format `node/param`"
169-
)
170-
)
171-
return
172-
if node not in content:
173-
content[node] = {"ros__parameters": {}}
174-
# handles nested keys for params in the form of dot notation
175-
current_level = content[node]["ros__parameters"]
176-
keys = pname.split(".")
177-
for key in keys[:-1]:
178-
if key not in current_level:
179-
current_level[key] = {}
180-
current_level = current_level[key]
181-
current_level[keys[-1]] = v
182-
return content
183-
184-
185-
def merge_ros_params_files(source, override, destination):
186-
"""Merge two ROS2 yaml parameter files into one, overriding the values in the first one with the values in `override`"""
187-
import yaml
188-
189-
with open(source, "r") as f:
190-
source_params = yaml.safe_load(f)
191-
192-
with open(override, "r") as f:
193-
override_param = yaml.safe_load(f)
194-
override_params_ros = rosify_params(override_param)
195-
196-
merged_params = deep_merge_dicts(source_params, override_params_ros)
197-
with open(destination, "w") as f:
198-
yaml.dump(merged_params, f)
199-
200109

201110
@pytest.fixture(scope="module")
202111
def follow_waypoints_proc():
@@ -222,7 +131,7 @@ def launch_description(follow_waypoints_proc, sim): #make sure sim is initialize
222131
new_params_file = "all_params.yaml"
223132
try:
224133
merge_ros_params_files(
225-
source_params_file, ARTEFACTS_PARAMS_FILE, new_params_file
134+
source_params_file, ARTEFACTS_PARAMS_FILE, new_params_file, rosify=True
226135
)
227136
except FileNotFoundError:
228137
pass
@@ -352,7 +261,7 @@ def validate_nav2_output(output):
352261
@pytest.mark.parametrize("waypoint_idx", range(len(waypoints["waypoints"])))
353262
def test_1_reached_waypoint(follow_waypoints_proc, launch_context, sim, artefacts_metrics, waypoint_idx):
354263
"""Check that each waypoint is reached"""
355-
#TODO, without async, check timing could be way too late
264+
# Somehow the test does not seem to be called before the end of the full waypoints navigation, preventing the use of now()
356265

357266
# Additional assertion using simulation state - wait for entity to be available
358267
print("Now checking entity state after goal completion...")

0 commit comments

Comments
 (0)