IsRunning

845 views
Skip to first unread message

Andrew Hooper

unread,
Dec 26, 2015, 11:22:49 PM12/26/15
to accelstepper
I have been trying to create a machine that uses a single stepper and an encoder for feedback and have found that I am doing a lot of checks to see if the motor is at the position. I guess  my application is a little backward as I am using the encoder in a kind of closed loop double checking that the motor reaches the target by syncing moth motor and encoder.

I was however wondering if adding a "IsRunning" function so that if the motor is running then any changes to the destination are skipped until IsRunning == 0. Obviously if the motor is heading to a destination IsRunning = 1.

In my code I am doing something like this..

    case 92:
    //BONUCE - Short Strokes
     digitalWrite(Enable_Pin, LOW);           
    // Extend the shaft dirX = 0
    // read the encoder and if it is not >= target then increase the step on the motor.
    if((stepper1.readEnc() <= 50) && (dirX == 0)){
      //move the motor forward
      stepper1.moveTo((50 * 3.19)+1);
    }else{
    dirX = 1;
    }
    // reverse the direction of shaft
    if(dirX == 1){
      // Retract the shaft dirX = 1
      // read the encoder and if it is not <= 1 then increase the step on the motor. 
      if(stepper1.readEnc() >= 5){
           //move the motor backward
        stepper1.moveTo(5);  //bring it just past the zero point to be sure
      }else{
        dirX = 0;
      }
    }  
    break;

And I have found that trying to use the stepper1.distanceToGo() seems to not work resulting in the destination being updated every cycle.

I Would appreciate any thoughts on this. 

Sandy Noble

unread,
Dec 27, 2015, 6:33:27 AM12/27/15
to accels...@googlegroups.com
You could check if the speed is non-zero to see if the motor is running. distanceToGo() is reliable though - but remember that if you are moving at a low speed, you will not necessarily take a step every cycle, so it's correct that the distanceToGo() would not change after each cycle.

I'll mention that run() itself returns a true or false, where true indicates a step was taken, and false indicates no step was taken this time, so you could look at that, and only bother checking the encoder if run() suggested a step was taken.

Last thing, I came across this while writing https://github.com/euphy/AccelStepperEncoder, and that there's a physical lag between the motor being signalled to step, the motor actually moving, and the motion being transferred to the encoder (and the actual thing you're moving, moving!). In my case, I have quite a sloppy drive chain, so I have a lot of stretch and wiggle between the drive and the encoder. But the process of taking the step, and then not seeing any change on the encoder reminded me that while the software moves in discrete, instantaneous jumps, the physical nature of the beast is more complex!


sn

--
You received this message because you are subscribed to the Google Groups "accelstepper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to accelstepper...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Sandy Noble

Andrew Hooper

unread,
Dec 27, 2015, 6:47:05 AM12/27/15
to accelstepper


On Monday, 28 December 2015 00:33:27 UTC+13, Sandy Noble wrote:
You could check if the speed is non-zero to see if the motor is running. distanceToGo() is reliable though - but remember that if you are moving at a low speed, you will not necessarily take a step every cycle, so it's correct that the distanceToGo() would not change after each cycle.

I'll mention that run() itself returns a true or false, where true indicates a step was taken, and false indicates no step was taken this time, so you could look at that, and only bother checking the encoder if run() suggested a step was taken.

Last thing, I came across this while writing https://github.com/euphy/AccelStepperEncoder, and that there's a physical lag between the motor being signalled to step, the motor actually moving, and the motion being transferred to the encoder (and the actual thing you're moving, moving!). In my case, I have quite a sloppy drive chain, so I have a lot of stretch and wiggle between the drive and the encoder. But the process of taking the step, and then not seeing any change on the encoder reminded me that while the software moves in discrete, instantaneous jumps, the physical nature of the beast is more complex!


sn

Thanks Sandy,
I am using the AccelStepperEncoder and seems to work well for my project :)

    case 93:
    //BONUCE - Random Moves
    digitalWrite(Enable_Pin, LOW); // turn on the drived
