Seems like extra steps are being sent by AccelStepper

1,882 views
Skip to first unread message

Ray Kimber

unread,
Oct 23, 2017, 12:11:58 PM10/23/17
to accelstepper

Hi, I'm experiencing a problem using some simple functions of AccelStepper.  Since I'm just starting out using AccelStepper, I tried a simple test of making the stepper motor go CW and CCW the same number of steps (100).  The problem is that as it cycles back and forth it slowly advances further CW, i.e., it never really returns to the starting point (0).  This "drift" might be 1-2 steps each cycle.  It's pretty slow.

 

I'm using an Arduino Uno, a NEMA 17, 0.4,A 12V, 4-wire Bipolar 56.6oz.in stepper motor, a TB6600 Stepper Motor Driver from the Robot Shop (Product Code : RB-Dfr-727), and a 12V, 3A power supply.

 

My code is:

 

#include <AccelStepper.h>

 

const int pinSTEP=3;

const int pinDIR=4;

 

AccelStepper stepper(1, pinSTEP, pinDIR);

 

void setup()

{ 

  stepper.setMaxSpeed(200.0);

  stepper.setAcceleration(2500.0);

  stepper.moveTo(100);

}

 

void loop()

{

    if(stepper.distanceToGo() == 0)

    {

//    stepper.stop();  // I tried this but it didn't help

      stepper.moveTo(-stepper.currentPosition());

//    delay(10);         // // I tried this also but it didn't help

    }

    stepper.run();

}

 

I read through all of the relevant questions raised on the AccelStepper Google Group and tried different ideas from there as well as everything I could think of with no success including

 

  1. Using different commands (e.g., move vs. moveTo)

  2. Various combinations of max speeds and accelerations (max speed up to 1000, accel up to 12500)

  3. Different run commands (run, runToPosition, etc.)

  4. Load (a thin aluminum 12” rail) and no load (bare shaft)

  5. Adding a short delay (a few msec) between when the direction is changed and the run command (see above)

  6. Using my own position variables, so as not to depend on currentPosition()

  7. Using floating point numbers for max speed and acceleration

  8. Up to 16:1 microstepping and no microstepping

  9. Adding a stop() command when distanceToGo == 0 is true (see above)

     

    When I try some basic code (see below) to accomplish the same thing without AccelStepper, the problem doesn't occur.  This suggests that there's nothing wrong with the motor, driver, Arduino, and how they're wired.

     

    // defines pins numbers

    const int stepPin = 3; 

    const int dirPin = 4;

    const int SPEED = 1000; // this is the width of the pulses 

     

    void setup()

    {

       // Sets the two pins as Outputs

       pinMode(stepPin,OUTPUT); 

       pinMode(dirPin,OUTPUT);

       pinMode(LED_BUILTIN, OUTPUT);

    }

     

    void loop() {

      digitalWrite(dirPin,HIGH); // Enables the motor to move in a particular direction

       // Makes 200 pulses for making one full cycle rotation (no microstepping)

       for(int x = 0; x < 200; x++)

      {

         digitalWrite(stepPin,HIGH); 

         delayMicroseconds(SPEED); 

         digitalWrite(stepPin,LOW); 

         delayMicroseconds(SPEED); 

      }

      digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)

      delay(1000); // One second delay

      

      digitalWrite(dirPin,LOW); //Changes the rotations direction

      // Makes 400 pulses for making two full cycle rotation

      for(int x = 0; x < 200; x++)

      {

          digitalWrite(stepPin,HIGH);

          delayMicroseconds(SPEED);

          digitalWrite(stepPin,LOW);

          delayMicroseconds(SPEED);

      }

      digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW

      delay(1000);

    }

     

Thanks in advance for any ideas about what's going on.

 

Ray Kimber

 

Mike McCauley

unread,
Oct 23, 2017, 5:13:35 PM10/23/17
to accels...@googlegroups.com
Try with a lower max speed and acceleration 

Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Raymond Kimber

unread,
Oct 23, 2017, 5:22:29 PM10/23/17
to accels...@googlegroups.com

still does it with max speed = 100/acc = 1000, and also with max speed = 50/acc = 250, all with no microstepping (200 steps/rev) and no load (bare motor shaft).

 

Ray Kimber

1.      Using different commands (e.g., move vs. moveTo)

2.      Various combinations of max speeds and accelerations (max speed up to 1000, accel up to 12500)

3.      Different run commands (run, runToPosition, etc.)

4.      Load (a thin aluminum 12” rail) and no load (bare shaft)

5.      Adding a short delay (a few msec) between when the direction is changed and the run command (see above)

6.      Using my own position variables, so as not to depend on currentPosition()

7.      Using floating point numbers for max speed and acceleration

8.      Up to 16:1 microstepping and no microstepping

9.      Adding a stop() command when distanceToGo == 0 is true (see above)

--
You received this message because you are subscribed to a topic in the Google Groups "accelstepper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/accelstepper/ZGCEFtEHOkM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to accelstepper...@googlegroups.com.

