motor-rs 0.6.1

Rust port of EPICS motor record
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Unified per-axis runtime (Phase 6).
//!
//! Converges command, status polling, and delay scheduling into a single
//! tokio task per axis, eliminating the `SharedDeviceState` mutex.
//!
//! External interface: `AxisHandle` with typed async methods.

use std::sync::{Arc, Mutex};
use std::time::Duration;

use asyn_rs::interfaces::motor::{AsynMotor, MotorStatus};
use asyn_rs::user::AsynUser;
use tokio::sync::{mpsc, oneshot};

use crate::device_state::*;
use crate::flags::MotorCommand;

/// Commands sent to the AxisRuntime via the AxisHandle.
#[derive(Debug)]
pub(crate) enum AxisCommand {
    /// Execute motor commands and manage polling.
    Execute {
        actions: DeviceActions,
        reply: oneshot::Sender<()>,
    },
    /// Request current status.
    GetStatus {
        reply: oneshot::Sender<Option<MotorStatus>>,
    },
    /// Start active polling.
    StartPolling,
    /// Stop active polling.
    StopPolling,
    /// Schedule a delay.
    ScheduleDelay { duration: Duration },
    /// Shutdown the runtime.
    Shutdown,
}

/// Cloneable handle to an AxisRuntime.
#[derive(Clone)]
pub struct AxisHandle {
    tx: mpsc::Sender<AxisCommand>,
    io_intr_rx_take: Arc<Mutex<Option<mpsc::Receiver<()>>>>,
}

impl AxisHandle {
    /// Send device actions to the axis and wait for execution.
    pub async fn execute(&self, actions: DeviceActions) {
        let (reply_tx, reply_rx) = oneshot::channel();
        let _ = self.tx.send(AxisCommand::Execute { actions, reply: reply_tx }).await;
        let _ = reply_rx.await;
    }

    /// Get the latest motor status.
    pub async fn get_status(&self) -> Option<MotorStatus> {
        let (reply_tx, reply_rx) = oneshot::channel();
        let _ = self.tx.send(AxisCommand::GetStatus { reply: reply_tx }).await;
        reply_rx.await.ok().flatten()
    }

    /// Start polling.
    pub async fn start_polling(&self) {
        let _ = self.tx.send(AxisCommand::StartPolling).await;
    }

    /// Stop polling.
    pub async fn stop_polling(&self) {
        let _ = self.tx.send(AxisCommand::StopPolling).await;
    }

    /// Schedule a delay.
    pub async fn schedule_delay(&self, _id: u64, duration: Duration) {
        let _ = self.tx.send(AxisCommand::ScheduleDelay { duration }).await;
    }

    /// Take the I/O Intr receiver (can only be called once).
    pub fn take_io_intr_receiver(&self) -> Option<mpsc::Receiver<()>> {
        self.io_intr_rx_take.lock().ok()?.take()
    }

    /// Shutdown the runtime.
    pub async fn shutdown(&self) {
        let _ = self.tx.send(AxisCommand::Shutdown).await;
    }
}

/// Per-axis runtime that owns the motor driver handle exclusively.
pub struct AxisRuntime {
    motor: Box<dyn AsynMotor>,
    cmd_rx: mpsc::Receiver<AxisCommand>,
    io_intr_tx: mpsc::Sender<()>,
    poll_interval: Duration,
    latest_status: Option<MotorStatus>,
    active_polling: bool,
    status_seq: u64,
}

/// Create an AxisRuntime and its handle.
pub fn create_axis_runtime(
    motor: Box<dyn AsynMotor>,
    poll_interval: Duration,
) -> (AxisRuntime, AxisHandle) {
    let (cmd_tx, cmd_rx) = mpsc::channel(64);
    let (io_intr_tx, io_intr_rx) = mpsc::channel(16);

    let runtime = AxisRuntime {
        motor,
        cmd_rx,
        io_intr_tx,
        poll_interval,
        latest_status: None,
        active_polling: false,
        status_seq: 0,
    };

    let handle = AxisHandle {
        tx: cmd_tx,
        io_intr_rx_take: Arc::new(Mutex::new(Some(io_intr_rx))),
    };

    (runtime, handle)
}

