Detecting a Motor Stall in Programme

I am using the TMC260 Motor Driver Shield with an Arduino Uno to drive a Stepper Motor. I have got a programme which works to drive the motor to a datum point and then to a final desired position. However, to make this operation better, I am trying to detect when the motor stalls and as soon as it stalls it will take the motor to the final delivery position rather than wait until the maximum steps have been completed. Can anyone assist please?

Motor Shield : TMC260_Stepper_Motor_Driver_Shield_SKU__DRI003-DFRobot

#include <SPI.h>
#include <TMC26XStepper.h>

//we have a stepper motor with 20 steps per rotation,CS pin 6, dir pin 4, step pin 5 and a current of 500mA
TMC26XStepper tmc26XStepper = TMC26XStepper(20,6,4,5,500);

int CorrectLengthButton = 12;  //Assigns the button to Pin 12
int CorrectLengthButton_state = 0;  //Sets the button state to 0 for analysis
int StartButton_Latch_state = 0;
int StallGuardStart = 100;

void setup() 
{
    Serial.begin(9600);
    Serial.println("==============================");
    Serial.println("GCS STEPPER MOTOR REWORK");
    Serial.println("==============================");
    //set this according to you stepper
    Serial.println("CONFIGURING STEPPER MOVEMENT CYCLE");
    //char constant_off_time, char blank_time, char hysteresis_start, char hysteresis_end, char hysteresis_decrement
    tmc26XStepper.setSpreadCycleChopper(5,1,8,6,1);
    tmc26XStepper.setRandomOffTime(1);
    tmc26XStepper.SPI_setCoilCurrent(50);
    tmc26XStepper.setMicrosteps(32);
    tmc26XStepper.setStallGuardThreshold(4,0);
    Serial.println("config finished, starting");
    Serial.println("started");

    pinMode(CorrectLengthButton, INPUT);
}

void loop() 
{
  tmc26XStepper.move();
  // put your main code here, to run repeatedly:
  CorrectLengthButton_state = digitalRead(CorrectLengthButton);       //Reads the Start Button Pin
    Serial.print("Stall Guard: ");
    Serial.println(tmc26XStepper.getCurrentStallGuardReading());
    Serial.print("Current: ");
    Serial.println(tmc26XStepper.getCurrentCSReading());
    Serial.print("Stall Reached: ");
    Serial.println(tmc26XStepper.isStallGuardReached());    
  if(CorrectLengthButton_state == HIGH)                               // Analyses if the button has been pressed
  {
    StartButton_Latch_state = 1;
    SetZero();
    SetFinalLength(); 
                             


      //tmc26XStepper.SPI_setSpeed(100);    //Set speed at 100 RPM
      //tmc26XStepper.SPI_step(282);       //set step at 282 steps,  that is to say stepper will turn a circle forwards
      //tmc26XStepper.spi_start() ;         //start stepper
      //Serial.print("Stall Guard: ");
      //Serial.println(tmc26XStepper.getCurrentStallGuardReading());
      //delay(4000);
  }

  if(tmc26XStepper.isStallGuardReached() == -1)
  {
    //tmc26XStepper.un_start();
    SetFinalLength();                      //Runs the SetFinalLength code to take the actuator to the delivery position required
    StartButton_Latch_state = 2;
  }

  if(StartButton_Latch_state == 2)
  {
    tmc26XStepper.un_start();
    StartButton_Latch_state = 0;
  }
}
  
void SetZero()
{
      tmc26XStepper.SPI_setSpeed(100);    //Set speed at 100 RPM
      tmc26XStepper.SPI_step(-553);   //set step at -553 steps,  that is to say stepper will turn a in reverse - 553 is maximum number of steps from Max out to Max in.        
      tmc26XStepper.spi_start() ;         //start stepper
      //delay(4000); 

}

void SetFinalLength()
{
      tmc26XStepper.SPI_setSpeed(100);    //Set speed at 100 RPM
      tmc26XStepper.SPI_step(282);       //set step at 282 steps,  that is to say stepper will turn forward, taking to delivery position.
      tmc26XStepper.spi_start() ;         //start stepper

}

the current stall guard reading is not working properly and where I am using SPI i am getting no stall guard readings as the motor is driving so it can't detect the stall during the operation.

Impossible with stepper motors where stall current equals hold current.

is this a continuation of this older thread?

yes it is.

If a stepper-motor stalls. The physical position becomes unknown.
is your final delivery position at a smaller distance than the maximum of steps?

Does your final delivery position have a sensor that is reporting "arrived at position"?

If you don't have a sensor at your finally delivery position you can't stop at this final delivery position anymore.

You can drive a number of steps but as the motor was stalling the position direct after the stall is unknown and this means that the position after driving a certain number of steps the position is unknown too.

The only way to make your code know again at what physical position your device is
is to repeat the referencing.

You are asking for some details without haven given the overview.
It might well be that trying to solve your problem on the basic idea you have will not work.
You should make use of the knowledge and the experience of the community by describing your project with much more details.

Otherwise there is a danger that you wil tinker aorund for days and weeks without solving the problem.

best regards Stefan

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.