stealthchop/stealthchop.rs
1//! StealthChop configuration example for TMC2209.
2//!
3//! This example shows how to configure StealthChop for quiet motor operation,
4//! including automatic switching to SpreadCycle at high velocities.
5
6#![allow(unused)]
7
8use tmc2209_uart::{MicrostepResolution, Tmc2209};
9
10/// Configure StealthChop with velocity-based mode switching.
11#[cfg(feature = "blocking")]
12fn configure_stealthchop<U, E>(uart: U) -> Result<(), tmc2209_uart::Error<E>>
13where
14 U: embedded_io::Read<Error = E> + embedded_io::Write<Error = E>,
15{
16 let mut driver = Tmc2209::new(uart, 0);
17
18 // Basic setup
19 driver.set_current(16, 8, 4)?;
20 driver.set_microsteps(MicrostepResolution::M256)?;
21
22 // =========================================================================
23 // StealthChop Configuration
24 // =========================================================================
25
26 // Enable StealthChop mode (this is the default)
27 driver.enable_stealthchop()?;
28
29 // Configure StealthChop PWM parameters
30 // - pwm_ofs: Base PWM amplitude (0-255)
31 // - pwm_grad: PWM amplitude gradient (0-255)
32 // - autoscale: Enable automatic current scaling
33 // - autograd: Enable automatic gradient adaptation
34 driver.configure_stealthchop(
35 36, // pwm_ofs (default)
36 14, // pwm_grad (default)
37 true, // autoscale enabled (recommended)
38 true, // autograd enabled (recommended)
39 )?;
40
41 // =========================================================================
42 // Velocity-Based Mode Switching
43 // =========================================================================
44
45 // Set StealthChop/SpreadCycle velocity threshold (TPWMTHRS)
46 // Below this TSTEP value, StealthChop is used
47 // Above this velocity (lower TSTEP), SpreadCycle is used
48 driver.set_stealthchop_threshold(500)?;
49
50 // =========================================================================
51 // Chopper Configuration
52 // =========================================================================
53
54 // Fine-tune chopper parameters for your motor
55 driver.configure_chopper(
56 3, // toff
57 4, // hstrt
58 1, // hend
59 2, // tbl
60 )?;
61
62 // =========================================================================
63 // Current Sense Configuration
64 // =========================================================================
65
66 // For high-current motors, use low sensitivity VSENSE
67 driver.set_vsense(false)?;
68
69 // Enable the driver
70 driver.set_enabled(true)?;
71
72 // =========================================================================
73 // Monitor StealthChop Status
74 // =========================================================================
75
76 // Start moving
77 driver.set_velocity(1000)?;
78
79 // Check if StealthChop or SpreadCycle is active
80 let _is_stealth = driver.is_stealthchop_active()?;
81
82 // Check actual current being used
83 let _actual_cs = driver.actual_current_scale()?;
84
85 Ok(())
86}
87
88fn main() {
89 println!("TMC2209 StealthChop Example");
90 println!("This example shows how to configure silent operation.");
91}