use serde::{Deserialize, Serialize};
use syunit::*;
#[derive(Debug, Default, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
pub struct ServoConst
{
pub t_max : Force,
pub omega_max : Velocity,
pub gamma_max : Gamma,
pub pwm_min : Time,
pub pwm_max : Time,
pub f_pwm : Velocity
}
impl ServoConst
{
pub const ERROR : Self = Self {
t_max: Force::ZERO,
omega_max: Velocity::ZERO,
gamma_max: Gamma::ZERO,
pwm_min: Time::ZERO,
pwm_max: Time::ZERO,
f_pwm: Velocity::ZERO
};
pub const MG996R : Self = Self {
t_max: Force(1.08),
omega_max: Velocity(8.5),
gamma_max: Gamma(core::f32::consts::PI),
pwm_min: Time(0.00075),
pwm_max: Time(0.00225),
f_pwm: Velocity(50.0)
};
pub fn default_pulse(&self) -> Time {
(self.pwm_min + self.pwm_max) / 2.0
}
pub fn default_pos(&self) -> Gamma {
self.gamma_max / 2.0
}
pub fn period_time(&self) -> Time {
1.0 / self.f_pwm
}
pub fn pulse_for_factor(&self, factor : f32) -> Time {
if (1.0 < factor) & (0.0 > factor) {
panic!("Bad factor! ({})", factor);
}
self.pwm_min + (self.pwm_max - self.pwm_min) * factor
}
pub fn pulse_for_angle(&self, gamma : Gamma) -> Time {
self.pulse_for_factor(gamma / self.gamma_max)
}
}