Trying to do a homing and then bouncing

Hi folks,

I am trying to create a simple program to control a stepper (by means of a A4988 driver assembled on a CNC Shield) that moves a leadscrew. I started using the ‘bounce’ example of AccelStepper library with some modifications, since bouncing a load is all I need.

The problem has appeared when I have tried to add some code to make a simple homing. My plan is is to do the homing without switches just moving the nut of the leadscrew to de mechanical hardstop. As I only need the homing to be done at the start, I have put this part of the code at the start. The problem is that it is not executed at all, the mechanism starts bouncing directly without moving slowly to the end stop beforehand.

Any help is welcome.

Thanks
Izar

#include <AccelStepper.h>

// for the Arduino Uno + CNC shield V3 + A4988 + FL42STH47-1684A

#define MOTOR_X_ENABLE_PIN 8
#define MOTOR_X_STEP_PIN 2
#define MOTOR_X_DIR_PIN 5

AccelStepper motor_X(1, MOTOR_X_STEP_PIN, MOTOR_X_DIR_PIN);

void setup()
{
motor_X.setEnablePin(MOTOR_X_ENABLE_PIN);
motor_X.setPinsInverted(false, false, true);

//HOMING: Move to the leadscrew hardstop

motor_X.setAcceleration(500); // Set Acceleration of Stepper
motor_X.move(-15000);
motor_X.setMaxSpeed(500); // Set Max Speed of Stepper (Slower to get better accuracy)
motor_X.run();

//Define the parameters for the bouncing movement on the loop

motor_X.setAcceleration(6000);
motor_X.move(4000);
motor_X.setMaxSpeed(2000);
motor_X.enableOutputs();
}

void loop()
{
if (motor_X.distanceToGo() == 0)
motor_X.moveTo(-motor_X.currentPosition());

motor_X.run();
}

To make it easy for people to help you please modify your post and use the code button </>

so your code looks like this and is easy to copy to a text editor. See How to use the Forum

What exactly happens when you try your program and what do you want it to do that is different?

The usual way to do homing is to move towards the limit switch one step at a time and check the switch between steps.

…R
Stepper Motor Basics
Simple Stepper Code

  motor_X.setAcceleration(500);  // Set Acceleration of Stepper
  motor_X.move(-15000);
  motor_X.setMaxSpeed(500);      // Set Max Speed of Stepper (Slower to get better accuracy)
  motor_X.run();

The run() method tells the class to determine if it is time to step ONCE. It may, or may not, be time to step. So, your stepper may, or may not, step ONCE.

There are other methods, such as runToPosition() to get the stepper to spin until it has stepped the required number of times.

Just spinning the motor until it slams the carriage into the physical limit sounds like you are trying to break the machine.

There is nothing in your code, even of were written correctly, that makes the stepper stop when some condition becomes true.

A limit switch is a must for a homing action. Looking at your code I saw nothing that would stop the motor when the end of travel is encountered. A recipe for damage.

A micro switch or a opto interrupter device is very inexpensive compared to the cost of a motor, power supply, stepper driver, or lead/ball screw and nut. All items that could suffer if driven onto a hard stop.

ROG

When I wrote Reply #1 I had not noticed that there was no limit switch.

I don't see anything wrong in principle with driving a stepper motor very slowly against a fixed end-stop as a means of establishing the home position. Of course the end-stop must be designed so that there will be no damage to any part of the machine.

...R

Robin2: I don't see anything wrong in principle with driving a stepper motor very slowly against a fixed end-stop as a means of establishing the home position. Of course the end-stop must be designed so that there will be no damage to any part of the machine.

...R

If the machine is designed with a jog switch, and there is a human pushing the switch, and the human is bright enough to stop pressing the switch when the mechanism is at the home position, I'd agree with you.

None of those apply to OP though.

PaulS: If the machine is designed with a jog switch, and there is a human pushing the switch, and the human is bright enough to stop pressing the switch when the mechanism is at the home position, I'd agree with you.

All of that is very sensible but I am not convinced that it is essential.

If the motor is moving slowly (which is essential) and it is set to do 500 steps but encounters the end stop after 300 steps it will just miss steps for the remaining 200 steps. Not very elegant - but I don't think the motor will come to any harm.

...R