Stop function Stepper Motor

Hello,

I am working on a Project with a stepper motor. a small example of my code is:


// 400 step = 1 revolution -> linear motion.

#include <AccelStepper.h>


double receivedSteps = 0; //Number of steps
double receivedSpeed = 0; //Steps / second
double receivedAcceleration = 0; //Steps / second^2

char receivedCommand;

//-------------------------------------------------------------------------------
int directionMultiplier = 1; // = 1: positive direction, = -1: negative direction
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag

// direction Digital 9 (CCW), pulses Digital 10 (CLK)
AccelStepper stepper(1, 10, 9);
 
void setup()
{
  
    
    Serial.begin(9600); //define baud rate
    
    Serial.println("Demonstration of AccelStepper Library"); //print a messages
    Serial.println("Send 'C' for printing the commands.");

    //setting up some default values for maximum speed and maximum acceleration
    stepper.setMaxSpeed(6200); //SPEED = Steps / second -> default
    stepper.setAcceleration(1200); //ACCELERATION = Steps /(second)^2
    
    //aLastState = digitalRead(outputA); 
}
 
void loop()
{

    
    checkSerial(); //check serial port for new commands
    RunTheMotor(); //function to handle the motor 
 
}
 

void RunTheMotor() //function for the motor
{
    if (runallowed == true)
    {
        //stepper.enableOutputs(); //enable pins
        stepper.run(); //step the motor (this will step the motor by 1 step at each loop)  
    }
    
}


void StopTheMotor() //function for the motor
{
    if (runallowed == true)
    {
       receivedAcceleration = 1200.0;
       stepper.setAcceleration(receivedAcceleration);
       stepper.stop(); //stop motor 
       stepper.runToPosition();
       //counter = 0;
       runallowed = false; //disable running   
    }
}
 
void checkSerial() //function for receiving the commands
{  
    if (Serial.available() > 0) //if something comes from the computer
    {
        receivedCommand = Serial.read(); // pass the value to the receivedCommad variable
        newData = true; //indicate that there is a new data by setting this bool to true
 
        if (newData == true) //we only enter this long switch-case statement if there is a new command from the computer
        {
            switch (receivedCommand) //we check what is the command
            {
 
            case 'D': //P uses the move() function of the AccelStepper library, which means that it moves relatively to the current position.              

                receivedSteps = Serial.parseFloat(); //value for the steps
                receivedSteps = receivedSteps;
                receivedSpeed = Serial.parseFloat(); //value for the speed
                receivedSpeed = receivedSpeed;
                if(receivedSpeed > 6000) {
                receivedAcceleration = receivedSpeed*0.2; // update acceleration
                stepper.setAcceleration(receivedAcceleration);
                }
                if(receivedSpeed > 20000) { receivedSpeed = 20000;}
                directionMultiplier = 1; //We define the direction
                RotateRelative();
                break;
               
 
            case 'U': //N uses the move() function of the AccelStepper library, which means that it moves relatively to the current position.      
               
                receivedSteps = Serial.parseFloat(); //value for the steps
                receivedSteps = receivedSteps;
                receivedSpeed = Serial.parseFloat(); //value for the speed 
                receivedSpeed = receivedSpeed;
                if(receivedSpeed > 6000) {
                receivedAcceleration = receivedSpeed*0.2; // update acceleration
                stepper.setAcceleration(receivedAcceleration);
                }
                if(receivedSpeed > 20000) { receivedSpeed = 20000;}
                directionMultiplier = -1; //We define the direction
                RotateRelative();
                break;
 
                //example: N2000 400 - 2000 steps (5 revolution with 400 step/rev microstepping) and 500 steps/s speed; will rotate in the other direction
                //In theory, this movement should take 5 second
           
            case 'S': // Stops the motor

                StopTheMotor();
                break;

            case 'E': // Stops the motor but only use for Emergency

                stepper.stop();
                stepper.setSpeed(0);
                break; 
 
            default:  

                break;
            }
        }
        //after we went through the above tasks, newData is set to false again, so we are ready to receive new commands again.
        newData = false;       
    }
}
 
 
void RotateRelative()
{
    //We move X steps from the current position of the stepper motor in a given direction.
    //The direction is determined by the multiplier (+1 or -1)
   
    runallowed = true; //allow running - this allows entering the RunTheMotor() function.
    stepper.setMaxSpeed(receivedSpeed); //set speed
    stepper.move(directionMultiplier * receivedSteps); //set relative distance and direction
}
 

my Problem is that i want for any Emergency to stop the Motor without deceleration. if I use steopper.Setspeed(0) the Motor will Stop immediately but in the next command the Motor will try to continue the last instruction than excute the current instruction. is there any possibility to avoid this?

thank you all

Post that code! How can we help you fix the broken code if you post some other code? Is there a problem with the code you posted, and if so, what?

sure. the code is too long, that's why i didn't want to post any unrelated info

If you knew which part was unrelated to the problem, you would know what the problem is and would not have to ask.
You can post a shortened but executable code that shows the problem.

now it's a shortened executable code.
the case I am dealing now with, is when you run the code at some high speed and for some reason I need to stop the Motor immediately without deceleration. if I use the command E (setting the speed to 0 steps/sec) it works, but whenever i give another instruction -> the Motor make some steps according to the last instruction and than will execute the current command

I would set the acceleration to a very high value before calling stop(). Than it should stop immediately. Setting speed(0) isn't a good idea, because speed is internally overwritten by the run commands.

Which Arduino board are you using?

    stepper.setMaxSpeed(6200); //SPEED = Steps / second -> default

On AVR boards AccelStepper isn't able to reach such high speeds.

i did think about setting the Acceleration to a very high value and tried it. when the stepper Motor is running at a high speed the fast deceleration will bring it to clamp and loose steps.
i am using now a starter kit ( Elegoo Uno R3 _ ATMECA328P )

Of course you should use the fast acceleration only for an emergency stop. You will always loose steps in this case.

i thought maybe there's a possibility for a better Stop. like setting the speed to 0, which will cause an immediately Stop but solving the problem will be caused by this stop.

I really don't understand what your intention ist. You cannot stop a stepper from high speed immediately without loosing steps. That's not a problem with the software, it's mechanics..

i will try to explain better:

if the Motor is running at high speed and i want to avoid decelerating from this speed to 0 and directly stop it, i can use the function setSpeed(0), this will stop the Motor immediately. but as soon as i give an other command the Motor will rotate in the previous direction about a revolution and than execute the new command. my question was if i can solve this somehow. that i stop the motor with setspeed(0) and if i add a delay or anything else to the code so i can eliminate the extra rotation

I tried to explain you, that setSpeed(0) is NOT a good idea to stop the motor. And obviously you see why, but don't want to accept it.
You are manipulating internal values for normal operation with acceleration/braking. But Accelstepper 'knows' nothing about it, and actually wants to keep turning the motor. When using acceleration and deceleration, setSpeed() is internaly used by AccelStepper, und therefore you should not use it.

now it's clear. thank you

I do have a question regarding programming of the Motor-driver
I am thing about using Stepper-Motor-Shield-IFX9201-XMC1300 to save place. this driver requires using the library <IFX9201_XMC1300_StepperMotor.h>. however with this library i cannot use all the options of the acceleration and the code i have. do you think, that i can use both libraries simultaneously, I mean the configuration of the resolution, pin ..... and the commands of Accelstepper.h in the main code?

I do not know this board. But the datasheet says that it has a Step and Dir input. So it should be compatible with Accelstepper.

thank you, for you response, do you recommend any Board with small size and can be used with power supply 36 v and provide up to 6 A?

like this one Pololu High-Power Stepper Motor Driver 36v4

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.