forked from jasonxyliu/Lang2LTL-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_map.py
119 lines (100 loc) · 5.03 KB
/
load_map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import argparse
import sys
import os
from bosdyn.api.graph_nav import map_pb2
def load_map(path):
"""
Load a map from the given file path.
:param path: Path to the root directory of the map.
:return: the graph, waypoints, waypoint snapshots and edge snapshots.
"""
with open(os.path.join(path, 'graph'), 'rb') as graph_file:
# Load the graph file and deserialize it. The graph file is a protobuf containing only the waypoints and the
# edges between them.
data = graph_file.read()
current_graph = map_pb2.Graph()
current_graph.ParseFromString(data)
# Set up maps from waypoint ID to waypoints, edges, snapshots, etc.
current_waypoints = {}
current_waypoint_snapshots = {}
current_edge_snapshots = {}
current_anchors = {}
current_anchored_world_objects = {}
# Load the anchored world objects first so we can look in each waypoint snapshot as we load it.
for anchored_world_object in current_graph.anchoring.objects:
current_anchored_world_objects[anchored_world_object.id] = (
anchored_world_object,)
# For each waypoint, load any snapshot associated with it.
for waypoint in current_graph.waypoints:
current_waypoints[waypoint.id] = waypoint
if len(waypoint.snapshot_id) == 0:
continue
# Load the snapshot. Note that snapshots contain all of the raw data in a waypoint and may be large.
file_name = os.path.join(
path, 'waypoint_snapshots', waypoint.snapshot_id)
if not os.path.exists(file_name):
continue
with open(file_name, 'rb') as snapshot_file:
waypoint_snapshot = map_pb2.WaypointSnapshot()
waypoint_snapshot.ParseFromString(snapshot_file.read())
current_waypoint_snapshots[waypoint_snapshot.id] = waypoint_snapshot
for fiducial in waypoint_snapshot.objects:
if not fiducial.HasField('apriltag_properties'):
continue
str_id = str(fiducial.apriltag_properties.tag_id)
if (str_id in current_anchored_world_objects and
len(current_anchored_world_objects[str_id]) == 1):
# Replace the placeholder tuple with a tuple of (wo, waypoint, fiducial).
anchored_wo = current_anchored_world_objects[str_id][0]
current_anchored_world_objects[str_id] = (
anchored_wo, waypoint, fiducial)
# Similarly, edges have snapshot data.
for edge in current_graph.edges:
if len(edge.snapshot_id) == 0:
continue
file_name = os.path.join(path, 'edge_snapshots', edge.snapshot_id)
if not os.path.exists(file_name):
continue
with open(file_name, 'rb') as snapshot_file:
edge_snapshot = map_pb2.EdgeSnapshot()
edge_snapshot.ParseFromString(snapshot_file.read())
current_edge_snapshots[edge_snapshot.id] = edge_snapshot
for anchor in current_graph.anchoring.anchors:
current_anchors[anchor.id] = anchor
print(
f'Loaded graph with {len(current_graph.waypoints)} waypoints, {len(current_graph.edges)} edges, '
f'{len(current_graph.anchoring.anchors)} anchors, and {len(current_graph.anchoring.objects)} anchored world objects'
)
return (current_graph, current_waypoints, current_waypoint_snapshots,
current_edge_snapshots, current_anchors, current_anchored_world_objects)
def extract_waypoints(graph):
# NOTE: based on this tutorial: https://dev.bostondynamics.com/python/examples/graph_nav_view_map/readme
list_waypoints = {}
for w in graph.waypoints:
list_waypoints[w.id] = {
'position': {
'x': w.waypoint_tform_ko.position.x,
'y': w.waypoint_tform_ko.position.y,
'z': w.waypoint_tform_ko.position.z,
},
'rotation': {
'x': w.waypoint_tform_ko.rotation.x,
'y': w.waypoint_tform_ko.rotation.y,
'z': w.waypoint_tform_ko.rotation.z,
'w': w.waypoint_tform_ko.rotation.w,
},
'name': w.annotations.name,
}
return list_waypoints
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('path', type=str, help='Map to draw.')
parser.add_argument('-a', '--anchoring', action='store_true',
help='Draw the map according to the anchoring (in seed frame).')
options = parser.parse_args(argv)
# Load the map from the given file.
(current_graph, current_waypoints, current_waypoint_snapshots, current_edge_snapshots,
current_anchors, current_anchored_world_objects) = load_map(options.path)
print(extract_waypoints(current_graph))
if __name__ == '__main__':
main(sys.argv[1:])