basic/
basic.rs

1//! Basic motor control example for TMC2209.
2//!
3//! This example demonstrates basic motor control operations.
4//! It is meant to be adapted for your specific hardware platform.
5//!
6//! Note: This is a documentation example showing API usage.
7//! For a complete working example, you need to integrate with
8//! your platform's UART implementation.
9
10// This example requires std for compilation but the library is no_std
11#![allow(unused)]
12
13use tmc2209_uart::{MicrostepResolution, Tmc2209};
14
15/// Example function showing basic motor control.
16///
17/// Replace `YourUartType` with your platform's UART type that implements
18/// `embedded_io::Read` and `embedded_io::Write`.
19#[cfg(feature = "blocking")]
20fn basic_motor_control<U, E>(uart: U) -> Result<(), tmc2209_uart::Error<E>>
21where
22    U: embedded_io::Read<Error = E> + embedded_io::Write<Error = E>,
23{
24    // Create driver with slave address 0
25    let mut driver = Tmc2209::new(uart, 0);
26
27    // Check if the driver is responding
28    if !driver.is_connected() {
29        // Handle connection error
30        return Err(tmc2209_uart::Error::NoResponse);
31    }
32
33    // Clear any previous errors
34    driver.clear_gstat()?;
35
36    // Configure motor current
37    // IRUN=20 (run current), IHOLD=10 (hold current), IHOLDDELAY=6
38    driver.set_current(20, 10, 6)?;
39
40    // Set microstep resolution to 16 with interpolation to 256
41    driver.set_microsteps(MicrostepResolution::M16)?;
42    driver.set_interpolation(true)?;
43
44    // Enable StealthChop for quiet operation
45    driver.enable_stealthchop()?;
46
47    // Enable the driver
48    driver.set_enabled(true)?;
49
50    // Start moving the motor forward
51    driver.set_velocity(5000)?;
52
53    // Check status
54    let status = driver.drv_status()?;
55
56    if status.ot() {
57        // Overtemperature shutdown - stop immediately!
58        driver.stop()?;
59        driver.set_enabled(false)?;
60    }
61
62    if status.otpw() {
63        // Overtemperature warning - reduce current or add cooling
64        println!("Warning: Motor temperature is high");
65    }
66
67    // Reverse direction
68    driver.set_velocity(-5000)?;
69
70    // Stop the motor
71    driver.stop()?;
72
73    Ok(())
74}
75
76fn main() {
77    println!("TMC2209 Basic Example");
78    println!("This example shows API usage patterns.");
79    println!("Integrate with your platform's UART for actual use.");
80}