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

fix: allow targets to specify how inspection points should be weighted #20

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 8 additions & 0 deletions safe_autonomy_simulation/sims/inspection/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Target(spacecraft.CWHSpacecraft):
Parent entity of spacecraft, by default None
use_jax : bool, optional
EXPERIMENTAL: Use JAX to accelerate state transition computation, by default False.
half_weighted : bool, optional
weight only half of the target (other half has zero weight). Default false (weight all points)
"""

def __init__(
Expand All @@ -56,6 +58,7 @@ def __init__(
material: mat.Material = defaults.CWH_MATERIAL,
parent: typing.Union[e.PhysicalEntity, None] = None,
use_jax: bool = False,
half_weighted: bool = False
):
super().__init__(
name=name,
Expand All @@ -75,6 +78,7 @@ def __init__(
num_points=num_points,
radius=radius,
priority_vector=priority_vector,
half_weighted=half_weighted
)

@property
Expand Down Expand Up @@ -142,6 +146,8 @@ class SixDOFTarget(spacecraft.SixDOFSpacecraft):
List of children entities of spacecraft, by default []
use_jax : bool, optional
EXPERIMENTAL: Use JAX to accelerate state transition computation, by default False.
half_weighted : bool, optional
weight only half of the target (other half has zero weight). Default false (weight all points)
"""

def __init__(
Expand Down Expand Up @@ -170,6 +176,7 @@ def __init__(
parent: typing.Union[e.PhysicalEntity, None] = None,
children: list[e.PhysicalEntity] = [],
use_jax: bool = False,
half_weighted: bool = False
):
super().__init__(
name=name,
Expand Down Expand Up @@ -200,6 +207,7 @@ def __init__(
num_points=num_points,
radius=radius,
priority_vector=priority_vector,
half_weighted=half_weighted
)

@property
Expand Down
43 changes: 42 additions & 1 deletion test/sims/inspection/test_target/test_six_dof_target.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import pytest
import numpy as np
import safe_autonomy_simulation
Expand Down Expand Up @@ -42,7 +43,7 @@ def test_init_default(name, num_points, radius):


@pytest.mark.parametrize(
"name, num_points, radius, priority_vector, position, velocity, orientation, angular_velocity, m, n, inertia_matrix, ang_acc_limit, ang_vel_limit, inertia_wheel, acc_limit_wheel, vel_limit_wheel, thrust_control_limit, body_frame_thrust, trajectory_samples, integration_method, material, parent",
"name, num_points, radius, priority_vector, position, velocity, orientation, angular_velocity, m, n, inertia_matrix, ang_acc_limit, ang_vel_limit, inertia_wheel, acc_limit_wheel, vel_limit_wheel, thrust_control_limit, body_frame_thrust, trajectory_samples, integration_method, material, parent, half_weighted",
[
(
"target",
Expand All @@ -67,6 +68,32 @@ def test_init_default(name, num_points, radius):
"RK45",
safe_autonomy_simulation.sims.spacecraft.defaults.CWH_MATERIAL,
None,
False,
),
(
"target",
10,
1,
np.array([1, 0, 0]),
np.array([1, 0, 0]),
np.array([0, 0, 0]),
np.array([0, 0, 0, 1]),
np.array([0, 0, 0]),
1,
1,
np.identity(3),
0.01,
0.03,
1e-5,
180,
576,
1.0,
True,
10,
"RK45",
safe_autonomy_simulation.sims.spacecraft.defaults.CWH_MATERIAL,
None,
True,
),
],
)
Expand All @@ -93,6 +120,7 @@ def test_init_args(
integration_method,
material,
parent,
half_weighted,
):
target = safe_autonomy_simulation.sims.inspection.SixDOFTarget(
name=name,
Expand All @@ -117,6 +145,7 @@ def test_init_args(
integration_method=integration_method,
material=material,
parent=parent,
half_weighted=half_weighted
)
assert target.name == name
assert target.inspection_points.radius == radius
Expand All @@ -136,3 +165,15 @@ def test_init_args(
assert target.dynamics.integration_method == integration_method
assert target.material == material
assert target.parent == parent

weighted_point_count = 0

for point in target.inspection_points.points.values():
if point.weight > 0.0:
weighted_point_count += 1

max_weighted_count = math.ceil(target.inspection_points.num_points / 2) + 2 if half_weighted else target.inspection_points.num_points
min_weighted_count = math.ceil(target.inspection_points.num_points / 2) - 2 if half_weighted else target.inspection_points.num_points

# There is some wiggle room in number of points and weighting, so exact numbers cannot be used
assert min_weighted_count <= weighted_point_count <= max_weighted_count
33 changes: 32 additions & 1 deletion test/sims/inspection/test_target/test_target.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import math
import pytest
import numpy as np
import safe_autonomy_simulation
Expand Down Expand Up @@ -43,7 +44,7 @@ def test_init_default(name, num_points, radius):


@pytest.mark.parametrize(
"name, num_points, radius, priority_vector, position, velocity, m, n, trajectory_samples, integration_method, material, parent",
"name, num_points, radius, priority_vector, position, velocity, m, n, trajectory_samples, integration_method, material, parent, half_weighted",
[
(
"target",
Expand All @@ -58,6 +59,22 @@ def test_init_default(name, num_points, radius):
"RK45",
safe_autonomy_simulation.sims.spacecraft.defaults.CWH_MATERIAL,
None,
False,
),
(
"target",
50,
1,
np.array([1, 0, 0]),
np.array([1, 0, 0]),
np.array([0, 0, 0]),
1,
1,
10,
"RK45",
safe_autonomy_simulation.sims.spacecraft.defaults.CWH_MATERIAL,
None,
True,
),
],
)
Expand All @@ -74,6 +91,7 @@ def test_init_args(
integration_method,
material,
parent,
half_weighted
):
target = safe_autonomy_simulation.sims.inspection.Target(
name=name,
Expand All @@ -88,6 +106,7 @@ def test_init_args(
integration_method=integration_method,
material=material,
parent=parent,
half_weighted=half_weighted
)
assert target.name == name
assert target.inspection_points.radius == radius
Expand All @@ -104,3 +123,15 @@ def test_init_args(
assert target.dynamics.integration_method == integration_method
assert target.material == material
assert target.parent == parent

weighted_point_count = 0

for point in target.inspection_points.points.values():
if point.weight > 0.0:
weighted_point_count += 1

max_weighted_count = math.ceil(target.inspection_points.num_points / 2) + 2 if half_weighted else target.inspection_points.num_points
min_weighted_count = math.ceil(target.inspection_points.num_points / 2) - 2 if half_weighted else target.inspection_points.num_points

# There is some wiggle room in number of points and weighting, so exact numbers cannot be used
assert min_weighted_count <= weighted_point_count <= max_weighted_count
Loading