impl AxisRuntime {
    /// Run the axis runtime. This function runs indefinitely until shutdown.
    pub async fn run(mut self) {
        // Initial poll
        self.poll_motor().await;

        loop {
            if self.active_polling {
                tokio::select! {
                    cmd = self.cmd_rx.recv() => {
                        match cmd {
                            Some(cmd) => {
                                if self.handle_command(cmd).await {
                                    return;
                                }
                            }
                            None => return,
                        }
                    }
                    _ = tokio::time::sleep(self.poll_interval) => {
                        self.poll_motor().await;
                    }
                }
            } else {
                match self.cmd_rx.recv().await {
                    Some(cmd) => {
                        if self.handle_command(cmd).await {
                            return;
                        }
                    }
                    None => return,
                }
            }
        }
    }

    /// Handle a command. Returns true if runtime should shut down.
    async fn handle_command(&mut self, cmd: AxisCommand) -> bool {
        match cmd {
            AxisCommand::Execute { actions, reply } => {
                self.execute_actions(&actions);
                self.apply_poll_directive(&actions);
                if let Some(ref delay) = actions.schedule_delay {
                    let dur = delay.duration;
                    let tx = self.io_intr_tx.clone();
                    // Spawn a delay task - when done, it just triggers io_intr
                    tokio::spawn(async move {
                        tokio::time::sleep(dur).await;
                        let _ = tx.send(()).await;
                    });
                }
                let _ = reply.send(());
                false
            }
            AxisCommand::GetStatus { reply } => {
                let _ = reply.send(self.latest_status.clone());
                false
            }
            AxisCommand::StartPolling => {
                self.active_polling = true;
                false
            }
            AxisCommand::StopPolling => {
                self.active_polling = false;
                false
            }
            AxisCommand::ScheduleDelay { duration } => {
                self.active_polling = false;
                let tx = self.io_intr_tx.clone();
                tokio::spawn(async move {
                    tokio::time::sleep(duration).await;
                    let _ = tx.send(()).await;
                });
                false
            }
            AxisCommand::Shutdown => true,
        }
    }

    fn execute_actions(&mut self, actions: &DeviceActions) {
        let user = AsynUser::new(0);
        for cmd in &actions.commands {
            let result = match cmd {
                MotorCommand::MoveAbsolute { position, velocity, acceleration } => {
                    self.motor.move_absolute(&user, *position, *velocity, *acceleration)
                }
                MotorCommand::MoveVelocity { direction, velocity, acceleration } => {
                    let target = if *direction { 1e9 } else { -1e9 };
                    self.motor.move_absolute(&user, target, *velocity, *acceleration)
                }
                MotorCommand::Home { forward, velocity, acceleration: _ } => {
                    self.motor.home(&user, *velocity, *forward)
                }
                MotorCommand::Stop { acceleration } => {
                    self.motor.stop(&user, *acceleration)
                }
                MotorCommand::SetPosition { position } => {
                    self.motor.set_position(&user, *position)
                }
                MotorCommand::SetClosedLoop { enable } => {
                    self.motor.set_closed_loop(&user, *enable)
                }
                MotorCommand::Poll => Ok(()),
            };
            if let Err(e) = result {
                tracing::error!("motor command error: {e}");
            }
        }
    }

    fn apply_poll_directive(&mut self, actions: &DeviceActions) {
        match actions.poll {
            PollDirective::Start => self.active_polling = true,
            PollDirective::Stop => self.active_polling = false,
            PollDirective::None => {}
        }
    }

