Skip to content

Targets with a speed

A move command normally ends with the motor standing at its target. The optional second parameter of the move commands is the speed the motor shall have at the target:

motor.moveAbsAsync(10'000);        // stop at 10000
motor.moveAbsAsync(10'000, 2'000); // pass 10000 at 2000 steps/s and run on at that speed

The motor reaches the position exactly at that speed and then keeps it, like after rotateAsync(), until the next command.

The speed is normally given without a sign. The motor then passes the target in the direction in which it comes to it: counting up if the target is above the position of the motor, counting down if it is below. With a negative speed the motor passes the target in the opposite direction: it goes past the target, turns and comes back through it.

// the motor is at position 0
motor.moveAbsAsync(10'000,  2'000); // passes 10000 counting up
motor.moveAbsAsync(10'000, -2'000); // goes beyond 10000, turns, and passes 10000 counting down

What counts is where the motor is at the moment of the command. (A motor that is exactly at the target has no way to come from: it passes the target in the direction it is moving in, or counting up if it stands still, and the other way round with a negative speed.)

If the target is too close to reach the speed, the motor takes a run-up: it first moves away from the target, reverses, and then passes the target at the required speed. This can happen when a target is given or changed while the motor runs, and when a motor that stands still is given a target that is too close for the speed. A target far enough ahead needs no run-up.

The arrival function

onArrival() registers a function of the sketch that is called when the motor reaches a target. The function can give the next command, which then follows without a stop. In this way a motor runs through a series of positions, each with its own speed:

/*==========================================================================
 * 06 TargetWithSpeed: pass a position at a given speed, and chain moves
 *
 * The motor passes 10000 at 2000 steps/s and 40000 at 12000 steps/s, and
 * stops at 50000.
 *==========================================================================*/

#include "TeensyStep.h"
using namespace TeensyStep;

Stepper motor(2, 3);

struct Waypoint
{
    int32_t position; // steps
    float speed;      // steps/s at this position
};

const Waypoint track[] = {
    {10'000, 2'000},  // slow section: pass 10000 at 2000 steps/s
    {40'000, 12'000}, // fast section: pass 40000 at 12000 steps/s
    {50'000, 0},      // stop at the end
};
const unsigned nrOfWaypoints = sizeof(track) / sizeof(track[0]);

volatile unsigned next = 0;

void nextWaypoint() // called at every arrival, from an interrupt: keep it short
{
    if (next < nrOfWaypoints)
    {
        motor.moveAbsAsync(track[next].position, track[next].speed);
        next = next + 1;
    }
}

void setup()
{
    motor.setMaxSpeed(15'000).setAcceleration(20'000);
    motor.onArrival(nextWaypoint);

    nextWaypoint(); // start with the first waypoint
}

void loop()
{
}

The motor passes position 10,000 at 2000 steps/s, accelerates, passes 40,000 at 12,000 steps/s and stops at 50,000.

The arrival function is called from an interrupt

Keep it short, and do not use Serial, delay() or blocking move commands in it (1). The Async commands, the settings and the getters of the library may be used. Variables that the function shares with loop() have to be declared volatile.

  1. Interrupt context

    While the arrival function runs, the step pulses of the motors that use the same timer wait. A function that takes long delays these steps.

The arrival function is also called when a move without a speed ends at its target. It is not called after stop() or emergencyStop(). onArrival(nullptr) removes the function.

An arrival function can have the motor as its parameter. It is then called with the motor that has arrived, so that one function can serve several motors:

void arrived(Stepper& motor)   // called for x and for y
{
    motor.moveRelAsync(-1000); // the motor that has arrived goes back
}

void setup()
{
    x.onArrival(arrived);
    y.onArrival(arrived);
}

Blocking moves with a speed

moveAbs(pos, speed) and moveRel(delta, speed) return when the motor has reached the target. The motor is then still running at speed:

motor.moveAbs(10'000, 2'000); // returns when the motor passes 10000
motor.moveAbs(50'000);        // goes on from there and stops at 50000

Following a calculated path

A target with a speed is also the way to let a motor follow a path that is calculated while it moves: a timer calculates at short intervals where the motor has to be next and how fast it moves there. Because the motor arrives at each point with the right speed, it runs through the points smoothly. In the following sketch a motor swings back and forth like a pendulum:

/*==========================================================================
 * 07 Follower: a motor follows a calculated path
 *
 * The motor swings back and forth like a pendulum (a cosine); a timer gives
 * it a new target with a speed every 10 ms.
 *==========================================================================*/

#include "TeensyStep.h"
using namespace TeensyStep;

Stepper motor(2, 3);
IntervalTimer timer;

constexpr float dt        = 0.01f;         // s, period of the calculation
constexpr float amplitude = 5'000;         // steps
constexpr float omega     = TWO_PI * 0.5f; // rad/s: one swing forth and back in 2 s

void tick() // called every 10 ms
{
    static float t = 0;
    t += dt; // time of the next call

    float position = amplitude * cosf(omega * t);
    float speed    = fabsf(amplitude * omega * sinf(omega * t)); // without a sign: the motor passes the point
    motor.moveAbsAsync(position, speed);                         // in the direction in which it comes to it
}

void setup()
{
    motor.setMaxSpeed(20'000).setAcceleration(100'000);
    motor.moveAbs(amplitude); // to the start of the path

    timer.priority(255); // lowest priority: the calculation must not delay the step pulses
    timer.begin(tick, dt * 1'000'000);
}

void loop()
{
}

The timer runs every 10 ms and gives the motor the position and the speed of the curve for the time of its next call. The speed is given without its sign, so that the motor passes each point in the direction in which it comes to it. The sketch first moves the motor to the start of the curve, where the speed is 0, so that the motor does not have to catch up with a curve that is already moving.

The motor reaches every point at the right speed, but the library does not control when it gets there: it is on its way with the acceleration it is allowed. Set the acceleration of the motor well above what the curve needs (here about 50,000 steps/s²); the motor then stays close to the curve, in this sketch within about 70 of the 5000 steps.

The example 15_RoseFunction uses the same method for a slide that follows the angle of a rotating spindle and draws a rose curve.