#include "Motion86.h"
#include "Encoder.h"
#include "SD.h"
// Generate machine objects. You can use up to three machines, machine 0~2, each with three axes.。
Machine machine(0);
// Stepper Motor enable pin。
int EnablePin = 4;
// This gcode file is stored in the SD card.
char *filename = "auto.gcode" ;
File gfile;
char buf[256];
int ptr;
bool working;
// Set the preset feed rate ratio.
volatile double FeedrateRatio = 1.0;
bool enc_triggered = false ;
void encoder_callback( int flag);
void setup() {
while (!Serial);
SD.begin();
// In this example, the hand wheel has been installed on Enc3.
Enc3.begin(MODE_AB_PHASE);
Enc3.setRange(1); // The interrupt will be triggered every time
Enc3.attachInterrupt(encoder_callback);
pinMode(EnablePin, OUTPUT);
// If necessary, the direction of motion of the axis can be reversed.
// In this example, you need to reverse the direction of the x-axis and the y-axis.
machine.config_ReverseDirection(AXIS_X);
machine.config_ReverseDirection(AXIS_Y);
// PPU (pulse per unit) is a virtual unit of length, depending on different needs.
// In this example, the unit length of the x-axis is set to 80 pulses, which corresponds to 1 mm in actual application.
machine.config_PPU(AXIS_X, 80.0);
machine.config_PPU(AXIS_Y, 80.0);
machine.config_PPU(AXIS_Z, 1600.0);
// Set software limits for machine movement.
machine.config_PosLimit(AXIS_X, 0, 300);
machine.config_PosLimit(AXIS_Y, 0, 200);
machine.config_PosLimit(AXIS_Z, 0, 300);
// Set the pin used by the limit switch to set the home point.
machine.config_HomePins(2, 7, 8);
// Before controlling, the machine must be started.
machine.machineOn();
// Activate software limits.
machine.enableSoftLimit();
// Sets the feed rate to the home point.
machine.setHomeSpeed(1000, 1000, 200);
// Open the gcode file in the SD card
if (SD.exists(filename)) {
gfile = SD.open(filename);
working = true ;
} else {
Serial.print( "File not exists: " );
Serial.println(filename);
while (1);
}
// Start the stepper motor.
digitalWrite(EnablePin, LOW);
// Return to the home point defined by the limit switches.
machine.home();
}
void loop() {
// Read and parse gcode files.
if (working && !machine.isCmdBufferFull()) {
ptr = 0;
while (gfile.available()) {
buf[ptr] = gfile.read();
if (buf[ptr] == '\n' ) {
buf[ptr + 1] = '\0' ;
machine.gcode(buf);
break ;
} else {
ptr ++;
}
}
if (!gfile.available())
{
Serial.println( "GCODE FINISH" );
gfile.close();
working = false ;
}
}
// Override feed rate when interrupt occurs。
if (enc_triggered) {
if (FeedrateRatio > 2.0) FeedrateRatio = 2.0;
if (FeedrateRatio < 0.1) FeedrateRatio = 0.1;
machine.feedrateOverride(FeedrateRatio);
enc_triggered = false ;
Serial.print( "FeedrateRatio = " );
Serial.println(machine.getFeedrateOverride());
}
}
void encoder_callback( int flag) {
// The feed rate ratio should be greater than 0.
if (flag == INTR_OVERFLOW && FeedrateRatio < 2.0)
FeedrateRatio += 0.01;
else if (flag == INTR_UNDERFLOW && FeedrateRatio > 0.0)
FeedrateRatio -= 0.01;
enc_triggered = true ;
}
|