Skip to content

Commit 1243d5d

Browse files
committed
improve sol1/sol2 in complete_valid_solution
1 parent 459533a commit 1243d5d

File tree

1 file changed

+43
-48
lines changed

1 file changed

+43
-48
lines changed

orbita3d_kinematics/src/position/inverse.rs

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -195,56 +195,50 @@ impl Orbita3dKinematicsModel {
195195
}
196196
}
197197

198-
let mut sol1 = None;
199-
let mut sol2 = None;
200-
201-
all_solutions.iter().filter(|sol| self.check_gammas(Vector3::from(**sol)).is_ok()).for_each(|sol| {
202-
if sol1.is_none() {
203-
sol1 = Some(*sol);
204-
} else if sol2.is_none() {
205-
sol2 = Some(*sol);
206-
} else {
207-
unreachable!()
208-
}
209-
});
198+
log::debug!("all solutions: {:?}", all_solutions);
199+
let mut solution_iter = all_solutions
200+
.iter()
201+
.filter(|sol| self.check_gammas(Vector3::from(**sol)).is_ok());
202+
203+
let Some(sol1) = solution_iter.next() else {
204+
log::debug!(
205+
"NO VALID SOLUTION! target: {:?}\n thetas: {:?}\nall_solutions: {:?}",
206+
target_rpy,
207+
thetas,
208+
all_solutions
209+
);
210+
let rot = conversion::intrinsic_roll_pitch_yaw_to_matrix(
211+
target_rpy[0],
212+
target_rpy[1],
213+
target_rpy[2],
214+
);
215+
return Err(InverseSolutionErrorKind::InvalidSolution(
216+
rot,
217+
compute_gammas(thetas.into()),
218+
));
219+
};
210220

211-
log::debug!(
212-
"all solutions: {:?}\nvalid solutions: {:?}",
213-
all_solutions,
214-
(sol1, sol2)
215-
);
216-
let thetas = match (sol1, sol2) {
217-
(None, None) => {
218-
log::debug!(
219-
"NO VALID SOLUTION! target: {:?}\n thetas: {:?}\nall_solutions: {:?}",
220-
target_rpy,
221-
thetas,
222-
all_solutions
223-
);
224-
let rot = conversion::intrinsic_roll_pitch_yaw_to_matrix(
225-
target_rpy[0],
226-
target_rpy[1],
227-
target_rpy[2],
228-
);
229-
return Err(InverseSolutionErrorKind::InvalidSolution(
230-
rot,
231-
compute_gammas(thetas.into()),
232-
));
233-
}
234-
(Some(sol1), None) => sol1,
235-
(None, Some(_)) => unreachable!(),
236-
(Some(sol1), Some(sol2)) => {
221+
log::debug!("valid solution 1: {:?}", sol1);
222+
223+
let sol2 = solution_iter.next();
224+
225+
let thetas = match sol2 {
226+
None => sol1,
227+
Some(sol2) => {
237228
// here we have the 2 solutions (both 2pi complement), we chose the one with the same yaw sign
238-
let mut yaw_sign = (target_rpy[2] + self.offset).signum();
239-
let mut theta_sign = sol1[0].signum();
240229

241230
// If the yaw or thetas are very close to zero, treat them as effectively zero
242-
if (target_rpy[2] + self.offset).abs() < TOLERANCE_ZERO_YAW {
243-
yaw_sign = 0.0;
244-
}
245-
if sol1[0].abs() < TOLERANCE_ZERO_YAW {
246-
theta_sign = 0.0;
247-
}
231+
let yaw_sign = if (target_rpy[2] + self.offset).abs() < TOLERANCE_ZERO_YAW {
232+
0.0
233+
} else {
234+
// otherwise get the sign
235+
(target_rpy[2] + self.offset).signum()
236+
};
237+
let theta_sign = if sol1[0].abs() < TOLERANCE_ZERO_YAW {
238+
0.0
239+
} else {
240+
sol1[0].signum()
241+
};
248242
// Compare the yaw sign and theta sign
249243
// but now accounting for near-zero values
250244
if theta_sign == yaw_sign {
@@ -254,8 +248,9 @@ impl Orbita3dKinematicsModel {
254248
}
255249
}
256250
};
257-
// log::debug!("valid Thetas {:?}", thetas);
258-
Ok(thetas)
251+
252+
log::debug!("valid Thetas {:?}", thetas);
253+
Ok(*thetas)
259254
}
260255

261256
fn find_thetas_from_v(&self, v: Matrix3<f64>) -> Vector3<f64> {

0 commit comments

Comments
 (0)