I am creating a scanning system where I am using a stepper motor to raise a scanner to an endstop, then lowering the scanner until it measure a certain distance away from an object. I have the code fully working, but am struggling getting the motor to spin faster without stalling.
My setup is a typical 3D printer z-axis. I took an ender 3 z motor and 4 start lead screw and ender 3 frame and I am using an arduino mega to run the z motor and read in an endstop switch at the top of the frame.
I am running the arduino into a TMC2209 motor driver, using STEP and DIR to run the stepper motor. Like I said, everything works fine, but the raising and lowering of the scanner is SLOW. I extracted just the motor control portion of my code to play around and see if I can speed things up. That code is below.
You can see the stepperRPM is set at 720 and this works great and really speeds things up when I hook up the motor wires to a free standing motor, but when I plug in my z axis motor it struggles and sometimes gets into state where the raising, or lowering stutters, like it is skipping steps and the only way to fix is to power everything off for a minute and restart, and it works for a bit, then stutters again.
I bought a higher torque motor, thinking I had to much load on the motor, but it doesn't appear this is the case. What I am trying to raise and lower is much, much lighter than a 3d printer x gantry. I haven't tried the higher torque motor yet, hoping to get some help here.
Do I need to accel/decel? Is there a better motor control library to use than the Basic Stepper Driver Library?
One other question that I have is that when I use the stepper.rotate() function, no matter the number I put in there, like 360 or 720, it doesn't actually rotate the full degrees before running the next line of code, so that is why I have the for loop of 100. Maybe I don't understand the stepper.rotate() function and how it actually works.
Any help would be appreciated. Thanks.
#include "BasicStepperDriver.h"
// Stepper Motor Driver Outputs
#define stepsPerRev 200
#define stepperRPM 720
#define stepperMicrosteps 1
#define stepperMotorDirOut 5
#define stepperMotorStepOut 6
#define stepperMotorEnableOut 7
const int16_t raiseRotationAngle = 360;
const int16_t lowerRotationAngle = -360;
BasicStepperDriver stepper(stepsPerRev, stepperMotorDirOut, stepperMotorStepOut, stepperMotorEnableOut);
void setup(void)
{
Serial.begin(9600);
stepper.begin(stepperRPM, stepperMicrosteps);
stepper.setEnableActiveState(LOW);
// Set motor driver outputs
pinMode(stepperMotorEnableOut, OUTPUT);
pinMode(stepperMotorStepOut, OUTPUT);
pinMode(stepperMotorDirOut, OUTPUT);
}
void loop()
{
digitalWrite(stepperMotorEnableOut, HIGH);
//Serial.println("Start of Loop");
stepper.enable();
for(int x = 0; x<100; x++)
{
stepper.rotate(raiseRotationAngle);
Serial.print("Rotation Count: ");
Serial.println(x);
}
for(int x = 0; x<100; x++)
{
stepper.rotate(lowerRotationAngle);
Serial.print("Rotation Count: ");
Serial.println(x);
}
stepper.disable();
}