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
}