Accelstepper change end position while moving

Hey all,

I’m trying to make a hand follow device for some research project.
One of the things the devices should be able to do is to stop going to an end position and move to a new position from where it was.

code snap…

// stepperX was moving to 1, because of a trigger stop moving and go to 12000
stepperX.stop();
stepperX.moveTo(12000);
and call
stepperX.run();

what happens is that the motor stops but then still moves towards the 1 for a few seconds and then switches direction and goes to 12000.

This is my code:

Homeing in setup works its the part in loop that failes

#include <AccelStepper.h>

// Define the Pins used
#define home_switchX 3 // x end switch
#define home_switchY 14 // y end switch
// Define some steppers and the pins the will use
AccelStepper stepperX(1, 54, 55);
AccelStepper stepperY(1, 60, 61);

long initial_homing=-1;  // Used to Home Stepper at startup


void setup()
{  
    pinMode(38 , OUTPUT);
    pinMode(56 , OUTPUT);
    digitalWrite(38 , LOW);
    digitalWrite(56 , LOW);

    pinMode(home_switchX, INPUT_PULLUP);
    pinMode(home_switchY, INPUT_PULLUP);

    delay(5);  // Wait for EasyDriver wake up

    
    Serial.begin(115200);
    stepperX.setMaxSpeed(3000.0);
    stepperX.setAcceleration(1000.0);
        
    stepperY.setMaxSpeed(3000.0);
    stepperY.setAcceleration(100.0);

    // homeing x
     while (digitalRead(home_switchX)) 
     {  // Make the Stepper move CCW until the switch is activated   
        stepperX.moveTo(initial_homing);  // Set the position to move to
        initial_homing--;  // Decrease by 1 for next move if needed
        stepperX.run();  // Start moving the stepper
        delay(5);
     }
      stepperX.setCurrentPosition(0);  // Set the current position as zero for now
      initial_homing=1;
      while (!digitalRead(home_switchX)) 
      { // Make the Stepper move CW until the switch is deactivated
         stepperX.moveTo(initial_homing);  
         stepperX.run();
         initial_homing++;
         delay(5);
      }
  
      stepperX.setCurrentPosition(0);

      // homeing y
     while (digitalRead(home_switchY)) 
     {  // Make the Stepper move CCW until the switch is activated   
        stepperY.moveTo(initial_homing);  // Set the position to move to
        initial_homing--;  // Decrease by 1 for next move if needed
        stepperY.run();  // Start moving the stepper
        delay(5);
     }
      stepperY.setCurrentPosition(0);  // Set the current position as zero for now
      initial_homing=1;
      while (!digitalRead(home_switchY)) 
      { // Make the Stepper move CW until the switch is deactivated
         stepperY.moveTo(initial_homing);  
         stepperY.run();
         initial_homing++;
         delay(5);
      }
  
      stepperY.setCurrentPosition(0);
       stepperX.setMaxSpeed(300000.0);
    stepperX.setAcceleration(10000.0);
        
    stepperY.setMaxSpeed(300000.0);
    stepperY.setAcceleration(10000.0);
}

void loop()
{
  long TravelX;
  long TravelY;
  

    if(!digitalRead(home_switchY)) //end switch abused as input to move between position 1 and 12000
    {
     stepperX.stop();
     stepperX.moveTo(12000);
    }
    if(!digitalRead(home_switchX)) //end switch abused as input to move between position 1 and 12000
    {
     stepperX.stop();
     stepperX.moveTo(1);
    }
    bool delayQ=true;   
    if ((stepperX.distanceToGo() != 0)) {
        stepperX.run();
        delayQ=false;
    }
    if ((stepperY.distanceToGo() != 0)) {
        stepperY.run();
        delayQ=false;
    }

   
}

AFAIK the stepper.stop() function works through the necessary deceleration to bring the motor to a stop - hence it is not immediate. If acceleration is necessary then an immediate stop risks missing steps.

If acceleration is not necessary then you probably don't get much advantage from the AccelStepper library.

...R
Stepper Motor Basics
Simple Stepper Code

The code is all wrong.

Don’t use while loops, >>> call run() for every motor each time through loop() <<<, and simply call
moveTo() once when you want to change target, it will all magically work, complete with
ramping up and down. You can call stop() if you want to stop immediately.

Never call delay() in an AccelStepper sketch, that’s guaranteed to foul things up, and
never call any other blocking function or use a loop that blocks (if you have to use
a loop, then put calls to run() for every motor in that loop too, otherwise you freeze-out
AccelStepper completely)

Hello Mark,

The while loops are taken from an example i found (stepper_test_met_ramps Created by Yvan / https://Brainy-Bits.com). In de setup function i'm homeing the stepper. In the loop function i'm not using while loops which indeed would mess things up. And the home works perfect

What i find strange is that the motor does stop briefly then accelerates again and slows down again to eventually change direction.

What i need to do is receive data from the computer two longs and go to that position, if new values come in they overrule the old ones, so changing direction.

kind regards,
Tom

tdeweyer:
What i need to do is receive data from the computer two longs and go to that position, if new values come in they overrule the old ones, so changing direction.

Have you considered Reply #1 ?

...R

I'm aware that the stop function is with deacceleration. If stop is called should i wait before calling run? And is there a way to now if the motor stopped?
Or should i just call moveto with a new position, and will accelstepper handle the acceleration needed to change direction?

Thanks
Tom

tdeweyer:
I'm aware that the stop function is with deacceleration. If stop is called should i wait before calling run? And is there a way to now if the motor stopped?

Most (all?) of that can be answered by studying the AccelStepper documentation and writing some short test programs.

Have you considered the distanceToGo() function?

...R

tdeweyer:
I’m aware that the stop function is with deacceleration. If stop is called should i wait before calling run?

What part of “>>> call run() for every motor each time through loop() <<<” did you have
problems with?!?

Ditto “Never call delay()”

Always call run() all the time, as rapidly as possible, which is why its should be every time round loop()
and why you shouldn’t use any blocking code that would prevent run() being called, and certainly
never delay.

Thanks for the replies,

I was using distancetogo for another part but not in de loop.
I will check the manual again and try to see what i overlooked.

kind regards,
Tom