Stepper setSpeed 120x slower than programmed

262 views
Skip to first unread message

Jordan Watkins

unread,
Mar 28, 2017, 7:18:57 PM3/28/17
to accelstepper
Working on making a very simple micrometer driver. I need to start with an initial speed and accelerate over time. Ran into a problem with it decelerating after going half the distance, so I've added a check statement and doubled the distance.

But my problem is that the given initial speed that I thought I set with setSpeed is about 120 times lower than what my measured value was. Is there something I missing in my code or some math that I'm not thinking about? The speeds should all be in steps/second and the acceleration in steps/second^2.

Any help would be great!

#include <AccelStepper.h>

// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
AccelStepper stepper(4, 4, 5, 6, 7);

long checkdist = stepper.currentPosition();

void setup()
{
  Serial.begin(9600);
  Serial.println("Stepper test!");
  
  stepper.setMaxSpeed(0.36666667);
  stepper.setSpeed(0.06087957);
  stepper.setAcceleration(0.00002231);
}

void loop()
{
  checkdist = stepper.currentPosition();
  if(checkdist >= 2921){
    stepper.stop();
  }
  else
  {
    stepper.moveTo(6000);
    stepper.run();
  }
}

gregor

unread,
Mar 29, 2017, 3:36:44 AM3/29/17
to accelstepper
Hi,

http://www.airspayce.com/mikem/arduino/AccelStepper/classAccelStepper.html#ace236ede35f87c63d18da25810ec9736
Set the target position. The run() function will try to move the motor (at most one step per call) from the current position to the target position set by the most recent call to this function. Caution: moveTo() also recalculates the speed for the next step. If you are trying to use constant speed movements, you should call setSpeed() after calling moveTo().
this may be the cause of your problem. try this:

void setup()
{
 Serial.begin(9600);
 Serial.println("Stepper test!");
 
 stepper.setMaxSpeed(0.36666667);
 stepper.setAcceleration(0.00002231);

 stepper
.moveTo(6000);
 stepper.setSpeed(0.06087957);
}

void loop()
{
 
//slightly changed loop(), because stop() does not work that way and
 
//calling moveTo() and stop() very often is not necessary and/or may
 
//cause problems
  checkdist = stepper.currentPosition();
 if(checkdist < 2921){
   stepper.run();
 }
}

 

Jordan Watkins

unread,
Mar 29, 2017, 5:00:26 PM3/29/17
to accelstepper
gregor,

Thanks for the speedy reply. I implemented these changes and added a toggle switch to the code. I changed the final velocity to match the velocity that the motor should move at at its declared final position. I took another set of data and found that it's now only 9 times slower than what it should be. I will now check if increasing the final position and final speed affects the initial speed. Maybe if I set the final position and max velocity at large values it will more closely approximate what I want. Dropping a copy of the new code.

#include <AccelStepper.h>

// create an instance of the stepper 
// class and the pins it's attached to

AccelStepper stepper(4, 4, 5, 6, 7);

// Declarations

long checkdist = stepper.currentPosition();
int TGLPIN = 3;

void setup()
{
  // Digital Input
  pinMode(TGLPIN, INPUT);
  
  Serial.begin(9600);
  Serial.println("AccelStepper Micrometer");
  
  // set Max Speed and Acceleration
  stepper.setMaxSpeed(0.53333333);
  stepper.setAcceleration(0.00002231);

  // set final position and initial speed
  stepper.moveTo(6009);
  stepper.setSpeed(0.06087460);
}

void loop()
{
  checkdist = stepper.currentPosition();
  while(digitalRead(TGLPIN)==LOW){
    if(checkdist < 2921){
      stepper.run();
    }
  }
}

Jordan Watkins

unread,
Mar 31, 2017, 2:16:02 PM3/31/17
to accelstepper
Update:

Changing the maxspeed and moveto did not change the value of initial velocity. Will now try multiplying the setspeed (what i supposed as the initial speed) to 9 times larger and see if the effect is matched even if I don't understand why.

Code has not changed from previous iteration besides specific values of above mentioned variables.

Jordan Watkins

unread,
Apr 3, 2017, 1:51:49 PM4/3/17
to accelstepper
Update

Still no solution from what I understand. Here's what I tried:

maxspeed maxspeed maxspeed maxspeed maxspeed
0.97968568 0.97968568 0.97968568 0.97968568 0.97968568
moveto moveto moveto moveto moveto
21460 21460 21460 21460 21460
setspeed setspeed setspeed setspeed setspeed
0.558603907 0 0.0608746 0.03043730 0.30973925
checkdist checkdist checkdist checkdist checkdist
364 364 364 364 364
a 2.22442E-05 0 2.23317E-05 2.22142E-05 2.2225E-05
v 0.006731645 0 0.006446182 0.006857827 0.0068081

Setting the setspeed as a new value had little effect on the acceleration or initial velocity except when I set it to 0 which makes it not move either at all or at an extremely slow speed. Makes sense that the acceleration didn't change, but I'm clueless as to why I can't seem to get the initial speed to change. 

Any suggestions would be appreciated.

Jordan Watkins

unread,
Apr 4, 2017, 2:28:11 AM4/4/17
to accelstepper
I have found out that the setAcceleration value has a large effect on the initial velocity of the motor. 

Set Values
v    0.0000    0.1000    0.1000    0.1000
a    0.0000    0.0100    0.1000    1.0000

Measured Values
v    0.0000    0.1444    0.4801    1.3782
a    0.0000    0.0100    0.0993    1.0350

I must be missing something about the way the code works.
Reply all
Reply to author
Forward
0 new messages