faster way to setAcceleration

55 views
Skip to first unread message

Jonathan Newbrough

unread,
Apr 20, 2025, 2:33:38 PMApr 20
to accelstepper
Howdy!

I have a small addition that improves performance of the setAcceleration method for my use case.  Do you take pull requests?  Or feel free to add on my behalf if you think it would be useful.

I change acceleration frequently to coordinate multiple servos to reach a target at the same time.  I calculate and cache sqrt of a few different acceleration values ahead of time so I can pass the sqrt instead of acceleration directly.  Here is the method added to AccelStepper.cpp:

void AccelStepper::setAccelerationRoot(float sqrtAcceleration)
{
    float acceleration = sqrtAcceleration*sqrtAcceleration;

    if (acceleration == 0.0)
    return;
    if (_acceleration != acceleration)
    {
    // Recompute _n per Equation 17
    _n = _n * (_acceleration / acceleration);
    // New c0 per Equation 7, with correction per Equation 15
    //_c0 = 0.676 * sqrt(2.0 / acceleration) * 1000000.0; // Equation 15
    _c0 = 956008.36816 / sqrtAcceleration;
    _acceleration = acceleration;
    computeNewSpeed();
    }
}

Corresponding addition to AccelStepper.h:

    void    setAccelerationRoot(float sqrtAcceleration);


Thanks for the great lib!

Jonathan

gjgsm...@gmail.com

unread,
Apr 20, 2025, 7:51:39 PMApr 20
to accelstepper
Thanks  Jonathan, interesting... I could possibly have a use for this if I'm understanding it correctly.
Assuming it does a fast change of acceleration, while the stepper is moving, to affect its speed and hence arrival time at the nominated target to coincide with another stepper(s)? Or are you doing the calculations at the start of the movement?
Can you elaborate on its usage and what calculations you are doing to coordinate the steppers, maybe a code example...
Geoff

Jonathan Newbrough

unread,
Apr 20, 2025, 10:21:56 PMApr 20
to accelstepper
a fast change of acceleration, while the stepper is moving, to affect its speed and hence arrival time at the nominated target to coincide with another stepper

gjgsm:
...a fast change of acceleration, while the stepper is moving, to affect its speed and hence arrival time at the nominated target to coincide with another stepper..

Yes, exactly this.

are you doing the calculations at the start of the movement?

The calculations are before the movement, and updated in realtime.

Can you elaborate on its usage and what calculations you are doing to coordinate the steppers, maybe a code example...

My calling code will have a path of points over time, say (t, X, Y, Z) for times t0, t1, t2, ...  I want the servos to move such that:
1. They reach X, Y and Z at time t.  Report deviation if not exactly at that position.
2. All servos are accelerating/decelerating OR all are at a steady speed at the same time
3. Speed of each servo at t minimizes the acceleration/deceleration needed to reach the next point

Each servo has its own AVR chip, driver and a handful of sensors.  Clocks are synced with an interrupt.  A raspberry pi will send them each the next few points to follow, read deviation each reports from the last few points, and adjust the path accordingly.

I'll post some code once I have everything working.

I don't want to share a lib that uses a modified version of AccelStepper, so hopefully my change makes it into the repo.

Jonathan
 

Geoff Smith

unread,
Apr 21, 2025, 12:14:36 AMApr 21
to accels...@googlegroups.com
Thanks Jonathan, look forward to hearing more about it.

--
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.
To view this discussion visit https://groups.google.com/d/msgid/accelstepper/26fd11af-00b7-4c0e-9195-6d15a6f1c67en%40googlegroups.com.


--
Regards

Geoff Smith
Reply all
Reply to author
Forward
0 new messages