Skip to content

Error handling

No command of the library fails silently. Every command either works in the current state of the motor, or it reports why not and leaves the motor unchanged.

What a command returns

Every command returns an errorCode:

Value Meaning
errorCode::OK 0 the command was executed
a warning negative the command was executed with a restriction
an error positive the command was not executed; the motor is unchanged
errorCode err = motor.moveAbsAsync(5000);
if (err != errorCode::OK)
{
    Serial.printf("moveAbsAsync returned %d\n", (int)err);
}

isWarning(code) and isError(code) tell the two kinds apart, and describe(code) returns a short text for a code.

Warnings

Code Value Reported when
speedLimited −1000 a speed is outside the supported range, a speed at the target is above the maximum speed, or the speed of a move of a path is above what its members allow. The limit is used
accelerationLimited −1001 an acceleration is outside the supported range, or the acceleration of a group is above what its members allow. The limit is used
targetSpeedNotReached −1002 a move of a GroupPath is too short for its speeds. The group arrives as close to the speed at the targets as it can

Errors

Code Value Reported when
invalidArgument 1000 an argument is invalid, for example a group move with the wrong number of targets
motorBusy 1001 the motor is moving in another group, or on its own while a group with it is started
noTimer 1002 no hardware timer is free for this move, see Timers and interrupts
notSupported 1003 the command is not possible in this state, for example new targets for a moving group, or a hardware setting during a move
internal 1999 inconsistent state inside the library; please report it

The values are in a range of their own, so that a printed code identifies the library.

An error function

Checking every return value is tedious, and the setters return the motor and not a code. attachErrFunc() registers a function of the sketch that is called for every warning and every error:

/*==========================================================================
 * 10 Errors: find out when something does not work
 *
 * Every command returns an errorCode. Two ways to react: a) check the
 * returned code, b) attach a function that is called for every warning and error.
 *==========================================================================*/

#include "TeensyStep.h"
using namespace TeensyStep;

Stepper x(2, 3);
Stepper y(4, 5);
Stepper z(6, 7);

StepperGroup xy{x, y};
StepperGroup yz{y, z};

void onError(errorCode err) // b) called for every warning and error
{
    Serial.printf("TeensyStep reported %d\n", (int)err);
}

void setup()
{
    Serial.begin(115200);
    while (!Serial) {}

    attachErrFunc(onError);

    xy.moveAbsAsync({10'000, 5'000}); // y is now moving in group xy

    errorCode err = yz.moveAbsAsync({0, 3'000}); // a) y is busy: the command is refused
    if (err == errorCode::motorBusy)
    {
        Serial.println("yz could not start, waiting for xy");
        while (xy.isMoving()) {}
        yz.moveAbsAsync({0, 3'000});
    }
}

void loop()
{
}

The error function can be called from an interrupt, when the command that caused it was given from one. attachErrFunc(nullptr) removes the function.

The ready-made error handler

During development the simplest way is the handler that comes with the library:

void setup()
{
    Serial.begin(115200);
    attachErrFunc(ErrorHandler(Serial));
    ...
}
  • A warning is printed as W-1000: <text>, and the sketch continues.
  • An error is printed as E-1002: <text>. Then the handler stops all motors at once and blinks the built-in LED in an endless loop: the sketch is halted.

The handler follows the pattern of the one in TeensyTimerTool.