4

I am working on a Python code (below) that accelerates a stepper motor until it reaches a specific amount of steps.

for s in range (steps):
    if s < accelerationsteps:
        lateststep = self.oneStep(direction, stepstyle)
        time.sleep(s_per_s)
        s_per_s = s_per_s - ((astart - aend) / accelerationsteps)

s_per_s = time in between each step

astart = starting speed in second/step (for example 0.5)

aend = speed at which the acceleration should stop (for example 0.05)

accelerationsteps = amount of steps over which the acceleration should happen

The problem is that the velocity increases in step per second per step instead of step per second per second, and is therefore increased exponentially instead of linear. I have found this article that explains in mathematical terms how one can achieve a linear increase with a Stepper Motor but I have not managed to translate that into my Python code.

I would highly appreciate it if someone could help me with this and I think it would be very useful for people using Steppers on the Raspberry Pi in general (I have only found a solution for the Arduino here)

1 Answer 1

4

One easy (though approximate) way is to make it all run by time, rather than by steps. So the time.sleep() period gets to be constant, and you keep track of the current speed and when it will next be time to step. So long as the time.sleep() period is significantly less than the time to do one step, you'll get fairly smooth acceleration.

Something faintly like:

accel = 20.0  # steps/sec/sec
time_passed = 0.000
steps_done = 0
cur_speed = 0  # steps/sec
time_for_next_step = 0.0

while (steps_done < steps_needed):
    if (time_passed >= time_for_next_step): 
        self.oneStep(direction, stepstyle)
        steps_done += 1
        time_for_next_step = time_passed + 1.0/cur_speed
    time.sleep(1);  # 1 millisecond, I assume
    time_passed += 0.001
    cur_speed += accel/1000.0

Because the delay period is constant, it also means that the overhead of the loop itself, is closer to a constant % increment to that delay, rather than growing as the delay shrinks. That makes things smoother.

I haven't tested this, but it should be close to right.... Hope it helps!

-steve

3
  • Oops, I forgot to increment steps_done, which will make this take a bit too long to finish... :(
    – TextGeek
    Commented Dec 16, 2015 at 21:24
  • Hi, thank you very much, I will make some trials with this. I think it's definitely a step in the right direction. I do think that as the acceleration modifies the total duration, I will need to guess how long the the acceleration takes, but I would like to be able to predict this. In my old formula I was able to say, accelerate over 20 of the 200 (total) steps (= 20% of the whole). Now I will end up guessing that maybe x seconds should fast be enough.
    – LuukS
    Commented Dec 17, 2015 at 12:21
  • Good point. You can probably measure that fairly reliably off the actual installed motor (of course it will depend on lots of physical variables). BTW, if interested check out the stackExchange for 3d printing that's trying to get started at area51.stackexchange.com/proposals/82438/3d-printing
    – TextGeek
    Commented Dec 17, 2015 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.