aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSébastien Crozet <sebcrozet@dimforge.com>2024-06-09 12:11:30 +0200
committerSébastien Crozet <sebastien@crozet.re>2024-06-09 13:20:09 +0200
commita8a0f297f52d4336c0d3b0effc24401e8066183b (patch)
treea654c41b2d64dc229f8ae1ab2e52de2ac1a957b9 /src
parenta5a4152815ab88d4117a80d97b42476d48b1eb69 (diff)
downloadrapier-a8a0f297f52d4336c0d3b0effc24401e8066183b.tar.gz
rapier-a8a0f297f52d4336c0d3b0effc24401e8066183b.tar.bz2
rapier-a8a0f297f52d4336c0d3b0effc24401e8066183b.zip
feat: add RevoluteJoint::angle to compute the revolute joint’s angle
Diffstat (limited to 'src')
-rw-r--r--src/dynamics/joint/revolute_joint.rs71
1 files changed, 70 insertions, 1 deletions
diff --git a/src/dynamics/joint/revolute_joint.rs b/src/dynamics/joint/revolute_joint.rs
index ba27351..4dfed5c 100644
--- a/src/dynamics/joint/revolute_joint.rs
+++ b/src/dynamics/joint/revolute_joint.rs
@@ -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 joint’s 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
+ );
+ }
+ }
+}