Ray Kimber

unread,
Oct 23, 2017, 5:26:53 PM10/23/17
to accelstepper

still does it with max speed = 100/acc = 1000, and also with max speed = 50/acc = 250, all with no microstepping (200 steps/rev) and no load (bare motor shaft).



Ray Kimber

unread,
Oct 23, 2017, 11:44:51 PM10/23/17
to accelstepper

I tried a more basic sketch and discovered that one extra step is sent each time the move/run commands are executed.  With no microstepping 200 steps is a complete rev.  If I move CW 200 and then CCW, I drift to the CW direction 2 steps (one for each move/run);  If I move CW 200-1 steps and CCW -200-1 steps, I return to the starting point after each move.  The code is


#include <AccelStepper.h>
const int pinSTEP=3;
const int pinDIR=4;
bool flag = false;
AccelStepper stepper(1, pinSTEP, pinDIR);
void setup()

  stepper.setMaxSpeed(200.0);
  stepper.setAcceleration(500.0);
  stepper.move(200);
}
void loop()
{
  if(stepper.distanceToGo() == 0)
  {
    if(flag)
    {
      stepper.move(200-1);
      flag = false;
    }
    else
    {
      stepper.move(-200-1);
      flag = true;
    }
    delay(500);
  }
  stepper.run();
}
 

Bart Van der Haagen

unread,
Nov 2, 2017, 2:57:31 PM11/2/17
to accelstepper
have you tryed to set the acceluration speed as high as the maxspeed.  also if you give a command for 100 steps with a ecceluration of 1000 it will never reach maxspeed. 

Op maandag 23 oktober 2017 18:11:58 UTC+2 schreef Ray Kimber:

Ray Kimber

unread,
Nov 3, 2017, 2:34:32 PM11/3/17
to accelstepper

Thanks for the suggestion, Bart.  I tried several more combinations of max speed and acceleration as you suggested:

          max speed          acceleration        #of steps in move command
               500                     500                        200
               200                     200                        200
                  20                      20                        200
             1000                   1000                        200
             1000                   3000                        200

In each case the result was the same:  there would be a 1-step CW drift with each move step executed.  And in each case (I only tried the 2nd, 4th and 5th cases) when I reduced the move arguments by 1 step (from 200 to 200-1 and -200 to -200-1, respectively), the 1-step drift stopped.  I choose 200 steps for each test case so that the motor shaft would make a complete revolution with each move statement (the motor had no microstepping configured).  As I've said in previous posts, this problem occurred whether I used microstepping or not.  My code is

#include <AccelStepper.h>
const int pinSTEP=3;
const int pinDIR=4;
bool flag = false;
int k;
AccelStepper stepper(1, pinSTEP, pinDIR);
void setup()

  stepper.setMaxSpeed(1000.0);
  stepper.setAcceleration(1000.0);
  stepper.move(200);
}
void loop()
{
  if(stepper.distanceToGo() == 0)
  {

Ray Kimber

unread,
Nov 3, 2017, 3:08:26 PM11/3/17
to accelstepper
I have a question for the AccelStepper developers:  can anybody duplicate this problem I'm having?  It's a pretty simple test, just using a move command to move CW one rotation and CCW one rotation and see if it comes back to its original position each time.  And if it doesn't come back to the starting point each time, and it's not just something wrong in the setup, or in the understanding of how the software works, it would seem that there's a bug in the software.  -- Ray Kimber
 

Mike McCauley

unread,
Nov 3, 2017, 4:53:39 PM11/3/17
to accels...@googlegroups.com
HEllo,

is it possible you have the polarity of your step pulse incorrect?
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474

Ray Kimber

unread,
Nov 8, 2017, 7:14:08 PM11/8/17
to accelstepper

I found the problem.  I hooked up a scope to the step input to my driver to see if the Arduino was putting out the same number of steps for each move cycle (CW and CCW).  It turned out it was, which suggested there was nothing wrong with the software.  But I noticed that the pulses were very narrow, which led me to wonder if their pulse width was too short duration for the circuitry.  I read through the AccelStepper.h file and saw that there was a setMinPulseWidth function, and that 20 usec was a typical width.  I inserted the setMinPulseWidth(20) statement in the setup and the problem went away (I commented it out and the problem returned). 


I tested the fix with various speeds, accelerations, and microstep settings and it worked fine.  Problem solved.

Thanks to everybody that responded. 

 

Ray Kimber

 

Elkaid Kaidus

unread,
Apr 4, 2020, 10:11:02 AM4/4/20
to accelstepper
Thanks Ray,

I know it's been a while but you saved my day with your finding as I was experiencing the same issue as yours using TB6600 drivers

Thanks again and have a great day

Raymond Kimber

unread,
Apr 4, 2020, 11:34:54 AM4/4/20
to accels...@googlegroups.com

Glad it helped.  Stay safe.

 

Ray Kimber

--

You received this message because you are subscribed to a topic in the Google Groups "accelstepper" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/accelstepper/ZGCEFtEHOkM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to accelstepper...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages