Skip to main content

autocore_std/motion/
seek_probe.rs

1/// Seek Probe — Jog an axis in the negative direction until a sensor triggers.
2///
3/// This function block implements a simple seek-to-probe state machine:
4/// 1. On a rising edge of `execute`, the axis begins jogging in the negative direction
5///    using the jog speed/accel/decel from [`AxisConfig`](super::AxisConfig).
6/// 2. When the sensor input goes high, the axis is halted.
7/// 3. Once the axis has fully stopped, the operation is complete.
8///
9/// The axis must be enabled and at a position > 0 before executing.
10/// Jog velocity, acceleration, and deceleration are taken from
11/// [`AxisConfig::jog_speed`](super::AxisConfig::jog_speed),
12/// [`AxisConfig::jog_accel`](super::AxisConfig::jog_accel), and
13/// [`AxisConfig::jog_decel`](super::AxisConfig::jog_decel).
14///
15/// # Example
16///
17/// ```ignore
18/// use autocore_std::motion::{Axis, AxisConfig, SeekProbe};
19/// use autocore_std::motion::axis_view::AxisView;
20///
21/// struct MyProgram {
22///     axis: Axis,
23///     seek: SeekProbe,
24/// }
25///
26/// fn process_tick(&mut self, ctx: &mut TickContext<Self::Memory>) {
27///     let mut view = ctx.gm.axis_view();
28///     self.axis.tick(&mut view, ctx.client);
29///
30///     self.seek.call(
31///         &mut self.axis,
32///         &mut view,
33///         ctx.gm.start_seek,    // execute: bool
34///         ctx.gm.ball_sensor,   // sensor: bool
35///     );
36///
37///     if self.seek.done {
38///         log::info!("Probe found at position {:.3}", self.axis.position);
39///     }
40///     if self.seek.state.is_error() {
41///         log::error!("Seek failed: error_code={}", self.seek.state.error_code);
42///     }
43/// }
44/// ```
45///
46/// # Error Codes
47///
48/// | Code | Meaning |
49/// |------|---------|
50/// | 1 | Panic/abort while motion was active |
51/// | 100 | Axis position is not > 0 at start |
52/// | 120 | Axis error or control disabled during motion |
53/// | 200 | Axis reported error when stopping |
54///
55/// # State Diagram
56///
57/// ```text
58/// ┌──────────┐  execute ↑  ┌────────────┐ position>0  ┌────────────────┐
59/// │ 10: Idle │──────────►│ 100: Start │────────────►│ 120: Jogging   │
60/// └──────────┘           └────────────┘             │  (negative)    │
61///       ▲                      │ pos<=0             └───────┬────────┘
62///       │                      ▼                   sensor │  │ axis error
63///       │                 error_code=100                   ▼  ▼
64///       │                                          ┌────────────────┐
65///       │◄──── done=true ◄─────────────────────────│ 200: Stopping  │
66///       │◄──── error=true ◄────────────────────────│                │
67///       │                                          └────────────────┘
68///       │◄──── error=true ◄── 250: Motion Error
69/// ```
70
71use crate::fb::StateMachine;
72use super::axis::Axis;
73use super::axis_view::{AxisView, AxisHandle};
74
75/// Blanket implementation to allow old-style (Axis, AxisView) calls.
76impl<'a, V: AxisView> AxisHandle for (&'a mut Axis, &'a mut V) {
77    fn position(&self) -> f64 { self.0.position }
78    fn config(&self) -> &super::axis_config::AxisConfig { self.0.config() }
79    fn move_relative(&mut self, distance: f64, vel: f64, accel: f64, decel: f64) {
80        self.0.move_relative(self.1, distance, vel, accel, decel);
81    }
82    fn move_absolute(&mut self, position: f64, vel: f64, accel: f64, decel: f64) {
83        self.0.move_absolute(self.1, position, vel, accel, decel);
84    }
85    fn halt(&mut self) { self.0.halt(self.1); }
86    fn is_busy(&self) -> bool { self.0.is_busy }
87    fn is_error(&self) -> bool { self.0.is_error }
88    fn motor_on(&self) -> bool { self.0.motor_on }
89}
90
91/// Seek Probe function block.
92///
93/// Jogs an axis in the negative direction until a sensor triggers,
94/// then halts and reports completion.
95#[derive(Debug, Clone)]
96pub struct SeekProbe {
97    /// Output: operation completed successfully.
98    pub done: bool,
99    /// Output: operation failed — check `state.error_code`.
100    pub error: bool,
101    /// State machine with index, error_code, timers, and messages.
102    pub state: StateMachine,
103}
104
105impl SeekProbe {
106    /// Create a new SeekProbe in the idle state.
107    pub fn new() -> Self {
108        Self {
109            done: false,
110            error: false,
111            state: StateMachine::new(),
112        }
113    }
114
115    /// The FB is busy running.
116    pub fn is_busy(&self) -> bool {
117        self.state.index > 10
118    }
119
120    /// The last requested command resulted in an error.
121    pub fn is_error(&self) -> bool {
122        self.error || self.state.is_error()
123    }
124
125    /// Return the current error code.
126    pub fn error_code(&self) -> i32 {
127        self.state.error_code
128    }
129
130    /// Start the FB processing a seek cycle.
131    pub fn start(&mut self) {
132        self.state.clear_error();
133        self.done = false;
134        self.error = false;
135        self.state.index = 100;
136    }
137
138    /// Stop the FB from processing and send back to idle.
139    /// Safely halts the axis if it was currently moving.
140    pub fn reset(&mut self, handle: &mut impl AxisHandle) {
141        if self.state.index > 10 {
142            handle.halt();
143        }
144        self.done = false;
145        self.error = false;
146        self.state.index = 10;
147    }
148
149    /// Execute one scan cycle of the seek-probe state machine.
150    ///
151    /// # Arguments
152    ///
153    /// * `handle` — The axis handle (e.g. `AxisLift` or `(&mut axis, &mut view)`).
154    /// * `sensor` — When this goes high during jogging, the axis halts.
155    pub fn tick(
156        &mut self,
157        handle: &mut impl AxisHandle,
158        sensor: bool,
159    ) {
160        match self.state.index {
161            0 => {
162                // Reset / initialize
163                self.done = false;
164                self.state.index = 10;
165            }
166
167            10 => {
168                // Idle — wait for start() to be called
169            }
170
171            100 => {
172                // Start motion — verify position > 0, then jog negative
173                if handle.position() > 0.0 {
174                    let speed = handle.config().jog_speed;
175                    let accel = handle.config().jog_accel;
176                    let decel = handle.config().jog_decel;
177                    // Jog negative: move toward 0 at jog speed.
178                    // The sensor trigger will halt before reaching the target.
179                    handle.move_relative(-handle.position(), speed, accel, decel);
180                    self.state.index = 120;
181                } else {
182                    self.done = false;
183                    self.error = true;
184                    self.state.set_error(100, "Axis position must be > 0 to start seek");
185                    self.state.index = 10;
186                }
187            }
188
189            120 => {
190                // Jogging negative — wait for sensor or error
191                if sensor {
192                    handle.halt();
193                    self.state.index = 200;
194                } else if handle.is_error() || !handle.motor_on() {
195                    self.state.set_error(120, "Axis error or control disabled during seek");
196                    self.state.index = 250;
197                }
198            }
199
200            200 => {
201                // Wait for axis to come to a complete stop
202                if !handle.is_busy() {
203                    if handle.is_error() {
204                        self.done = false;
205                        self.error = true;
206                        self.state.set_error(200, "Axis error while stopping");
207                    } else {
208                        self.done = true;
209                        self.error = false;
210                    }
211                    self.state.index = 10;
212                }
213            }
214
215            250 => {
216                // Motion error during jog — halt and return to idle
217                handle.halt();
218                self.done = false;
219                self.error = true;
220                self.state.index = 10;
221            }
222
223            _ => {
224                self.state.index = 0;
225            }
226        }
227
228        self.state.call();
229    }
230
231    /// Abort the current operation immediately.
232    ///
233    /// If motion is active (states > 10), the axis is halted and
234    /// an error is set with code 1.
235    pub fn abort(&mut self, handle: &mut impl AxisHandle) {
236        self.reset(handle);
237        self.error = true;
238        self.state.set_error(1, "Seek aborted");
239    }
240}
241
242impl Default for SeekProbe {
243    fn default() -> Self {
244        Self::new()
245    }
246}