New stepper motor driver (TMC2209) runs too slow, how to speed up?

image

I left MS2 floating, reads zero volts. This works OK:

const int dirPinX = 13;
const int stepPinX = 12;

const int dirPinY = 8;
const int stepPinY = 7;

void setup()
{
  pinMode(stepPinX, OUTPUT);
  pinMode(dirPinX, OUTPUT);
  pinMode(stepPinY, OUTPUT);
  pinMode(dirPinY, OUTPUT);
}

void loop()
{
  stepX();
  stepY();
}

void stepX()
{
  digitalWrite(stepPinX, HIGH);
  delayMicroseconds(5);
  digitalWrite(stepPinX, LOW);
  delayMicroseconds(50);
}

void stepY()
{
  digitalWrite(stepPinY, HIGH);
  delayMicroseconds(5);
  digitalWrite(stepPinY, LOW);
  delayMicroseconds(50);
}

Here is the program that runs too slow:

/*
   Uno
*/

const int dirPinX = 13;
const int stepPinX = 12;

const int dirPinY = 8;
const int stepPinY = 7;


int resetXcount = 0, resetYcount = 0, stepXcount = 0, stepYcount = 0;

//float deltaTheta = PI/8000;             // Original
float deltaTheta = PI / 8000;             // Adjust this when gear ratio changes
float A = 200;       // Amplitude - scaling (stretching/shifting)
float deltaX = 0, deltaY = 0, theta = 0;
float x_1 = 0, x_2 = 0, y_1 = 0, y_2 = 0;

//----------------------------ONCE----------------------------
void setup()
{
  pinMode(stepPinX, OUTPUT);
  pinMode(dirPinX, OUTPUT);
  pinMode(stepPinY, OUTPUT);
  pinMode(dirPinY, OUTPUT);

  deltaX  = 0;   //(dirPinX, LOW);  // left
  deltaY  = 0;   //(dirPinY, LOW);  // towards
  for (int i = 0; i < 4200; i++)
  {
    stepX();
    stepY();

    digitalWrite(stepPinX, LOW);
    digitalWrite(stepPinY, LOW);
    delayMicroseconds(50);
  }
}

//----------------------------LOOP----------------------------
void loop()
{
  //  1: Circle

  x_1 = cos(theta) * A * 100;
  x_2 = cos(theta + deltaTheta) * A * 100;

  y_1 = 0.9 * sin(theta) * A * 100;         // Original
  y_2 = 0.9 * sin((theta + deltaTheta)) * A * 100; //

  drawLocus();
  theta += deltaTheta;


}

void drawLocus()
{

  deltaX += (x_2 - x_1) * 100;

  if (abs(deltaX) > 100)  // && abs(cos(theta)) < 0.9
  {
    stepX();

    if (deltaX < 0)
    {
      deltaX += 100;
      stepXcount++;
    }
    else
    {
      deltaX -= 100;
      stepXcount--;
    }
  }

  deltaY += (y_2 - y_1) * 100;

  if (abs(deltaY) > 100)  //  && abs(sin(theta)) < 0.95
  {
    stepY();

    if (deltaY < 0)
    {
      deltaY += 100;
      stepYcount++;
    }
    else
    {
      deltaY -= 100;
      stepYcount--;
    }
  }
  digitalWrite(stepPinX, LOW);
  digitalWrite(stepPinY, LOW);
  delayMicroseconds(50);
}





void stepX()
{
  if (deltaX < 0) // Set motor direction clockwise
    digitalWrite(dirPinX, HIGH);
  else
    digitalWrite(dirPinX, LOW);

  digitalWrite(stepPinX, HIGH);
  delayMicroseconds(5);
}


void stepY()
{
  if (deltaY < 0) // Set motor direction clockwise
    digitalWrite(dirPinY, HIGH);
  else
    digitalWrite(dirPinY, LOW);

  digitalWrite(stepPinY, HIGH);
  delayMicroseconds(5);

}

Any suggestions?

How slow is too slow? Is the 50us delay in each loop making it too slow?

The TMC2209 only needs 100ns pulse width, so you could put the digitalWrite(stepPinX, LOW); cleanup code inside StepX(), etc...

Oops -- I missed the 4 trig operations per loop. They could take significant time.

I looked up the TMC2209-Datasheet
"smallest" microstepping is 1/8


1 Like

Well that wiki is wrong then. I thought I was running it at 1/2 microstepping. Seems like I'm running it at 1/32.

To determine this mount something on the motor-axle that looks to one side. Then run the motor for a certain amout of steps.

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