Best way to stop the motor

3,255 views
Skip to first unread message

Joe Varghese John

unread,
Apr 22, 2016, 4:45:33 PM4/22/16
to accelstepper
Hi All,

I am using stepper.moveTo() and stepper.run() in a loop to run the motor with acceleration. Its working fine.  But when I want to interrupt the movement, what is best way to do it.

1) when I use stepper.stop() to stop the motor, its stopping.  but when I start the next movement say in the opposite direction than before stopping, it moves in the original direction with max deceleration and after reaching zero speed, it travels back.

2) instead, before stopping if I use stepper.moveTo(stepper.currentPosition()+1) and then stepper.runToPosition() then what happens is that it decelerates with max rate (overshoots) and then comes back to the position specified.

both these makes logic, but is not what I am looking for.

What I need is...


1) if I use stop() command, I want the driver to forget previous deceleration pending so that when I start again, its a fresh one

or 

2) what is the best way to stop at the maximum deceleration speed but i dont want to come back.


What is the best practice for an aborting a movement in between.

Thanks for your help.
Joe

gregor

unread,
Apr 22, 2016, 5:00:03 PM4/22/16
to accelstepper
Hi,

On Friday, April 22, 2016 at 10:45:33 PM UTC+2, Joe Varghese John wrote:
2) what is the best way to stop at the maximum deceleration speed but i dont want to come back.
this is exactly what the stop() command does - it calculates a new target position for the stepper taking into account the acceleration.
You need to call stop() once and then use run() or runToPosition().

the example below shows how to use stop() correctly.
#include <AccelStepper.h>

// Define some steppers and the pins the will use
AccelStepper stepper1; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5

#define STOP_BTN A0    //input pin for stop button

bool curr_state, last_state;

void setup()
{  
    stepper1
.setMaxSpeed(200.0);
    stepper1
.setAcceleration(100.0);
    stepper1
.moveTo(2400000);

    pinMode
(STOP_BTN, INPUT_PULLUP);     //connect a normally open switch between A0 and Ground
    curr_state
= digitalRead(A0);
    last_state
= curr_state;
}

void loop()
{
  last_state
= curr_state;
 
  curr_state
= digitalRead(STOP_BTN);
 
if ((curr_state != last_state) && (curr_state == LOW))    //call stop() once when the button was pressed
    stepper1
.stop();
     
  stepper1
.run();
}


Joe Varghese John

unread,
Apr 23, 2016, 12:40:11 AM4/23/16
to accelstepper
Ohh, I didn't think that way. Thanks a lot Gregor. I got it now.

I was calling stop and breaking the loop. I was thinking that the stop made it to stop, but rather I didn't call run or run to position and that's why it stopped. My bad.

Thanks again for the wonderful help
Joe

Reply all
Reply to author
Forward
0 new messages