Go Down

Topic: accelerating stepper between 2 limit switches (Read 144 times) previous topic - next topic

ArthurK6

Hello!
Is there a way to accelerate stepper and to run it until it reaches limit switch? It has to be moving back and forth between 2 limit switches (distance between them is changeable).  I need it to achieve higher speeds because it tends to "freeze" on startup if started without acceleration, when there is load on stepper.
I am using accelstepper library.
Code: [Select]
if (condition == true)
  {
  if (digitalRead(10) == HIGH )
  {
    digitalWrite(5, LOW);
    CW = false;
  }
  if (digitalRead(9) == HIGH )
  {
    digitalWrite(5, HIGH);
    CW = true;     

  }
 if (CW == true){
   
    stepper.setSpeed(current_speed);
    stepper.setAcceleration(current_acc);
   
    stepper.run();
  }else {

    stepper.setSpeed((-1)*current_speed);
    stepper.setAcceleration(current_acc);
   
    stepper.run();
  }
   
 
  }

Robin2

Your code is missing any stepper.move() commands that tell the AccelStepper library how far the motor is to move. Without that stepper.run() will do nothing.

The library assumes that it will accelerate and decelerate the motor over the course of the specified number of steps. obviously there can be no deceleration when a limit switch is triggered - and an immediate stop might result in missed steps.

If an immediate stop is acceptable then you could trick the library by telling it to go a number of steps that is much greater than the actual distance so it has not started deceleration when the limit switch is triggered. Then stop calling stepper.run() and set the current position back to 0 (or whatever).

Of course if the limit switch is always at the same position you only need to set the zero position once and you could do that slowly in setup().

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

zenData

#2
Jan 11, 2018, 11:55 am Last Edit: Jan 11, 2018, 12:08 pm by zenData
if you want good speed you need a driver like the tb6600. rates at 4.5A. this also delivers more torque and speed than most equivalent drives.

it is controlled with a direction pins (fwd/rev) and a pwm input.

...sounds like you need to ramp down ur speed at the end.

quite simple, way to do this is:
1. count the steps from start to finish. then tell your drive to go full speed until you are near the end the slow then crawl.
 - so if you have 20 000 steps from start to end, you go full speed till you did 19 500 steps, then slow till 19 999 steps, then stop. (this way you dont even need a limit switch. if you really really need this, i will reply again)



see example below:

//stepper start and ramp down at end


//if your "steps / travel" are "less then the end step value"     and    "end step value subtract current steps value"    are greater or equal than    500 steps,   then direction fwd and full speed.
  if ((currentSteps < endSteps) && ((endSteps - currentSteps)  >= 500))
  {
  digitalWrite(stepperDirectionPin, LOW);
  analogWrite(stepperPwm, 255);            // fast
  }

  else if ((currentSteps < endSteps) && ((endSteps - currentSteps)  >= 250))     //  4
  {
  digitalWrite(stepperDirectionPin, LOW);
  analogWrite(stepperPwm, 125);             // half speed
  }

 else if ((currentSteps < endSteps) && ((endSteps - currentSteps)  >= 1))     //  0
  {
  analogWrite(stepperPwm, LOW);            //stop   
  }

...hope this helps!

MarkT

AccelStepper library has a stop() method for stopping.

Look at the AccelStepper library examples - the run() method must be run very frequently for the
library to function, you can't use delay() in your code, for instance.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up