Weird behaviour when using moveTo negative position and then run()

899 views
Skip to first unread message

Paul Hurley

unread,
Oct 1, 2015, 5:48:11 PM10/1/15
to accelstepper
I'm using the AccelStepper library with a Pololu style DRV8825 driver.  I have seen weird behaviour when trying move negative values (i.e counter clockwise) that I think I've narrowed down to run() (because runSpeedToPosition() works )

If I run this minimal example, the motor spins continously, slowing in speed.  It never stops.  literally.

//Minimum Working Example for accelstepper problem

#define MOTHERBOARD 3  // AccelStepper Driver compatible (easydriver/pololu)
#include <Wire.h>

#include <MultiStepper.h>
#include <AccelStepper.h>


#include <Arduino.h>

AccelStepper m1(1,2,3); //initialise accelstepper for a two wire board, pin 5 step, pin 4 dir

void setup() {
  Serial.begin(115200);
  // put your setup code here, to run once:
  m1.setEnablePin(4);                       //These give compilation errors that these don't exist !
  m1.setPinsInverted(false, false, true);   //For boards that enable=low
  m1.setAcceleration(10000);                  //set acceleration, even though we only single step
  m1.setMaxSpeed(10000);
  m1.enableOutputs();

void loop() {
  Serial.print("Loop ");
 
  int i;
  long pos = -50;
  Serial.print("M1 cp"); Serial.println(m1.currentPosition());
  m1.moveTo(m1.currentPosition()+pos);
  m1.setSpeed(1000);
  Serial.print("M1 dtg"); Serial.println(m1.distanceToGo());
  while (m1.distanceToGo() != 0) {
        m1.run();
        Serial.print("M1 dtg"); Serial.println(m1.distanceToGo());
     }
 
 
  delay(500);
 
}

However, If I change it to this example, it works as expected (i.e it moves 50 steps anti clockwise, then pauses and repeats, just at a constant speed)

//Minimum Working Example for accelstepper problem

#define MOTHERBOARD 3  // AccelStepper Driver compatible (easydriver/pololu)
#include <Wire.h>

#include <MultiStepper.h>
#include <AccelStepper.h>


#include <Arduino.h>

AccelStepper m1(1,2,3); //initialise accelstepper for a two wire board, pin 5 step, pin 4 dir

void setup() {
  Serial.begin(115200);
  // put your setup code here, to run once:
  m1.setEnablePin(4);                       //These give compilation errors that these don't exist !
  m1.setPinsInverted(false, false, true);   //For boards that enable=low
  m1.setAcceleration(10000);                  //set acceleration, even though we only single step
  m1.setMaxSpeed(10000);
  m1.enableOutputs();

void loop() {
  Serial.print("Loop ");
 
  int i;
  long pos = -50;
  Serial.print("M1 cp"); Serial.println(m1.currentPosition());
  m1.moveTo(m1.currentPosition()+pos);
  m1.setSpeed(1000);
  Serial.print("M1 dtg"); Serial.println(m1.distanceToGo());
  while (m1.distanceToGo() != 0) {
        m1.runSpeedToPosition();
        Serial.print("M1 dtg"); Serial.println(m1.distanceToGo());
     }
 
 
  delay(500);
 
}

I don't see this problem with positive values of pos (i.e positive / clockwise movements)

Any ideas ?

Paul.

gregor

unread,
Oct 1, 2015, 6:06:44 PM10/1/15
to accelstepper
the only thing that i see at first glance is that you use setSpeed() in your loop.
setSpeed() is for setting a constant speed and is meant to be used with runSpeed() or runSpeedToPosition(), but not with run(). 

  Serial.print("M1 cp"); Serial.println(m1.currentPosition());
  m1.moveTo(m1.currentPosition()+pos);
  m1.setSpeed(1000);    //remove this line

  Serial.print("M1 dtg"); Serial.println(m1.distanceToGo());
  while (m1.distanceToGo() != 0) {
        m1.run();
        Serial.print("M1 dtg"); Serial.println(m1.distanceToGo());
     }

also, you could use move() instead of moveTo():
m1.move(pos);  //equivalent to m1.moveTo(m1.currentPosition()+pos);

and you could also replace your while loop with this:
while(m1.run())  {   //run() returns true when the stepper is still moving towards its target position.

Sandy Noble

unread,
Oct 2, 2015, 4:17:52 PM10/2/15
to accels...@googlegroups.com

Your target is negative, so run() will usually attempt to calculate a negative speed. You're manually setting a positive speed, so instructing it to move _away_ from the target. These are quite conflicting messages, so I'm not surprised that the behaviour is odd. What would you expect to happen in this case?

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.
Reply all
Reply to author
Forward
0 new messages