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