accel_stepper/
multi_driver.rs1use crate::{utils::DurationHelpers, Device, Driver, SystemClock};
2#[allow(unused_imports)]
3use arrayvec::ArrayVec;
4use core::time::Duration;
5
6pub struct MultiDriver {
8 #[cfg(feature = "std")]
9 drivers: Vec<Driver>,
10 #[cfg(not(feature = "std"))]
11 drivers: ArrayVec<[Driver; MultiDriver::MAX_DRIVERS]>,
12}
13
14impl MultiDriver {
15 pub const MAX_DRIVERS: usize = 10;
18
19 pub fn new() -> MultiDriver {
20 MultiDriver {
21 drivers: Default::default(),
22 }
23 }
24
25 pub fn push_driver(&mut self, driver: Driver) { self.drivers.push(driver); }
33
34 pub fn drivers(&self) -> &[Driver] { &self.drivers }
35
36 pub fn drivers_mut(&mut self) -> &mut [Driver] { &mut self.drivers }
37
38 pub fn move_to(&mut self, positions: &[i64]) {
45 assert_eq!(positions.len(), self.drivers.len());
46
47 if self.drivers.is_empty() {
48 return;
49 }
50
51 let longest_time = self
53 .drivers
54 .iter()
55 .zip(positions)
56 .map(|(d, p)| time_to_move(d, *p))
57 .max()
58 .expect("There is always a least one time");
59
60 if longest_time == Duration::new(0, 0) {
61 return;
63 }
64
65 let longest_time = longest_time.as_secs_f32_2();
66
67 for (i, driver) in self.drivers.iter_mut().enumerate() {
70 let distance = positions[i] - driver.current_position();
71
72 driver.move_to(positions[i]);
73 driver.set_speed(distance as f32 / longest_time);
74 }
75 }
76
77 pub fn poll<D, C>(
85 &mut self,
86 devices: &mut [D],
87 clock: &C,
88 ) -> Result<(), D::Error>
89 where
90 D: Device,
91 C: SystemClock,
92 {
93 assert_eq!(devices.len(), self.drivers.len());
94
95 for (driver, dev) in self.drivers.iter_mut().zip(devices.iter_mut()) {
96 driver.poll(dev, clock)?;
97 }
98
99 Ok(())
100 }
101
102 pub fn is_running(&self) -> bool {
104 self.drivers.iter().any(|d| d.is_running())
105 }
106}
107
108fn time_to_move(driver: &Driver, pos: i64) -> Duration {
109 let distance = driver.current_position() - pos;
110
111 Duration::from_secs_f32_2(distance.abs() as f32 / driver.max_speed())
112}