Skip to main content

autocore_std/motion/
axis_config.rs

1//! Axis configuration: unit conversion, jog defaults, limits.
2//!
3//! [`AxisConfig`] stores the encoder resolution and user-unit scaling,
4//! plus jog parameters, home position, and software position limits.
5//! It is used by [`Axis`](super::Axis) for all unit conversions.
6
7/// Configuration for a single motion axis.
8///
9/// Stores encoder resolution, user-unit scaling, jog defaults,
10/// and position limits. Used by [`Axis`](super::Axis) internally
11/// and also available for direct use (e.g. logging, HMI display).
12///
13/// # Examples
14///
15/// ```
16/// use autocore_std::motion::AxisConfig;
17///
18/// // ClearPath with 12,800 counts/rev, user units = degrees
19/// let cfg = AxisConfig::new(12_800).with_user_scale(360.0);
20/// assert!((cfg.to_counts(45.0) - 1600.0).abs() < 0.01);
21/// assert!((cfg.to_user(1600.0) - 45.0).abs() < 0.01);
22///
23/// // mm on a 5 mm/rev ballscrew
24/// let mm = AxisConfig::new(12_800).with_user_scale(5.0);
25/// assert!((mm.to_counts(5.0) - 12800.0).abs() < 0.01);
26/// ```
27#[derive(Debug, Clone)]
28pub struct AxisConfig {
29    // ── Unit conversion (private — use to_counts/to_user) ──
30    counts_per_rev: f64,
31    user_per_rev: f64,
32    /// When true, invert the sign of all position/velocity conversions.
33    /// Use when the motor counts in the opposite direction to your user-unit convention.
34    pub invert_direction: bool,
35
36    // ── Jog defaults ──
37
38    /// Jog speed in user units/s.
39    pub jog_speed: f64,
40    /// Jog acceleration in user units/s².
41    pub jog_accel: f64,
42    /// Jog deceleration in user units/s².
43    pub jog_decel: f64,
44
45    // ── Home ──
46
47    /// User-unit position assigned at the home reference point.
48    pub home_position: f64,
49    /// Homing search speed in user units/s.
50    pub homing_speed: f64,
51    /// Homing acceleration in user units/s².
52    pub homing_accel: f64,
53    /// Homing deceleration in user units/s².
54    pub homing_decel: f64,
55
56    // ── Software position limits ──
57
58    /// Enable the maximum (positive) software position limit.
59    pub enable_max_position_limit: bool,
60    /// Enable the minimum (negative) software position limit.
61    pub enable_min_position_limit: bool,
62    /// Maximum position limit in user units.
63    pub max_position_limit: f64,
64    /// Minimum position limit in user units.
65    pub min_position_limit: f64,
66}
67
68impl AxisConfig {
69    /// Create a new configuration from encoder resolution.
70    ///
71    /// Default user units are revolutions (1.0 user unit = 1 revolution).
72    /// Call [`with_user_scale`](Self::with_user_scale) to change.
73    pub fn new(counts_per_rev: u32) -> Self {
74        Self {
75            counts_per_rev: counts_per_rev as f64,
76            user_per_rev: 1.0,
77            invert_direction: false,
78            jog_speed: 0.0,
79            jog_accel: 0.0,
80            jog_decel: 0.0,
81            home_position: 0.0,
82            homing_speed: 0.0,
83            homing_accel: 0.0,
84            homing_decel: 0.0,
85            enable_max_position_limit: false,
86            enable_min_position_limit: false,
87            max_position_limit: 0.0,
88            min_position_limit: 0.0,
89        }
90    }
91
92    /// Set user-units-per-revolution.
93    ///
94    /// - Degrees: `.with_user_scale(360.0)`
95    /// - mm on 5 mm/rev ballscrew: `.with_user_scale(5.0)`
96    pub fn with_user_scale(mut self, user_per_rev: f64) -> Self {
97        self.user_per_rev = user_per_rev;
98        self
99    }
100
101    // ── Conversion methods ──
102
103    /// Convert user units to encoder counts (f64).
104    ///
105    /// `to_counts(45.0)` on a 12800 cpr / 360° config → 1600.0
106    pub fn to_counts(&self, user_units: f64) -> f64 {
107        let sign = if self.invert_direction { -1.0 } else { 1.0 };
108        user_units * self.counts_per_user() * sign
109    }
110
111    /// Convert encoder counts to user units (f64).
112    ///
113    /// `to_user(1600.0)` on a 12800 cpr / 360° config → 45.0
114    pub fn to_user(&self, counts: f64) -> f64 {
115        let sign = if self.invert_direction { -1.0 } else { 1.0 };
116        counts * sign / self.counts_per_user()
117    }
118
119    /// Encoder counts per user unit (scale factor).
120    ///
121    /// For 12800 cpr / 360°: `counts_per_user() ≈ 35.556`
122    pub fn counts_per_user(&self) -> f64 {
123        self.counts_per_rev / self.user_per_rev
124    }
125}
126
127#[cfg(test)]
128mod tests {
129    use super::*;
130
131    #[test]
132    fn basic_conversion_degrees() {
133        let cfg = AxisConfig::new(12_800).with_user_scale(360.0);
134        assert!((cfg.to_counts(45.0) - 1600.0).abs() < 0.01);
135        assert!((cfg.to_counts(360.0) - 12800.0).abs() < 0.01);
136        assert!((cfg.to_user(1600.0) - 45.0).abs() < 0.01);
137        assert!((cfg.to_user(12800.0) - 360.0).abs() < 0.01);
138    }
139
140    #[test]
141    fn basic_conversion_mm() {
142        let cfg = AxisConfig::new(12_800).with_user_scale(5.0);
143        assert!((cfg.to_counts(5.0) - 12800.0).abs() < 0.01);
144        assert!((cfg.to_counts(2.5) - 6400.0).abs() < 0.01);
145    }
146
147    #[test]
148    fn counts_per_user() {
149        let cfg = AxisConfig::new(12_800).with_user_scale(360.0);
150        let cpu = cfg.counts_per_user();
151        assert!((cpu - 12800.0 / 360.0).abs() < 1e-9);
152    }
153
154    #[test]
155    fn default_revolutions() {
156        let cfg = AxisConfig::new(12_800);
157        // 1 rev = 12800 counts
158        assert!((cfg.to_counts(1.0) - 12800.0).abs() < 0.01);
159        assert!((cfg.to_counts(0.125) - 1600.0).abs() < 0.01);
160    }
161
162    #[test]
163    fn round_trip() {
164        let cfg = AxisConfig::new(12_800).with_user_scale(360.0);
165        let original = 45.0;
166        let counts = cfg.to_counts(original);
167        let back = cfg.to_user(counts);
168        assert!((back - original).abs() < 1e-9);
169    }
170
171    #[test]
172    fn defaults() {
173        let cfg = AxisConfig::new(12_800);
174        assert_eq!(cfg.jog_speed, 0.0);
175        assert_eq!(cfg.home_position, 0.0);
176        assert!(!cfg.enable_max_position_limit);
177        assert!(!cfg.enable_min_position_limit);
178    }
179}