diff options
| author | Crozet Sébastien <developer@crozet.re> | 2021-02-19 15:21:25 +0100 |
|---|---|---|
| committer | Crozet Sébastien <developer@crozet.re> | 2021-02-19 15:21:25 +0100 |
| commit | e9f17f32e8dda4b97d2eb7b2118b7373d0c554d0 (patch) | |
| tree | f20df00536634b840d5a9af5e2a040dd86b7306a /src/dynamics/joint/revolute_joint.rs | |
| parent | a1ddda5077811e07b1f6d067969d692eafa32577 (diff) | |
| download | rapier-e9f17f32e8dda4b97d2eb7b2118b7373d0c554d0.tar.gz rapier-e9f17f32e8dda4b97d2eb7b2118b7373d0c554d0.tar.bz2 rapier-e9f17f32e8dda4b97d2eb7b2118b7373d0c554d0.zip | |
Complete the implementation of non-simd joint motor for the revolute joint.
Diffstat (limited to 'src/dynamics/joint/revolute_joint.rs')
| -rw-r--r-- | src/dynamics/joint/revolute_joint.rs | 95 |
1 files changed, 93 insertions, 2 deletions
diff --git a/src/dynamics/joint/revolute_joint.rs b/src/dynamics/joint/revolute_joint.rs index ad7db0d..9af0ae6 100644 --- a/src/dynamics/joint/revolute_joint.rs +++ b/src/dynamics/joint/revolute_joint.rs @@ -1,6 +1,7 @@ -use crate::math::{Point, Real, Vector}; +use crate::dynamics::SpringModel; +use crate::math::{Isometry, Point, Real, Vector}; use crate::utils::WBasis; -use na::{Unit, Vector5}; +use na::{RealField, Unit, Vector5}; #[derive(Copy, Clone)] #[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))] @@ -22,6 +23,28 @@ pub struct RevoluteJoint { /// /// The impulse applied to the second body is given by `-impulse`. pub impulse: Vector5<Real>, + /// The target relative angular velocity the motor will attempt to reach. + pub motor_target_vel: Real, + /// The target relative angle along the joint axis the motor will attempt to reach. + pub motor_target_pos: Real, + /// The motor's stiffness. + /// See the documentation of `SpringModel` for more information on this parameter. + pub motor_stiffness: Real, + /// The motor's damping. + /// See the documentation of `SpringModel` for more information on this parameter. + pub motor_damping: Real, + /// The maximal impulse the motor is able to deliver. + pub motor_max_impulse: Real, + /// The angular impulse applied by the motor. + pub motor_impulse: Real, + /// The spring-like model used by the motor to reach the target velocity and . + pub motor_model: SpringModel, + // Used to handle cases where the position target ends up being more than pi radians away. + pub(crate) motor_last_angle: Real, + // The angular impulse expressed in world-space. + pub(crate) world_ang_impulse: Vector<Real>, + // The world-space orientation of the free axis of the first attached body. + pub(crate) prev_axis1: Vector<Real>, } impl RevoluteJoint { @@ -41,6 +64,74 @@ impl RevoluteJoint { basis1: local_axis1.orthonormal_basis(), basis2: local_axis2.orthonormal_basis(), impulse: na::zero(), + world_ang_impulse: na::zero(), + motor_target_vel: 0.0, + motor_target_pos: 0.0, + motor_stiffness: 0.0, + motor_damping: 0.0, + motor_max_impulse: Real::MAX, + motor_impulse: 0.0, + prev_axis1: *local_axis1, + motor_model: SpringModel::VelocityBased, + motor_last_angle: 0.0, } } + + pub fn configure_motor_model(&mut self, model: SpringModel) { + self.motor_model = model; + } + + pub fn configure_motor_velocity(&mut self, target_vel: Real, factor: Real) { + self.configure_motor(self.motor_target_pos, target_vel, 0.0, factor) + } + + pub fn configure_motor_position(&mut self, target_pos: Real, stiffness: Real, damping: Real) { + self.configure_motor(target_pos, 0.0, stiffness, damping) + } + + pub fn configure_motor( + &mut self, + target_pos: Real, + target_vel: Real, + stiffness: Real, + damping: Real, + ) { + self.motor_target_vel = target_vel; + self.motor_target_pos = target_pos; + self.motor_stiffness = stiffness; + self.motor_damping = damping; + } + + /// Estimates the current position of the motor angle. + pub fn estimate_motor_angle( + &self, + body_pos1: &Isometry<Real>, + body_pos2: &Isometry<Real>, + ) -> Real { + let motor_axis1 = body_pos1 * self.local_axis1; + let ref1 = body_pos1 * self.basis1[0]; + let ref2 = body_pos2 * self.basis2[0]; + + let last_angle_cycles = (self.motor_last_angle / Real::two_pi()).trunc() * Real::two_pi(); + + // Measure the position between 0 and 2-pi + let new_angle = if ref1.cross(&ref2).dot(&motor_axis1) < 0.0 { + Real::two_pi() - ref1.angle(&ref2) + } else { + ref1.angle(&ref2) + }; + + // The last angle between 0 and 2-pi + let last_angle_zero_two_pi = self.motor_last_angle - last_angle_cycles; + + // Figure out the smallest angle differance. + let mut angle_diff = new_angle - last_angle_zero_two_pi; + if angle_diff > Real::pi() { + angle_diff -= Real::two_pi() + } else if angle_diff < -Real::pi() { + angle_diff += Real::two_pi() + } + + self.motor_last_angle + angle_diff + } } |
