简体   繁体   中英

Stepper motor linear acceleration

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 )

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM