Hi all -
I am currently using the code borrowed from this post, on using a potentiometer to set speed and direction with TMC2209. I'll include the code again below, but I had a few questions about it that I was hoping people could help me understand.
Now, my understanding is that the original poster scaled an enormous range of values with the map function, which is why it is being run at 1/256 microsteps. For my purposes, I need it to operate a little faster, so I've set it to 128. This moves faster, but the driver/motor do start to become audible in project at this rate.
I'm curious about a few things here.
-
If "spreadCycle" is disabled, but I am mapping a huge range of values - what is the net result? How does it differ from "spreadCycle" mode?
-
I'm not entirely clear if "stealthChop" is active in this code. Apparently "pwm_autoscale" is necessary for "stealthChop", but there is no additional code that I understand to directly refer to it.
-
If I use a different ratio for the microstepping, does that effect the overall frequency, separate from the variable speed set by TMC_Driver.VACTUAL?
Thanks all!
#include <TMCStepper.h>
#include <SoftwareSerial.h> // Use software serial for the UART to TMC2209
# include <Streaming.h> // For debugging
#define EN_PIN 2 // Enable - PURPLE
#define DIR_PIN 3 // Direction - WHITE
#define STEP_PIN 4 // Step - ORANGE
#define SW_SCK 5 // Software Slave Clock (SCK) - BLUE
#define SW_TX 6 // SoftwareSerial receive pin - BROWN
#define SW_RX 7 // SoftwareSerial transmit pin - YELLOW
#define DRIVER_ADDRESS 0b00 // TMC2209 Driver address according to MS1 and MS2
#define R_SENSE 0.11f // SilentStepStick series use 0.11 ...and so does my fysetc TMC2209 (?)
#define POT1 A0 // Potentiometers to adjust speed and travel
#define POT2 A1
SoftwareSerial SoftSerial(SW_RX, SW_TX);
TMC2209Stepper TMC_Driver(&SoftSerial, R_SENSE, DRIVER_ADDRESS);
//== Setup ======================================================================================
void setup() {
Serial.begin(115200); // initialize hardware serial for debugging
SoftSerial.begin(115200); // initialize software serial for UART motor control
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, LOW); // Enable driver in hardware
pinMode(STEP_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
TMC_Driver.beginSerial(115200); // Initialize UART
TMC_Driver.begin(); // UART: Init SW UART (if selected) with default 115200 baudrate
TMC_Driver.toff(5); // Enables driver in software
TMC_Driver.rms_current(1770); // Set motor RMS current
TMC_Driver.microsteps(256); // Set microsteps
TMC_Driver.en_spreadCycle(false); // Toggle spreadCycle
TMC_Driver.pwm_autoscale(true); // Needed for stealthChop
}
//== Loop ========================================================================================
void loop() {
int potVal = analogRead(A0); // Read potentiometer (0-1023)
long stepperSpeed;
if (potVal <= 500) // In lower half of range turn counter clockwise
{ // (direction depends on motor wiring?)
stepperSpeed = map(potVal, 0, 500, -200000, 0);
}
else if (potVal >= 520) // In high half of range turn clockwise
{
stepperSpeed = map(potVal, 520, 1023, 0, 200000);
}else // Create a "dead zone" between CW and CCW
{ // if 500 < potVal <520
stepperSpeed = 0;
}
Serial << potVal << " " << stepperSpeed << endl;
TMC_Driver.VACTUAL(stepperSpeed);
TMC_Driver.step();
} // end loop