Acceleration with stepper motor

Hello, I am trying to implement acceleration and deceleration control as well as coordinate movement to control my stepper motors, and have come across this tutorial video from iforce2d https://www.youtube.com/watch?v=fHAO7SW-SZI&t=1989s and have read the sketch used to achieve linear coordinate control, it was well documented, however, I can't understand the part where he calculate the estStepsToSpeed, why the equation so complicate? How to derive it? I though it should be something simple like estStepsToSpeed = (v^2 - v0^2) / 2a?. Sorry for my bad english, since it not my first language.

struct stepperInfo {
  // externally defined parameters
  float acceleration;
  volatile unsigned long minStepInterval; // ie. max speed, smaller is faster
  void (*dirFunc)(int);
  void (*stepFunc)();

  // derived parameters
  unsigned int c0;                // step interval for first step, determines acceleration
  long stepPosition;              // current position of stepper (total of all movements taken so far)

  // per movement variables (only changed once per movement)
  volatile int dir;                        // current direction of movement, used to keep track of position
  volatile unsigned int totalSteps;        // number of steps requested for current movement
  volatile bool movementDone = false;      // true if the current movement has been completed (used by main program to wait for completion)
  volatile unsigned int rampUpStepCount;   // number of steps taken to reach either max speed, or half-way to the goal (will be zero until this number is known)
  volatile unsigned long estStepsToSpeed;  // estimated steps required to reach max speed
  volatile unsigned long estTimeForMove;   // estimated time (interrupt ticks) required to complete movement
  volatile unsigned long rampUpStepTime;
  volatile float speedScale;               // used to slow down this motor to make coordinated movement with other motors

  // per iteration variables (potentially changed every interrupt)
  volatile unsigned int n;                 // index in acceleration curve, used to calculate next interval
  volatile float d;                        // current interval length
  volatile unsigned long di;               // above variable truncated
  volatile unsigned int stepCount;         // number of steps completed in current movement
};

void resetStepper(volatile stepperInfo& si) {
  si.c0 = si.w;
  si.d = si.c0;
  si.di = si.d;
  si.stepCount = 0;
  si.n = 0;
  si.rampUpStepCount = 0;
  si.movementDone = false;
  si.speedScale = 1;

  float a = si.minStepInterval / (float)si.c0;
  a *= 0.676;

  float m = ((a*a - 1) / (-2 * a));
  float n = m * m;

  si.estStepsToSpeed = n;
}```

The AccelStepper library implements acceleration, and has been thoroughly tested. It is worth studying, even if you insist on doing everything by yourself.

Magic numbers like this, with no comment, are a sure sign of poor programming practice. It is a mistake to assume that anything you find on Youtube is good advice.

a *= 0.676;

2 Likes

Thanks for your help. I am currently using the accelstepper library in my project, the problem is that the library said that it not support both coordinate movement with acceleration and deceleration, iforce2d code achieved exactly what I need. That being said, I will still use the accelstepper library until someone help me understand the equation

The very best approach is to rewrite the code so that it executes an equation that you do understand, then test it to make sure it works as expected. Or write your own.

That goes for AccelStepper, too. If AccelStepper doesn't do what you want, add a function to the library that does! People do that all the time, and the world moves forward.

Why bother trying to figure out the other person's mistakes?

1 Like

you right, I should choose the way that work for me, not what work for others. Thanks again for taking time, your advice is very helpfull. I will update if my project work

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