pub mod ball;
pub mod fixed;
pub mod hinge;
pub mod prismatic;
pub use ball::BallJoint;
pub use fixed::FixedJoint;
pub use hinge::HingeJoint;
pub use prismatic::PrismaticJoint;
#[derive(Clone, Debug)]
pub struct MotorDrive {
pub target_pos: f64,
pub target_vel: f64,
pub kp: f64,
pub kd: f64,
pub max_force: f64,
}
impl MotorDrive {
pub fn new(target_pos: f64, max_force: f64) -> Self {
Self {
target_pos,
target_vel: 0.0,
kp: max_force,
kd: max_force * 0.1,
max_force,
}
}
pub fn with_gains(mut self, kp: f64, kd: f64) -> Self {
self.kp = kp;
self.kd = kd;
self
}
pub fn calculate_impulse(&self, current_pos: f64, current_vel: f64, dt: f64) -> f64 {
let error_pos = self.target_pos - current_pos;
let error_vel = self.target_vel - current_vel;
let force =
(self.kp * error_pos + self.kd * error_vel).clamp(-self.max_force, self.max_force);
force * dt
}
}