@@ -30,29 +30,73 @@ def parse_arguments(args):
30
30
return args
31
31
32
32
33
- class WaypointRecorder :
34
- def __init__ (self , spot : Spot , waypoint_file_path : str = WAYPOINT_YAML ):
35
- self .spot = spot
36
-
37
- self .waypoint_file = waypoint_file_path
38
-
39
- # Local copy of waypoints.yaml which keeps getting updated as new waypoints are added
40
- self .yaml_dict = self .read_yaml (self .waypoint_file )
41
- print ("Yaml: " , self .yaml_dict )
42
- self .initialize_yaml (self .yaml_dict )
43
- return
33
+ class YamlHandler :
34
+ """
35
+ Class to handle reading and writing to yaml files
36
+
37
+ How to use:
38
+ 1. Create a yaml file with the following format:
39
+
40
+ place_targets:
41
+ test_place_target:
42
+ - 3.0
43
+ - 0.0
44
+ - 0.8
45
+ clutter:
46
+ - test_place_target
47
+ clutter_amounts:
48
+ test_place_target: 1
49
+ object_targets:
50
+ 0: [penguin, test_place_target]
51
+ nav_targets:
52
+ dock:
53
+ - 1.5
54
+ - 0.0
55
+ - 0.0
56
+ test_place_target:
57
+ - 2.5
58
+ - 0.0
59
+ - 0.0
60
+
61
+ 2. Create an instance of this class
62
+ 3. Read the yaml file using the read_yaml method
63
+ 4. Modify the yaml_dict as needed
64
+ 5. Write the yaml file using the write_yaml method
65
+ """
66
+
67
+ def __init__ (self ):
68
+ pass
44
69
45
70
def create_yaml (self , waypoint_file : str ):
71
+ init_yaml_dict = """
72
+ place_targets: # i.e., where an object needs to be placed (x,y,z)
73
+ test_place_target:
74
+ - 3.0
75
+ - 0.0
76
+ - 0.8
77
+ clutter: # i.e., where an object is currently placed
78
+ # <receptacle where clutter exists>
79
+ - test_place_target
80
+ clutter_amounts: # i.e., how much clutter exists in each receptacle
81
+ # <receptacle where clutter exists>: <number of objects in that receptacle>
82
+ test_place_target: 1
83
+ object_targets: # i.e., where an object belongs / needs to be placed
84
+ # <Class_id>: [<object's name>, <which place_target it belongs to>]
85
+ 0: [penguin, test_place_target]
86
+ nav_targets: # i.e., where the robot needs to navigate to (x,y,yaw)
87
+ dock:
88
+ - 1.5
89
+ - 0.0
90
+ - 0.0
91
+ test_place_target:
92
+ - 2.5
93
+ - 0.0
94
+ - 0.0"""
46
95
with open (waypoint_file , "w+" ) as f :
47
- f .write ("" )
48
- f .close ()
49
-
50
- def initialize_yaml (self , yaml_dict ):
51
- # TODO: What if the yaml_dict is NONE???
52
-
53
- # Create a templated waypoints.yaml file here, by creating all the keys that are not present
54
- yaml_dict ["dock" ] = [1.5 , 0.0 , 0.0 ]
55
- # TODO: Add other keys here
96
+ yaml = ruamel .yaml .YAML () # defaults to round-trip if no parameters given
97
+ ruamel .yaml .dump (
98
+ yaml .load (init_yaml_dict ), f , Dumper = ruamel .yaml .RoundTripDumper
99
+ )
56
100
57
101
def read_yaml (self , waypoint_file : str ):
58
102
# Create waypoint file if it does not exist
@@ -62,16 +106,26 @@ def read_yaml(self, waypoint_file: str):
62
106
with open (waypoint_file , "r" ) as f :
63
107
yaml = ruamel .yaml .YAML () # defaults to round-trip if no parameters given
64
108
yaml_dict = yaml .load (f .read ())
65
- print (type (yaml_dict ))
66
109
return yaml_dict
67
110
68
- def write_yaml (self , yaml_dict ):
69
- with open (self . waypoint_file , "w" ) as f :
111
+ def write_yaml (self , waypoint_file : str , yaml_dict ):
112
+ with open (waypoint_file , "w" ) as f :
70
113
yaml = ruamel .yaml .YAML ()
71
114
yaml .dump (yaml_dict , f )
72
115
116
+
117
+ class WaypointRecorder :
118
+ def __init__ (self , spot : Spot , waypoint_file_path : str = WAYPOINT_YAML ):
119
+ self .spot = spot
120
+
121
+ # Local copy of waypoints.yaml which keeps getting updated as new waypoints are added
122
+ self .waypoint_file = waypoint_file_path
123
+ self .yaml_handler = YamlHandler ()
124
+ self .yaml_dict = self .yaml_handler .read_yaml (waypoint_file = self .waypoint_file )
125
+ return
126
+
73
127
def save_yaml (self ):
74
- self .write_yaml (self .yaml_dict )
128
+ self .yaml_handler . write_yaml (self . waypoint_file , self .yaml_dict )
75
129
print (f"Successfully saved all waypoints to file at { self .waypoint_file } :\n " )
76
130
77
131
def unmark_clutter (self , clutter_target_name : str ):
@@ -104,7 +158,9 @@ def record_nav_target(self, nav_target_name: str):
104
158
105
159
# Add nav_targets list if not present
106
160
if "nav_targets" not in self .yaml_dict :
107
- self .yaml_dict ["nav_targets" ] = {}
161
+ self .yaml_dict ["nav_targets" ] = {
162
+ "dock" : "[1.5, 0.0, 0.0]" ,
163
+ }
108
164
109
165
# Erase existing waypoint data if present
110
166
if nav_target_name in self .yaml_dict .get ("nav_targets" ):
@@ -128,7 +184,7 @@ def record_clutter_target(self, clutter_target_name: str):
128
184
129
185
# Add waypoint as clutter_amounts if it does not exist
130
186
if clutter_target_name not in self .yaml_dict .get ("clutter_amounts" ):
131
- self .yaml_dict ["clutter_amounts" ].update ({clutter_target_name : 0 })
187
+ self .yaml_dict ["clutter_amounts" ].update ({clutter_target_name : 1 })
132
188
print (
133
189
f"Added { clutter_target_name } in 'clutter_amounts' => ({ clutter_target_name } :{ self .yaml_dict .get ('clutter_amounts' ).get (clutter_target_name )} )"
134
190
)
@@ -167,7 +223,6 @@ def main(spot: Spot):
167
223
len ([i for i in arg_bools if i ]) == 1
168
224
), "Must pass in either -c, -p, or -n as an arg, and not more than one."
169
225
170
- # try:
171
226
# Create WaypointRecorder object with default waypoint file
172
227
waypoint_recorder = WaypointRecorder (spot = spot )
173
228
@@ -179,8 +234,7 @@ def main(spot: Spot):
179
234
waypoint_recorder .record_place_target (args .waypoint_name )
180
235
else :
181
236
raise NotImplementedError
182
- # finally:
183
- # Save updated yaml file
237
+
184
238
waypoint_recorder .save_yaml ()
185
239
186
240
0 commit comments