To use the node in your project you should have the jdavis-nodes/multistepperlite 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 require "https://github.com/gunakkoc/MultiStepperLite"
#include <MultiStepperLite.h>
node {
meta {
using Type = MultiStepperLite*;
}
MultiStepperLite steppers = MultiStepperLite(2); //initialize for 2 motors
// Adafruit_PN532 nfc = Adafruit_PN532(constant_input_IRQ, NOT_A_PORT);
void evaluate(Context ctx) {
auto idx = getValue<input_IDX>(ctx);
if (isSettingUp()) {
//SingleStepperLite stepper;
// Initialize pins and stepper
auto stepPin = constant_input_STEP;
auto dirPin = constant_input_DIR;
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(dirPin, LOW); // Default direction
steppers.init_stepper(idx,stepPin);
// emitValue<output_DEV>(ctx, &stepper);
}
// Handle STOP pulse
if (isInputDirty<input_STOP>(ctx)) {
steppers.stop(idx);
emitValue<output_DONE>(ctx, 1);
return;
}
// Handle PAUSE pulse
if (isInputDirty<input_PAUSE>(ctx)) {
steppers.pause(idx);
return;
}
// Handle GO pulse
if (isInputDirty<input_GO>(ctx)) {
auto pps = getValue<input_PPS>(ctx);
auto nSteps = getValue<input_N>(ctx);
// if (pps <= 0) {
// raiseError<output_ERR>(ctx);
// return;
// }
// Set direction based on nSteps sign
bool dir = nSteps < 0;
digitalWrite(getValue<input_DIR>(ctx), dir ? HIGH : LOW);
// Convert PPS to step interval (microseconds)
uint32_t stepInterval = 1000000 / pps;
if (nSteps == 0) {
steppers.start_continuous(idx,stepInterval);
} else {
steppers.start_finite(idx, stepInterval, abs(nSteps));
}
}
// Call do_tasks() unless paused or stopped
//if (stepper.is_running()) {
uint32_t now_us = micros();
steppers.do_tasks(now_us);
setImmediate();
//}
// Output position (remaining steps)
emitValue<output_POS>(ctx, steppers.get_remaining_steps(idx));
// Emit DONE if finished
if (steppers.is_finished(idx)) {
emitValue<output_DONE>(ctx, 1);
}
}
}