ustepper

hi guys,

can i please get some advice on the ustepper limit detection sketch as the motor doesn't stop...it crashes.
i have looked on git hub and cant make head nor tails out of it.

i'm attempting to make a linear rail with limit detection for the home position
any help appreciated

bennett4455:
i have looked on git hub and cant make head nor tails out of it.

At least you have a link to it and can look at it. Imagine how in the dark we are right now with no clue what code you are talking about.

Have you tested your limit switch(es) with a simple test sketch that does nothing else?

Much easier for everyone if you post the program here. This seems to be it

#include <uStepper.h>
#define MICROSTEP 16//microstep setting of uStepper
#define RES (200.0*MICROSTEP)/360.0//calculate step pr. degree
#define STEPPRMM 53.55//step pr. mm for the rail used in the demo
#define MMPRSTEP 1/STEPPRMM//mm pr. step
#define MMPRDEG MMPRSTEP*RES//mm pr. degree

uStepper stepper;

void setup() {
  // put your setup code here, to run once:
  stepper.setup();
  stepper.setCurrent(15.0);//use software current setting
  stepper.setMaxAcceleration(20000);
  stepper.setMaxVelocity(4000);
  Serial.begin(115200);
}

float limit()
{
  int checks=0;
  float pos=0.0;
  stepper.runContinous(CCW);
  while(checks < 3)//allows for 2 checks on movement error
  {
   pos = abs(stepper.encoder.getAngleMoved()-(stepper.getStepsSinceReset()*0.1125));//see current position error
   if(pos<5)//if position error is less than 5 steps it is okay...
    {
      checks=0;
    }
    else //if position error is 5 steps or more, count up checks
    {
      checks++;
    }
  }  
  checks=0;//reset checks for next endstop detection
  stepper.hardStop(SOFT);//stop motor without brake
  stepper.moveSteps(5*STEPPRMM, CW, SOFT);//back off with 5 mm
  while(stepper.getMotorState())//wait for motor to finish move
  {
    delay(1);//because Arduino does not accept the empty while loop for some reason....
  }
  stepper.encoder.setHome();//set new home position
  stepper.runContinous(CW);//do check in opposite direction
  while(checks < 3)//allows for 2 checks on movement error
  {
   pos = abs(stepper.encoder.getAngleMoved()-(stepper.getStepsSinceReset()*0.1125));//see current position error
   if(pos<5)//if position error is less than 5 steps it is okay...
    {
      checks=0;
    }
    else //if position error is 5 steps or more, count up checks
    {
      checks++;
    }
  }  
  stepper.hardStop(SOFT);//stop
  stepper.moveSteps(5*STEPPRMM, CCW, SOFT);//back off with 5 mm
  while(stepper.getMotorState())//wait for motor to finish move
  {
    delay(1);//because Arduino does not accept the empty while loop for some reason....
  }
  pos = stepper.encoder.getAngleMoved();//read current position
  stepper.moveSteps((pos*RES)/2, CCW, SOFT);//go to center
  while(stepper.getMotorState())//wait for motor to finish move
  {
    delay(1);//because Arduino does not accept the empty while loop for some reason....
  }
  return pos;//return max position
}

void loop() {
  Serial.println(limit()*MMPRDEG);//find end positions and read out the recorded end position
  stepper.setup(PID,SIXTEEN,10,5,2.0,2.0,0.6);//enable PID
  stepper.setCurrent(100.0);//use software current setting
  while(1);
}

…R

You also need to post a link to the documentation for the uStepper library.

There are no Serial.print() statements in the code to allow you to see what is happening - add some and tell us what they say.

The mind boggles at the need for a limit detection function to return a float.

…R
Stepper Motor Basics
Simple Stepper Code

The mind boggles at the need for a limit detection function to return a float

That alone should ring warning bells that the code was written by some one who did not know what they were doing.

Also comments like this

delay(1);//because Arduino does not accept the empty while loop for some reason....
  }

Are simply wrong. Of course you can put no statements into a while loop and it still works, I do it all the time.

Finally anyone who leaves in the default sketch comments is some one not to be followed.

hi again thanks for the reply's :slight_smile:

I was under the impression gaining feedback from the stepper motor could be used instead of limit switches, is this not the case?

I was under the impression gaining feedback from the stepper motor could be used instead of limit switches, is this not the case?

This is not the case, you can not obtain any feedback from the motor.

A normal DC motor will show a rise in current when it stalls and this can be used as a limit detector but this is not the case with a stepping motor because the maximum current it draws is when it is stopped, so you can't tell anything from the current draw from the motor. Still their is no way you get the current drawn from a motor it has to be done with external circuitry anyway.

that put stop to that then..... back to the drawing board
thanks for your input