    async fn poll_motor(&mut self) {
        let user = AsynUser::new(0);
        match self.motor.poll(&user) {
            Ok(status) => {
                self.status_seq += 1;
                self.latest_status = Some(status);
                let _ = self.io_intr_tx.send(()).await;
            }
            Err(e) => {
                tracing::error!("motor poll error: {e}");
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use asyn_rs::error::AsynResult;

    struct SimMotor {
        position: f64,
        target: f64,
        moving: bool,
    }

    impl SimMotor {
        fn new() -> Self {
            Self {
                position: 0.0,
                target: 0.0,
                moving: false,
            }
        }
    }

    impl AsynMotor for SimMotor {
        fn move_absolute(&mut self, _user: &AsynUser, pos: f64, _vel: f64, _acc: f64) -> AsynResult<()> {
            self.target = pos;
            self.moving = true;
            Ok(())
        }

        fn home(&mut self, _user: &AsynUser, _vel: f64, _forward: bool) -> AsynResult<()> {
            self.target = 0.0;
            self.moving = true;
            Ok(())
        }

        fn stop(&mut self, _user: &AsynUser, _acc: f64) -> AsynResult<()> {
            self.target = self.position;
            self.moving = false;
            Ok(())
        }

        fn set_position(&mut self, _user: &AsynUser, pos: f64) -> AsynResult<()> {
            self.position = pos;
            self.target = pos;
            Ok(())
        }

        fn poll(&mut self, _user: &AsynUser) -> AsynResult<MotorStatus> {
            // Simple sim: instantly reach target
            if self.moving {
                self.position = self.target;
                self.moving = false;
            }
            Ok(MotorStatus {
                position: self.position,
                encoder_position: self.position,
                done: !self.moving,
                moving: self.moving,
                ..MotorStatus::default()
            })
        }
    }

    #[tokio::test]
    async fn axis_runtime_basic() {
        let (runtime, handle) = create_axis_runtime(
            Box::new(SimMotor::new()),
            Duration::from_millis(50),
        );

        let runtime_handle = tokio::spawn(runtime.run());

        // Get initial status (from initial poll)
        tokio::time::sleep(Duration::from_millis(10)).await;
        let status = handle.get_status().await.unwrap();
        assert!(status.done);
        assert!((status.position - 0.0).abs() < 1e-10);

        // Execute a move
        let actions = DeviceActions {
            commands: vec![MotorCommand::MoveAbsolute {
                position: 10.0,
                velocity: 1.0,
                acceleration: 1.0,
            }],
            poll: PollDirective::Start,
            ..Default::default()
        };
        handle.execute(actions).await;

        // Wait for poll to pick up the move completion
        tokio::time::sleep(Duration::from_millis(100)).await;

        let status = handle.get_status().await.unwrap();
        assert!((status.position - 10.0).abs() < 1e-10);
        assert!(status.done);

        handle.shutdown().await;
        let _ = runtime_handle.await;
    }

    #[tokio::test]
    async fn axis_runtime_polling_start_stop() {
        let (runtime, handle) = create_axis_runtime(
            Box::new(SimMotor::new()),
            Duration::from_millis(20),
        );

        let runtime_handle = tokio::spawn(runtime.run());

        handle.start_polling().await;
        tokio::time::sleep(Duration::from_millis(50)).await;
        handle.stop_polling().await;

        handle.shutdown().await;
        let _ = runtime_handle.await;
    }

    #[tokio::test]
    async fn axis_runtime_set_position() {
        let (runtime, handle) = create_axis_runtime(
            Box::new(SimMotor::new()),
            Duration::from_millis(50),
        );

        let runtime_handle = tokio::spawn(runtime.run());

        let actions = DeviceActions {
            commands: vec![MotorCommand::SetPosition { position: 5.0 }],
            poll: PollDirective::None,
            ..Default::default()
        };
        handle.execute(actions).await;

        // Force a poll to update status
        handle.start_polling().await;
        tokio::time::sleep(Duration::from_millis(100)).await;

        let status = handle.get_status().await.unwrap();
        assert!((status.position - 5.0).abs() < 1e-10);

        handle.shutdown().await;
        let _ = runtime_handle.await;
    }

    #[tokio::test]
    async fn axis_runtime_io_intr() {
        let (runtime, handle) = create_axis_runtime(
            Box::new(SimMotor::new()),
            Duration::from_millis(50),
        );

        let mut io_intr_rx = handle.take_io_intr_receiver().unwrap();

        let runtime_handle = tokio::spawn(runtime.run());

        // Initial poll should trigger io_intr
        let result = tokio::time::timeout(Duration::from_millis(100), io_intr_rx.recv()).await;
        assert!(result.is_ok());

        handle.shutdown().await;
        let _ = runtime_handle.await;
    }

    #[tokio::test]
    async fn axis_handle_clone() {
        let (runtime, handle) = create_axis_runtime(
            Box::new(SimMotor::new()),
            Duration::from_millis(50),
        );

        let handle2 = handle.clone();

        let runtime_handle = tokio::spawn(runtime.run());

        tokio::time::sleep(Duration::from_millis(10)).await;
        let status = handle2.get_status().await.unwrap();
        assert!((status.position - 0.0).abs() < 1e-10);

        handle.shutdown().await;
        let _ = runtime_handle.await;
    }
}