Skip to main content

autocore_std/motion/
axis.rs

1//! Stateful motion controller for CiA 402 servo drives.
2//!
3//! [`Axis`] manages the CiA 402 protocol state machine internally,
4//! providing a clean high-level motion API. It owns an [`SdoClient`]
5//! for SDO operations during homing.
6//!
7//! # Usage
8//!
9//! ```ignore
10//! use autocore_std::motion::{Axis, AxisConfig};
11//!
12//! let config = AxisConfig::new(12_800).with_user_scale(360.0);
13//! let mut axis = Axis::new(config, "ClearPath_0");
14//!
15//! // In your control loop:
16//! axis.tick(&mut view, ctx.client);
17//!
18//! // Command the axis:
19//! axis.enable(&mut view);              // start enable sequence
20//! axis.move_absolute(&mut view, 45.0, 90.0, 180.0, 180.0);
21//! axis.home(&mut view, HomingMethod::CurrentPosition);
22//! ```
23
24use std::time::{Duration, Instant};
25
26use serde_json::json;
27use strum_macros::FromRepr;
28
29use crate::command_client::CommandClient;
30use crate::ethercat::{SdoClient, SdoResult};
31use crate::fb::Ton;
32use crate::motion::FbSetModeOfOperation;
33use super::axis_config::AxisConfig;
34use super::axis_view::AxisView;
35use super::homing::HomingMethod;
36use super::cia402::{
37    Cia402Control, Cia402Status, Cia402State,
38    ModesOfOperation, RawControlWord, RawStatusWord,
39};
40
41// ──────────────────────────────────────────────
42// Internal state machine
43// ──────────────────────────────────────────────
44
45#[derive(Debug, Clone, PartialEq)]
46enum AxisOp {
47    Idle,
48    Enabling(u8),
49    Disabling(u8),
50    Moving(MoveKind, u8, bool, bool),
51    Homing(u8),
52    SoftHoming(u8),
53    Halting(u8),
54    FaultRecovery(u8),
55}
56
57/// Sub-steps of `AxisOp::Halting`. Mirrors the multi-stage close-out used
58/// by soft-homing: wait for motion to stop, issue cancel_move, wait for
59/// setpoint_ack, clear new_setpoint, wait for setpoint_ack to drop.
60/// Without this sequence the PP handshake is left half-finished and the
61/// next `move_absolute` gets rejected with "set-point not acknowledged."
62#[repr(u8)]
63#[derive(Debug, Clone, PartialEq, FromRepr)]
64enum HaltState {
65    /// Halt bit set + new_setpoint cleared. Polling position for
66    /// stability before issuing cancel_move.
67    WaitStopped        = 0,
68    /// cancel_move issued. Waiting for SW bit 12 (setpoint_ack) AND
69    /// bit 10 (target_reached) — drive has accepted the cancel.
70    WaitCancelAck      = 10,
71    /// Ack received, new_setpoint cleared, single-setpoint flush bit
72    /// asserted. Waiting for SW bit 12 to drop so the next move's
73    /// rising edge is clean.
74    WaitCancelAckClear = 20,
75}
76
77/// How long each halt sub-stage may take before we error out.
78const HALT_STAGE_TIMEOUT: Duration = Duration::from_secs(3);
79
80/// Raw-encoder-count window within which the axis is considered "stopped."
81/// Sized to tolerate servo micro-oscillation during closed-loop hold.
82/// On a 10 000 cnt/mm drive this is 0.005 mm — below any meaningful
83/// motion but well above typical ±5 count hold jitter.
84const HALT_STABLE_WINDOW: i32 = 50;
85
86/// Velocity magnitude (in raw drive units, typically counts/s) at or
87/// below which the axis is considered "not moving" for the purposes of
88/// completing a halt. Used alongside position stability so we don't
89/// require *both* perfect position and zero velocity — either reliable
90/// indicator counts.
91const HALT_STOPPED_VELOCITY: i32 = 100;
92
93/// Consecutive ticks of stability required before issuing cancel_move.
94/// At a 10 ms scan period, 5 ticks = ~50 ms dwell — long enough for the
95/// drive to have actually settled, short enough not to stall the cycle.
96const HALT_STABLE_TICKS_REQUIRED: u8 = 5;
97
98#[repr(u8)]
99#[derive(Debug, Clone, PartialEq, FromRepr)]
100enum HomeState {
101    EnsurePpMode = 0,
102    WaitPpMode = 1,
103    Search = 5,
104    WaitSearching = 10,
105    WaitFoundSensor = 20,
106    WaitStoppedFoundSensor = 30,
107    WaitFoundSensorAck = 40,
108    WaitFoundSensorAckClear = 45,
109    DebounceFoundSensor = 50,
110    BackOff = 60,
111    WaitBackingOff = 70,
112    WaitLostSensor = 80,
113    WaitStoppedLostSensor = 90,
114    WaitLostSensorAck = 100,
115    WaitLostSensorAckClear = 120,
116    WaitHomeOffsetDone = 125,
117
118    WriteHomingModeOp = 160,
119    WaitWriteHomingModeOp = 165,
120    
121    WriteHomingMethod = 205,
122    WaitWriteHomingMethodDone = 210,
123    ClearHomingTrigger = 215,
124    TriggerHoming = 217,
125    WaitHomingStarted = 218,
126    WaitHomingDone = 220,
127    ResetHomingTrigger = 222,
128    WaitHomingTriggerCleared = 223,
129    WriteMotionModeOfOperation = 230,
130    WaitWriteMotionModeOfOperation = 235,
131    SendCurrentPositionTarget = 240,
132    WaitCurrentPositionTargetSent = 245
133
134}
135
136#[derive(Debug, Clone, PartialEq)]
137enum MoveKind {
138    Absolute,
139    Relative,
140}
141
142#[derive(Debug, Clone, Copy, PartialEq)]
143enum SoftHomeSensor {
144    PositiveLimit,
145    NegativeLimit,
146    HomeSensor,
147}
148
149#[derive(Debug, Clone, Copy, PartialEq)]
150enum SoftHomeSensorType {
151    /// PNP: sensor reads true when object detected (normally open)
152    Pnp,
153    /// NPN: sensor reads false when object detected (normally closed)
154    Npn,
155}
156
157// ──────────────────────────────────────────────
158// Axis
159// ──────────────────────────────────────────────
160
161/// Stateful motion controller for a CiA 402 servo drive.
162///
163/// Manages the CiA 402 protocol (state machine, PP handshake, homing)
164/// internally. Call [`tick()`](Self::tick) every control cycle to progress
165/// operations and update output fields.
166pub struct Axis {
167    config: AxisConfig,
168    sdo: SdoClient,
169
170    // ── Internal state ──
171    op: AxisOp,
172    home_offset: i32,
173    last_raw_position: i32,
174    op_started: Option<Instant>,
175    op_timeout: Duration,
176    homing_timeout: Duration,
177    move_start_timeout: Duration,
178    pending_move_target: i32,
179    pending_move_vel: u32,
180    pending_move_accel: u32,
181    pending_move_decel: u32,
182    homing_method: i8,
183    homing_sdo_tid: u32,
184    soft_home_sensor: SoftHomeSensor,
185    soft_home_sensor_type: SoftHomeSensorType,
186    soft_home_direction: f64,
187    halt_stable_count: u8,
188    prev_positive_limit: bool,
189    prev_negative_limit: bool,
190    prev_home_sensor: bool,
191
192
193
194    fb_mode_of_operation : FbSetModeOfOperation,
195
196    // ── Outputs (updated every tick) ──
197
198    /// True if a drive fault or operation timeout has occurred.
199    pub is_error: bool,
200    /// Drive error code (from status word or view error_code).
201    pub error_code: u32,
202    /// Human-readable error description.
203    pub error_message: String,
204    /// True when the drive is in Operation Enabled state.
205    pub motor_on: bool,
206    /// True when any operation is in progress (enable, move, home, fault recovery, etc.).
207    ///
208    /// Derived from the internal state machine — immediately true when a command
209    /// is issued, false when the operation completes or a fault cancels it.
210    /// Use this (not [`in_motion`](Self::in_motion)) to wait for operations to finish.
211    pub is_busy: bool,
212    /// True while a move operation specifically is active (subset of [`is_busy`](Self::is_busy)).
213    pub in_motion: bool,
214    /// True when velocity is positive.
215    pub moving_positive: bool,
216    /// True when velocity is negative.
217    pub moving_negative: bool,
218    /// Current position in user units (relative to home).
219    pub position: f64,
220    /// Current position in raw encoder counts (widened from i32).
221    pub raw_position: i64,
222    /// Current speed in user units/s (absolute value).
223    pub speed: f64,
224    /// True when position is at or beyond the maximum software limit.
225    pub at_max_limit: bool,
226    /// True when position is at or beyond the minimum software limit.
227    pub at_min_limit: bool,
228    /// True when the positive-direction hardware limit switch is active.
229    pub at_positive_limit_switch: bool,
230    /// True when the negative-direction hardware limit switch is active.
231    pub at_negative_limit_switch: bool,
232    /// True when the home reference sensor is active.
233    pub home_sensor: bool,
234
235
236    /// Timer used for delays between states.
237    ton : Ton
238}
239
240impl Axis {
241    /// Create a new Axis with the given configuration.
242    ///
243    /// `device_name` must match the device name in `project.json`
244    /// (used for SDO operations during homing).
245    pub fn new(config: AxisConfig, device_name: &str) -> Self {
246        let op_timeout = Duration::from_secs_f64(config.operation_timeout_secs);
247        let homing_timeout = Duration::from_secs_f64(config.homing_timeout_secs);
248        let move_start_timeout = op_timeout; // reuse operation timeout for move handshake
249        Self {
250            config,
251            sdo: SdoClient::new(device_name),
252            op: AxisOp::Idle,
253            home_offset: 0,
254            last_raw_position: 0,
255            op_started: None,
256            op_timeout,
257            homing_timeout,
258            move_start_timeout,
259            pending_move_target: 0,
260            pending_move_vel: 0,
261            pending_move_accel: 0,
262            pending_move_decel: 0,
263            homing_method: 37,
264            homing_sdo_tid: 0,
265            soft_home_sensor: SoftHomeSensor::HomeSensor,
266            soft_home_sensor_type: SoftHomeSensorType::Pnp,
267            soft_home_direction: 1.0,
268            halt_stable_count: 0,
269            prev_positive_limit: false,
270            prev_negative_limit: false,
271            prev_home_sensor: false,
272            is_error: false,
273            error_code: 0,
274            error_message: String::new(),
275            motor_on: false,
276            is_busy: false,
277            in_motion: false,
278            moving_positive: false,
279            moving_negative: false,
280            position: 0.0,
281            raw_position: 0,
282            speed: 0.0,
283            at_max_limit: false,
284            at_min_limit: false,
285            at_positive_limit_switch: false,
286            at_negative_limit_switch: false,
287            home_sensor: false,
288            ton: Ton::new(),
289            fb_mode_of_operation : FbSetModeOfOperation::new()
290        }
291    }
292
293    /// Get a reference to the axis configuration.
294    pub fn config(&self) -> &AxisConfig {
295        &self.config
296    }
297
298    // ═══════════════════════════════════════════
299    // Motion commands
300    // ═══════════════════════════════════════════
301
302    /// Start an absolute move to `target` in user units.
303    ///
304    /// The axis must be enabled (Operation Enabled) before calling this.
305    /// If the target exceeds a software position limit, the move is rejected
306    /// and [`is_error`](Self::is_error) is set.
307    pub fn move_absolute(
308        &mut self,
309        view: &mut impl AxisView,
310        target: f64,
311        vel: f64,
312        accel: f64,
313        decel: f64,
314    ) {
315        if let Some(msg) = self.check_target_limit(target, view) {
316            self.set_op_error(&msg);
317            return;
318        }
319
320        let cpu = self.config.counts_per_user();
321        let raw_target = self.config.to_counts(target).round() as i32 + self.home_offset;
322        let raw_vel = (vel * cpu).round() as u32;
323        let raw_accel = (accel * cpu).round() as u32;
324        let raw_decel = (decel * cpu).round() as u32;
325
326        // User-frame direction (consistent with sw_max/sw_min and hw_pos/hw_neg).
327        let user_pos = target > self.position;
328        let user_neg = target < self.position;
329
330        self.start_move(view, raw_target, raw_vel, raw_accel, raw_decel, MoveKind::Absolute, user_pos, user_neg);
331    }
332
333    /// Start a relative move by `distance` user units from the current position.
334    ///
335    /// The axis must be enabled (Operation Enabled) before calling this.
336    /// If the resulting position would exceed a software position limit,
337    /// the move is rejected and [`is_error`](Self::is_error) is set.
338    pub fn move_relative(
339        &mut self,
340        view: &mut impl AxisView,
341        distance: f64,
342        vel: f64,
343        accel: f64,
344        decel: f64,
345    ) {
346        log::info!("Axis: request to move relative dist {} vel {} accel {} decel {}",
347            distance, vel, accel, decel
348        );
349
350        if let Some(msg) = self.check_target_limit(self.position + distance, view) {
351            self.set_op_error(&msg);
352            return;
353        }
354
355        let cpu = self.config.counts_per_user();
356        let raw_distance = self.config.to_counts(distance).round() as i32;
357        let raw_vel = (vel * cpu).round() as u32;
358        let raw_accel = (accel * cpu).round() as u32;
359        let raw_decel = (decel * cpu).round() as u32;
360
361        log::info!("Axis starting relative move: request to move relative raw dist {} raw vel {} raw accel {} raw decel {}",
362            raw_distance, raw_vel, raw_accel, raw_decel
363        );
364
365        // Make sure bit 4 is off so that a new move can be commanded.
366        let mut cw = RawControlWord(view.control_word());
367        cw.set_bit(4, false); // new set-point
368        view.set_control_word(cw.raw());
369
370        // User-frame direction is the sign of `distance` (which is in user units).
371        let user_pos = distance > 0.0;
372        let user_neg = distance < 0.0;
373
374        self.start_move(view, raw_distance, raw_vel, raw_accel, raw_decel, MoveKind::Relative, user_pos, user_neg);
375    }
376
377    fn start_move(
378        &mut self,
379        view: &mut impl AxisView,
380        raw_target: i32,
381        raw_vel: u32,
382        raw_accel: u32,
383        raw_decel: u32,
384        kind: MoveKind,
385        user_pos: bool,
386        user_neg: bool,
387    ) {
388        self.pending_move_target = raw_target;
389        self.pending_move_vel = raw_vel;
390        self.pending_move_accel = raw_accel;
391        self.pending_move_decel = raw_decel;
392
393        // Set parameters on view
394        view.set_target_position(raw_target);
395        view.set_profile_velocity(raw_vel);
396        view.set_profile_acceleration(raw_accel);
397        view.set_profile_deceleration(raw_decel);
398
399        // Set control word: relative bit + trigger (new set-point).
400        // We also clear halt as belt-and-suspenders — limit-switch halts and
401        // explicit halt() both flow through the Halting state machine, which
402        // ends with halt already cleared, so this is normally a no-op.
403        let mut cw = RawControlWord(view.control_word());
404        cw.set_bit(6, kind == MoveKind::Relative);
405        cw.set_bit(8, false); // clear halt
406        cw.set_bit(4, true);  // new set-point
407        view.set_control_word(cw.raw());
408
409        // pos/neg stored in AxisOp::Moving are USER-frame so that check_limits
410        // can compare them directly against user-frame software limits and
411        // the integrator's user-frame limit-switch wiring without an
412        // axis-inversion swap.
413        self.op = AxisOp::Moving(kind, 1, user_pos, user_neg);
414        self.op_started = Some(Instant::now());
415    }
416
417    /// Halt the current move (decelerate to stop).
418    ///
419    /// This is a **multi-tick** operation. `halt()` starts the sequence:
420    ///
421    /// 1. Halt bit (CW 8) set, new_setpoint (CW 4) cleared.
422    /// 2. Wait for motor position to stabilize for ~100 ms.
423    /// 3. Issue cancel_move with current_position as target.
424    /// 4. Wait for setpoint_ack (SW 12) + target_reached (SW 10).
425    /// 5. Clear new_setpoint, set single_setpoint (CW 5).
426    /// 6. Wait for setpoint_ack to drop.
427    /// 7. Return to Idle.
428    ///
429    /// [`is_busy`](Self::is_busy) stays `true` for the whole sequence.
430    /// Callers that wait on `!is_busy()` after `halt()` (e.g.
431    /// [`super::move_to_load::MoveToLoad`]) will correctly block until
432    /// the drive's PP handshake is fully cleaned up, preventing a
433    /// "set-point not acknowledged" timeout on the *next* move.
434    pub fn halt(&mut self, view: &mut impl AxisView) {
435        self.command_halt(view);
436        self.halt_stable_count = 0;
437        self.last_raw_position = view.position_actual();
438        self.op_started = Some(Instant::now());
439        self.op = AxisOp::Halting(HaltState::WaitStopped as u8);
440    }
441
442    // ═══════════════════════════════════════════
443    // Drive control
444    // ═══════════════════════════════════════════
445
446    /// Start the enable sequence (Shutdown → ReadyToSwitchOn → OperationEnabled).
447    ///
448    /// The sequence is multi-tick. Check [`motor_on`](Self::motor_on) for completion.
449    pub fn enable(&mut self, view: &mut impl AxisView) {
450        // Step 0: set PP mode + cmd_shutdown
451        view.set_modes_of_operation(ModesOfOperation::ProfilePosition.as_i8());
452        let mut cw = RawControlWord(view.control_word());
453        cw.cmd_shutdown();
454        view.set_control_word(cw.raw());
455
456        self.op = AxisOp::Enabling(1);
457        self.op_started = Some(Instant::now());
458    }
459
460    /// Start the disable sequence (OperationEnabled → ReadyToSwitchOn).
461    ///
462    /// Uses the CiA 402 Shutdown command (controlword 0x06) rather than
463    /// Disable Operation (0x07). Per the spec, "Switched On" leaves motor
464    /// energization vendor-defined — Teknic and Inovance happen to
465    /// de-energize, Kollmorgen AKD keeps holding current. "Ready To
466    /// Switch On" has the HV section *guaranteed* off, so torque drops
467    /// on every compliant drive.
468    pub fn disable(&mut self, view: &mut impl AxisView) {
469        let mut cw = RawControlWord(view.control_word());
470        cw.cmd_shutdown();
471        cw.set_bit(4, false);
472        cw.set_bit(8, false);
473        cw.set_bit(7, false);
474        cw.set_bit(2, true);
475        view.set_control_word(cw.raw());
476
477        self.op = AxisOp::Disabling(1);
478        self.op_started = Some(Instant::now());
479    }
480
481    /// Clear the axis error state.
482    ///
483    /// Two paths depending on whether the drive itself is faulted:
484    ///
485    /// - **Drive in CiA 402 Fault / FaultReactionActive**: runs the full
486    ///   reset sequence — clear bit 7, then assert it next tick (rising
487    ///   edge on the drive's fault-reset bit), then wait for the drive to
488    ///   leave the Fault state. `is_busy` stays true through this sequence.
489    ///
490    /// - **Drive healthy** (e.g. the `is_error` was set by a software
491    ///   software-limit overshoot, an operation timeout, or a limit-switch
492    ///   close-out timeout — the drive itself is still in OperationEnabled):
493    ///   no CiA 402 handshake is needed. Just clears the software error
494    ///   flags and returns the axis to Idle synchronously. `is_busy` goes
495    ///   to false on the same call.
496    pub fn reset_faults(&mut self, view: &mut impl AxisView) {
497        let sw = RawStatusWord(view.status_word());
498        let drive_in_fault = matches!(
499            sw.state(),
500            Cia402State::Fault | Cia402State::FaultReactionActive
501        );
502
503        self.is_error = false;
504        self.error_code = 0;
505        self.error_message.clear();
506
507        if drive_in_fault {
508            // Step 0: clear bit 7 first (so next step produces a rising edge).
509            let mut cw = RawControlWord(view.control_word());
510            cw.cmd_clear_fault_reset();
511            view.set_control_word(cw.raw());
512            self.op = AxisOp::FaultRecovery(1);
513            self.op_started = Some(Instant::now());
514        } else {
515            // Software-only error — no drive handshake needed.
516            self.op = AxisOp::Idle;
517            self.op_started = None;
518            self.is_busy = false;
519            self.in_motion = false;
520        }
521    }
522
523    /// Start a homing sequence with the given homing method.
524    ///
525    /// **Integrated** methods delegate to the drive's built-in CiA 402
526    /// homing mode (SDO writes + homing trigger).
527    ///
528    /// **Software** methods are implemented by the Axis, which monitors
529    /// [`AxisView`] sensor signals for edge triggers and captures home.
530    pub fn home(&mut self, view: &mut impl AxisView, method: HomingMethod) {
531        if method.is_integrated() {
532            self.homing_method = match method {
533                HomingMethod::CurrentPosition => self.config.soft_home_method,
534                _ => method.cia402_code(),
535            };
536            self.op = AxisOp::Homing(0);
537            self.op_started = Some(Instant::now());
538            let _ = view;
539        } else {
540            self.configure_soft_homing(method);
541            self.start_soft_homing(view);
542        }
543    }
544
545    // ═══════════════════════════════════════════
546    // Position management
547    // ═══════════════════════════════════════════
548
549    /// Set the current position to the given user-unit value.
550    ///
551    /// Adjusts the internal home offset so that the current raw position
552    /// maps to `user_units`. Does not move the motor.
553    pub fn set_position(&mut self, view: &impl AxisView, user_units: f64) {
554        self.home_offset = view.position_actual() - self.config.to_counts(user_units).round() as i32;
555    }
556
557    /// Set the current position to `user_units` and persist it to the drive.
558    ///
559    /// Writes `0x607C` (home offset) to the drive in encoder counts, then runs
560    /// a CiA 402 homing cycle with method [`AxisConfig::soft_home_method`] so
561    /// the drive's reported position (`0x6064`) reads `user_units` afterward.
562    /// No motor motion occurs.
563    ///
564    /// For drives with absolute encoders configured in linear mode (e.g.
565    /// Inovance SV660N with `0x2002:02 = 1`), the resulting encoder offset
566    /// (`0x2005:2F`/`0x2005:31`) is auto-saved to EEPROM, so the new reference
567    /// survives a power cycle.
568    ///
569    /// **Preconditions:** drive must be enabled (OperationEnabled) and at rest
570    /// (no move in progress). The operation switches to HM mode internally and
571    /// back to PP on completion. Poll [`is_busy`](Self::is_busy) to know when
572    /// it finishes.
573    pub fn set_position_persistent(
574        &mut self,
575        client: &mut CommandClient,
576        user_units: f64,
577    ) {
578        // Reuse the SoftHoming state machine starting from the post-607C-write
579        // step (WaitHomeOffsetDone). We issue the 607C SDO write here; the
580        // state machine handles the rest: wait for ack, switch to HM, write
581        // 6098 = soft_home_method, trigger homing, wait for completion, switch
582        // back to PP, and zero the software home_offset since the drive owns
583        // it now.
584        self.config.home_position = user_units;
585        let desired_counts = self.config.to_counts(user_units).round() as i32;
586        self.homing_sdo_tid = self.sdo.write(client, 0x607C, 0, json!(desired_counts));
587        self.op = AxisOp::SoftHoming(HomeState::WaitHomeOffsetDone as u8);
588        self.op_started = Some(Instant::now());
589        log::info!(
590            "set_position_persistent: writing 0x607C = {} counts ({} user units)",
591            desired_counts, user_units
592        );
593    }
594
595    /// Set the home position in user units. This value is used by the next
596    /// `home()` call to set the axis position at the reference point.
597    /// Can be called at any time before homing.
598    pub fn set_home_position(&mut self, user_units: f64) {
599        self.config.home_position = user_units;
600    }
601
602    /// Set the maximum (positive) software position limit.
603    pub fn set_software_max_limit(&mut self, user_units: f64) {
604        self.config.max_position_limit = user_units;
605        self.config.enable_max_position_limit = true;
606    }
607
608    /// Set the minimum (negative) software position limit.
609    pub fn set_software_min_limit(&mut self, user_units: f64) {
610        self.config.min_position_limit = user_units;
611        self.config.enable_min_position_limit = true;
612    }
613
614    // ═══════════════════════════════════════════
615    // SDO pass-through
616    // ═══════════════════════════════════════════
617
618    /// Write an SDO value to the drive.
619    pub fn sdo_write(
620        &mut self,
621        client: &mut CommandClient,
622        index: u16,
623        sub_index: u8,
624        value: serde_json::Value,
625    ) {
626        self.sdo.write(client, index, sub_index, value);
627    }
628
629    /// Start an SDO read from the drive. Returns a transaction ID.
630    pub fn sdo_read(
631        &mut self,
632        client: &mut CommandClient,
633        index: u16,
634        sub_index: u8,
635    ) -> u32 {
636        self.sdo.read(client, index, sub_index)
637    }
638
639    /// Check the result of a previous SDO read.
640    pub fn sdo_result(
641        &mut self,
642        client: &mut CommandClient,
643        tid: u32,
644    ) -> SdoResult {
645        self.sdo.result(client, tid, Duration::from_secs(5))
646    }
647
648    // ═══════════════════════════════════════════
649    // Tick — call every control cycle
650    // ═══════════════════════════════════════════
651
652    /// Update outputs and progress the current operation.
653    ///
654    /// Must be called every control cycle. Does three things:
655    /// 1. Checks for drive faults
656    /// 2. Progresses the current multi-tick operation
657    /// 3. Updates output fields (position, velocity, status)
658    ///
659    /// Outputs are updated last so they reflect the final state after
660    /// all processing for this tick.
661    pub fn tick(&mut self, view: &mut impl AxisView, client: &mut CommandClient) {
662        self.check_faults(view);
663        self.progress_op(view, client);
664        self.update_outputs(view);
665        self.check_limits(view);
666    }
667
668    // ═══════════════════════════════════════════
669    // Internal: output update
670    // ═══════════════════════════════════════════
671
672    fn update_outputs(&mut self, view: &impl AxisView) {
673        let raw = view.position_actual();
674        self.raw_position = raw as i64;
675        self.position = self.config.to_user((raw - self.home_offset) as f64);
676
677        let vel = view.velocity_actual();
678        let user_vel = self.config.to_user(vel as f64);
679        self.speed = user_vel.abs();
680        self.moving_positive = user_vel > 0.0;
681        self.moving_negative = user_vel < 0.0;
682        self.is_busy = self.op != AxisOp::Idle;
683        self.in_motion = matches!(self.op, AxisOp::Moving(_, _, _, _) | AxisOp::SoftHoming(_));
684
685        let sw = RawStatusWord(view.status_word());
686        self.motor_on = sw.state() == Cia402State::OperationEnabled;
687
688        self.last_raw_position = raw;
689    }
690
691    // ═══════════════════════════════════════════
692    // Internal: fault check
693    // ═══════════════════════════════════════════
694
695    fn check_faults(&mut self, view: &impl AxisView) {
696        let sw = RawStatusWord(view.status_word());
697        let state = sw.state();
698
699        if matches!(state, Cia402State::Fault | Cia402State::FaultReactionActive) {
700            if !matches!(self.op, AxisOp::FaultRecovery(_)) {
701                self.is_error = true;
702                let ec = view.error_code();
703                if ec != 0 {
704                    self.error_code = ec as u32;
705                }
706                self.error_message = format!("Drive fault (state: {})", state);
707                // Cancel the current operation so is_busy goes false
708                self.op = AxisOp::Idle;
709                self.op_started = None;
710            }
711        }
712    }
713
714    // ═══════════════════════════════════════════
715    // Internal: operation timeout helper
716    // ═══════════════════════════════════════════
717
718    fn op_timed_out(&self) -> bool {
719        self.op_started
720            .map_or(false, |t| t.elapsed() > self.op_timeout)
721    }
722
723    fn homing_timed_out(&self) -> bool {
724        self.op_started
725            .map_or(false, |t| t.elapsed() > self.homing_timeout)
726    }
727
728    fn move_start_timed_out(&self) -> bool {
729        self.op_started
730            .map_or(false, |t| t.elapsed() > self.move_start_timeout)
731    }
732
733    /// Has the current operation exceeded the supplied stage timeout?
734    /// Used by the halt state machine so each sub-stage gets its own
735    /// budget rather than sharing the general `op_timeout`.
736    fn op_stage_timed_out(&self, limit: Duration) -> bool {
737        self.op_started
738            .map_or(false, |t| t.elapsed() > limit)
739    }
740
741    fn set_op_error(&mut self, msg: &str) {
742        self.is_error = true;
743        self.error_message = msg.to_string();
744        self.op = AxisOp::Idle;
745        self.op_started = None;
746        self.is_busy = false;
747        self.in_motion = false;
748        log::error!("Axis error: {}", msg);
749    }
750
751    fn restore_pp_after_error(&mut self, msg: &str) {
752        self.is_error = true;
753        self.error_message = msg.to_string();
754        self.op = AxisOp::SoftHoming(HomeState::WriteMotionModeOfOperation as u8);;
755        log::error!("Axis error: {}", msg);
756    }
757
758    fn finish_op_error(&mut self) {
759        self.op = AxisOp::Idle;
760        self.op_started = None;
761        self.is_busy = false;
762        self.in_motion = false;
763    }
764
765    fn complete_op(&mut self) {
766        self.op = AxisOp::Idle;
767        self.op_started = None;
768        // Clear busy/motion flags directly so callers that observe state on
769        // the same tick (e.g. check_limits, which runs after update_outputs)
770        // see Idle immediately. For complete_op calls inside progress_op,
771        // update_outputs would recompute these to the same values anyway.
772        self.is_busy = false;
773        self.in_motion = false;
774    }
775
776    // ═══════════════════════════════════════════
777    // Internal: position limits and limit switches
778    // ═══════════════════════════════════════════
779
780    /// Resolve the effective maximum software limit for this tick, combining
781    /// the static [`AxisConfig`] value (if enabled) with any dynamic limit
782    /// supplied by the [`AxisView`] (e.g. a GM-linked variable). The most
783    /// restrictive (smallest) value wins.
784    fn effective_max_limit(&self, view: &impl AxisView) -> Option<f64> {
785        let static_limit = if self.config.enable_max_position_limit {
786            Some(self.config.max_position_limit)
787        } else {
788            None
789        };
790        match (static_limit, view.dynamic_max_position_limit()) {
791            (Some(s), Some(d)) => Some(s.min(d)),
792            (Some(v), None) | (None, Some(v)) => Some(v),
793            (None, None) => None,
794        }
795    }
796
797    /// Resolve the effective minimum software limit for this tick. See
798    /// [`effective_max_limit`](Self::effective_max_limit) — the most
799    /// restrictive (largest) value wins.
800    fn effective_min_limit(&self, view: &impl AxisView) -> Option<f64> {
801        let static_limit = if self.config.enable_min_position_limit {
802            Some(self.config.min_position_limit)
803        } else {
804            None
805        };
806        match (static_limit, view.dynamic_min_position_limit()) {
807            (Some(s), Some(d)) => Some(s.max(d)),
808            (Some(v), None) | (None, Some(v)) => Some(v),
809            (None, None) => None,
810        }
811    }
812
813    /// Check whether a target position (in user units) exceeds a software limit.
814    /// Returns `Some(error_message)` if the target is out of range, `None` if OK.
815    /// Consults both the static [`AxisConfig`] limits and any dynamic limits
816    /// exposed by the view, taking whichever is most restrictive.
817    fn check_target_limit(&self, target: f64, view: &impl AxisView) -> Option<String> {
818        if let Some(max) = self.effective_max_limit(view) {
819            if target > max {
820                return Some(format!(
821                    "Target {:.3} exceeds max software limit {:.3}",
822                    target, max
823                ));
824            }
825        }
826        if let Some(min) = self.effective_min_limit(view) {
827            if target < min {
828                return Some(format!(
829                    "Target {:.3} exceeds min software limit {:.3}",
830                    target, min
831                ));
832            }
833        }
834        None
835    }
836
837    /// Check software position limits, hardware limit switches, and home sensor.
838    /// If a limit is violated and a move is in progress in that direction,
839    /// halt the drive and set an error. Moving in the opposite direction is
840    /// always allowed so the axis can be recovered.
841    ///
842    /// During software homing on a limit switch (`SoftHoming` + `SoftHomeSensor::PositiveLimit`
843    /// or `NegativeLimit`), the homed-on switch is suppressed so it triggers a home
844    /// event rather than an error halt. The opposite switch still protects.
845    fn check_limits(&mut self, view: &mut impl AxisView) {
846        // ── Software position limits (static config + dynamic GM-linked) ──
847        let eff_max = self.effective_max_limit(view);
848        let eff_min = self.effective_min_limit(view);
849        let sw_max = eff_max.map_or(false, |m| self.position >= m);
850        let sw_min = eff_min.map_or(false, |m| self.position <= m);
851
852        self.at_max_limit = sw_max;
853        self.at_min_limit = sw_min;
854
855        // ── Hardware limit switches ──
856        let hw_pos = view.positive_limit_active();
857        let hw_neg = view.negative_limit_active();
858
859        self.at_positive_limit_switch = hw_pos;
860        self.at_negative_limit_switch = hw_neg;
861
862        // ── Home sensor ──
863        self.home_sensor = view.home_sensor_active();
864
865        // ── Save previous sensor state for next tick's edge detection ──
866        self.prev_positive_limit = hw_pos;
867        self.prev_negative_limit = hw_neg;
868        self.prev_home_sensor = view.home_sensor_active();
869
870        // ── Halt logic (only while moving or soft-homing) ──
871        let mut commanded_positive = false;
872        let mut commanded_negative = false;
873
874        let is_moving = matches!(self.op, AxisOp::Moving(_, _, _, _));
875        let is_soft_homing = matches!(self.op, AxisOp::SoftHoming(_));
876
877        if !is_moving && !is_soft_homing {
878            return; // Only halt actively if we are currently driving into the limit
879        }
880
881        match &self.op {
882            AxisOp::Moving(_, _, pos, neg) => {
883                // Already user-frame (set by move_absolute / move_relative).
884                commanded_positive = *pos;
885                commanded_negative = *neg;
886            }
887            AxisOp::SoftHoming(_) => {
888                match self.soft_home_sensor {
889                    SoftHomeSensor::PositiveLimit => commanded_positive = true,
890                    SoftHomeSensor::NegativeLimit => commanded_negative = true,
891                    SoftHomeSensor::HomeSensor => {
892                        commanded_positive = self.moving_positive;
893                        commanded_negative = self.moving_negative;
894                    }
895                }
896            }
897            _ => {}
898        }
899
900        // During software homing, suppress the limit switch being homed on
901        let suppress_pos = is_soft_homing && self.soft_home_sensor == SoftHomeSensor::PositiveLimit;
902        let suppress_neg = is_soft_homing && self.soft_home_sensor == SoftHomeSensor::NegativeLimit;
903
904        let effective_hw_pos = hw_pos && !suppress_pos;
905        let effective_hw_neg = hw_neg && !suppress_neg;
906
907        // During soft homing, suppress software limits too (we need to move freely)
908        let effective_sw_max = sw_max && !is_soft_homing;
909        let effective_sw_min = sw_min && !is_soft_homing;
910
911        let positive_blocked = (effective_sw_max || effective_hw_pos) && commanded_positive;
912        let negative_blocked = (effective_sw_min || effective_hw_neg) && commanded_negative;
913
914
915        if positive_blocked || negative_blocked {
916            let mut cw = RawControlWord(view.control_word());
917            cw.set_bit(8, true); // halt
918            view.set_control_word(cw.raw());
919
920            let msg = if effective_hw_pos && commanded_positive {
921                "Positive limit switch active".to_string()
922            } else if effective_hw_neg && commanded_negative {
923                "Negative limit switch active".to_string()
924            } else if effective_sw_max && commanded_positive {
925                format!(
926                    "Software position limit: position {:.3} >= max {:.3}",
927                    self.position, eff_max.unwrap_or(self.position)
928                )
929            } else {
930                format!(
931                    "Software position limit: position {:.3} <= min {:.3}",
932                    self.position, eff_min.unwrap_or(self.position)
933                )
934            };
935
936            if is_soft_homing {
937                // Hitting the *opposite* limit during soft homing is a real
938                // fault — the axis went the wrong way and must be reset.
939                self.set_op_error(&msg);
940            } else {
941                // Regular move into a limit: enter the multi-stage Halting
942                // close-out (wait for motor stop → cancel queued setpoint →
943                // clear halt) so the drive ends in a clean state. Just
944                // clearing halt isn't enough — the previous move's target is
945                // still queued in the drive's PP buffer, and on the next
946                // start_move some drives ignore the new setpoint until that
947                // queued one is canceled.
948                //
949                // is_busy stays true through the close-out. Callers detect
950                // why the move ended by inspecting
951                // `at_positive_limit_switch` / `at_negative_limit_switch` /
952                // `at_max_limit` / `at_min_limit` once `!is_busy()`.
953                log::info!("Axis move halted by limit: {}", msg);
954                self.command_halt(view);
955                self.halt_stable_count = 0;
956                self.last_raw_position = view.position_actual();
957                self.op_started = Some(Instant::now());
958                self.op = AxisOp::Halting(HaltState::WaitStopped as u8);
959            }
960        }
961    }
962
963    // ═══════════════════════════════════════════
964    // Internal: operation progress
965    // ═══════════════════════════════════════════
966
967    fn progress_op(&mut self, view: &mut impl AxisView, client: &mut CommandClient) {
968        match self.op.clone() {
969            AxisOp::Idle => {}
970            AxisOp::Enabling(step) => self.tick_enabling(view, step),
971            AxisOp::Disabling(step) => self.tick_disabling(view, step),
972            AxisOp::Moving(kind, step, pos, neg) => self.tick_moving(view, kind, step, pos, neg),
973            AxisOp::Homing(step) => self.tick_homing(view, client, step),
974            AxisOp::SoftHoming(step) => self.tick_soft_homing(view, client, step),
975            AxisOp::Halting(step) => self.tick_halting(view, step),
976            AxisOp::FaultRecovery(step) => self.tick_fault_recovery(view, step),
977        }
978    }
979
980    // ── Enabling ──
981    // Step 0: (done in enable()) ensure PP + cmd_shutdown
982    // Step 1: wait ReadyToSwitchOn → cmd_enable_operation
983    // Step 2: wait OperationEnabled → capture home → Idle
984    fn tick_enabling(&mut self, view: &mut impl AxisView, step: u8) {
985        match step {
986            1 => {
987                let sw = RawStatusWord(view.status_word());
988                if sw.state() == Cia402State::ReadyToSwitchOn {
989                    let mut cw = RawControlWord(view.control_word());
990                    cw.cmd_enable_operation();
991                    view.set_control_word(cw.raw());
992                    self.op = AxisOp::Enabling(2);
993                } else if self.op_timed_out() {
994                    self.set_op_error("Enable timeout: waiting for ReadyToSwitchOn");
995                }
996            }
997            2 => {
998                let sw = RawStatusWord(view.status_word());
999                if sw.state() == Cia402State::OperationEnabled {
1000                    // NO - We do not do software-based encoder. That would break absolute encoders.
1001                    // self.home_offset = view.position_actual();
1002                    // log::info!("Axis enabled — home captured at {}", self.home_offset);
1003
1004                    // Possible TODO: Read the home_offset in the drive? 
1005
1006                    self.complete_op();
1007                } else if self.op_timed_out() {
1008                    self.set_op_error("Enable timeout: waiting for OperationEnabled");
1009                }
1010            }
1011            _ => self.complete_op(),
1012        }
1013    }
1014
1015    // ── Disabling ──
1016    // Step 0: (done in disable()) cmd_shutdown
1017    // Step 1: wait not OperationEnabled → Idle
1018    fn tick_disabling(&mut self, view: &mut impl AxisView, step: u8) {
1019        match step {
1020            1 => {
1021                let sw = RawStatusWord(view.status_word());
1022                if sw.state() != Cia402State::OperationEnabled {
1023                    self.complete_op();
1024                } else if self.op_timed_out() {
1025                    self.set_op_error("Disable timeout: drive still in OperationEnabled");
1026                }
1027            }
1028            _ => self.complete_op(),
1029        }
1030    }
1031
1032    // ── Moving ──
1033    // Step 0: (done in move_absolute/relative()) set params + trigger
1034    // Step 1: wait set_point_acknowledge → ack
1035    // Step 2: wait ack cleared (one tick)
1036    // Step 3: wait target_reached → Idle
1037    fn tick_moving(&mut self, view: &mut impl AxisView, kind: MoveKind, step: u8, pos: bool, neg: bool) {
1038        match step {
1039            1 => {
1040                // Wait for set-point acknowledge (bit 12)
1041                let sw = RawStatusWord(view.status_word());
1042                if sw.raw() & (1 << 12) != 0 {
1043                    // Ack: clear new set-point (bit 4)
1044                    let mut cw = RawControlWord(view.control_word());
1045                    cw.set_bit(4, false);
1046                    view.set_control_word(cw.raw());
1047                    self.op = AxisOp::Moving(kind, 2, pos, neg);
1048                } else if self.move_start_timed_out() {
1049                    self.set_op_error("Move timeout: set-point not acknowledged");
1050                }
1051            },
1052            2 => {
1053                // Wait for the drive to confirm it saw Bit 4 go low
1054                let sw = RawStatusWord(view.status_word());
1055                if sw.raw() & (1 << 12) == 0 {
1056                    // Handshake is officially reset. Now wait for physics.
1057                    self.op = AxisOp::Moving(kind, 3, pos, neg);
1058                }
1059            },
1060            3 => {
1061                // Wait for target reached (bit 10) — no timeout, moves can take arbitrarily long
1062                let sw = RawStatusWord(view.status_word());
1063                if sw.target_reached() {
1064                    self.complete_op();
1065                }
1066            },
1067            _ => self.complete_op(),
1068        }
1069    }
1070
1071    // ── Homing (hardware-delegated) ──
1072    // Step 0:  write homing method SDO (0x6098:0)
1073    // Step 1:  wait SDO ack
1074    // Step 2:  write homing speed SDO (0x6099:1 — search for switch)
1075    // Step 3:  wait SDO ack
1076    // Step 4:  write homing speed SDO (0x6099:2 — search for zero)
1077    // Step 5:  wait SDO ack
1078    // Step 6:  write homing accel SDO (0x609A:0)
1079    // Step 7:  wait SDO ack
1080    // Step 8:  switch to Homing mode (PDO field + SDO write 0x6060)
1081    // Step 9:  wait for Homing mode written + verified via SDO read-back (0x6061)
1082    // Step 10: trigger homing (rising edge on CW bit 4)
1083    // Step 11: wait for drive to acknowledge start (bit 12 clears)
1084    // Step 12: wait homing complete (bits 10+12 set, 13 clear)
1085    // Step 13: capture home offset, clear CW bit 4
1086    // Step 14: switch back to PP mode (PDO field + SDO write 0x6060)
1087    // Step 15: wait for PP mode written + verified, then → Idle
1088    //
1089    // If homing_speed and homing_accel are both 0, steps 2-7 are skipped
1090    // (preserves backward compatibility for users who pre-configure via SDO).
1091    //
1092    // Mode is switched AND verified over SDO (via FbSetModeOfOperation) rather
1093    // than trusting modes_of_operation_display(): on drives that don't PDO-map
1094    // 0x6060/0x6061 (e.g. Kollmorgen AKD) the cyclic display field is a codegen
1095    // stub mirrored from the commanded value, so it would falsely confirm.
1096    fn tick_homing(
1097        &mut self,
1098        view: &mut impl AxisView,
1099        client: &mut CommandClient,
1100        step: u8,
1101    ) {
1102        match step {
1103            0 => {
1104                // Write homing method via SDO (0x6098:0)
1105                self.homing_sdo_tid = self.sdo.write(
1106                    client,
1107                    0x6098,
1108                    0,
1109                    json!(self.homing_method),
1110                );
1111                self.op = AxisOp::Homing(1);
1112            }
1113            1 => {
1114                // Wait for SDO write ack
1115                match self.sdo.result(client, self.homing_sdo_tid, Duration::from_secs(5)) {
1116                    SdoResult::Ok(_) => {
1117                        // Skip speed/accel SDOs if both are zero
1118                        if self.config.homing_speed == 0.0 && self.config.homing_accel == 0.0 {
1119                            self.op = AxisOp::Homing(8);
1120                        } else {
1121                            self.op = AxisOp::Homing(2);
1122                        }
1123                    }
1124                    SdoResult::Pending => {
1125                        if self.homing_timed_out() {
1126                            self.set_op_error("Homing timeout: SDO write for homing method");
1127                        }
1128                    }
1129                    SdoResult::Err(e) => {
1130                        self.set_op_error(&format!("Homing SDO error: {}", e));
1131                    }
1132                    SdoResult::Timeout => {
1133                        self.set_op_error("Homing timeout: SDO write timed out");
1134                    }
1135                }
1136            }
1137            2 => {
1138                // Write homing speed (0x6099:1 — search for switch)
1139                let speed_counts = self.config.to_counts(self.config.homing_speed).round() as u32;
1140                self.homing_sdo_tid = self.sdo.write(
1141                    client,
1142                    0x6099,
1143                    1,
1144                    json!(speed_counts),
1145                );
1146                self.op = AxisOp::Homing(3);
1147            }
1148            3 => {
1149                match self.sdo.result(client, self.homing_sdo_tid, Duration::from_secs(5)) {
1150                    SdoResult::Ok(_) => { self.op = AxisOp::Homing(4); }
1151                    SdoResult::Pending => {
1152                        if self.homing_timed_out() {
1153                            self.set_op_error("Homing timeout: SDO write for homing speed (switch)");
1154                        }
1155                    }
1156                    SdoResult::Err(e) => { self.set_op_error(&format!("Homing SDO error: {}", e)); }
1157                    SdoResult::Timeout => { self.set_op_error("Homing timeout: SDO write timed out"); }
1158                }
1159            }
1160            4 => {
1161                // Write homing speed (0x6099:2 — search for zero, same value)
1162                let speed_counts = self.config.to_counts(self.config.homing_speed).round() as u32;
1163                self.homing_sdo_tid = self.sdo.write(
1164                    client,
1165                    0x6099,
1166                    2,
1167                    json!(speed_counts),
1168                );
1169                self.op = AxisOp::Homing(5);
1170            }
1171            5 => {
1172                match self.sdo.result(client, self.homing_sdo_tid, Duration::from_secs(5)) {
1173                    SdoResult::Ok(_) => { self.op = AxisOp::Homing(6); }
1174                    SdoResult::Pending => {
1175                        if self.homing_timed_out() {
1176                            self.set_op_error("Homing timeout: SDO write for homing speed (zero)");
1177                        }
1178                    }
1179                    SdoResult::Err(e) => { self.set_op_error(&format!("Homing SDO error: {}", e)); }
1180                    SdoResult::Timeout => { self.set_op_error("Homing timeout: SDO write timed out"); }
1181                }
1182            }
1183            6 => {
1184                // Write homing acceleration (0x609A:0)
1185                let accel_counts = self.config.to_counts(self.config.homing_accel).round() as u32;
1186                self.homing_sdo_tid = self.sdo.write(
1187                    client,
1188                    0x609A,
1189                    0,
1190                    json!(accel_counts),
1191                );
1192                self.op = AxisOp::Homing(7);
1193            }
1194            7 => {
1195                match self.sdo.result(client, self.homing_sdo_tid, Duration::from_secs(5)) {
1196                    SdoResult::Ok(_) => { self.op = AxisOp::Homing(8); }
1197                    SdoResult::Pending => {
1198                        if self.homing_timed_out() {
1199                            self.set_op_error("Homing timeout: SDO write for homing acceleration");
1200                        }
1201                    }
1202                    SdoResult::Err(e) => { self.set_op_error(&format!("Homing SDO error: {}", e)); }
1203                    SdoResult::Timeout => { self.set_op_error("Homing timeout: SDO write timed out"); }
1204                }
1205            }
1206            8 => {
1207                // Switch into Homing mode on BOTH channels:
1208                //  - the cyclic PDO field, so drives that PDO-map 0x6060 carry the
1209                //    correct mode on the wire, and
1210                //  - an SDO write + read-back of 0x6060/0x6061 (FbSetModeOfOperation),
1211                //    so drives that pin mode by SDO (e.g. Kollmorgen AKD, which does
1212                //    not PDO-map mode) actually transition AND are verified.
1213                // The SDO read-back replaces the old modes_of_operation_display()
1214                // check, which silently passed on PDO-unmapped drives because codegen
1215                // mirrors the commanded value into the display stub.
1216                view.set_modes_of_operation(ModesOfOperation::Homing.as_i8());
1217                // Ensure CW bit 4 starts LOW so the next step issues a clean rising edge.
1218                let mut cw = RawControlWord(view.control_word());
1219                cw.set_bit(4, false);
1220                view.set_control_word(cw.raw());
1221                self.fb_mode_of_operation.reset();
1222                self.fb_mode_of_operation.start(ModesOfOperation::Homing as i8);
1223                self.fb_mode_of_operation.tick(client, &mut self.sdo);
1224                self.op = AxisOp::Homing(9);
1225            }
1226            9 => {
1227                // Wait for the mode switch to be written and verified over SDO.
1228                self.fb_mode_of_operation.tick(client, &mut self.sdo);
1229                if !self.fb_mode_of_operation.is_busy() {
1230                    if self.fb_mode_of_operation.is_error() {
1231                        self.set_op_error(&format!(
1232                            "Homing: failed to enter Homing mode: {} {}",
1233                            self.fb_mode_of_operation.error_code(),
1234                            self.fb_mode_of_operation.error_message()
1235                        ));
1236                    } else {
1237                        self.op = AxisOp::Homing(10);
1238                    }
1239                }
1240            }
1241            10 => {
1242                // Trigger homing: rising edge on bit 4
1243                let mut cw = RawControlWord(view.control_word());
1244                cw.set_bit(4, true);
1245                view.set_control_word(cw.raw());
1246                self.op = AxisOp::Homing(11);
1247            }
1248            11 => {
1249                // One drive cycle after the trigger edge, then evaluate completion.
1250                //
1251                // We deliberately do NOT wait for bit 12 (homing attained) to clear
1252                // first. That old start-gate assumed the drive de-asserts attained at
1253                // the start of a new run and re-asserts on completion — true for
1254                // methods that take time to run, but an instantaneous method (35/37,
1255                // set-current-position) completes within one cycle, so bit 12 goes
1256                // high immediately and is never observed low. The gate's original
1257                // purpose (rejecting a stale PP set-point acknowledge in bit 12) is
1258                // now covered by verifying mode 6 over SDO in steps 8–9 before we
1259                // ever trigger: once in Homing mode, bit 12 carries homing semantics.
1260                let sw = view.status_word();
1261                if sw & (1 << 13) != 0 {
1262                    self.set_op_error("Homing error: drive reported homing failure");
1263                } else {
1264                    self.op = AxisOp::Homing(12);
1265                }
1266            }
1267            12 => {
1268                // Wait for homing complete.
1269                //
1270                // Per CiA-402, bit 12 (homing attained) with bit 13 (error) clear is
1271                // the authoritative "homed" indicator. We do NOT require bit 10
1272                // (target reached): a set-current-position home reports "attained,
1273                // target not reached" (bit12=1, bit10=0) and never sets bit 10.
1274                // Methods that move to a target set both, so bit 12 alone still holds.
1275                let sw = view.status_word();
1276                let error    = sw & (1 << 13) != 0;
1277                let attained = sw & (1 << 12) != 0;
1278
1279                if error {
1280                    self.set_op_error("Homing error: drive reported homing failure");
1281                } else if attained {
1282                    self.op = AxisOp::Homing(13);
1283                } else if self.homing_timed_out() {
1284                    self.set_op_error(&format!("Homing timeout: procedure did not complete (sw=0x{:04X})", sw));
1285                }
1286            }
1287            13 => {
1288                // Capture home offset, applying home_position so the reference
1289                // point reads as config.home_position in user units.
1290                self.home_offset = view.position_actual()
1291                    - self.config.to_counts(self.config.home_position).round() as i32;
1292                // Clear homing start bit in its own cycle before switching modes
1293                let mut cw = RawControlWord(view.control_word());
1294                cw.set_bit(4, false);
1295                view.set_control_word(cw.raw());
1296                self.op = AxisOp::Homing(14);
1297            }
1298            14 => {
1299                // One tick later (bit 4 already cleared in step 13, so the drive
1300                // sees the falling edge before the mode change), switch back to PP
1301                // mode. Same dual-channel approach as the switch into Homing: cyclic
1302                // PDO field plus an SDO write + read-back so it lands and is verified
1303                // on drives that don't PDO-map mode.
1304                view.set_modes_of_operation(ModesOfOperation::ProfilePosition.as_i8());
1305                self.fb_mode_of_operation.reset();
1306                self.fb_mode_of_operation.start(ModesOfOperation::ProfilePosition as i8);
1307                self.fb_mode_of_operation.tick(client, &mut self.sdo);
1308                self.op = AxisOp::Homing(15);
1309            }
1310            15 => {
1311                // Wait for the switch back to PP mode to be written and verified.
1312                self.fb_mode_of_operation.tick(client, &mut self.sdo);
1313                if !self.fb_mode_of_operation.is_busy() {
1314                    if self.fb_mode_of_operation.is_error() {
1315                        self.set_op_error(&format!(
1316                            "Homing: failed to restore PP mode: {} {}",
1317                            self.fb_mode_of_operation.error_code(),
1318                            self.fb_mode_of_operation.error_message()
1319                        ));
1320                    } else {
1321                        log::info!("Homing complete — home offset: {}", self.home_offset);
1322                        self.complete_op();
1323                    }
1324                }
1325            }
1326            _ => self.complete_op(),
1327        }
1328    }
1329
1330    // ── Software homing helpers ──
1331
1332    fn configure_soft_homing(&mut self, method: HomingMethod) {
1333        match method {
1334            HomingMethod::LimitSwitchPosPnp => {
1335                self.soft_home_sensor = SoftHomeSensor::PositiveLimit;
1336                self.soft_home_sensor_type = SoftHomeSensorType::Pnp;
1337                self.soft_home_direction = 1.0;
1338            }
1339            HomingMethod::LimitSwitchNegPnp => {
1340                self.soft_home_sensor = SoftHomeSensor::NegativeLimit;
1341                self.soft_home_sensor_type = SoftHomeSensorType::Pnp;
1342                self.soft_home_direction = -1.0;
1343            }
1344            HomingMethod::LimitSwitchPosNpn => {
1345                self.soft_home_sensor = SoftHomeSensor::PositiveLimit;
1346                self.soft_home_sensor_type = SoftHomeSensorType::Npn;
1347                self.soft_home_direction = 1.0;
1348            }
1349            HomingMethod::LimitSwitchNegNpn => {
1350                self.soft_home_sensor = SoftHomeSensor::NegativeLimit;
1351                self.soft_home_sensor_type = SoftHomeSensorType::Npn;
1352                self.soft_home_direction = -1.0;
1353            }
1354            HomingMethod::HomeSensorPosPnp => {
1355                self.soft_home_sensor = SoftHomeSensor::HomeSensor;
1356                self.soft_home_sensor_type = SoftHomeSensorType::Pnp;
1357                self.soft_home_direction = 1.0;
1358            }
1359            HomingMethod::HomeSensorNegPnp => {
1360                self.soft_home_sensor = SoftHomeSensor::HomeSensor;
1361                self.soft_home_sensor_type = SoftHomeSensorType::Pnp;
1362                self.soft_home_direction = -1.0;
1363            }
1364            HomingMethod::HomeSensorPosNpn => {
1365                self.soft_home_sensor = SoftHomeSensor::HomeSensor;
1366                self.soft_home_sensor_type = SoftHomeSensorType::Npn;
1367                self.soft_home_direction = 1.0;
1368            }
1369            HomingMethod::HomeSensorNegNpn => {
1370                self.soft_home_sensor = SoftHomeSensor::HomeSensor;
1371                self.soft_home_sensor_type = SoftHomeSensorType::Npn;
1372                self.soft_home_direction = -1.0;
1373            }
1374            _ => {} // integrated methods handled elsewhere
1375        }
1376    }
1377
1378    fn start_soft_homing(&mut self, view: &mut impl AxisView) {
1379        self.op = AxisOp::SoftHoming(HomeState::EnsurePpMode as u8);
1380        self.op_started = Some(Instant::now());
1381    }
1382
1383    fn check_soft_home_trigger(&self, view: &impl AxisView) -> bool {
1384        let raw = match self.soft_home_sensor {
1385            SoftHomeSensor::PositiveLimit => view.positive_limit_active(),
1386            SoftHomeSensor::NegativeLimit => view.negative_limit_active(),
1387            SoftHomeSensor::HomeSensor    => view.home_sensor_active(),
1388        };
1389        match self.soft_home_sensor_type {
1390            SoftHomeSensorType::Pnp => raw,    // PNP: true = detected
1391            SoftHomeSensorType::Npn => !raw,   // NPN: false = detected
1392        }
1393    }
1394
1395
1396    /// Calculate the maximum relative target for the specified direction.
1397    /// The result is adjusted for whether the motor direction has been inverted.
1398    fn calculate_max_relative_target(&self, direction : f64) -> i32 {
1399        let dir = if !self.config.invert_direction  {
1400            direction
1401        } 
1402        else {
1403            -direction
1404        };
1405
1406        let target = if dir > 0.0 {
1407            i32::MAX 
1408        }
1409        else {
1410            i32::MIN
1411        };
1412
1413        return target;
1414    }
1415
1416
1417    /// Convenient macro
1418    /// Configure the command word for an immediate halt
1419    /// and reset the new setpoint bit, which should cause
1420    /// status word bit 12 to clear
1421    pub fn command_halt(&self, view: &mut impl AxisView) {
1422        let mut cw = RawControlWord(view.control_word());
1423        cw.set_bit(8, true);  // halt
1424        cw.set_bit(4, false);  // reset new setpoint bit
1425        cw.set_bit(5, true);  // single set-point. If false, the new move will be queued!
1426        cw.set_bit(6, false); // absolute move
1427        view.set_control_word(cw.raw());        
1428    }
1429
1430    /// Convenient macro
1431    /// Configure the command word to clear the halt bit
1432    pub fn command_clear_halt(&self, view: &mut impl AxisView) {
1433        let mut cw = RawControlWord(view.control_word());
1434        cw.set_bit(8, false);  // reset halt
1435        view.set_control_word(cw.raw());        
1436    }
1437
1438
1439    /// Convenient macro.
1440    /// Configures command bits and targets to cancel a previous move.
1441    /// Bit 4 should be off before calling this function.
1442    /// The current absolute position will be used as the target, so there 
1443    /// should be no motion
1444    /// Halt will be turned on, if not already.]
1445    /// After this, wait for bit 12 to be true before clearing the halt bit.
1446    pub fn command_cancel_move(&self, view: &mut impl AxisView) {
1447
1448        let mut cw = RawControlWord(view.control_word());
1449
1450        cw.set_bit(4, true);  // new set-point
1451        cw.set_bit(5, true);  // single set-point. If false, the new move will be queued!
1452        cw.set_bit(6, false); // absolute move
1453
1454        if self.config().clear_halt_during_cancel {
1455            // Doesn't work with Teknic!
1456            cw.set_bit(8, false); // clear halt
1457        }
1458        
1459        view.set_control_word(cw.raw());        
1460
1461        let current_pos = view.position_actual();
1462        view.set_target_position(current_pos);
1463        view.set_profile_velocity(0);
1464    }
1465
1466
1467    /// Writes out the scaled homing speed into the bus.
1468    fn command_homing_speed(&self, view: &mut impl AxisView) {
1469        let cpu = self.config.counts_per_user();
1470        let vel = (self.config.homing_speed * cpu).round() as u32;
1471        let accel = (self.config.homing_accel * cpu).round() as u32;
1472        let decel = (self.config.homing_decel * cpu).round() as u32;
1473        view.set_profile_velocity(vel);
1474        view.set_profile_acceleration(accel);
1475        view.set_profile_deceleration(decel);        
1476    }
1477
1478    // ── Software homing state machine ──
1479    //
1480    // Phase 1: SEARCH (steps 0-3)
1481    //   Relative move in search direction until sensor triggers.
1482    //
1483    // Phase 2: HALT (steps 4-6)
1484    //   Stop the motor, cancel the old target.
1485    //
1486    // Phase 3: BACK-OFF (steps 7-11)
1487    //   Move opposite direction until sensor clears, then stop.
1488    //
1489    // Phase 4: SET HOME (steps 12-18)
1490    //   Write home offset to drive via SDO, trigger CurrentPosition homing,
1491    //   send hold set-point, complete.
1492    //
1493    fn tick_soft_homing(&mut self, view: &mut impl AxisView, client: &mut CommandClient, step: u8) {        
1494        match HomeState::from_repr(step) {
1495
1496            Some(HomeState::EnsurePpMode) => {
1497                //
1498                // If the drive crapped out in a previous mode, it might still be in homing mode.
1499                // Make sure we're in Profile Position mode.
1500                //
1501                log::info!("SoftHome: Ensuring PP mode..");
1502                self.fb_mode_of_operation.start(ModesOfOperation::ProfilePosition as i8);
1503                self.fb_mode_of_operation.tick(client, &mut self.sdo);
1504                self.op = AxisOp::SoftHoming(HomeState::WaitPpMode as u8);
1505            },
1506            Some(HomeState::WaitPpMode) => {
1507
1508                self.fb_mode_of_operation.tick(client, &mut self.sdo);
1509                if !self.fb_mode_of_operation.is_busy() {
1510                    if self.fb_mode_of_operation.is_error() {
1511                        self.set_op_error(&format!("Software homing SDO error writing homing mode of operation: {} {}", 
1512                            self.fb_mode_of_operation.error_code(), self.fb_mode_of_operation.error_message()
1513                        ));
1514                    }
1515                    else {
1516                        log::info!("SoftHome: Drive is in PP mode!");
1517
1518                        // If sensor is NOT triggered, search for it (issue a move).
1519                        // If sensor IS already triggered, skip search and go straight
1520                        // to the found-sensor halt/back-off sequence.
1521                        if !self.check_soft_home_trigger(view) {
1522                            log::info!("SoftHome: Not on home switch; seek out.");
1523                            self.op = AxisOp::SoftHoming(HomeState::Search as u8);
1524                        } else {
1525                            log::info!("SoftHome: Already on home switch, skipping ahead to back-off stage.");
1526                            self.op = AxisOp::SoftHoming(HomeState::WaitFoundSensor as u8);
1527                        }
1528                    }
1529                }
1530
1531
1532            },
1533
1534            // ── Phase 1: SEARCH ──
1535            Some(HomeState::Search) => {
1536                view.set_modes_of_operation(ModesOfOperation::ProfilePosition.as_i8());
1537
1538                // // Absolute move to a far-away position in the search direction.
1539                // // Use raw counts directly to avoid overflow with invert_direction.                
1540                // let far_counts = (self.soft_home_direction * 999_999.0 * cpu).round() as i32;
1541                // let target = if self.config.invert_direction { -far_counts } else { far_counts };
1542                // let target = target + view.position_actual(); // offset from current
1543
1544
1545                // move in a relative direction as far as possible
1546                // we will stop when we reach the switch
1547                let target = self.calculate_max_relative_target(self.soft_home_direction);
1548                view.set_target_position(target);
1549
1550                // let cpu = self.config.counts_per_user();
1551                // let vel = (self.config.homing_speed * cpu).round() as u32;
1552                // let accel = (self.config.homing_accel * cpu).round() as u32;
1553                // let decel = (self.config.homing_decel * cpu).round() as u32;
1554                // view.set_profile_velocity(vel);
1555                // view.set_profile_acceleration(accel);
1556                // view.set_profile_deceleration(decel);
1557
1558                self.command_homing_speed(view);
1559
1560                let mut cw = RawControlWord(view.control_word());
1561                cw.set_bit(4, true);  // new set-point
1562                cw.set_bit(6, true); // sets true for relative move
1563                cw.set_bit(8, false); // clear halt
1564                cw.set_bit(13, true); // move relative to the actual current motor position
1565                view.set_control_word(cw.raw());
1566
1567                log::info!("SoftHome[0]: SEARCH relative target={} vel={} dir={} pos={}",
1568                    target, self.config.homing_speed, self.soft_home_direction, view.position_actual());
1569                self.op = AxisOp::SoftHoming(HomeState::WaitSearching as u8);
1570            }
1571            Some(HomeState::WaitSearching) => {
1572                if self.check_soft_home_trigger(view) {
1573                    log::debug!("SoftHome[1]: sensor triggered during ack wait");
1574                    self.op = AxisOp::SoftHoming(HomeState::WaitFoundSensor as u8);
1575                    return;
1576                }
1577                let sw = RawStatusWord(view.status_word());
1578                if sw.raw() & (1 << 12) != 0 {
1579                    let mut cw = RawControlWord(view.control_word());
1580                    cw.set_bit(4, false);
1581                    view.set_control_word(cw.raw());
1582                    log::debug!("SoftHome[1]: set-point ack received, clearing bit 4");
1583                    self.op = AxisOp::SoftHoming(HomeState::WaitFoundSensor as u8);
1584                } else if self.homing_timed_out() {
1585                    self.set_op_error("Software homing timeout: set-point not acknowledged");
1586                }
1587            }
1588            // Some(HomeState::WaitSensor) => {
1589            //     if self.check_soft_home_trigger(view) {
1590            //         log::debug!("SoftHome[2]: sensor triggered during transition");
1591            //         self.op = AxisOp::SoftHoming(4);
1592            //         return;
1593            //     }
1594            //     log::debug!("SoftHome[2]: transition → monitoring");
1595            //     self.op = AxisOp::SoftHoming(3);
1596            // }
1597            Some(HomeState::WaitFoundSensor) => {
1598                if self.check_soft_home_trigger(view) {
1599                    log::info!("SoftHome[3]: sensor triggered at pos={}. HALTING", view.position_actual());
1600                    log::info!("ControlWord is : {} ", view.control_word());
1601
1602                    let mut cw = RawControlWord(view.control_word());
1603                    cw.set_bit(8, true);  // halt
1604                    cw.set_bit(4, false);  // reset new setpoint bit
1605                    view.set_control_word(cw.raw());
1606
1607
1608                    self.halt_stable_count = 0;
1609                    self.op = AxisOp::SoftHoming(HomeState::WaitStoppedFoundSensor as u8);
1610                } else if self.homing_timed_out() {
1611                    self.set_op_error("Software homing timeout: sensor not detected");
1612                }
1613            }
1614
1615
1616            Some(HomeState::WaitStoppedFoundSensor) => {
1617                const STABLE_WINDOW: i32 = 1;
1618                const STABLE_TICKS_REQUIRED: u8 = 10;
1619
1620                // let mut cw = RawControlWord(view.control_word());
1621                // cw.set_bit(8, true);
1622                // view.set_control_word(cw.raw());
1623
1624                let pos = view.position_actual();
1625                if (pos - self.last_raw_position).abs() <= STABLE_WINDOW {
1626                    self.halt_stable_count = self.halt_stable_count.saturating_add(1);
1627                } else {
1628                    self.halt_stable_count = 0;
1629                }
1630
1631                if self.halt_stable_count >= STABLE_TICKS_REQUIRED {
1632
1633                    log::info!("SoftHome[5] motor is stopped. Cancel move and wait for bit 12 go true.");
1634                    self.command_cancel_move(view);
1635                    self.op = AxisOp::SoftHoming(HomeState::WaitFoundSensorAck as u8);
1636
1637                } else if self.homing_timed_out() {
1638                    self.set_op_error("Software homing timeout: motor did not stop after sensor trigger");
1639                }
1640            }
1641            Some(HomeState::WaitFoundSensorAck) => {
1642                let sw = RawStatusWord(view.status_word());
1643                if sw.raw() & (1 << 12) != 0 &&  sw.raw() & (1 << 10) != 0 {
1644
1645                    log::info!("SoftHome[6]: relative move cancel ack received. Waiting before back-off...");
1646
1647                    // reset bit 4 so we're clear for the next move
1648                    let mut cw = RawControlWord(view.control_word());
1649                    cw.set_bit(4, false);  // reset new setpoint bit
1650                    cw.set_bit(5, true); // single setpoint -- flush out any previous
1651                    view.set_control_word(cw.raw());
1652
1653                    self.op = AxisOp::SoftHoming(HomeState::WaitFoundSensorAckClear as u8);
1654
1655                } else if self.homing_timed_out() {
1656                    self.set_op_error("Software homing timeout: cancel not acknowledged");
1657                }
1658            },
1659            Some(HomeState::WaitFoundSensorAckClear) => {
1660                let sw = RawStatusWord(view.status_word());
1661                // CRITICAL: Wait for the drive to acknowledge that the setpoint is gone
1662                if sw.raw() & (1 << 12) == 0 { 
1663
1664                    // turn off halt and it still shouldn't move
1665                    self.command_clear_halt(view);
1666
1667                    log::info!("SoftHome[6]: Handshake cleared (Bit 12 is LOW). Proceeding to delay.");
1668                    self.op = AxisOp::SoftHoming(HomeState::DebounceFoundSensor as u8);
1669                    self.ton.call(false, Duration::from_secs(3));
1670                }                   
1671            },
1672            // Delay before back-off (60 = wait ~1 second for drive to settle)
1673            Some(HomeState::DebounceFoundSensor) => {
1674                self.ton.call(true, Duration::from_secs(3));
1675
1676                let sw = RawStatusWord(view.status_word());
1677                if self.ton.q && sw.raw() & (1 << 12) == 0 { 
1678                    self.ton.call(false, Duration::from_secs(3));
1679                    log::info!("SoftHome[6.a.]: delay complete, starting back-off from pos={} cw=0x{:04X} sw={:04x}",
1680                    view.position_actual(), view.control_word(), view.status_word());
1681                    self.op = AxisOp::SoftHoming(HomeState::BackOff as u8);
1682                }
1683            }
1684
1685            // ── Phase 3: BACK-OFF until sensor clears ──
1686            Some(HomeState::BackOff) => {
1687
1688                let target = (self.calculate_max_relative_target(-self.soft_home_direction)) / 2;
1689                view.set_target_position(target);
1690
1691
1692                self.command_homing_speed(view);            
1693
1694                let mut cw = RawControlWord(view.control_word());
1695                cw.set_bit(4, true);  // new set-point                
1696                cw.set_bit(6, true); // relative move                
1697                cw.set_bit(13, true); // relative from current, actualy position
1698                view.set_control_word(cw.raw());
1699                log::info!("SoftHome[7]: BACK-OFF absolute target={} vel={} pos={} cw=0x{:04X}",
1700                    target, self.config.homing_speed, view.position_actual(), cw.raw());
1701                self.op = AxisOp::SoftHoming(HomeState::WaitBackingOff as u8);
1702            }
1703            Some(HomeState::WaitBackingOff) => {
1704                let sw = RawStatusWord(view.status_word());
1705                if sw.raw() & (1 << 12) != 0 {
1706                    let mut cw = RawControlWord(view.control_word());
1707                    cw.set_bit(4, false);
1708                    view.set_control_word(cw.raw());
1709                    log::info!("SoftHome[WaitBackingOff]: back-off ack received, pos={}", view.position_actual());
1710                    self.op = AxisOp::SoftHoming(HomeState::WaitLostSensor as u8);
1711                } else if self.homing_timed_out() {
1712                    self.set_op_error("Software homing timeout: back-off not acknowledged");
1713                }
1714            }
1715            Some(HomeState::WaitLostSensor) => {
1716                if !self.check_soft_home_trigger(view) {
1717                    log::info!("SoftHome[WaitLostSensor]: sensor lost at pos={}. Halting...", view.position_actual());
1718
1719                    self.command_halt(view);
1720                    self.op = AxisOp::SoftHoming(HomeState::WaitStoppedLostSensor as u8);
1721                } else if self.homing_timed_out() {
1722                    self.set_op_error("Software homing timeout: sensor did not clear during back-off");
1723                }
1724            }
1725            Some(HomeState::WaitStoppedLostSensor)  => {
1726                const STABLE_WINDOW: i32 = 1;
1727                const STABLE_TICKS_REQUIRED: u8 = 10;
1728
1729                // let mut cw = RawControlWord(view.control_word());
1730                // cw.set_bit(8, true);
1731                // view.set_control_word(cw.raw());
1732
1733                let pos = view.position_actual();
1734                if (pos - self.last_raw_position).abs() <= STABLE_WINDOW {
1735                    self.halt_stable_count = self.halt_stable_count.saturating_add(1);
1736                } else {
1737                    self.halt_stable_count = 0;
1738                }
1739
1740                if self.halt_stable_count >= STABLE_TICKS_REQUIRED {
1741                    log::info!("SoftHome[WaitStoppedLostSensor] motor is stopped. Cancel move and wait for bit 12 go true.");
1742                    self.command_cancel_move(view);
1743                    self.op = AxisOp::SoftHoming(HomeState::WaitLostSensorAck as u8);
1744                } else if self.homing_timed_out() {
1745                    self.set_op_error("Software homing timeout: motor did not stop after back-off");
1746                }
1747            }
1748            Some(HomeState::WaitLostSensorAck) => {
1749                let sw = RawStatusWord(view.status_word());
1750                if sw.raw() & (1 << 12) != 0 &&  sw.raw() & (1 << 10) != 0 {
1751
1752                    log::info!("SoftHome[WaitLostSensorAck]: relative move cancel ack received. Waiting before back-off...");
1753
1754                    // reset bit 4 so we're clear for the next move
1755                    let mut cw = RawControlWord(view.control_word());
1756                    cw.set_bit(4, false);  // reset new setpoint bit
1757                    view.set_control_word(cw.raw());
1758
1759                     self.op = AxisOp::SoftHoming(HomeState::WaitLostSensorAckClear as u8);
1760
1761
1762                } else if self.homing_timed_out() {
1763                    self.set_op_error("Software homing timeout: cancel not acknowledged");
1764                }
1765            }
1766            Some(HomeState::WaitLostSensorAckClear) => {
1767                // CRITICAL: Wait for the drive to acknowledge that the setpoint is gone
1768                let sw = RawStatusWord(view.status_word());
1769                if sw.raw() & (1 << 12) == 0 { 
1770
1771                    self.command_clear_halt(view);
1772
1773                    let desired_counts = self.config.to_counts(self.config.home_position).round() as i32;
1774                    // let current_pos = view.position_actual();
1775                    // let offset = desired_counts - current_pos;
1776                    self.homing_sdo_tid = self.sdo.write(
1777                        client, 0x607C, 0, json!(desired_counts),
1778                    );
1779
1780                    log::info!("SoftHome[WaitLostSensorAckClear]: Handshake cleared (Bit 12 is LOW). Writing home offset {} [{} counts].",
1781                        self.config.home_position, desired_counts
1782                    );
1783
1784                    self.op = AxisOp::SoftHoming(HomeState::WaitHomeOffsetDone as u8);
1785
1786                }                
1787            },
1788
1789            Some(HomeState::WaitHomeOffsetDone) => {
1790                // Wait for home offset SDO ack
1791                match self.sdo.result(client, self.homing_sdo_tid, Duration::from_secs(5)) {
1792                    SdoResult::Ok(_) => { self.op = AxisOp::SoftHoming(HomeState::WriteHomingModeOp as u8); }
1793                    SdoResult::Pending => {
1794                        if self.homing_timed_out() {
1795                            self.set_op_error("Software homing timeout: home offset SDO write");
1796                        }
1797                    }
1798                    SdoResult::Err(e) => {
1799                        self.set_op_error(&format!("Software homing SDO error: {}", e));
1800                    }
1801                    SdoResult::Timeout => {
1802                        self.set_op_error("Software homing: home offset SDO timed out");
1803                    }
1804                }
1805            },            
1806            Some(HomeState::WriteHomingModeOp) => {
1807
1808                // Switch the mode of operation into Homing Mode so that we can execute
1809                // the homing command.
1810
1811                self.fb_mode_of_operation.reset();
1812                self.fb_mode_of_operation.start(ModesOfOperation::Homing as i8);
1813                self.fb_mode_of_operation.tick(client, &mut self.sdo);
1814                self.op = AxisOp::SoftHoming(HomeState::WaitWriteHomingModeOp as u8);
1815
1816                
1817            },       
1818            Some(HomeState::WaitWriteHomingModeOp) => {
1819                // Wait for method SDO ack
1820                self.fb_mode_of_operation.tick(client, &mut self.sdo);
1821
1822                if !self.fb_mode_of_operation.is_busy() {
1823                    if self.fb_mode_of_operation.is_error() {
1824                        self.set_op_error(&format!("Software homing SDO error writing homing mode of operation: {} {}", 
1825                            self.fb_mode_of_operation.error_code(), self.fb_mode_of_operation.error_message()
1826                        ));
1827                    }
1828                    else {
1829                        log::info!("SoftHome: Drive is now in Homing Mode.");
1830                        self.op = AxisOp::SoftHoming(HomeState::WriteHomingMethod as u8);
1831                    }
1832                }
1833            },
1834            Some(HomeState::WriteHomingMethod) => {
1835                // Write homing method = CurrentPosition (default 37, configurable
1836                // via AxisConfig::soft_home_method — Inovance SV660N needs 35).
1837                self.homing_sdo_tid = self.sdo.write(
1838                    client, 0x6098, 0, json!(self.config.soft_home_method),
1839                );
1840                self.op = AxisOp::SoftHoming(HomeState::WaitWriteHomingMethodDone as u8);
1841            }
1842            Some(HomeState::WaitWriteHomingMethodDone) => {
1843                // Wait for method SDO ack
1844                match self.sdo.result(client, self.homing_sdo_tid, Duration::from_secs(5)) {
1845                    SdoResult::Ok(_) => { 
1846                        log::info!("SoftHome: Successfully wrote homing method.");
1847                        self.op = AxisOp::SoftHoming(HomeState::ClearHomingTrigger as u8); 
1848                    }
1849                    SdoResult::Pending => {
1850                        if self.homing_timed_out() {
1851                            self.restore_pp_after_error("Software homing timeout: homing method SDO write");
1852                        }
1853                    }
1854                    SdoResult::Err(e) => {
1855                        self.restore_pp_after_error(&format!("Software homing SDO error: {}", e));
1856                    }
1857                    SdoResult::Timeout => {
1858                        self.restore_pp_after_error("Software homing: homing method SDO timed out");
1859                    }
1860                }
1861            }
1862            Some(HomeState::ClearHomingTrigger) => {
1863                // Switch to homing mode and ensure CW bit 4 starts LOW, so the
1864                // next state can issue a clean rising edge the drive will see.
1865                let mut cw = RawControlWord(view.control_word());
1866                cw.set_bit(4, false);
1867                view.set_control_word(cw.raw());
1868                self.op = AxisOp::SoftHoming(HomeState::TriggerHoming as u8);
1869            }
1870            Some(HomeState::TriggerHoming) => {
1871                // Rising edge on CW bit 4 to start homing.
1872                let mut cw = RawControlWord(view.control_word());
1873                cw.set_bit(4, true);
1874                view.set_control_word(cw.raw());
1875                log::info!("SoftHome[TriggerHoming]: start homing");
1876                self.op = AxisOp::SoftHoming(HomeState::WaitHomingStarted as u8);
1877            }
1878            Some(HomeState::WaitHomingStarted) => {
1879                // Wait for the drive to clear bit 12 (Homing attained) to acknowledge
1880                // the start of homing. Without this handshake, stale bit 12 carried
1881                // over from the previous mode (e.g. PP set-point acknowledge) would
1882                // cause WaitHomingDone to pass instantly, and the drive would never
1883                // actually perform the homing method.
1884                let sw = view.status_word();
1885                let error = sw & (1 << 13) != 0;
1886                if error {
1887                    self.restore_pp_after_error("Software homing: drive reported homing error");
1888                } else if sw & (1 << 12) == 0 {
1889                    self.op = AxisOp::SoftHoming(HomeState::WaitHomingDone as u8);
1890                } else if self.homing_timed_out() {
1891                    self.restore_pp_after_error(&format!("Software homing timeout: drive did not acknowledge homing start (sw=0x{:04X})", sw));
1892                }
1893            }
1894            Some(HomeState::WaitHomingDone) => {
1895                // Wait for homing complete (bit 12 attained + bit 10 reached).
1896                let sw = view.status_word();
1897                let error    = sw & (1 << 13) != 0;
1898                let attained = sw & (1 << 12) != 0;
1899                let reached  = sw & (1 << 10) != 0;
1900
1901                if error {
1902                    self.restore_pp_after_error("Software homing: drive reported homing error");
1903                } else if attained && reached {
1904                    log::info!("SoftHome[WaitHomingDone]: homing complete (sw=0x{:04X})", sw);
1905                    self.op = AxisOp::SoftHoming(HomeState::ResetHomingTrigger as u8);
1906                } else if self.homing_timed_out() {
1907                    self.restore_pp_after_error(&format!("Software homing timeout: drive homing did not complete (sw=0x{:04X} attained={} reached={})", sw, attained, reached));
1908                }
1909            }
1910            Some(HomeState::ResetHomingTrigger) => {
1911                // Clear CW bit 4 first, in its own RxPDO cycle, so the drive sees
1912                // the falling edge *before* we change modes_of_operation away from
1913                // Homing. Changing both at once can leave the drive committing
1914                // ambiguous state.
1915                let mut cw = RawControlWord(view.control_word());
1916                cw.set_bit(4, false);
1917                view.set_control_word(cw.raw());
1918                self.op = AxisOp::SoftHoming(HomeState::WaitHomingTriggerCleared as u8);
1919            }
1920            Some(HomeState::WaitHomingTriggerCleared) => {
1921                // One tick later, switch back to PP mode and record that the drive
1922                // now owns the offset so our software-side offset is zero.
1923                self.home_offset = 0; // drive handles it now
1924                self.op = AxisOp::SoftHoming(HomeState::WriteMotionModeOfOperation as u8);
1925            }
1926
1927
1928            Some(HomeState::WriteMotionModeOfOperation) => {
1929
1930                // Switch back to PP motion mode
1931
1932                self.fb_mode_of_operation.reset();
1933                self.fb_mode_of_operation.start(ModesOfOperation::ProfilePosition as i8);
1934                self.fb_mode_of_operation.tick(client, &mut self.sdo);
1935                self.op = AxisOp::SoftHoming(HomeState::WaitWriteMotionModeOfOperation  as u8);
1936                
1937            },       
1938            Some(HomeState::WaitWriteMotionModeOfOperation) => {
1939                // Wait for method SDO ack
1940                self.fb_mode_of_operation.tick(client, &mut self.sdo);
1941
1942                if !self.fb_mode_of_operation.is_busy() {
1943                    if self.fb_mode_of_operation.is_error() {
1944                        self.set_op_error(&format!("Software homing SDO error writing homing mode of operation: {} {}", 
1945                            self.fb_mode_of_operation.error_code(), self.fb_mode_of_operation.error_message()
1946                        ));
1947                    }
1948                    else {
1949                        if self.is_error {
1950                            log::error!("Drive back in PP mode after error. Homing sequence did not complete!");
1951                            self.finish_op_error();
1952                        }
1953                        else {
1954                            // Set the target position so this drive doesn't go wandering off after homing
1955                            // changed the position
1956                            self.op = AxisOp::SoftHoming(HomeState::SendCurrentPositionTarget as u8);
1957                        }
1958                        
1959                    }
1960                }
1961            },
1962
1963            Some(HomeState::SendCurrentPositionTarget) => {
1964                // Hold position: send set-point to current position
1965                let current_pos = view.position_actual();
1966                view.set_target_position(current_pos);
1967                view.set_profile_velocity(0);
1968                let mut cw = RawControlWord(view.control_word());
1969                cw.set_bit(4, true);
1970                cw.set_bit(5, true);
1971                cw.set_bit(6, false); // absolute
1972                view.set_control_word(cw.raw());
1973                self.op = AxisOp::SoftHoming(HomeState::WaitCurrentPositionTargetSent as u8);
1974            }
1975            Some(HomeState::WaitCurrentPositionTargetSent) => {
1976                // Wait for hold ack
1977                let sw = RawStatusWord(view.status_word());
1978                if sw.raw() & (1 << 12) != 0 {
1979                    let mut cw = RawControlWord(view.control_word());
1980                    cw.set_bit(4, false);
1981                    view.set_control_word(cw.raw());
1982                    log::info!("Software homing complete — position set to {} user units",
1983                        self.config.home_position);
1984                    self.complete_op();
1985                } else if self.homing_timed_out() {
1986                    self.set_op_error("Software homing timeout: hold position not acknowledged");
1987                }
1988            }
1989            _ => self.complete_op(),
1990        }
1991    }
1992
1993    // ── Halting ──
1994    //
1995    // Three-stage close-out of the PP handshake, mirroring the soft-home
1996    // stop sequence (WaitStoppedFoundSensor → WaitFoundSensorAck →
1997    // WaitFoundSensorAckClear). Leaving any stage out results in a dirty
1998    // handshake that makes the next `move_absolute` time out at
1999    // "set-point not acknowledged."
2000    //
2001    // Step 0: (done in halt()) command_halt — bit 8 set, bit 4 cleared.
2002    // Step 1: wait for position to be stable → command_cancel_move.
2003    // Step 2: wait for SW bit 12 AND bit 10 → clear bit 4, set bit 5.
2004    // Step 3: wait for SW bit 12 to drop → Idle.
2005    fn tick_halting(&mut self, view: &mut impl AxisView, step: u8) {
2006        match HaltState::from_repr(step) {
2007            Some(HaltState::WaitStopped) => {
2008                // `update_outputs` writes `last_raw_position` at the end
2009                // of the previous tick, so this compares delta across
2010                // exactly one scan period.
2011                let pos = view.position_actual();
2012                let pos_stable = (pos - self.last_raw_position).abs() <= HALT_STABLE_WINDOW;
2013
2014                let vel = view.velocity_actual().abs();
2015                let vel_stopped = vel <= HALT_STOPPED_VELOCITY;
2016
2017                // Either signal is sufficient — position jitter during
2018                // servo hold can exceed the window even when the drive
2019                // reports ~0 velocity, and vice versa on slow drives
2020                // that report stale velocity briefly.
2021                if pos_stable || vel_stopped {
2022                    self.halt_stable_count = self.halt_stable_count.saturating_add(1);
2023                } else {
2024                    self.halt_stable_count = 0;
2025                }
2026
2027                if self.halt_stable_count >= HALT_STABLE_TICKS_REQUIRED {
2028                    self.command_cancel_move(view);
2029                    self.op_started = Some(Instant::now());
2030                    self.op = AxisOp::Halting(HaltState::WaitCancelAck as u8);
2031                } else if self.op_stage_timed_out(HALT_STAGE_TIMEOUT) {
2032                    self.set_op_error("Halt timeout: motor did not stop");
2033                }
2034            }
2035            Some(HaltState::WaitCancelAck) => {
2036                let sw = RawStatusWord(view.status_word());
2037                let setpoint_ack   = sw.raw() & (1 << 12) != 0;
2038                // let target_reached = sw.raw() & (1 << 10) != 0;
2039                if setpoint_ack /* && target_reached */  {
2040                    // Reset the rising edge for the next move; bit 5 flushes
2041                    // any queued setpoint so we start clean.
2042                    let mut cw = RawControlWord(view.control_word());
2043                    cw.set_bit(4, false);
2044                    cw.set_bit(5, true);
2045                    view.set_control_word(cw.raw());
2046                    self.op_started = Some(Instant::now());
2047                    self.op = AxisOp::Halting(HaltState::WaitCancelAckClear as u8);
2048                } else if self.op_stage_timed_out(HALT_STAGE_TIMEOUT) {
2049                    self.set_op_error("Halt timeout: cancel not acknowledged");
2050                }
2051            }
2052            Some(HaltState::WaitCancelAckClear) => {
2053                let sw = RawStatusWord(view.status_word());
2054                if sw.raw() & (1 << 12) == 0 {
2055                    // setpoint_ack dropped — drive is ready for the next move.
2056                    self.command_clear_halt(view);                    
2057                    self.complete_op();
2058                } else if self.op_stage_timed_out(HALT_STAGE_TIMEOUT) {
2059                    self.set_op_error("Halt timeout: ack did not clear");
2060                }
2061            }
2062            None => {
2063                log::warn!("Axis halt: unknown sub-step {}, forcing idle", step);
2064                self.complete_op();
2065            }
2066        }
2067    }
2068
2069    // ── Fault Recovery ──
2070    // Step 0: (done in reset_faults()) clear bit 7
2071    // Step 1: assert bit 7 (fault reset rising edge)
2072    // Step 2: wait fault cleared → Idle
2073    fn tick_fault_recovery(&mut self, view: &mut impl AxisView, step: u8) {
2074        match step {
2075            1 => {
2076                // Assert fault reset (rising edge on bit 7)
2077                let mut cw = RawControlWord(view.control_word());
2078                cw.cmd_fault_reset();
2079                view.set_control_word(cw.raw());
2080                self.op = AxisOp::FaultRecovery(2);
2081            }
2082            2 => {
2083                // Wait for fault to clear
2084                let sw = RawStatusWord(view.status_word());
2085                let state = sw.state();
2086                if !matches!(state, Cia402State::Fault | Cia402State::FaultReactionActive) {
2087                    log::info!("Fault cleared (drive state: {})", state);
2088                    self.complete_op();
2089                } else if self.op_timed_out() {
2090                    self.set_op_error("Fault reset timeout: drive still faulted");
2091                }
2092            }
2093            _ => self.complete_op(),
2094        }
2095    }
2096}
2097
2098// ──────────────────────────────────────────────
2099// Tests
2100// ──────────────────────────────────────────────
2101
2102#[cfg(test)]
2103mod tests {
2104    use super::*;
2105
2106    /// Mock AxisView for testing.
2107    struct MockView {
2108        control_word: u16,
2109        status_word: u16,
2110        target_position: i32,
2111        profile_velocity: u32,
2112        profile_acceleration: u32,
2113        profile_deceleration: u32,
2114        modes_of_operation: i8,
2115        modes_of_operation_display: i8,
2116        position_actual: i32,
2117        velocity_actual: i32,
2118        error_code: u16,
2119        positive_limit: bool,
2120        negative_limit: bool,
2121        home_sensor: bool,
2122    }
2123
2124    impl MockView {
2125        fn new() -> Self {
2126            Self {
2127                control_word: 0,
2128                status_word: 0x0040, // SwitchOnDisabled
2129                target_position: 0,
2130                profile_velocity: 0,
2131                profile_acceleration: 0,
2132                profile_deceleration: 0,
2133                modes_of_operation: 0,
2134                modes_of_operation_display: 1, // PP
2135                position_actual: 0,
2136                velocity_actual: 0,
2137                error_code: 0,
2138                positive_limit: false,
2139                negative_limit: false,
2140                home_sensor: false,
2141            }
2142        }
2143
2144        fn set_state(&mut self, state: u16) {
2145            self.status_word = state;
2146        }
2147    }
2148
2149    impl AxisView for MockView {
2150        fn control_word(&self) -> u16 { self.control_word }
2151        fn set_control_word(&mut self, word: u16) { self.control_word = word; }
2152        fn set_target_position(&mut self, pos: i32) { self.target_position = pos; }
2153        fn set_profile_velocity(&mut self, vel: u32) { self.profile_velocity = vel; }
2154        fn set_profile_acceleration(&mut self, accel: u32) { self.profile_acceleration = accel; }
2155        fn set_profile_deceleration(&mut self, decel: u32) { self.profile_deceleration = decel; }
2156        fn set_modes_of_operation(&mut self, mode: i8) { self.modes_of_operation = mode; }
2157        fn modes_of_operation_display(&self) -> i8 { self.modes_of_operation_display }
2158        fn status_word(&self) -> u16 { self.status_word }
2159        fn position_actual(&self) -> i32 { self.position_actual }
2160        fn velocity_actual(&self) -> i32 { self.velocity_actual }
2161        fn error_code(&self) -> u16 { self.error_code }
2162        fn positive_limit_active(&self) -> bool { self.positive_limit }
2163        fn negative_limit_active(&self) -> bool { self.negative_limit }
2164        fn home_sensor_active(&self) -> bool { self.home_sensor }
2165    }
2166
2167    fn test_config() -> AxisConfig {
2168        AxisConfig::new(12_800).with_user_scale(360.0)
2169    }
2170
2171    /// Helper: create axis + mock client channels.
2172    fn test_axis() -> (Axis, CommandClient, tokio::sync::mpsc::UnboundedSender<mechutil::ipc::CommandMessage>, tokio::sync::mpsc::UnboundedReceiver<String>) {
2173        use tokio::sync::mpsc;
2174        let (write_tx, write_rx) = mpsc::unbounded_channel();
2175        let (response_tx, response_rx) = mpsc::unbounded_channel();
2176        let client = CommandClient::new(write_tx, response_rx);
2177        let axis = Axis::new(test_config(), "TestDrive");
2178        (axis, client, response_tx, write_rx)
2179    }
2180
2181    #[test]
2182    fn axis_config_conversion() {
2183        let cfg = test_config();
2184        // 45 degrees = 1600 counts
2185        assert!((cfg.to_counts(45.0) - 1600.0).abs() < 0.01);
2186    }
2187
2188    #[test]
2189    fn enable_sequence_sets_pp_mode_and_shutdown() {
2190        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2191        let mut view = MockView::new();
2192
2193        axis.enable(&mut view);
2194
2195        // Should have set PP mode
2196        assert_eq!(view.modes_of_operation, ModesOfOperation::ProfilePosition.as_i8());
2197        // Should have issued shutdown command (bits 1,2 set; 0,3,7 clear)
2198        assert_eq!(view.control_word & 0x008F, 0x0006);
2199        // Should be in Enabling state
2200        assert_eq!(axis.op, AxisOp::Enabling(1));
2201
2202        // Simulate drive reaching ReadyToSwitchOn
2203        view.set_state(0x0021); // ReadyToSwitchOn
2204        axis.tick(&mut view, &mut client);
2205
2206        // Should have issued enable_operation (bits 0-3 set; 7 clear)
2207        assert_eq!(view.control_word & 0x008F, 0x000F);
2208        assert_eq!(axis.op, AxisOp::Enabling(2));
2209
2210        // Simulate drive reaching OperationEnabled
2211        view.set_state(0x0027); // OperationEnabled
2212        axis.tick(&mut view, &mut client);
2213
2214        // Should be idle now, motor_on = true
2215        assert_eq!(axis.op, AxisOp::Idle);
2216        assert!(axis.motor_on);
2217    }
2218
2219    #[test]
2220    fn move_absolute_sets_target() {
2221        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2222        let mut view = MockView::new();
2223        view.set_state(0x0027); // OperationEnabled
2224        axis.tick(&mut view, &mut client); // update outputs
2225
2226        // Move to 45 degrees at 90 deg/s, 180 deg/s² accel/decel
2227        axis.move_absolute(&mut view, 45.0, 90.0, 180.0, 180.0);
2228
2229        // Target should be ~1600 counts (45° at 12800 cpr / 360°)
2230        assert_eq!(view.target_position, 1600);
2231        // Velocity: 90 deg/s * (12800/360) ≈ 3200 counts/s
2232        assert_eq!(view.profile_velocity, 3200);
2233        // Accel: 180 deg/s² * (12800/360) ≈ 6400 counts/s²
2234        assert_eq!(view.profile_acceleration, 6400);
2235        assert_eq!(view.profile_deceleration, 6400);
2236        // Bit 4 (new set-point) should be set
2237        assert!(view.control_word & (1 << 4) != 0);
2238        // Bit 6 (relative) should be clear for absolute move
2239        assert!(view.control_word & (1 << 6) == 0);
2240        // Should be in Moving state
2241        assert!(matches!(axis.op, AxisOp::Moving(MoveKind::Absolute, 1, _, _)));
2242    }
2243
2244    #[test]
2245    fn move_relative_sets_relative_bit() {
2246        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2247        let mut view = MockView::new();
2248        view.set_state(0x0027);
2249        axis.tick(&mut view, &mut client);
2250
2251        axis.move_relative(&mut view, 10.0, 90.0, 180.0, 180.0);
2252
2253        // Bit 6 (relative) should be set
2254        assert!(view.control_word & (1 << 6) != 0);
2255        assert!(matches!(axis.op, AxisOp::Moving(MoveKind::Relative, 1, _, _)));
2256    }
2257
2258    #[test]
2259    fn move_completes_on_target_reached() {
2260        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2261        let mut view = MockView::new();
2262        view.set_state(0x0027); // OperationEnabled
2263        axis.tick(&mut view, &mut client);
2264
2265        axis.move_absolute(&mut view, 45.0, 90.0, 180.0, 180.0);
2266
2267        // Step 1: simulate set-point acknowledge (bit 12)
2268        view.status_word = 0x1027; // OperationEnabled + bit 12
2269        axis.tick(&mut view, &mut client);
2270        // Should have cleared bit 4
2271        assert!(view.control_word & (1 << 4) == 0);
2272
2273        // Step 2: simulate target reached (bit 10)
2274        view.status_word = 0x0427; // OperationEnabled + bit 10
2275        axis.tick(&mut view, &mut client);
2276        // Should be idle now
2277        assert_eq!(axis.op, AxisOp::Idle);
2278        assert!(!axis.in_motion);
2279    }
2280
2281    #[test]
2282    fn fault_detected_sets_error() {
2283        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2284        let mut view = MockView::new();
2285        view.set_state(0x0008); // Fault
2286        view.error_code = 0x1234;
2287
2288        axis.tick(&mut view, &mut client);
2289
2290        assert!(axis.is_error);
2291        assert_eq!(axis.error_code, 0x1234);
2292        assert!(axis.error_message.contains("fault"));
2293    }
2294
2295    #[test]
2296    fn fault_recovery_sequence() {
2297        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2298        let mut view = MockView::new();
2299        view.set_state(0x0008); // Fault
2300
2301        axis.reset_faults(&mut view);
2302        // Step 0: bit 7 should be cleared
2303        assert!(view.control_word & 0x0080 == 0);
2304
2305        // Step 1: tick should assert bit 7
2306        axis.tick(&mut view, &mut client);
2307        assert!(view.control_word & 0x0080 != 0);
2308
2309        // Step 2: simulate fault cleared → SwitchOnDisabled
2310        view.set_state(0x0040);
2311        axis.tick(&mut view, &mut client);
2312        assert_eq!(axis.op, AxisOp::Idle);
2313        assert!(!axis.is_error);
2314    }
2315
2316    #[test]
2317    fn reset_faults_software_only_is_synchronous() {
2318        // When the axis hits a software error (e.g. target outside the
2319        // software limits) the drive stays in OperationEnabled — there is
2320        // no CiA 402 fault to clear. reset_faults() must return the axis
2321        // to Idle on the same call without engaging the drive's bit-7
2322        // fault-reset handshake.
2323        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2324        let mut view = MockView::new();
2325        view.set_state(0x0027); // OperationEnabled
2326        axis.tick(&mut view, &mut client);
2327
2328        // Trigger a software-only error via target outside software limit.
2329        axis.set_software_min_limit(207.0);
2330        axis.move_absolute(&mut view, 200.0, 90.0, 180.0, 180.0);
2331        assert!(axis.is_error, "move past software limit must set is_error");
2332        assert!(!axis.is_busy);
2333
2334        // Capture cw before reset. reset_faults must not modify it for
2335        // a software-only error (drive is healthy, no handshake needed).
2336        let cw_before = view.control_word;
2337
2338        axis.reset_faults(&mut view);
2339
2340        // Synchronous: error cleared, axis already Idle, not busy.
2341        assert!(!axis.is_error);
2342        assert_eq!(axis.error_code, 0);
2343        assert!(axis.error_message.is_empty());
2344        assert!(!axis.is_busy);
2345        assert_eq!(axis.op, AxisOp::Idle);
2346        // No drive handshake — control word untouched.
2347        assert_eq!(view.control_word, cw_before);
2348    }
2349
2350    #[test]
2351    fn disable_sequence() {
2352        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2353        let mut view = MockView::new();
2354        view.set_state(0x0027); // OperationEnabled
2355
2356        axis.disable(&mut view);
2357        // Should have sent shutdown command
2358        assert_eq!(view.control_word & 0x008F, 0x0006);
2359
2360        // Simulate drive leaving OperationEnabled
2361        view.set_state(0x0021); // ReadyToSwitchOn
2362        axis.tick(&mut view, &mut client);
2363        assert_eq!(axis.op, AxisOp::Idle);
2364    }
2365
2366    #[test]
2367    fn position_tracks_with_home_offset() {
2368        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2369        let mut view = MockView::new();
2370        view.set_state(0x0027);
2371        view.position_actual = 5000;
2372
2373        // Enable to capture home offset
2374        axis.enable(&mut view);
2375        view.set_state(0x0021);
2376        axis.tick(&mut view, &mut client);
2377        view.set_state(0x0027);
2378        axis.tick(&mut view, &mut client);
2379
2380        // Home offset should be 5000
2381        assert_eq!(axis.home_offset, 5000);
2382
2383        // Position should be 0 (at home)
2384        assert!((axis.position - 0.0).abs() < 0.01);
2385
2386        // Move actual position to 5000 + 1600 = 6600
2387        view.position_actual = 6600;
2388        axis.tick(&mut view, &mut client);
2389
2390        // Should read as 45 degrees
2391        assert!((axis.position - 45.0).abs() < 0.1);
2392    }
2393
2394    #[test]
2395    fn set_position_adjusts_home_offset() {
2396        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2397        let mut view = MockView::new();
2398        view.position_actual = 3200;
2399
2400        axis.set_position(&view, 90.0);
2401        axis.tick(&mut view, &mut client);
2402
2403        // home_offset = 3200 - to_counts(90.0) = 3200 - 3200 = 0
2404        assert_eq!(axis.home_offset, 0);
2405        assert!((axis.position - 90.0).abs() < 0.01);
2406    }
2407
2408    #[test]
2409    fn halt_runs_multi_stage_close_out() {
2410        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2411        let mut view = MockView::new();
2412        view.set_state(0x0027);
2413
2414        axis.halt(&mut view);
2415
2416        // halt() sets CW bit 8 (halt) and clears CW bit 4 (new setpoint).
2417        assert!(view.control_word & (1 << 8) != 0, "halt bit must be set");
2418        assert!(view.control_word & (1 << 4) == 0, "new_setpoint must be cleared");
2419
2420        // Halt is a multi-stage process. The first stage is WaitStopped.
2421        assert!(matches!(axis.op, AxisOp::Halting(_)),
2422                "halt should enter Halting state, not Idle");
2423        let AxisOp::Halting(step) = axis.op.clone() else { unreachable!() };
2424        assert_eq!(step, HaltState::WaitStopped as u8);
2425
2426        // Tick alone does NOT immediately return to Idle anymore —
2427        // WaitStopped polls position stability (5 consecutive ticks
2428        // with position stable or velocity near zero). With a static
2429        // MockView, position stays at 0 and velocity stays at 0, so
2430        // vel_stopped is true every tick → the counter accumulates
2431        // and the state progresses after HALT_STABLE_TICKS_REQUIRED.
2432        for _ in 0..HALT_STABLE_TICKS_REQUIRED {
2433            axis.tick(&mut view, &mut client);
2434        }
2435        // After enough stable ticks we advance to WaitCancelAck.
2436        assert!(matches!(axis.op, AxisOp::Halting(_)));
2437        let AxisOp::Halting(step) = axis.op.clone() else { unreachable!() };
2438        assert_eq!(step, HaltState::WaitCancelAck as u8,
2439                   "should advance past WaitStopped once position/velocity is stable");
2440
2441        // axis.is_busy must remain true through the whole halt sequence so
2442        // callers like MoveToLoad don't see "stopped" until the PP state
2443        // is fully cleaned up.
2444        assert!(axis.is_busy, "is_busy must stay true across Halting stages");
2445    }
2446
2447    #[test]
2448    fn is_busy_tracks_operations() {
2449        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2450        let mut view = MockView::new();
2451
2452        // Idle — not busy
2453        axis.tick(&mut view, &mut client);
2454        assert!(!axis.is_busy);
2455
2456        // Enable — busy
2457        axis.enable(&mut view);
2458        axis.tick(&mut view, &mut client);
2459        assert!(axis.is_busy);
2460
2461        // Complete enable
2462        view.set_state(0x0021);
2463        axis.tick(&mut view, &mut client);
2464        view.set_state(0x0027);
2465        axis.tick(&mut view, &mut client);
2466        assert!(!axis.is_busy);
2467
2468        // Move — busy
2469        axis.move_absolute(&mut view, 45.0, 90.0, 180.0, 180.0);
2470        axis.tick(&mut view, &mut client);
2471        assert!(axis.is_busy);
2472        assert!(axis.in_motion);
2473    }
2474
2475    #[test]
2476    fn fault_during_move_cancels_op() {
2477        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2478        let mut view = MockView::new();
2479        view.set_state(0x0027); // OperationEnabled
2480        axis.tick(&mut view, &mut client);
2481
2482        // Start a move
2483        axis.move_absolute(&mut view, 45.0, 90.0, 180.0, 180.0);
2484        axis.tick(&mut view, &mut client);
2485        assert!(axis.is_busy);
2486        assert!(!axis.is_error);
2487
2488        // Fault occurs mid-move
2489        view.set_state(0x0008); // Fault
2490        axis.tick(&mut view, &mut client);
2491
2492        // is_busy should be false, is_error should be true
2493        assert!(!axis.is_busy);
2494        assert!(axis.is_error);
2495        assert_eq!(axis.op, AxisOp::Idle);
2496    }
2497
2498    #[test]
2499    fn move_absolute_rejected_by_max_limit() {
2500        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2501        let mut view = MockView::new();
2502        view.set_state(0x0027);
2503        axis.tick(&mut view, &mut client);
2504
2505        axis.set_software_max_limit(90.0);
2506        axis.move_absolute(&mut view, 100.0, 90.0, 180.0, 180.0);
2507
2508        // Should not have started a move — error instead
2509        assert!(axis.is_error);
2510        assert_eq!(axis.op, AxisOp::Idle);
2511        assert!(axis.error_message.contains("max software limit"));
2512    }
2513
2514    #[test]
2515    fn move_absolute_rejected_by_min_limit() {
2516        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2517        let mut view = MockView::new();
2518        view.set_state(0x0027);
2519        axis.tick(&mut view, &mut client);
2520
2521        axis.set_software_min_limit(-10.0);
2522        axis.move_absolute(&mut view, -20.0, 90.0, 180.0, 180.0);
2523
2524        assert!(axis.is_error);
2525        assert_eq!(axis.op, AxisOp::Idle);
2526        assert!(axis.error_message.contains("min software limit"));
2527    }
2528
2529    #[test]
2530    fn move_relative_rejected_by_max_limit() {
2531        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2532        let mut view = MockView::new();
2533        view.set_state(0x0027);
2534        axis.tick(&mut view, &mut client);
2535
2536        // Position is 0, max limit 50 — relative move of +60 should be rejected
2537        axis.set_software_max_limit(50.0);
2538        axis.move_relative(&mut view, 60.0, 90.0, 180.0, 180.0);
2539
2540        assert!(axis.is_error);
2541        assert_eq!(axis.op, AxisOp::Idle);
2542        assert!(axis.error_message.contains("max software limit"));
2543    }
2544
2545    #[test]
2546    fn move_within_limits_allowed() {
2547        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2548        let mut view = MockView::new();
2549        view.set_state(0x0027);
2550        axis.tick(&mut view, &mut client);
2551
2552        axis.set_software_max_limit(90.0);
2553        axis.set_software_min_limit(-90.0);
2554        axis.move_absolute(&mut view, 45.0, 90.0, 180.0, 180.0);
2555
2556        // Should have started normally
2557        assert!(!axis.is_error);
2558        assert!(matches!(axis.op, AxisOp::Moving(MoveKind::Absolute, 1, _, _)));
2559    }
2560
2561    #[test]
2562    fn runtime_limit_halts_move_in_violated_direction() {
2563        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2564        let mut view = MockView::new();
2565        view.set_state(0x0027);
2566        axis.tick(&mut view, &mut client);
2567
2568        axis.set_software_max_limit(45.0);
2569        // Start a move to exactly the limit (allowed)
2570        axis.move_absolute(&mut view, 45.0, 90.0, 180.0, 180.0);
2571
2572        // Simulate the drive overshooting past 45° (position_actual in counts)
2573        // home_offset is 0, so 1650 counts = 46.4°
2574        view.position_actual = 1650;
2575        view.velocity_actual = 100; // moving positive
2576
2577        // Simulate set-point ack so we're in Moving step 2
2578        view.status_word = 0x1027;
2579        axis.tick(&mut view, &mut client);
2580        view.status_word = 0x0027;
2581        axis.tick(&mut view, &mut client);
2582
2583        // Should have halted cleanly without error. The axis enters the
2584        // Halting close-out (caller waits for !is_busy then checks
2585        // at_max_limit).
2586        assert!(!axis.is_error);
2587        assert!(axis.at_max_limit);
2588        assert!(axis.is_busy);
2589        assert!(matches!(axis.op, AxisOp::Halting(_)));
2590        // Halt bit (bit 8) should be set
2591        assert!(view.control_word & (1 << 8) != 0);
2592    }
2593
2594    #[test]
2595    fn runtime_limit_allows_move_in_opposite_direction() {
2596        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2597        let mut view = MockView::new();
2598        view.set_state(0x0027);
2599        // Start at 50° (past max limit)
2600        view.position_actual = 1778; // ~50°
2601        axis.set_software_max_limit(45.0);
2602        axis.tick(&mut view, &mut client);
2603        assert!(axis.at_max_limit);
2604
2605        // Move back toward 0 — should be allowed even though at max limit
2606        axis.move_absolute(&mut view, 0.0, 90.0, 180.0, 180.0);
2607        assert!(!axis.is_error);
2608        assert!(matches!(axis.op, AxisOp::Moving(MoveKind::Absolute, 1, _, _)));
2609
2610        // Simulate moving negative — limit check should not halt
2611        view.velocity_actual = -100;
2612        view.status_word = 0x1027; // ack
2613        axis.tick(&mut view, &mut client);
2614        // Still moving, no error from limit
2615        assert!(!axis.is_error);
2616    }
2617
2618    #[test]
2619    fn positive_limit_switch_halts_positive_move() {
2620        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2621        let mut view = MockView::new();
2622        view.set_state(0x0027);
2623        axis.tick(&mut view, &mut client);
2624
2625        // Start a move in the positive direction
2626        axis.move_absolute(&mut view, 45.0, 90.0, 180.0, 180.0);
2627        view.velocity_actual = 100; // moving positive
2628        // Simulate set-point ack so we're in Moving step 2
2629        view.status_word = 0x1027;
2630        axis.tick(&mut view, &mut client);
2631        view.status_word = 0x0027;
2632
2633        // Now the positive limit switch trips
2634        view.positive_limit = true;
2635        axis.tick(&mut view, &mut client);
2636
2637        // Limit hit during a regular move enters the Halting close-out:
2638        // no error, halt bit asserted, drive's queued setpoint will be
2639        // canceled and halt cleared before the axis returns to Idle.
2640        // Caller waits for !is_busy then inspects at_positive_limit_switch.
2641        assert!(!axis.is_error);
2642        assert!(axis.at_positive_limit_switch);
2643        assert!(axis.is_busy);
2644        assert!(matches!(axis.op, AxisOp::Halting(_)));
2645        // Halt bit should be set
2646        assert!(view.control_word & (1 << 8) != 0);
2647    }
2648
2649    #[test]
2650    fn negative_limit_switch_halts_negative_move() {
2651        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2652        let mut view = MockView::new();
2653        view.set_state(0x0027);
2654        axis.tick(&mut view, &mut client);
2655
2656        // Start a move in the negative direction
2657        axis.move_absolute(&mut view, -45.0, 90.0, 180.0, 180.0);
2658        view.velocity_actual = -100; // moving negative
2659        view.status_word = 0x1027;
2660        axis.tick(&mut view, &mut client);
2661        view.status_word = 0x0027;
2662
2663        // Negative limit switch trips
2664        view.negative_limit = true;
2665        axis.tick(&mut view, &mut client);
2666
2667        // Limit hit during a regular move enters the Halting close-out:
2668        // no error, halt bit asserted, drive's queued setpoint will be
2669        // canceled and halt cleared before the axis returns to Idle.
2670        // Caller waits for !is_busy then inspects at_negative_limit_switch.
2671        assert!(!axis.is_error);
2672        assert!(axis.at_negative_limit_switch);
2673        assert!(axis.is_busy);
2674        assert!(matches!(axis.op, AxisOp::Halting(_)));
2675        assert!(view.control_word & (1 << 8) != 0);
2676    }
2677
2678    #[test]
2679    fn limit_halt_cleanup_then_next_move_starts_cleanly() {
2680        // After a limit-switch halt, the axis must run the full Halting
2681        // close-out (motor stop → cancel queued setpoint → clear halt) so
2682        // the drive ends in a clean state. The next move then starts via
2683        // the normal one-tick start_move path.
2684        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2685        let mut view = MockView::new();
2686        view.set_state(0x0027); // OperationEnabled
2687        axis.tick(&mut view, &mut client);
2688
2689        // Drive an initial move into a positive limit.
2690        axis.move_absolute(&mut view, 45.0, 90.0, 180.0, 180.0);
2691        view.velocity_actual = 100;
2692        view.status_word = 0x1027;
2693        axis.tick(&mut view, &mut client);
2694        view.status_word = 0x0027;
2695        view.positive_limit = true;
2696        axis.tick(&mut view, &mut client);
2697
2698        // Limit halt: in Halting close-out, halt asserted, busy, no error.
2699        assert!(!axis.is_error);
2700        assert!(axis.is_busy);
2701        assert!(matches!(axis.op, AxisOp::Halting(_)));
2702        assert!(view.control_word & (1 << 8) != 0);
2703
2704        // Drive the close-out to completion. WaitStopped polls position
2705        // stability; with static MockView (vel=0 after we clear it), it
2706        // accumulates HALT_STABLE_TICKS_REQUIRED and advances to
2707        // WaitCancelAck.
2708        view.velocity_actual = 0;
2709        view.positive_limit = false;
2710        for _ in 0..HALT_STABLE_TICKS_REQUIRED {
2711            axis.tick(&mut view, &mut client);
2712        }
2713        assert!(matches!(axis.op, AxisOp::Halting(_)));
2714        let AxisOp::Halting(step) = axis.op.clone() else { unreachable!() };
2715        assert_eq!(step, HaltState::WaitCancelAck as u8);
2716
2717        // WaitCancelAck: drive acks the cancel. We mock by setting bit 12.
2718        view.status_word = 0x1027;
2719        axis.tick(&mut view, &mut client);
2720        let AxisOp::Halting(step) = axis.op.clone() else { unreachable!() };
2721        assert_eq!(step, HaltState::WaitCancelAckClear as u8);
2722
2723        // WaitCancelAckClear: drive drops bit 12. Halting completes,
2724        // halt bit cleared, op back to Idle.
2725        view.status_word = 0x0027;
2726        axis.tick(&mut view, &mut client);
2727        assert_eq!(axis.op, AxisOp::Idle);
2728        assert!(!axis.is_busy);
2729        assert!(view.control_word & (1 << 8) == 0, "halt should be cleared after close-out");
2730
2731        // Now a fresh move should start via the normal one-tick path —
2732        // bit 4 asserted immediately, op at step 1.
2733        axis.move_absolute(&mut view, 30.0, 90.0, 180.0, 180.0);
2734        assert!(view.control_word & (1 << 4) != 0, "new-setpoint asserted on start_move tick");
2735        assert!(view.control_word & (1 << 8) == 0, "halt stays cleared");
2736        assert!(matches!(axis.op, AxisOp::Moving(MoveKind::Absolute, 1, _, _)));
2737    }
2738
2739    #[test]
2740    fn limit_switch_allows_move_in_opposite_direction() {
2741        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2742        let mut view = MockView::new();
2743        view.set_state(0x0027);
2744        // Positive limit is active, but we're moving negative (retreating)
2745        view.positive_limit = true;
2746        view.velocity_actual = -100;
2747        axis.tick(&mut view, &mut client);
2748        assert!(axis.at_positive_limit_switch);
2749
2750        // Move in the negative direction should be allowed
2751        axis.move_absolute(&mut view, -10.0, 90.0, 180.0, 180.0);
2752        view.status_word = 0x1027;
2753        axis.tick(&mut view, &mut client);
2754
2755        // Should still be moving, no error
2756        assert!(!axis.is_error);
2757        assert!(matches!(axis.op, AxisOp::Moving(_, _, _, _)));
2758    }
2759
2760    #[test]
2761    fn limit_switch_ignored_when_not_moving() {
2762        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2763        let mut view = MockView::new();
2764        view.set_state(0x0027);
2765        view.positive_limit = true;
2766
2767        axis.tick(&mut view, &mut client);
2768
2769        // Output flag is set, but no error since we're not moving
2770        assert!(axis.at_positive_limit_switch);
2771        assert!(!axis.is_error);
2772    }
2773
2774    #[test]
2775    fn home_sensor_output_tracks_view() {
2776        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2777        let mut view = MockView::new();
2778        view.set_state(0x0027);
2779
2780        axis.tick(&mut view, &mut client);
2781        assert!(!axis.home_sensor);
2782
2783        view.home_sensor = true;
2784        axis.tick(&mut view, &mut client);
2785        assert!(axis.home_sensor);
2786
2787        view.home_sensor = false;
2788        axis.tick(&mut view, &mut client);
2789        assert!(!axis.home_sensor);
2790    }
2791
2792    #[test]
2793    fn velocity_output_converted() {
2794        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
2795        let mut view = MockView::new();
2796        view.set_state(0x0027);
2797        // 3200 counts/s = 90 deg/s
2798        view.velocity_actual = 3200;
2799
2800        axis.tick(&mut view, &mut client);
2801
2802        assert!((axis.speed - 90.0).abs() < 0.1);
2803        assert!(axis.moving_positive);
2804        assert!(!axis.moving_negative);
2805    }
2806
2807    // ── Software homing tests ──
2808
2809    fn soft_homing_config() -> AxisConfig {
2810        let mut cfg = AxisConfig::new(12_800).with_user_scale(360.0);
2811        cfg.homing_speed = 10.0;
2812        cfg.homing_accel = 20.0;
2813        cfg.homing_decel = 20.0;
2814        cfg
2815    }
2816
2817    fn soft_homing_axis() -> (Axis, CommandClient, tokio::sync::mpsc::UnboundedSender<mechutil::ipc::CommandMessage>, tokio::sync::mpsc::UnboundedReceiver<String>) {
2818        use tokio::sync::mpsc;
2819        let (write_tx, write_rx) = mpsc::unbounded_channel();
2820        let (response_tx, response_rx) = mpsc::unbounded_channel();
2821        let client = CommandClient::new(write_tx, response_rx);
2822        let axis = Axis::new(soft_homing_config(), "TestDrive");
2823        (axis, client, response_tx, write_rx)
2824    }
2825
2826    /// Helper: enable the axis and put it in OperationEnabled state.
2827    fn enable_axis(axis: &mut Axis, view: &mut MockView, client: &mut CommandClient) {
2828        view.set_state(0x0027); // OperationEnabled
2829        axis.tick(view, client);
2830    }
2831
2832    /// Helper: drive the soft homing state machine through phases 2-4
2833    /// (halt, back-off, set home). Call after sensor triggers (step 4).
2834    /// `trigger_pos`: position where sensor triggered
2835    /// `clear_sensor`: closure to deactivate the sensor on the view
2836    fn complete_soft_homing(
2837        axis: &mut Axis,
2838        view: &mut MockView,
2839        client: &mut CommandClient,
2840        resp_tx: &tokio::sync::mpsc::UnboundedSender<mechutil::ipc::CommandMessage>,
2841        trigger_pos: i32,
2842        clear_sensor: impl FnOnce(&mut MockView),
2843    ) {
2844        use mechutil::ipc::CommandMessage as IpcMsg;
2845
2846        // Phase 2: HALT (steps 4-6)
2847        // Step 4: halt
2848        axis.tick(view, client);
2849        assert!(matches!(axis.op, AxisOp::SoftHoming(5)));
2850
2851        // Step 5: motor decelerating then stopped
2852        view.position_actual = trigger_pos + 100;
2853        axis.tick(view, client);
2854        view.position_actual = trigger_pos + 120;
2855        axis.tick(view, client);
2856        // 10 stable ticks
2857        for _ in 0..10 { axis.tick(view, client); }
2858        assert!(matches!(axis.op, AxisOp::SoftHoming(6)));
2859
2860        // Step 6: cancel ack → delay step 60
2861        view.status_word = 0x1027;
2862        axis.tick(view, client);
2863        assert!(matches!(axis.op, AxisOp::SoftHoming(60)));
2864        view.status_word = 0x0027;
2865
2866        // Step 60: delay (~1 second = 100 ticks)
2867        for _ in 0..100 { axis.tick(view, client); }
2868        assert!(matches!(axis.op, AxisOp::SoftHoming(7)));
2869
2870        // Phase 3: BACK-OFF (steps 7-11)
2871        // Step 7: start back-off move
2872        axis.tick(view, client);
2873        assert!(matches!(axis.op, AxisOp::SoftHoming(8)));
2874
2875        // Step 8: ack
2876        view.status_word = 0x1027;
2877        axis.tick(view, client);
2878        assert!(matches!(axis.op, AxisOp::SoftHoming(9)));
2879        view.status_word = 0x0027;
2880
2881        // Step 9: sensor still active, then clears
2882        axis.tick(view, client);
2883        assert!(matches!(axis.op, AxisOp::SoftHoming(9)));
2884        clear_sensor(view);
2885        view.position_actual = trigger_pos - 200;
2886        axis.tick(view, client);
2887        assert!(matches!(axis.op, AxisOp::SoftHoming(10)));
2888
2889        // Step 10-11: halt after back-off, wait stable
2890        axis.tick(view, client);
2891        assert!(matches!(axis.op, AxisOp::SoftHoming(11)));
2892        for _ in 0..10 { axis.tick(view, client); }
2893        assert!(matches!(axis.op, AxisOp::SoftHoming(12)));
2894
2895        // Phase 4: SET HOME (steps 12-19)
2896        // Step 12: cancel ack + SDO write home offset
2897        view.status_word = 0x1027;
2898        axis.tick(view, client);
2899        view.status_word = 0x0027;
2900        assert!(matches!(axis.op, AxisOp::SoftHoming(13)));
2901
2902        // Step 13: SDO ack for home offset
2903        let tid = axis.homing_sdo_tid;
2904        resp_tx.send(IpcMsg::response(tid, json!(null))).unwrap();
2905        client.poll();
2906        axis.tick(view, client);
2907        assert!(matches!(axis.op, AxisOp::SoftHoming(14)));
2908
2909        // Step 14→15: SDO write homing method, ack
2910        axis.tick(view, client);
2911        let tid = axis.homing_sdo_tid;
2912        resp_tx.send(IpcMsg::response(tid, json!(null))).unwrap();
2913        client.poll();
2914        axis.tick(view, client);
2915        assert!(matches!(axis.op, AxisOp::SoftHoming(16)));
2916
2917        // Step 16: switch to homing mode + trigger
2918        view.modes_of_operation_display = ModesOfOperation::Homing.as_i8();
2919        axis.tick(view, client);
2920        assert!(matches!(axis.op, AxisOp::SoftHoming(17)));
2921
2922        // Step 17: homing complete (attained + reached)
2923        view.status_word = 0x1427; // bit 12 + bit 10
2924        axis.tick(view, client);
2925        assert!(matches!(axis.op, AxisOp::SoftHoming(18)));
2926        view.modes_of_operation_display = ModesOfOperation::ProfilePosition.as_i8();
2927        view.status_word = 0x0027;
2928
2929        // Step 18: hold position
2930        axis.tick(view, client);
2931        assert!(matches!(axis.op, AxisOp::SoftHoming(19)));
2932
2933        // Step 19: hold ack
2934        view.status_word = 0x1027;
2935        axis.tick(view, client);
2936        view.status_word = 0x0027;
2937
2938        assert_eq!(axis.op, AxisOp::Idle);
2939        assert!(!axis.is_busy);
2940        assert!(!axis.is_error);
2941        assert_eq!(axis.home_offset, 0); // drive handles it now
2942    }
2943
2944    #[test]
2945    fn soft_homing_pnp_home_sensor_full_sequence() {
2946        let (mut axis, mut client, resp_tx, _write_rx) = soft_homing_axis();
2947        let mut view = MockView::new();
2948        enable_axis(&mut axis, &mut view, &mut client);
2949
2950        axis.home(&mut view, HomingMethod::HomeSensorPosPnp);
2951
2952        // Phase 1: search
2953        axis.tick(&mut view, &mut client); // step 0→1
2954        view.status_word = 0x1027;
2955        axis.tick(&mut view, &mut client); // step 1→2 (ack)
2956        view.status_word = 0x0027;
2957        axis.tick(&mut view, &mut client); // step 2→3
2958
2959        // Sensor triggers
2960        view.home_sensor = true;
2961        view.position_actual = 5000;
2962        axis.tick(&mut view, &mut client);
2963        assert!(matches!(axis.op, AxisOp::SoftHoming(4)));
2964
2965        complete_soft_homing(&mut axis, &mut view, &mut client, &resp_tx, 5000,
2966            |v| { v.home_sensor = false; });
2967    }
2968
2969    #[test]
2970    fn soft_homing_npn_home_sensor_full_sequence() {
2971        let (mut axis, mut client, resp_tx, _write_rx) = soft_homing_axis();
2972        let mut view = MockView::new();
2973        // NPN: sensor reads true normally, false when detected
2974        view.home_sensor = true;
2975        enable_axis(&mut axis, &mut view, &mut client);
2976
2977        axis.home(&mut view, HomingMethod::HomeSensorPosNpn);
2978
2979        // Phase 1: search
2980        axis.tick(&mut view, &mut client);
2981        view.status_word = 0x1027;
2982        axis.tick(&mut view, &mut client);
2983        view.status_word = 0x0027;
2984        axis.tick(&mut view, &mut client);
2985
2986        // NPN: sensor goes false = detected
2987        view.home_sensor = false;
2988        view.position_actual = 3000;
2989        axis.tick(&mut view, &mut client);
2990        assert!(matches!(axis.op, AxisOp::SoftHoming(4)));
2991
2992        complete_soft_homing(&mut axis, &mut view, &mut client, &resp_tx, 3000,
2993            |v| { v.home_sensor = true; }); // NPN: back to true = cleared
2994    }
2995
2996    #[test]
2997    fn soft_homing_limit_switch_suppresses_halt() {
2998        let (mut axis, mut client, _resp_tx, _write_rx) = soft_homing_axis();
2999        let mut view = MockView::new();
3000        enable_axis(&mut axis, &mut view, &mut client);
3001
3002        // Software homing on positive limit switch (rising edge)
3003        axis.home(&mut view, HomingMethod::LimitSwitchPosPnp);
3004
3005        // Progress through initial steps
3006        axis.tick(&mut view, &mut client); // step 0 → 1
3007        view.status_word = 0x1027; // ack
3008        axis.tick(&mut view, &mut client); // step 1 → 2
3009        view.status_word = 0x0027;
3010        axis.tick(&mut view, &mut client); // step 2 → 3
3011
3012        // Positive limit switch trips — should NOT halt (suppressed)
3013        view.positive_limit = true;
3014        view.velocity_actual = 100; // moving positive
3015        view.position_actual = 8000;
3016        axis.tick(&mut view, &mut client);
3017
3018        // Should have detected rising edge → step 4, NOT an error halt
3019        assert!(matches!(axis.op, AxisOp::SoftHoming(4)));
3020        assert!(!axis.is_error);
3021    }
3022
3023    #[test]
3024    fn soft_homing_opposite_limit_still_protects() {
3025        let (mut axis, mut client, _resp_tx, _write_rx) = soft_homing_axis();
3026        let mut view = MockView::new();
3027        enable_axis(&mut axis, &mut view, &mut client);
3028
3029        // Software homing on home sensor (positive direction)
3030        axis.home(&mut view, HomingMethod::HomeSensorPosPnp);
3031
3032        // Progress through initial steps
3033        axis.tick(&mut view, &mut client); // step 0 → 1
3034        view.status_word = 0x1027; // ack
3035        axis.tick(&mut view, &mut client); // step 1 → 2
3036        view.status_word = 0x0027;
3037        axis.tick(&mut view, &mut client); // step 2 → 3
3038
3039        // Negative limit switch trips while searching positive (shouldn't happen
3040        // in practice, but tests protection)
3041        view.negative_limit = true;
3042        view.velocity_actual = -100; // moving negative
3043        axis.tick(&mut view, &mut client);
3044
3045        // Should have halted with error (negative limit protects)
3046        assert!(axis.is_error);
3047        assert!(axis.error_message.contains("Negative limit switch"));
3048    }
3049
3050    #[test]
3051    // fn soft_homing_sensor_already_active_rejects() {
3052    //     let (mut axis, mut client, _resp_tx, _write_rx) = soft_homing_axis();
3053    //     let mut view = MockView::new();
3054    //     enable_axis(&mut axis, &mut view, &mut client);
3055
3056    //     // Home sensor is already active (rising edge would never happen)
3057    //     view.home_sensor = true;
3058    //     axis.tick(&mut view, &mut client); // update prev state
3059
3060    //     axis.home(&mut view, HomingMethod::HomeSensorPosPnp);
3061
3062    //     // Should have been rejected immediately
3063    //     assert!(axis.is_error);
3064    //     assert!(axis.error_message.contains("already in trigger state"));
3065    //     assert_eq!(axis.op, AxisOp::Idle);
3066    // }
3067
3068    #[test]
3069    fn soft_homing_negative_direction_sets_negative_target() {
3070        let (mut axis, mut client, _resp_tx, _write_rx) = soft_homing_axis();
3071        let mut view = MockView::new();
3072        enable_axis(&mut axis, &mut view, &mut client);
3073
3074        axis.home(&mut view, HomingMethod::HomeSensorNegPnp);
3075        axis.tick(&mut view, &mut client); // step 0
3076
3077        // Target should be negative (large negative value in counts)
3078        assert!(view.target_position < 0);
3079    }
3080
3081    #[test]
3082    fn home_integrated_method_starts_hardware_homing() {
3083        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
3084        let mut view = MockView::new();
3085        enable_axis(&mut axis, &mut view, &mut client);
3086
3087        axis.home(&mut view, HomingMethod::CurrentPosition);
3088        assert!(matches!(axis.op, AxisOp::Homing(0)));
3089        assert_eq!(axis.homing_method, 37);
3090    }
3091
3092    #[test]
3093    fn home_integrated_arbitrary_code() {
3094        let (mut axis, mut client, _resp_tx, _write_rx) = test_axis();
3095        let mut view = MockView::new();
3096        enable_axis(&mut axis, &mut view, &mut client);
3097
3098        axis.home(&mut view, HomingMethod::Integrated(35));
3099        assert!(matches!(axis.op, AxisOp::Homing(0)));
3100        assert_eq!(axis.homing_method, 35);
3101    }
3102
3103    #[test]
3104    fn hardware_homing_skips_speed_sdos_when_zero() {
3105        use mechutil::ipc::CommandMessage;
3106
3107        let (mut axis, mut client, resp_tx, mut write_rx) = test_axis();
3108        let mut view = MockView::new();
3109        enable_axis(&mut axis, &mut view, &mut client);
3110
3111        // Config has homing_speed = 0 and homing_accel = 0 (defaults)
3112        axis.home(&mut view, HomingMethod::Integrated(37));
3113
3114        // Step 0: writes homing method SDO
3115        axis.tick(&mut view, &mut client);
3116        assert!(matches!(axis.op, AxisOp::Homing(1)));
3117
3118        // Drain the SDO write message
3119        let _ = write_rx.try_recv();
3120
3121        // Simulate SDO ack — need to use the correct tid from the sdo write
3122        let tid = axis.homing_sdo_tid;
3123        resp_tx.send(CommandMessage::response(tid, serde_json::json!(null))).unwrap();
3124        client.poll();
3125        axis.tick(&mut view, &mut client);
3126
3127        // Should have skipped to step 8 (set homing mode)
3128        assert!(matches!(axis.op, AxisOp::Homing(8)));
3129    }
3130
3131    #[test]
3132    fn hardware_homing_writes_speed_sdos_when_nonzero() {
3133        use mechutil::ipc::CommandMessage;
3134
3135        let (mut axis, mut client, resp_tx, mut write_rx) = soft_homing_axis();
3136        let mut view = MockView::new();
3137        enable_axis(&mut axis, &mut view, &mut client);
3138
3139        // Config has homing_speed = 10.0, homing_accel = 20.0
3140        axis.home(&mut view, HomingMethod::Integrated(37));
3141
3142        // Step 0: writes homing method SDO
3143        axis.tick(&mut view, &mut client);
3144        assert!(matches!(axis.op, AxisOp::Homing(1)));
3145        let _ = write_rx.try_recv();
3146
3147        // SDO ack for homing method
3148        let tid = axis.homing_sdo_tid;
3149        resp_tx.send(CommandMessage::response(tid, serde_json::json!(null))).unwrap();
3150        client.poll();
3151        axis.tick(&mut view, &mut client);
3152        // Should go to step 2 (write speed SDO), not skip to 8
3153        assert!(matches!(axis.op, AxisOp::Homing(2)));
3154    }
3155
3156    #[test]
3157    fn soft_homing_edge_during_ack_step() {
3158        let (mut axis, mut client, _resp_tx, _write_rx) = soft_homing_axis();
3159        let mut view = MockView::new();
3160        enable_axis(&mut axis, &mut view, &mut client);
3161
3162        axis.home(&mut view, HomingMethod::HomeSensorPosPnp);
3163        axis.tick(&mut view, &mut client); // step 0 → 1
3164
3165        // Sensor rises during step 1 (before ack)
3166        view.home_sensor = true;
3167        view.position_actual = 2000;
3168        axis.tick(&mut view, &mut client);
3169
3170        // Should jump straight to step 4 (edge detected)
3171        assert!(matches!(axis.op, AxisOp::SoftHoming(4)));
3172    }
3173
3174    #[test]
3175    fn soft_homing_applies_home_position() {
3176        let mut cfg = soft_homing_config();
3177        cfg.home_position = 90.0;
3178
3179        use tokio::sync::mpsc;
3180        let (write_tx, _write_rx) = mpsc::unbounded_channel();
3181        let (resp_tx, response_rx) = mpsc::unbounded_channel();
3182        let mut client = CommandClient::new(write_tx, response_rx);
3183        let mut axis = Axis::new(cfg, "TestDrive");
3184
3185        let mut view = MockView::new();
3186        enable_axis(&mut axis, &mut view, &mut client);
3187
3188        axis.home(&mut view, HomingMethod::HomeSensorPosPnp);
3189
3190        // Search phase
3191        axis.tick(&mut view, &mut client);
3192        view.status_word = 0x1027;
3193        axis.tick(&mut view, &mut client);
3194        view.status_word = 0x0027;
3195        axis.tick(&mut view, &mut client);
3196
3197        // Sensor triggers
3198        view.home_sensor = true;
3199        view.position_actual = 5000;
3200        axis.tick(&mut view, &mut client);
3201        assert!(matches!(axis.op, AxisOp::SoftHoming(4)));
3202
3203        // Complete full sequence (halt, back-off, set home via drive)
3204        complete_soft_homing(&mut axis, &mut view, &mut client, &resp_tx, 5000,
3205            |v| { v.home_sensor = false; });
3206
3207        // After completion, home_offset = 0 (drive handles it)
3208        assert_eq!(axis.home_offset, 0);
3209    }
3210
3211    #[test]
3212    fn soft_homing_default_home_position_zero() {
3213        let (mut axis, mut client, resp_tx, _write_rx) = soft_homing_axis();
3214        let mut view = MockView::new();
3215        enable_axis(&mut axis, &mut view, &mut client);
3216
3217        axis.home(&mut view, HomingMethod::HomeSensorPosPnp);
3218
3219        // Search phase
3220        axis.tick(&mut view, &mut client);
3221        view.status_word = 0x1027;
3222        axis.tick(&mut view, &mut client);
3223        view.status_word = 0x0027;
3224        axis.tick(&mut view, &mut client);
3225
3226        // Sensor triggers
3227        view.home_sensor = true;
3228        view.position_actual = 5000;
3229        axis.tick(&mut view, &mut client);
3230
3231        complete_soft_homing(&mut axis, &mut view, &mut client, &resp_tx, 5000,
3232            |v| { v.home_sensor = false; });
3233
3234        assert_eq!(axis.home_offset, 0);
3235    }
3236}