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 /// CiA 402 homing method used when the axis needs to declare "current position
56 /// = home offset" — i.e. the terminal step of software homing (after a sensor
57 /// search) and the integrated [`HomingMethod::CurrentPosition`] variant.
58 ///
59 /// Methods 37 and 35 are functionally equivalent; 37 is the newer CiA 402
60 /// addition. Default: 37 (works on Teknic ClearPath and other modern drives).
61 /// Set to 35 for drives whose 6098h range stops at 35 (Inovance SV660N).
62 pub soft_home_method: i8,
63
64 // ── Timeouts ──
65
66 /// Timeout for general operations (enable, disable, halt, fault recovery) in seconds.
67 /// Default: 7s.
68 pub operation_timeout_secs: f64,
69 /// Timeout for homing operations in seconds. Default: 30s.
70 pub homing_timeout_secs: f64,
71
72 // ── Software position limits ──
73
74 /// Enable the maximum (positive) software position limit.
75 pub enable_max_position_limit: bool,
76 /// Enable the minimum (negative) software position limit.
77 pub enable_min_position_limit: bool,
78 /// Maximum position limit in user units.
79 pub max_position_limit: f64,
80 /// Minimum position limit in user units.
81 pub min_position_limit: f64,
82}
83
84impl AxisConfig {
85 /// Create a new configuration from encoder resolution.
86 ///
87 /// Default user units are revolutions (1.0 user unit = 1 revolution).
88 /// Call [`with_user_scale`](Self::with_user_scale) to change.
89 pub fn new(counts_per_rev: u32) -> Self {
90 Self {
91 counts_per_rev: counts_per_rev as f64,
92 user_per_rev: 1.0,
93 invert_direction: false,
94 jog_speed: 0.0,
95 jog_accel: 0.0,
96 jog_decel: 0.0,
97 home_position: 0.0,
98 homing_speed: 0.0,
99 homing_accel: 0.0,
100 homing_decel: 0.0,
101 soft_home_method: 37,
102 operation_timeout_secs: 7.0,
103 homing_timeout_secs: 30.0,
104 enable_max_position_limit: false,
105 enable_min_position_limit: false,
106 max_position_limit: 0.0,
107 min_position_limit: 0.0,
108 }
109 }
110
111 /// Set user-units-per-revolution.
112 ///
113 /// - Degrees: `.with_user_scale(360.0)`
114 /// - mm on 5 mm/rev ballscrew: `.with_user_scale(5.0)`
115 pub fn with_user_scale(mut self, user_per_rev: f64) -> Self {
116 self.user_per_rev = user_per_rev;
117 self
118 }
119
120 // ── Conversion methods ──
121
122 /// Convert user units to encoder counts (f64).
123 ///
124 /// `to_counts(45.0)` on a 12800 cpr / 360° config → 1600.0
125 pub fn to_counts(&self, user_units: f64) -> f64 {
126 let sign = if self.invert_direction { -1.0 } else { 1.0 };
127 user_units * self.counts_per_user() * sign
128 }
129
130 /// Convert encoder counts to user units (f64).
131 ///
132 /// `to_user(1600.0)` on a 12800 cpr / 360° config → 45.0
133 pub fn to_user(&self, counts: f64) -> f64 {
134 let sign = if self.invert_direction { -1.0 } else { 1.0 };
135 counts * sign / self.counts_per_user()
136 }
137
138 /// Encoder counts per user unit (scale factor).
139 ///
140 /// For 12800 cpr / 360°: `counts_per_user() ≈ 35.556`
141 pub fn counts_per_user(&self) -> f64 {
142 self.counts_per_rev / self.user_per_rev
143 }
144}
145
146#[cfg(test)]
147mod tests {
148 use super::*;
149
150 #[test]
151 fn basic_conversion_degrees() {
152 let cfg = AxisConfig::new(12_800).with_user_scale(360.0);
153 assert!((cfg.to_counts(45.0) - 1600.0).abs() < 0.01);
154 assert!((cfg.to_counts(360.0) - 12800.0).abs() < 0.01);
155 assert!((cfg.to_user(1600.0) - 45.0).abs() < 0.01);
156 assert!((cfg.to_user(12800.0) - 360.0).abs() < 0.01);
157 }
158
159 #[test]
160 fn basic_conversion_mm() {
161 let cfg = AxisConfig::new(12_800).with_user_scale(5.0);
162 assert!((cfg.to_counts(5.0) - 12800.0).abs() < 0.01);
163 assert!((cfg.to_counts(2.5) - 6400.0).abs() < 0.01);
164 }
165
166 #[test]
167 fn counts_per_user() {
168 let cfg = AxisConfig::new(12_800).with_user_scale(360.0);
169 let cpu = cfg.counts_per_user();
170 assert!((cpu - 12800.0 / 360.0).abs() < 1e-9);
171 }
172
173 #[test]
174 fn default_revolutions() {
175 let cfg = AxisConfig::new(12_800);
176 // 1 rev = 12800 counts
177 assert!((cfg.to_counts(1.0) - 12800.0).abs() < 0.01);
178 assert!((cfg.to_counts(0.125) - 1600.0).abs() < 0.01);
179 }
180
181 #[test]
182 fn round_trip() {
183 let cfg = AxisConfig::new(12_800).with_user_scale(360.0);
184 let original = 45.0;
185 let counts = cfg.to_counts(original);
186 let back = cfg.to_user(counts);
187 assert!((back - original).abs() < 1e-9);
188 }
189
190 #[test]
191 fn defaults() {
192 let cfg = AxisConfig::new(12_800);
193 assert_eq!(cfg.jog_speed, 0.0);
194 assert_eq!(cfg.home_position, 0.0);
195 assert!(!cfg.enable_max_position_limit);
196 assert!(!cfg.enable_min_position_limit);
197 }
198}