Section 9: Motor Setup

Let’s continue setting up the motor in our sketch on line 168 of MotorControl.h. Here we will learn about more Trinamic parameters to set up.

// Start all the UART communications functions behind the scenes
driver.begin(); 

//For operation with StealthChop, this parameter is not used, but it is required to enable the motor. In case of operation with StealthChop only, any setting is OK
driver.toff(4);

//Recommended blank time select value
driver.blank_time(24); 

// Disbaled to use the extrenal current sense resistors 
driver.I_scale_analog(false);

// Use the external Current Sense Resistors. Do not use the internal resistor as it can't handle high current.
driver.internal_Rsense(false); 

//Microstep resolution selected by MSTEP register and NOT from the legacy pins.
driver.mstep_reg_select(true);

// Set the current in milliamps
driver.rms_current(current); 

//Set the stall value from 0-255. Higher value will make it stall quicker
driver.SGTHRS(stall);

//Set the number of microsteps. the TMC2209 uses MicroPlyer in order to turn every step into 256 microsteps. However, it’s not as accurate when you want to be precise. So set the microsteps as high as you can, and if your motor is not going as fast as you want it to, decrease the microsteps to 128, 64, 32, 16, 8, 4, 2. or 1. I like to keep 64 because it allows for a high top speed. However, your IC (ESP32) bust be able to send a very fast pulse for higher microstepping.
driver.microsteps(motor_microsteps); 

// Minimum speed at which point to turn on StallGuard. StallGuard does not work as very low speeds such as the beginning of acceleration so we need to keep it off until it reaches a reliable speed.
driver.TCOOLTHRS(tcools);

// DisableStealthChop PWM mode/ Page 25 of datasheet
driver.TPWMTHRS(0); 

// Turn off smart current control, known as CoolStep. It's a neat feature but is more complex and messes with StallGuard.
driver.semin(0); 

driver.shaft(true); // Set the shaft direction clockwise.
driver.shaft(false); // Set the shaft direction counter-clockwise.

// Disable SpreadCycle. We want StealthChop becuase it works with StallGuard.
driver.en_spreadCycle(false); 

// Enable UART control
driver.pdn_disable(true); 

// Set the ESP32 pin that will toggle the direction of the motor

stepper->setDirectionPin(DIR_PIN);

// Set the ESP32 pin that will enable and disable the motor driver. We usually want the driver disabled when it's not running. The downside of this is that the motor will spin freely. If you need it to hold the position, do not disable the driver.

stepper->setEnablePin(ENABLE_PIN);

// The FastAccelStepper library has a neat "Auto Enable" feature that automatically enables and disables your board for you. When it needs to be moved it will enable the board, and when the movement is complete it will disable it. Again, when disabled the motor will spin freely.

stepper->setAutoEnable(true);

NEXT SECTION: 10

 

Course Sections:

Back to blog

Leave a comment