Joint API and joint motors improvements
This commit is contained in:
committed by
Sébastien Crozet
parent
e740493b98
commit
fb20d72ee2
@@ -1,11 +1,12 @@
|
||||
use crate::dynamics::joint::{JointAxesMask, JointData};
|
||||
use crate::dynamics::joint::{GenericJoint, GenericJointBuilder, JointAxesMask};
|
||||
use crate::dynamics::{JointAxis, MotorModel};
|
||||
use crate::math::{Point, Real};
|
||||
|
||||
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
#[repr(transparent)]
|
||||
pub struct SphericalJoint {
|
||||
data: JointData,
|
||||
data: GenericJoint,
|
||||
}
|
||||
|
||||
impl Default for SphericalJoint {
|
||||
@@ -16,40 +17,128 @@ impl Default for SphericalJoint {
|
||||
|
||||
impl SphericalJoint {
|
||||
pub fn new() -> Self {
|
||||
let data =
|
||||
JointData::default().lock_axes(JointAxesMask::X | JointAxesMask::Y | JointAxesMask::Z);
|
||||
let data = GenericJointBuilder::new(JointAxesMask::LOCKED_SPHERICAL_AXES).build();
|
||||
Self { data }
|
||||
}
|
||||
|
||||
pub fn data(&self) -> &JointData {
|
||||
pub fn data(&self) -> &GenericJoint {
|
||||
&self.data
|
||||
}
|
||||
|
||||
pub fn set_local_anchor1(&mut self, anchor1: Point<Real>) -> &mut Self {
|
||||
self.data.set_local_anchor1(anchor1);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_local_anchor2(&mut self, anchor2: Point<Real>) -> &mut Self {
|
||||
self.data.set_local_anchor2(anchor2);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the spring-like model used by the motor to reach the desired target velocity and position.
|
||||
pub fn set_motor_model(&mut self, axis: JointAxis, model: MotorModel) -> &mut Self {
|
||||
self.data.set_motor_model(axis, model);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the target velocity this motor needs to reach.
|
||||
pub fn set_motor_velocity(
|
||||
&mut self,
|
||||
axis: JointAxis,
|
||||
target_vel: Real,
|
||||
factor: Real,
|
||||
) -> &mut Self {
|
||||
self.data.set_motor_velocity(axis, target_vel, factor);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the target angle this motor needs to reach.
|
||||
pub fn set_motor_position(
|
||||
&mut self,
|
||||
axis: JointAxis,
|
||||
target_pos: Real,
|
||||
stiffness: Real,
|
||||
damping: Real,
|
||||
) -> &mut Self {
|
||||
self.data
|
||||
.set_motor_position(axis, target_pos, stiffness, damping);
|
||||
self
|
||||
}
|
||||
|
||||
/// Configure both the target angle and target velocity of the motor.
|
||||
pub fn set_motor(
|
||||
&mut self,
|
||||
axis: JointAxis,
|
||||
target_pos: Real,
|
||||
target_vel: Real,
|
||||
stiffness: Real,
|
||||
damping: Real,
|
||||
) -> &mut Self {
|
||||
self.data
|
||||
.set_motor(axis, target_pos, target_vel, stiffness, damping);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_motor_max_force(&mut self, axis: JointAxis, max_force: Real) -> &mut Self {
|
||||
self.data.set_motor_max_force(axis, max_force);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn set_limits(&mut self, axis: JointAxis, limits: [Real; 2]) -> &mut Self {
|
||||
self.data.set_limits(axis, limits);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<GenericJoint> for SphericalJoint {
|
||||
fn into(self) -> GenericJoint {
|
||||
self.data
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(feature = "serde-serialize", derive(Serialize, Deserialize))]
|
||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
||||
pub struct SphericalJointBuilder(SphericalJoint);
|
||||
|
||||
impl Default for SphericalJointBuilder {
|
||||
fn default() -> Self {
|
||||
Self(SphericalJoint::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl SphericalJointBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self(SphericalJoint::new())
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn local_anchor1(mut self, anchor1: Point<Real>) -> Self {
|
||||
self.data = self.data.local_anchor1(anchor1);
|
||||
self.0.set_local_anchor1(anchor1);
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn local_anchor2(mut self, anchor2: Point<Real>) -> Self {
|
||||
self.data = self.data.local_anchor2(anchor2);
|
||||
self.0.set_local_anchor2(anchor2);
|
||||
self
|
||||
}
|
||||
|
||||
/// Set the spring-like model used by the motor to reach the desired target velocity and position.
|
||||
#[must_use]
|
||||
pub fn motor_model(mut self, axis: JointAxis, model: MotorModel) -> Self {
|
||||
self.data = self.data.motor_model(axis, model);
|
||||
self.0.set_motor_model(axis, model);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the target velocity this motor needs to reach.
|
||||
#[must_use]
|
||||
pub fn motor_velocity(mut self, axis: JointAxis, target_vel: Real, factor: Real) -> Self {
|
||||
self.data = self.data.motor_velocity(axis, target_vel, factor);
|
||||
self.0.set_motor_velocity(axis, target_vel, factor);
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the target angle this motor needs to reach.
|
||||
#[must_use]
|
||||
pub fn motor_position(
|
||||
mut self,
|
||||
axis: JointAxis,
|
||||
@@ -57,14 +146,14 @@ impl SphericalJoint {
|
||||
stiffness: Real,
|
||||
damping: Real,
|
||||
) -> Self {
|
||||
self.data = self
|
||||
.data
|
||||
.motor_position(axis, target_pos, stiffness, damping);
|
||||
self.0
|
||||
.set_motor_position(axis, target_pos, stiffness, damping);
|
||||
self
|
||||
}
|
||||
|
||||
/// Configure both the target angle and target velocity of the motor.
|
||||
pub fn motor_axis(
|
||||
#[must_use]
|
||||
pub fn motor(
|
||||
mut self,
|
||||
axis: JointAxis,
|
||||
target_pos: Real,
|
||||
@@ -72,26 +161,31 @@ impl SphericalJoint {
|
||||
stiffness: Real,
|
||||
damping: Real,
|
||||
) -> Self {
|
||||
self.data = self
|
||||
.data
|
||||
.motor_axis(axis, target_pos, target_vel, stiffness, damping);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn motor_max_impulse(mut self, axis: JointAxis, max_impulse: Real) -> Self {
|
||||
self.data = self.data.motor_max_impulse(axis, max_impulse);
|
||||
self.0
|
||||
.set_motor(axis, target_pos, target_vel, stiffness, damping);
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn limit_axis(mut self, axis: JointAxis, limits: [Real; 2]) -> Self {
|
||||
self.data = self.data.limit_axis(axis, limits);
|
||||
pub fn motor_max_force(mut self, axis: JointAxis, max_force: Real) -> Self {
|
||||
self.0.set_motor_max_force(axis, max_force);
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn limits(mut self, axis: JointAxis, limits: [Real; 2]) -> Self {
|
||||
self.0.set_limits(axis, limits);
|
||||
self
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn build(self) -> SphericalJoint {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<JointData> for SphericalJoint {
|
||||
fn into(self) -> JointData {
|
||||
self.data
|
||||
impl Into<GenericJoint> for SphericalJointBuilder {
|
||||
fn into(self) -> GenericJoint {
|
||||
self.0.into()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user