if((stepper1.readEnc() >= 50) && (stepper1.distanceToGo() == 0)){
  dirX = 1;
  Serial.println("Shaft IN");
}
if((stepper1.readEnc() <= 5) && (stepper1.distanceToGo() == 0)){
  dirX = 0;
  Serial.println("Shaft OUT");
}
    
    if((stepper1.readEnc() < 50) && (dirX == 0)){
      //move shaft out
      stepper1.moveTo((50* 3.19)+1);  //bring it just past the zero point to be sure
    } else {
//      dirX = 1;
    }
    if((stepper1.readEnc() > 5) && (dirX == 1)){
      //move shaft in
      stepper1.moveTo(5);  //bring it just past the zero point to be sure
    } else {
//      dirX = 0;
    }


Your description would explain why I am getting multiple responses from the above code.
The rest of the code is working but the above part was going to move the shaft a random distance out and a random distance our but the stepper1.distanceToGo(), I went backwards to something simple to see where the issue was and this appears to have been the problem.

I will try and pull the true/false from the run and see if that works a little better in the morning :)

Thanks again.
 

gregor christandl

unread,
Dec 27, 2015, 6:51:45 AM12/27/15
to accelstepper

Hi,
run() returns true when the stepper has not yet reached its destination, even if the stepper was not stepped. runSpeed() returns true if the stepper was stepped.

Sandy Noble

unread,
Dec 27, 2015, 7:11:57 AM12/27/15
to accels...@googlegroups.com
Gregor is right, that's my mistake. Been a while :) 

Andrew Hooper

unread,
Dec 27, 2015, 4:15:51 PM12/27/15
to accelstepper
Well i tried this and it kind of works but at the apex of the strokes the motor vibrates im guessing until the run() command catches up.

    //BONUCE - Random Moves
    digitalWrite(Enable_Pin, LOW); // turn on the drived
if((stepper1.readEnc() >= 50) && (stepper1.run() == 0)){
  dirX = 1;
//  Serial.println(stepper1.run());
}
if((stepper1.readEnc() <= 5) && (stepper1.run() == 0)){
  dirX = 0;
//  Serial.println(stepper1.run());
}


Andrew Hooper

unread,
Dec 27, 2015, 8:40:24 PM12/27/15
to accelstepper
Now this was a bit of a long winded way of doing things but it seems to be working. Feel free to make any suggestions that could trim this down a little or make it a little more reliable. 
Great thing is that if the shaft jams in either direction and steps are skipped it will catch up.

    case 93:
    //BONUCE - Random Moves
    digitalWrite(Enable_Pin, LOW); // turn on the driver

if((stepper1.readEnc() >= thisrndmove) && (dirX == 0)){
  dirX = 1;
  lastrndmove = random(5, 50); //store the last random move.
//  Serial.print("Shaft Out ");
//  Serial.println(stepper1.readEnc());
}
if((stepper1.readEnc() <= lastrndmove) && (dirX == 1)){
  dirX = 0;
  thisrndmove = random(20, 100); //store the last random move.
//  Serial.println("Shaft In ");
//  Serial.println(stepper1.readEnc());
}
    
    if((stepper1.readEnc() < thisrndmove) && (dirX == 0)){
      //move shaft out
      stepper1.moveTo((thisrndmove * 3.19)+1);  //bring it just past the zero point to be sure
    }
    if((stepper1.readEnc() > lastrndmove) && (dirX == 1)){
      //move shaft in
      stepper1.moveTo((lastrndmove * 3.19)+1);  //bring it just past the zero point to be sure
    }

   break;


Mike McCauley

unread,
Dec 30, 2015, 10:07:16 PM12/30/15
to accels...@googlegroups.com
Thanks,

So is isRunning() still required?

Cheers.
--
Mike McCauley VK4AMM mi...@airspayce.com
Airspayce Pty Ltd 9 Bulbul Place Currumbin Waters QLD 4223 Australia
http://www.airspayce.com
Phone +61 7 5598-7474

Andrew Hooper

unread,
Jan 1, 2016, 9:17:12 PM1/1/16
to accelstepper

Thanks,

So is isRunning() still required?


Hi Mike, Not required but would be handy :)
The GRBL source i use for another project has a similar function that you just check to see if the motors are running before issuing another command. its nice and fast and saves interrupting any of the other processes going on. pe perfect for a Switch/Case :)

Mike McCauley

unread,
Jan 1, 2016, 10:13:35 PM1/1/16
to accels...@googlegroups.com, Andrew Hooper
OK, isRunning added to new version 1.49.

Cheers.
Reply all
Reply to author
Forward
0 new messages