accel_stepper/lib.rs
1//! A Rust port of the popular [`AccelStepper`][original] Arduino stepper
2//! library.
3//!
4//! # Basic Usage
5//!
6//! The most common way of using this crate is by driving an axis to a
7//! particular location.
8//!
9//! ```rust
10//! use accel_stepper::{Driver, SystemClock};
11//! # use core::time::Duration;
12//! # use core::cell::RefCell;
13//!
14//! let mut axis = Driver::new();
15//! // Make sure to set your device's motion parameters
16//! axis.set_max_speed(500.0);
17//! axis.set_acceleration(100.0);
18//!
19//! // The axis needs a clock for timing purposes. This could be an
20//! // `accel_stepper::OperatingSystemClock` when compiled with the `std`
21//! // feature, or your device's external oscillator
22//!
23//! #[derive(Debug, Default)]
24//! struct TickingClock(RefCell<Duration>);
25//!
26//! impl SystemClock for TickingClock {
27//! fn elapsed(&self) -> Duration {
28//! let mut ticks = self.0.borrow_mut();
29//! *ticks = *ticks + Duration::from_millis(10);
30//! ticks.clone()
31//! }
32//! }
33//!
34//! let clock = TickingClock::default();
35//!
36//! let mut forward = 0;
37//! let mut back = 0;
38//!
39//! {
40//! // for testing purposes, we'll create a Device which counts the number
41//! // of forward/backward steps
42//! let mut dev = accel_stepper::func_device(|| forward += 1, || back += 1);
43//!
44//! // set the desired location
45//! axis.move_to(17);
46//!
47//! // keep polling the axis until it reaches that location
48//! while axis.is_running() {
49//! axis.poll(&mut dev, &clock)?;
50//! }
51//! }
52//!
53//! // we should have arrived at our destination
54//! assert_eq!(17, axis.current_position());
55//!
56//! // it takes 17 steps forward to reach position 17
57//! assert_eq!(17, forward);
58//! assert_eq!(0, back);
59//! # Result::<(), Box<dyn std::error::Error>>::Ok(())
60//! ```
61//!
62//! # Cargo Features
63//!
64//! To minimise compile time and code size, this crate uses cargo features.
65//!
66//! - `std` - Enable functionality which depends on the standard library (e.g.
67//! the OS clock)
68//! - `hal` - Enable functionality which implements [`Device`] on top of traits
69//! from the [`embedded-hal`][hal] crate.
70//!
71//! [original]: http://www.airspayce.com/mikem/arduino/AccelStepper/index.html
72//! [hal]: https://crates.io/crates/embedded-hal
73
74#![cfg_attr(not(feature = "std"), no_std)]
75
76#[cfg(all(not(feature = "std"), test))]
77#[macro_use]
78extern crate std;
79
80mod clock;
81mod device;
82mod driver;
83#[cfg(feature = "hal")]
84mod hal_devices;
85mod multi_driver;
86mod utils;
87
88pub use crate::{
89 clock::SystemClock,
90 device::{fallible_func_device, func_device, Device, StepContext},
91 driver::Driver,
92 multi_driver::MultiDriver,
93 utils::CummulativeSteps,
94};
95
96#[cfg(feature = "std")]
97pub use crate::clock::OperatingSystemClock;
98
99#[cfg(feature = "hal")]
100pub use crate::hal_devices::*;