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 halt(&mut self) { self.0.halt(self.1); }
83 fn is_busy(&self) -> bool { self.0.is_busy }
84 fn is_error(&self) -> bool { self.0.is_error }
85 fn motor_on(&self) -> bool { self.0.motor_on }
86}
87
88/// Seek Probe function block.
89///
90/// Jogs an axis in the negative direction until a sensor triggers,
91/// then halts and reports completion.
92#[derive(Debug, Clone)]
93pub struct SeekProbe {
94 /// Output: operation completed successfully.
95 pub done: bool,
96 /// Output: operation failed — check `state.error_code`.
97 pub error: bool,
98 /// State machine with index, error_code, timers, and messages.
99 pub state: StateMachine,
100}
101
102impl SeekProbe {
103 /// Create a new SeekProbe in the idle state.
104 pub fn new() -> Self {
105 Self {
106 done: false,
107 error: false,
108 state: StateMachine::new(),
109 }
110 }
111
112 /// The FB is busy running.
113 pub fn is_busy(&self) -> bool {
114 self.state.index > 10
115 }
116
117 /// The last requested command resulted in an error.
118 pub fn is_error(&self) -> bool {
119 self.error || self.state.is_error()
120 }
121
122 /// Return the current error code.
123 pub fn error_code(&self) -> i32 {
124 self.state.error_code
125 }
126
127 /// Start the FB processing a seek cycle.
128 pub fn start(&mut self) {
129 self.state.clear_error();
130 self.done = false;
131 self.error = false;
132 self.state.index = 100;
133 }
134
135 /// Stop the FB from processing and send back to idle.
136 /// Safely halts the axis if it was currently moving.
137 pub fn reset(&mut self, handle: &mut impl AxisHandle) {
138 if self.state.index > 10 {
139 handle.halt();
140 }
141 self.done = false;
142 self.error = false;
143 self.state.index = 10;
144 }
145
146 /// Execute one scan cycle of the seek-probe state machine.
147 ///
148 /// # Arguments
149 ///
150 /// * `handle` — The axis handle (e.g. `AxisLift` or `(&mut axis, &mut view)`).
151 /// * `sensor` — When this goes high during jogging, the axis halts.
152 pub fn tick(
153 &mut self,
154 handle: &mut impl AxisHandle,
155 sensor: bool,
156 ) {
157 match self.state.index {
158 0 => {
159 // Reset / initialize
160 self.done = false;
161 self.state.index = 10;
162 }
163
164 10 => {
165 // Idle — wait for start() to be called
166 }
167
168 100 => {
169 // Start motion — verify position > 0, then jog negative
170 if handle.position() > 0.0 {
171 let speed = handle.config().jog_speed;
172 let accel = handle.config().jog_accel;
173 let decel = handle.config().jog_decel;
174 // Jog negative: move toward 0 at jog speed.
175 // The sensor trigger will halt before reaching the target.
176 handle.move_relative(-handle.position(), speed, accel, decel);
177 self.state.index = 120;
178 } else {
179 self.done = false;
180 self.error = true;
181 self.state.set_error(100, "Axis position must be > 0 to start seek");
182 self.state.index = 10;
183 }
184 }
185
186 120 => {
187 // Jogging negative — wait for sensor or error
188 if sensor {
189 handle.halt();
190 self.state.index = 200;
191 } else if handle.is_error() || !handle.motor_on() {
192 self.state.set_error(120, "Axis error or control disabled during seek");
193 self.state.index = 250;
194 }
195 }
196
197 200 => {
198 // Wait for axis to come to a complete stop
199 if !handle.is_busy() {
200 if handle.is_error() {
201 self.done = false;
202 self.error = true;
203 self.state.set_error(200, "Axis error while stopping");
204 } else {
205 self.done = true;
206 self.error = false;
207 }
208 self.state.index = 10;
209 }
210 }
211
212 250 => {
213 // Motion error during jog — halt and return to idle
214 handle.halt();
215 self.done = false;
216 self.error = true;
217 self.state.index = 10;
218 }
219
220 _ => {
221 self.state.index = 0;
222 }
223 }
224
225 self.state.call();
226 }
227
228 /// Abort the current operation immediately.
229 ///
230 /// If motion is active (states > 10), the axis is halted and
231 /// an error is set with code 1.
232 pub fn abort(&mut self, handle: &mut impl AxisHandle) {
233 self.reset(handle);
234 self.error = true;
235 self.state.set_error(1, "Seek aborted");
236 }
237}
238
239impl Default for SeekProbe {
240 fn default() -> Self {
241 Self::new()
242 }
243}