@@ -40,7 +40,7 @@ def moving_initialize(window,dvl_samples,*,dvl_to_imu,imu_to_dvl,gyro_bias,gyro_
4040 if not 8 <= n <= 24 :raise ValueError ('moving_dvl_samples_required' )
4141 mount = rotation (dvl_to_imu );lever = vector (imu_to_dvl )
4242 bg0 = vector (gyro_bias );ba0 = vector (accel_bias )
43- bg_cov = covariance (gyro_bias_covariance ,3 );ba_cov = covariance (accel_bias_covariance ,3 )
43+ bg_cov = covariance (gyro_bias_covariance ,3 , allow_singular = True );ba_cov = covariance (accel_bias_covariance ,3 , allow_singular = True )
4444 noise = np .asarray (noise_density ,dtype = float )
4545 if noise .shape != (4 ,) or not np .isfinite (noise ).all () or np .any (noise <= 0 ):raise ValueError ('imu_noise_calibration_required' )
4646 if not math .isfinite (gravity_m_s2 ) or not 9 < gravity_m_s2 < 11 :raise ValueError ('invalid_gravity' )
@@ -68,7 +68,18 @@ def moving_initialize(window,dvl_samples,*,dvl_to_imu,imu_to_dvl,gyro_bias,gyro_
6868 dimension = 9 + 3 * (n - 1 )+ 3 * n
6969 def ri (i ):return 9 + 3 * (i - 1 )
7070 def vi (i ):return 9 + 3 * (n - 1 )+ 3 * i
71- wb = np .linalg .inv (np .linalg .cholesky (bg_cov ));wa = np .linalg .inv (np .linalg .cholesky (ba_cov ))
71+ def prior_basis (c ):
72+ values ,axes = np .linalg .eigh (c );positive = values > 0
73+ return axes [:,positive ],(axes [:,positive ]/ np .sqrt (values [positive ])).T
74+ bg_axes ,wb = prior_basis (bg_cov );ba_axes ,wa = prior_basis (ba_cov )
75+ # Zero-variance calibrated directions are exact constraints, not tiny-noise
76+ # pseudo-observations. Optimize only the remaining degrees of freedom.
77+ columns = [np .eye (dimension )[:,i ] for i in range (3 )]
78+ for offset ,axes in ((3 ,bg_axes ),(6 ,ba_axes )):
79+ for axis in axes .T :
80+ column = np .zeros (dimension );column [offset :offset + 3 ]= axis ;columns .append (column )
81+ columns .extend (np .eye (dimension )[:,i ] for i in range (9 ,dimension ))
82+ basis = np .array (columns ).T ;free_dimension = basis .shape [1 ]
7283 def apply (s ,delta ,amount = 1. ):
7384 return dict (g = s ['g' ]+ amount * delta [:3 ],bg = s ['bg' ]+ amount * delta [3 :6 ],ba = s ['ba' ]+ amount * delta [6 :9 ],
7485 r = [s ['r' ][0 ]]+ [s ['r' ][i ]@exp_so3 (amount * delta [ri (i ):ri (i )+ 3 ]) for i in range (1 ,n )],
@@ -113,25 +124,25 @@ def rot_res(relative,bias):return log_so3((p['rotation']@exp_so3(p['bias_jacobia
113124 iterations = 0
114125 for iterations in range (12 ):
115126 residual ,j = build (state );cost = float (residual @residual )
116- delta = np .linalg .lstsq (j ,- residual ,rcond = 1e-10 )[0 ]
127+ delta = basis @ np .linalg .lstsq (j @ basis ,- residual ,rcond = 1e-10 )[0 ]
117128 if np .linalg .norm (delta )< 1e-6 :break
118129 accepted = False
119130 for step in (1. ,.5 ,.25 ,.125 ,.0625 ,.03125 ):
120131 candidate = apply (state ,delta ,step );candidate_residual ,_ = build (candidate ,False )
121132 if candidate_residual @candidate_residual < cost :
122133 state = candidate ;accepted = True ;break
123134 if not accepted :
124- if np .linalg .norm (j .T @residual ,np .inf )< 1e-3 :break
135+ if np .linalg .norm (( j @ basis ) .T @residual ,np .inf )< 1e-3 :break
125136 raise ValueError ('moving_alignment_not_converged' )
126137 else :
127138 raise ValueError ('moving_alignment_iteration_limit' )
128- residual ,j = build (state );singular = np .linalg .svd (j ,compute_uv = False )
139+ residual ,j = build (state );reduced_j = j @ basis ; singular = np .linalg .svd (reduced_j ,compute_uv = False )
129140 if singular [- 1 ]<= singular [0 ]* 1e-10 :raise ValueError ('moving_alignment_rank_deficient' )
130- reduced = float (residual @residual )/ max (1 ,len (residual )- dimension )
141+ reduced = float (residual @residual )/ max (1 ,len (residual )- free_dimension )
131142 if reduced > 5 or np .max (np .abs (residual ))> 8 :raise ValueError ('moving_alignment_residual_rejected' )
132143 if np .linalg .norm (state ['bg' ]- bg0 )> .05 or np .linalg .norm (state ['ba' ]- ba0 )> .5 :
133144 raise ValueError ('moving_bias_linearization_exceeded' )
134- posterior = np .linalg .solve (j .T @j ,np .eye (dimension )) * max (1. ,reduced )
145+ posterior = basis @ np .linalg .solve (reduced_j .T @reduced_j ,np .eye (free_dimension ))@ basis . T * max (1. ,reduced )
135146 def endpoint (s ):
136147 up = - s ['g' ]/ np .linalg .norm (s ['g' ]);world = gravity_attitude (up )
137148 return world @s ['v' ][- 1 ],world @s ['r' ][- 1 ]
@@ -145,12 +156,12 @@ def endpoint(s):
145156 p = transform @posterior @transform .T ;p [:3 ,:3 ]= np .eye (3 )* 1e-8 ;p = (p + p .T )* .5
146157 estimator = InertialFilter (stamp = times [- 1 ],position = [0 ,0 ,0 ],velocity = velocity ,attitude = attitude ,
147158 gyro_bias = state ['bg' ],accel_bias = state ['ba' ],state_covariance = p ,noise_density = noise ,gravity = [0 ,0 ,- gravity_m_s2 ])
148- data_j = j [:3 * n + 6 * (n - 1 )]
159+ data_j = j [:3 * n + 6 * (n - 1 )]@ basis
149160 data_singular = np .linalg .svd (data_j ,compute_uv = False )
150161 return estimator ,dict (algorithm_version = ALGORITHM_VERSION ,start = times [0 ],end = times [- 1 ],dvl_samples = n ,
151162 iterations = iterations + 1 ,reduced_chi_square = reduced ,max_whitened_residual = float (np .max (np .abs (residual ))),
152163 covariance_condition = float ((singular [0 ]/ singular [- 1 ])** 2 ),data_rank = int (np .sum (data_singular > data_singular [0 ]* 1e-8 )),
153- variables = dimension ,calibration_priors_used = True ,bias_observability_qualified = False ,
164+ variables = free_dimension , fixed_bias_directions = 6 - bg_axes . shape [ 1 ] - ba_axes . shape [ 1 ] ,calibration_priors_used = True ,bias_observability_qualified = False ,
154165 attitude_three_sigma_deg = float (3 * np .sqrt (np .linalg .eigvalsh (p [6 :9 ,6 :9 ]).max ())* 180 / math .pi ),
155166 velocity_three_sigma_m_s = float (3 * np .sqrt (np .linalg .eigvalsh (p [3 :6 ,3 :6 ]).max ())),
156167 stationary_required = False ,orientation_measurement_used = False ,navigation_ready = False ,
0 commit comments