Independent acceleration and deceleration values

911 views
Skip to first unread message

Sebastian Gracki

unread,
Feb 5, 2015, 5:40:55 PM2/5/15
to accels...@googlegroups.com
Hi all,

one thing that's missing in the AccelStepper lib and was asked for several times in this group is the possibility to set different values for acceleration and deceleration.
And since I also really like to have this feature, I added a deceleration value to the lib. It can be set independent from the acceleration in the attached version of the lib.

The deceleration value is used if:
 - stepper is reaching its target position.
 - you call stepper.stop();
 - you change the maxSpeed to a lower value. The stepper will decelerate smoothly now instead of instantly changing to the lower speed.

I also noticed, that the acceleration time is (although fixed in version 1.41) still ~10% longer than the exact calculated time.
I changed the calculation of _c0 with Equation 15 in the linked MIT paper, and voila, the difference is now only ~2%, which can be traced back to the approximations done in the timing calculations.
I checked it with random acceleration, deceleration and speed values in the attached example.

So feel free to use the extensions, try it (I checked it with some cheap chinese 28BYJ48 steppers), give some feedback and maybe the changes will be implemented in the next official version.

Salut

SG
AccelDecel.zip

Mike McCauley

unread,
Feb 6, 2015, 5:17:08 PM2/6/15
to accels...@googlegroups.com
Hi,

thanks for your note.

Im not sure I understand why different values for accel and decel rates are
required. I would have thought that if acceleration was required it should be
the same for accel and decel.

I would be more enthusiastic about this if you can put forward a real use
case.

Cheers.
--
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

Sebastian Gracki

unread,
Feb 7, 2015, 6:29:53 AM2/7/15
to accels...@googlegroups.com
Hi Mike,

that's my usecase: https://www.youtube.com/watch?v=4csMGd3cjfo

Actually I'm trying to build this clock on my own. Sure it will work with same acceleration and decelertion rates.
But in my opinion it looks nicer if the deceleration is much lower than acceleration.

However Equation 15 should be implemented into the lib, since the enumerator change from 2 to 1 in _c0 calculation is empiric.

SG

Sandy Noble

unread,
Feb 7, 2015, 7:22:57 AM2/7/15
to accels...@googlegroups.com
That clock is fantastic - would love to see a successful re-implementation. My tuppence-worth: adding features that have a very subjective benefit is a bit of feature creep. Each new feature adds to the maintenance and support load. Perfect application for a fork or a subclass maybe? I'm not sure what the best way to publish this kind of extension is.

sn

--
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.



--
Sandy Noble

Mike McCauley

unread,
Feb 7, 2015, 5:28:55 PM2/7/15
to accels...@googlegroups.com

Hi,

new version 1.45 implements this. Thanks for the 

suggestion.

 

Cheers.

bastian...@gmail.com

unread,
May 13, 2015, 11:20:56 AM5/13/15
to accels...@googlegroups.com
Hello,
I was wondering too whether acceleration and deceleration take place at the same rate when set through setAcceleration(...)
I assume it does, but I cannot find the above announced implementation that would permit me to set a different deceleration rate.

Thanks in advance
Bastian

Sandy Noble

unread,
May 13, 2015, 3:25:28 PM5/13/15
to accels...@googlegroups.com
Yes, they are the same - but I'm not sure what would happen if you changed the acceleration speed while the motor was running! Would be interested to try it and see though.


sn

--
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.



--
Sandy Noble

Peter Tjeerdsma

unread,
Aug 29, 2016, 3:52:53 AM8/29/16
to accelstepper
I've been looking for a deceleration feature like this, not so much for the separate deceleration value Sebastian implemented (though that is quite nice), but rather for deceleration from a faster to a slower speed in the same direction.

Mike, your last post here said "new version 1.45 implements this. Thanks for the suggestion."  But v1.53 does not decelerate in the same direction.  It also errors on 'class AccelStepper has no member named "setDeceleration([value]).  So I take it you were only referring to the Equation 15 issue, not Sebastian's overall deceleration features.

My sketch is based on the code you posted on another thread, which does just what I need with v1.53, except for the deceleration.  Maybe there's another way to get the same result?  I'd rather not run a hacked version of the library, no matter how clean it is!

Thanks,
Peter

//Set up Stepper Shield and Accel Library
//adapted from Adafruit_MotorShield / Accel_MultiStepper sample
#include <AccelStepper.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>


Adafruit_MotorShield AFMStop(0x60); // Default address, no jumpers
Adafruit_StepperMotor *myStepper = AFMStop.getStepper(200, 2);


void forwardstep2() {  
  myStepper
->onestep(FORWARD, DOUBLE);
}
void backwardstep2() {  
  myStepper
->onestep(BACKWARD, DOUBLE);
}


AccelStepper stepper(forwardstep2, backwardstep2);




void setup()
{  
 
AFMStop.begin(); // Start the top shield


 
Serial.begin(9600);
 
Serial.println("Accel / Decel test");
}




int runState = HIGH;
int faderSpeed = 0;
int faderAccel = 20;
int faderMaxspeed = 100;
#define INPUT_READ_INTERVAL 100
unsigned long last_input_time = 0;
int last_speed = 0;




void loop()
{




// Code adapted from https://groups.google.com/forum/#!topic/accelstepper/CVO28NaApKY
 
// Every INPUT_READ_INTERVAL milliseconds, read inputs.
 
// We do this infrequently to prevent interfering
 
// with the stepper motor high speed stepping
 
// Get the current joystick position as analog value




 
unsigned long current_time = millis();
 
if (current_time - last_input_time > INPUT_READ_INTERVAL)
 
{
   
int joystick_in = analogRead(0);
   
// Map the raw analog value to speed range from -MAX_SPEED to MAX_SPEED
   
int desired_speed = map(joystick_in, 0, 1023, -faderMaxspeed, faderMaxspeed);
   
if(desired_speed != last_speed) {
     
Serial.println(desired_speed);
      last_speed
= desired_speed;
   
}


   
// Based on the input, set targets and max speed
    stepper
.setMaxSpeed(abs(desired_speed*3));
    stepper
.setAcceleration(faderAccel);
    stepper
.setDeceleration(faderAccel); //Only works with modified library from https://groups.google.com/forum/#!topic/accelstepper/kAT9ZG2IRhM
   
   
if (desired_speed == 0 && stepper.speed() == 0)
   
{
     
// Prevent running off the end of the position range
        stepper
.setCurrentPosition(0);
   
}
   
else if (desired_speed < 0)
   
{
      stepper
.moveTo(-1000000000);
   
}
   
else if (desired_speed > 0)
   
{
      stepper
.moveTo(1000000000);
   
}
    last_input_time
= current_time;
 
 
}


 
if (runState) stepper.run();
 
else myStepper->release();


}

gregor

unread,
Aug 29, 2016, 5:56:10 AM8/29/16
to accelstepper
Hi, 

new version 1.45 implements this. Thanks for the 

suggestion.

refers to 
However Equation 15 should be implemented into the lib, since the enumerator change from 2 to 1 in _c0 calculation is empiric.

You can try to calculate the the deceleration by yourself and periodically update the maximum speed.
Reply all
Reply to author
Forward
0 new messages