Skip to content

Commit e02c28e

Browse files
authored
Merge pull request #111 from ichumuh/fix_viewmodifications
semantic annotations now actually work
2 parents 1566721 + 915c2ed commit e02c28e

13 files changed

Lines changed: 382 additions & 272 deletions

File tree

examples/persistence_of_annotated_worlds.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ Next, we create a semantic annotation that describes the table.
5555

5656
```{code-cell} ipython3
5757
table_semantic_annotation = Table([b for b in world.bodies if "top" in str(b.name)][0])
58-
world.add_semantic_annotation(table_semantic_annotation)
58+
with world.modify_world():
59+
world.add_semantic_annotation(table_semantic_annotation)
5960
print(table_semantic_annotation)
6061
```
6162

src/semantic_digital_twin/exceptions.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from dataclasses import dataclass
44
from typing import Iterable, Tuple, Union
55

6-
from typing_extensions import Optional, List, Type, TYPE_CHECKING
6+
from typing_extensions import Optional, List, Type, TYPE_CHECKING, Callable
77

88
from .datastructures.prefixed_name import PrefixedName
99

@@ -38,6 +38,15 @@ def __post_init__(self):
3838
super().__init__(msg)
3939

4040

41+
@dataclass
42+
class MissingWorldModificationContextError(UsageError):
43+
function: Callable
44+
45+
def __post_init__(self):
46+
msg = f"World function '{self.function.__name__}' was called without a 'with world.modify_world():' context manager."
47+
super().__init__(msg)
48+
49+
4150
@dataclass
4251
class DuplicateSemanticAnnotationError(UsageError):
4352
semantic_annotations: List[SemanticAnnotation]

src/semantic_digital_twin/reasoning/world_reasoner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def infer_semantic_annotations(self) -> List[SemanticAnnotation]:
4141
4242
:return: The inferred semantic annotations of the world.
4343
"""
44-
result = self.reason()
44+
with self.world.modify_world():
45+
result = self.reason()
4546
return result.get("semantic_annotations", [])
4647

4748
def reason(self) -> Dict[str, Any]:

src/semantic_digital_twin/robots/hsrb.py

Lines changed: 120 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -45,125 +45,136 @@ def from_world(cls, world: World) -> Self:
4545
Creates an HSRB (Human Support Robot B) semantic annotation from a World that was parsed from
4646
resources/urdf/robots/hsrb.urdf. Assumes all URDF link names exist in the world.
4747
"""
48-
hsrb = cls(
49-
name=PrefixedName("hsrb", prefix=world.name),
50-
root=world.get_body_by_name("base_footprint"),
51-
_world=world,
52-
)
48+
with world.modify_world():
49+
hsrb = cls(
50+
name=PrefixedName("hsrb", prefix=world.name),
51+
root=world.get_body_by_name("base_footprint"),
52+
_world=world,
53+
)
5354

54-
gripper_thumb = Finger(
55-
name=PrefixedName("thumb", prefix=hsrb.name.name),
56-
root=world.get_body_by_name("hand_l_proximal_link"),
57-
tip=world.get_body_by_name("hand_l_finger_tip_frame"),
58-
_world=world,
59-
)
55+
gripper_thumb = Finger(
56+
name=PrefixedName("thumb", prefix=hsrb.name.name),
57+
root=world.get_body_by_name("hand_l_proximal_link"),
58+
tip=world.get_body_by_name("hand_l_finger_tip_frame"),
59+
_world=world,
60+
)
6061

61-
gripper_finger = Finger(
62-
name=PrefixedName("finger", prefix=hsrb.name.name),
63-
root=world.get_body_by_name("hand_r_proximal_link"),
64-
tip=world.get_body_by_name("hand_r_finger_tip_frame"),
65-
_world=world,
66-
)
62+
gripper_finger = Finger(
63+
name=PrefixedName("finger", prefix=hsrb.name.name),
64+
root=world.get_body_by_name("hand_r_proximal_link"),
65+
tip=world.get_body_by_name("hand_r_finger_tip_frame"),
66+
_world=world,
67+
)
6768

