feat: add RevoluteJoint::angle to compute the revolute joint’s angle

This commit is contained in:
Sébastien Crozet
2024-06-09 12:11:30 +02:00
committed by Sébastien Crozet
parent a5a4152815
commit a8a0f297f5
3 changed files with 92 additions and 10 deletions

View File

@@ -1,6 +1,6 @@
use crate::dynamics::joint::{GenericJoint, GenericJointBuilder, JointAxesMask};
use crate::dynamics::{JointAxis, JointLimits, JointMotor, MotorModel};
use crate::math::{Point, Real};
use crate::math::{Point, Real, Rotation};
#[cfg(feature = "dim3")]
use crate::math::UnitVector;
@@ -75,6 +75,29 @@ impl RevoluteJoint {
self
}
/// The angle along the free degree of freedom of this revolute joint in `[-π, π]`.
///
/// # Parameters
/// - `rb_rot1`: the rotation of the first rigid-body attached to this revolute joint.
/// - `rb_rot2`: the rotation of the second rigid-body attached to this revolute joint.
pub fn angle(&self, rb_rot1: &Rotation<Real>, rb_rot2: &Rotation<Real>) -> Real {
let joint_rot1 = rb_rot1 * self.data.local_frame1.rotation;
let joint_rot2 = rb_rot2 * self.data.local_frame2.rotation;
let ang_err = joint_rot1.inverse() * joint_rot2;
#[cfg(feature = "dim3")]
if joint_rot1.dot(&joint_rot2) < 0.0 {
-ang_err.i.asin() * 2.0
} else {
ang_err.i.asin() * 2.0
}
#[cfg(feature = "dim2")]
{
ang_err.angle()
}
}
/// The motor affecting the joints rotational degree of freedom.
#[must_use]
pub fn motor(&self) -> Option<&JointMotor> {
@@ -248,3 +271,49 @@ impl From<RevoluteJointBuilder> for GenericJoint {
val.0.into()
}
}
#[cfg(test)]
mod test {
#[test]
fn test_revolute_joint_angle() {
use crate::math::{Real, Rotation};
use crate::na::RealField;
#[cfg(feature = "dim3")]
use crate::{math::Vector, na::vector};
#[cfg(feature = "dim2")]
let revolute = super::RevoluteJointBuilder::new().build();
#[cfg(feature = "dim2")]
let rot1 = Rotation::new(1.0);
#[cfg(feature = "dim3")]
let revolute = super::RevoluteJointBuilder::new(Vector::y_axis()).build();
#[cfg(feature = "dim3")]
let rot1 = Rotation::new(vector![0.0, 1.0, 0.0]);
let steps = 100;
// The -pi and pi values will be checked later.
for i in 1..steps {
let delta = -Real::pi() + i as Real * Real::two_pi() / steps as Real;
#[cfg(feature = "dim2")]
let rot2 = Rotation::new(1.0 + delta);
#[cfg(feature = "dim3")]
let rot2 = Rotation::new(vector![0.0, 1.0 + delta, 0.0]);
approx::assert_relative_eq!(revolute.angle(&rot1, &rot2), delta, epsilon = 1.0e-5);
}
// Check the special case for -pi and pi that may return an angle with a flipped sign
// (because they are equivalent).
for delta in [-Real::pi(), Real::pi()] {
#[cfg(feature = "dim2")]
let rot2 = Rotation::new(1.0 + delta);
#[cfg(feature = "dim3")]
let rot2 = Rotation::new(vector![0.0, 1.0 + delta, 0.0]);
approx::assert_relative_eq!(
revolute.angle(&rot1, &rot2).abs(),
delta.abs(),
epsilon = 1.0e-2
);
}
}
}