fastaccelstepper-device

jdavis-nodes/fastaccelstepper/fastaccelstepper-device

No description
fastaccelstepper-device
@/fastaccelstepper-device
STEPport
DIRport
ENport
SPDnumber
ACCnumber
fastaccelstepper-device
STEP
DIR
EN
SPD
ACC
DEV
DEV@/fastaccelstepper-device
To use the node in your project you should have the jdavis-nodes/fastaccelstepper library installed. Use the “File → Add Library” menu item in XOD IDE if you don’t have it yet. See Using libraries for more info.

C++ implementation

#pragma XOD error_raise enable
#pragma XOD require "https://github.com/gin66/FastAccelStepper"

// Include the FastAccelStepper library
#include <FastAccelStepper.h>

node {
    meta {
        // Define the custom type as a pointer to FastAccelStepper
        using Type = FastAccelStepper*;

        // Static engine shared across all stepper instances
        static FastAccelStepperEngine& getEngine() {
            static FastAccelStepperEngine engine;
            static bool initialized = false;
            if (!initialized) {
                engine.init();
                initialized = true;
            }
            return engine;
        }
    }

    // Store the FastAccelStepper instance
    FastAccelStepper* stepper = nullptr;

    void evaluate(Context ctx) {
        // Only evaluate during setup
        if (!isSettingUp())
            return;

        // Validate the step and direction pins
        static_assert(isValidDigitalPort(constant_input_STEP), "must be a valid digital port for STEP");
        static_assert(isValidDigitalPort(constant_input_DIR), "must be a valid digital port for DIR");

        // Get input values
        auto stepPin = constant_input_STEP;
        auto dirPin = constant_input_DIR;
        auto enablePin = constant_input_EN;
        auto maxSpeed = getValue<input_SPD>(ctx); // Max speed in steps/s
        auto maxAccel = getValue<input_ACC>(ctx); // Max acceleration in steps/s^2

        // Get the shared engine
        FastAccelStepperEngine& engine = getEngine();

        // Create and configure the stepper
        stepper = engine.stepperConnectToPin(stepPin);
        if (stepper) {
            stepper->setDirectionPin(dirPin);
            if (enablePin != NOT_A_PIN) {
                stepper->setEnablePin(enablePin);
                stepper->enableOutputs();
            }
            stepper->setSpeedInHz(maxSpeed);
            stepper->setAcceleration(maxAccel);
        } else {
            raiseError(ctx); // Failed to initialize stepper
            return;
        }

        // Emit the stepper instance
        emitValue<output_DEV>(ctx, stepper);
    }
}