68-
gripper = ParallelGripper(
69-
name=PrefixedName("gripper", prefix=hsrb.name.name),
70-
root=world.get_body_by_name("hand_palm_link"),
71-
tool_frame=world.get_body_by_name("hand_gripper_tool_frame"),
72-
thumb=gripper_thumb,
73-
finger=gripper_finger,
74-
front_facing_axis=Vector3(0, 0, 1),
75-
front_facing_orientation=Quaternion(-1, 0, -1, 0),
76-
_world=world,
77-
)
69+
gripper = ParallelGripper(
70+
name=PrefixedName("gripper", prefix=hsrb.name.name),
71+
root=world.get_body_by_name("hand_palm_link"),
72+
tool_frame=world.get_body_by_name("hand_gripper_tool_frame"),
73+
thumb=gripper_thumb,
74+
finger=gripper_finger,
75+
front_facing_axis=Vector3(0, 0, 1),
76+
front_facing_orientation=Quaternion(-1, 0, -1, 0),
77+
_world=world,
78+
)
7879

79-
# the min and max height are incorrect, same with the FoV. needs to be corrected using the real robot
80-
hand_camera = Camera(
81-
name=PrefixedName("hand_camera", prefix=hsrb.name.name),
82-
root=world.get_body_by_name("hand_camera_frame"),
83-
forward_facing_axis=Vector3(1, 0, 0),
84-
field_of_view=FieldOfView(horizontal_angle=0.99483, vertical_angle=0.75049),
85-
minimal_height=0.75049,
86-
maximal_height=0.99483,
87-
_world=world,
88-
)
80+
# the min and max height are incorrect, same with the FoV. needs to be corrected using the real robot
81+
hand_camera = Camera(
82+
name=PrefixedName("hand_camera", prefix=hsrb.name.name),
83+
root=world.get_body_by_name("hand_camera_frame"),
84+
forward_facing_axis=Vector3(1, 0, 0),
85+
field_of_view=FieldOfView(
86+
horizontal_angle=0.99483, vertical_angle=0.75049
87+
),
88+
minimal_height=0.75049,
89+
maximal_height=0.99483,
90+
_world=world,
91+
)
8992

90-
arm = Arm(
91-
name=PrefixedName("arm", prefix=hsrb.name.name),
92-
root=world.get_body_by_name("torso_lift_link"),
93-
tip=world.get_body_by_name("hand_palm_link"),
94-
manipulator=gripper,
95-
sensors={hand_camera},
96-
_world=world,
97-
)
98-
hsrb.add_arm(arm)
99-
100-
# Create camera and neck
101-
head_center_camera = Camera(
102-
name=PrefixedName("head_center_camera", prefix=hsrb.name.name),
103-
root=world.get_body_by_name("head_center_camera_frame"),
104-
forward_facing_axis=Vector3(1, 0, 0),
105-
field_of_view=FieldOfView(horizontal_angle=0.99483, vertical_angle=0.75049),
106-
minimal_height=0.75049,
107-
maximal_height=0.99483,
108-
_world=world,
109-
)
93+
arm = Arm(
94+
name=PrefixedName("arm", prefix=hsrb.name.name),
95+
root=world.get_body_by_name("torso_lift_link"),
96+
tip=world.get_body_by_name("hand_palm_link"),
97+
manipulator=gripper,
98+
sensors={hand_camera},
99+
_world=world,
100+
)
101+
hsrb.add_arm(arm)
102+
103+
# Create camera and neck
104+
head_center_camera = Camera(
105+
name=PrefixedName("head_center_camera", prefix=hsrb.name.name),
106+
root=world.get_body_by_name("head_center_camera_frame"),
107+
forward_facing_axis=Vector3(1, 0, 0),
108+
field_of_view=FieldOfView(
109+
horizontal_angle=0.99483, vertical_angle=0.75049
110+
),
111+
minimal_height=0.75049,
112+
maximal_height=0.99483,
113+
_world=world,
114+
)
110115

111-
head_r_camera = Camera(
112-
name=PrefixedName("head_right_camera", prefix=hsrb.name.name),
113-
root=world.get_body_by_name("head_r_stereo_camera_link"),
114-
forward_facing_axis=Vector3(1, 0, 0),
115-
field_of_view=FieldOfView(horizontal_angle=0.99483, vertical_angle=0.75049),
116-
minimal_height=0.75049,
117-
maximal_height=0.99483,
118-
_world=world,
119-
)
116+
head_r_camera = Camera(
117+
name=PrefixedName("head_right_camera", prefix=hsrb.name.name),
118+
root=world.get_body_by_name("head_r_stereo_camera_link"),
119+
forward_facing_axis=Vector3(1, 0, 0),
120+
field_of_view=FieldOfView(
121+
horizontal_angle=0.99483, vertical_angle=0.75049
122+
),
123+
minimal_height=0.75049,
124+
maximal_height=0.99483,
125+
_world=world,
126+
)
120127

