4444from __future__ import annotations
4545
4646import logging
47+ import pickle
4748import time
4849from threading import Event
4950
@@ -206,6 +207,21 @@ def visualize(cf_poses: list[Pose], bs_poses: list[Pose]):
206207 plt .show ()
207208
208209
210+ def write_to_file (name : str ,
211+ origin : LhCfPoseSample ,
212+ x_axis : list [LhCfPoseSample ],
213+ xy_plane : list [LhCfPoseSample ],
214+ samples : list [LhCfPoseSample ]):
215+ with open (name , 'wb' ) as handle :
216+ data = (origin , x_axis , xy_plane , samples )
217+ pickle .dump (data , handle , protocol = pickle .HIGHEST_PROTOCOL )
218+
219+
220+ def load_from_file (name : str ):
221+ with open (name , 'rb' ) as handle :
222+ return pickle .load (handle )
223+
224+
209225def estimate_geometry (origin : LhCfPoseSample ,
210226 x_axis : list [LhCfPoseSample ],
211227 xy_plane : list [LhCfPoseSample ],
@@ -282,7 +298,12 @@ def data_written(_):
282298 event .wait ()
283299
284300
285- def connect_and_estimate (uri : str ):
301+ def estimate_from_file (file_name : str ):
302+ origin , x_axis , xy_plane , samples = load_from_file (file_name )
303+ estimate_geometry (origin , x_axis , xy_plane , samples )
304+
305+
306+ def connect_and_estimate (uri : str , file_name : str | None = None ):
286307 """Connect to a Crazyflie, collect data and estimate the geometry of the system"""
287308 print (f'Step 1. Connecting to the Crazyflie on uri { uri } ...' )
288309 with SyncCrazyflie (uri , cf = Crazyflie (rw_cache = './cache' )) as scf :
@@ -343,6 +364,10 @@ def connect_and_estimate(uri: str):
343364 samples = record_angles_sequence (scf , recording_time_s )
344365 print (' Recording ended' )
345366
367+ if file_name :
368+ write_to_file (file_name , origin , x_axis , xy_plane , samples )
369+ print (f'Wrote data to file { file_name } ' )
370+
346371 print ('Step 6. Estimating geometry...' )
347372 bs_poses = estimate_geometry (origin , x_axis , xy_plane , samples )
348373 print (' Geometry estimated' )
@@ -361,4 +386,12 @@ def connect_and_estimate(uri: str):
361386 cflib .crtp .init_drivers ()
362387
363388 uri = uri_helper .uri_from_env (default = 'radio://0/80/2M/E7E7E7E7E7' )
364- connect_and_estimate (uri )
389+
390+ # Set a file name to write the measurement data to file. Useful for debugging
391+ file_name = None
392+ # file_name = 'lh_geo_estimate_data.pickle'
393+
394+ connect_and_estimate (uri , file_name = file_name )
395+
396+ # Run the estimation on data from file instead of live measurements
397+ # estimate_from_file(file_name)
0 commit comments