-
Notifications
You must be signed in to change notification settings - Fork 46
Description
The following Arduino sketch is to run a stepper motor for little while from Arduino Nano using TMC2209 driver in UART mode until it detects a stall to come to a complete stop and then run backward for few turns. The motor runs and stops when it detects a stall. but it failed to run backward. because the DIAG pin output stays HIGH once the motor stalled and stopped. (FYI: the DIAG pin output also stays high when the motor stops by itself after it completing the set move distance) . I tried to reset the Driver by toggling the EN pin but no changes. The diag ping output reset only when the VM power cycles.
#include <TMCStepper.h>
#include <SoftwareSerial.h>
#include <AccelStepper.h>
#include <TMC2209.h>
// Define pins
#define EN_PIN 6 // Enable pin (LOW active)
#define DIR_PIN 5 // Direction pin
#define STEP_PIN 4 // Step pin (not used in VACTUAL mode)
#define STALL_PIN_X 3 // Connected to DIAG pin on the TMC2209
// Software Serial pins
#define SW_TX 8
#define SW_RX 10
#define R_SENSE 0.11f // Check your Stepper_A board's sense resistor value
#define STALLA_VALUE 90
SoftwareSerial SoftSerial(SW_RX, SW_TX);
TMC2209Stepper driver_A(&SoftSerial, R_SENSE, 0b00); // Stepper_A address 0b00 (default)
AccelStepper Stepper_A = AccelStepper(Stepper_A.DRIVER, STEP_PIN, DIR_PIN);
bool stalled_A = false;
void stallInterruptX(){ // flag set for motor A when motor stalls
stalled_A = true;
}
void setup() {
Serial.begin(9600); // Hardware serial for debugging
SoftSerial.begin(115200); // Start software serial communication with the Stepper_A
attachInterrupt(digitalPinToInterrupt(STALL_PIN_X), stallInterruptX, CHANGE);
pinMode(EN_PIN, OUTPUT);
pinMode(DIR_PIN, OUTPUT);
pinMode(STEP_PIN, OUTPUT);
pinMode(STALL_PIN_X, INPUT_PULLUP);
digitalWrite(EN_PIN, LOW); // Enable the Stepper_A in hardware
driver_A.begin(); // Initiate pins and registeries
driver_A.rms_current(2200); // Set stepperA current to 600mA. The command is the same as command TMC2130.setCurrent(600, 0.11, 0.5);
//driverA.en_pwm_mode(1); // Enable extremely quiet stepping
driver_A.pwm_autoscale(1);
driver_A.microsteps(16);
driver_A.TCOOLTHRS(0xFFFFF); // 20bit max
driver_A.SGTHRS(STALLA_VALUE);
driver_A.toff(5);
driver_A.en_spreadCycle(false); // Use stealthChop (silent operation)
Serial.println("TMC2209 Initialized via Software Serial");
Serial.println(driver_A.microsteps());
delay(5000);
Stepper_A.setMaxSpeed(4000); // 100mm/s @ 80 steps/mm
Stepper_A.setAcceleration(12000); // 2000mm/s^2
Stepper_A.setEnablePin(EN_PIN);
Stepper_A.setPinsInverted(false, false, true);
Stepper_A.enableOutputs();
Serial.println("now running the motor");
Serial.println(stalled_A);
run_stepper_A();
}
// THE FOLLOWING FUNCTION RUN THE MOTOR UNTIL STALL DETECTED
void run_stepper_A(){
Stepper_A.setSpeed(4000);
Stepper_A.move(50000);
while(1){
Stepper_A.run();
//Serial.println(driver_A.SG_RESULT());
if (stalled_A){
Stepper_A.stop();
Stepper_A.runSpeedToPosition();
Stepper_A.setCurrentPosition(0);
break;}
}
digitalWrite(EN_PIN, HIGH); // RESET THE DRIVER BY TOGGLING THE EN PIN
delay(500);
digitalWrite(EN_PIN, LOW);
Serial.println("now running backward");
Stepper_A.setSpeed(4000); // running the motor backward after the stall detected
Stepper_A.move(-1000);
while (Stepper_A.distanceToGo() != 0) {
Stepper_A.run();
}
Stepper_A.stop();
Stepper_A.runSpeedToPosition();
}
void loop() {
}