121-
head_l_camera = Camera(
122-
name=PrefixedName("head_left_camera", prefix=hsrb.name.name),
123-
root=world.get_body_by_name("head_l_stereo_camera_link"),
124-
forward_facing_axis=Vector3(1, 0, 0),
125-
field_of_view=FieldOfView(horizontal_angle=0.99483, vertical_angle=0.75049),
126-
minimal_height=0.75049,
127-
maximal_height=0.99483,
128-
_world=world,
129-
)
128+
head_l_camera = Camera(
129+
name=PrefixedName("head_left_camera", prefix=hsrb.name.name),
130+
root=world.get_body_by_name("head_l_stereo_camera_link"),
131+
forward_facing_axis=Vector3(1, 0, 0),
132+
field_of_view=FieldOfView(
133+
horizontal_angle=0.99483, vertical_angle=0.75049
134+
),
135+
minimal_height=0.75049,
136+
maximal_height=0.99483,
137+
_world=world,
138+
)
130139

131-
head_rgbd_camera = Camera(
132-
name=PrefixedName("head_rgbd_camera", prefix=hsrb.name.name),
133-
root=world.get_body_by_name("head_rgbd_sensor_link"),
134-
forward_facing_axis=Vector3(1, 0, 0),
135-
field_of_view=FieldOfView(horizontal_angle=0.99483, vertical_angle=0.75049),
136-
minimal_height=0.75049,
137-
maximal_height=0.99483,
138-
_world=world,
139-
)
140+
head_rgbd_camera = Camera(
141+
name=PrefixedName("head_rgbd_camera", prefix=hsrb.name.name),
142+
root=world.get_body_by_name("head_rgbd_sensor_link"),
143+
forward_facing_axis=Vector3(1, 0, 0),
144+
field_of_view=FieldOfView(
145+
horizontal_angle=0.99483, vertical_angle=0.75049
146+
),
147+
minimal_height=0.75049,
148+
maximal_height=0.99483,
149+
_world=world,
150+
)
140151

141-
neck = Neck(
142-
name=PrefixedName("neck", prefix=hsrb.name.name),
143-
sensors={
144-
head_center_camera,
145-
head_r_camera,
146-
head_l_camera,
147-
head_rgbd_camera,
148-
},
149-
root=world.get_body_by_name("head_pan_link"),
150-
tip=world.get_body_by_name("head_tilt_link"),
151-
_world=world,
152-
)
153-
hsrb.add_neck(neck)
154-
155-
# Create torso
156-
torso = Torso(
157-
name=PrefixedName("torso", prefix=hsrb.name.name),
158-
root=world.get_body_by_name("base_link"),
159-
tip=world.get_body_by_name("torso_lift_link"),
160-
_world=world,
161-
)
162-
hsrb.add_torso(torso)
152+
neck = Neck(
153+
name=PrefixedName("neck", prefix=hsrb.name.name),
154+
sensors={
155+
head_center_camera,
156+
head_r_camera,
157+
head_l_camera,
158+
head_rgbd_camera,
159+
},
160+
root=world.get_body_by_name("head_pan_link"),
161+
tip=world.get_body_by_name("head_tilt_link"),
162+
_world=world,
163+
)
164+
hsrb.add_neck(neck)
165+
166+
# Create torso
167+
torso = Torso(
168+
name=PrefixedName("torso", prefix=hsrb.name.name),
169+
root=world.get_body_by_name("base_link"),
170+
tip=world.get_body_by_name("torso_lift_link"),
171+
_world=world,
172+
)
173+
hsrb.add_torso(torso)
163174

164-
world.add_semantic_annotation(hsrb, exists_ok=True)
175+
world.add_semantic_annotation(hsrb, exists_ok=True)
165176

166-
vel_limits = defaultdict(lambda: 1)
167-
hsrb.tighten_dof_velocity_limits_of_1dof_connections(new_limits=vel_limits)
177+
vel_limits = defaultdict(lambda: 1)
178+
hsrb.tighten_dof_velocity_limits_of_1dof_connections(new_limits=vel_limits)
168179

169180
return hsrb

0 commit comments

Comments
 (0)