I am working on a system that uses a stepper motor. It is a 1000mm linear rail. I have a DM542T stepper driver. I want it to move 250mm and then stop for a few seconds, then move another 500mm and stop for a few seconds, and then come back to the starting position. The starting position is in the very corner from where it first started moving. I am running into a few problems. This is my first time using stepper motors.
-
I use this formula where i provide the distance in mm and it runs the steps according to it.
"targetPosition_steps = targetPosition_mm / TRAVEL_PER_REVOLUTION * STEPS_PER_REVOLUTION;''
However, instead of working at 250 mm and 500mm, it works at 960 and 1235 respectively. -
It doesn't go back to the starting position but goes back to the 250mm position.
-
If I shut off the driver or unplug the motor, it starts moving in a completely different way than it is intended to.
I have provided the code below. Right now, It stops at the positions I mentioned but goes further after the second position and comes back to stop at 250mm. I have no idea why because it is supposed to stop at those two positions and come back.
Please let me know how to fix this and what i am doing wrong.
#include <AccelStepper.h>
#include <EEPROM.h> // Include EEPROM library
// Define stepper motor connections and parameters
#define STEP_PIN 9
#define DIR_PIN 8
#define STEPS_PER_REVOLUTION 200 // Assuming a standard 1.8 degree stepper motor
#define TRAVEL_PER_REVOLUTION 1 // Assuming 1mm travel per revolution of the lead screw
// Create stepper motor object
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
// Define the original position
const int ORIGINAL_POSITION_ADDRESS = 0; // EEPROM address to store original position
int originalPosition = 0; // Variable to store original position
void setup() {
// Set maximum speed and acceleration
stepper.setMaxSpeed(1000); // Set the maximum speed in steps per second
stepper.setAcceleration(1000); // Set the acceleration in steps per second^2
// Read original position from EEPROM
originalPosition = EEPROM.read(ORIGINAL_POSITION_ADDRESS);
// Set initial position to original position
stepper.setCurrentPosition(originalPosition);
}
void loop() {
// Store the original position
originalPosition = stepper.currentPosition();
// Move the motor to 250mm position in forward direction
moveStepperToPosition(960);
delay(1000); // Stop for 1 second
// Move the motor to another 500mm position in forward direction
moveStepperToPosition(1235);
delay(1000); // Stop for 1 second
// Move the motor back to the original position
moveStepperToPosition(originalPosition);
}
void moveStepperToPosition(float targetPosition_mm) {
int targetPosition_steps = targetPosition_mm / TRAVEL_PER_REVOLUTION * STEPS_PER_REVOLUTION;
// Determine direction based on target position relative to current position
if (targetPosition_steps > stepper.currentPosition()) {
stepper.setPinsInverted(false, false, true); // Set direction to move forward
} else {
stepper.setPinsInverted(false, false, false); // Set direction to move backward
}
stepper.moveTo(targetPosition_steps);
// Run the motor until it reaches the target position
while (stepper.distanceToGo() != 0 || abs(stepper.currentPosition() - targetPosition_steps) > 1) {
stepper.run();
}
}