accelstepper not reaching the desired position

Hello all

I am having a bit of a problem with accelstepper.. I use the following code:

  positions[0] = destinationTheta;
  positions[1] = destinationRho;

  steppers.moveTo(positions);
  int temp;
  while (steppers.run())
  {
    temp ++;
    if (temp > 50000)
    {      
      Serial.println(destinationTheta);
      Serial.println(destinationRho);
      Serial.println(steppertheta.currentPosition());
      Serial.println(stepperrho.currentPosition());
      temp = 0;
    }
  }

the output is as follows:

8000.00
4800.00
165
99
8000.00
4800.00
329
198
8000.00
4800.00
494
296
...
...
...
...
8000.00
4800.00
7999
4799
8000.00
4800.00
7999
4799
8000.00
4800.00
7999
4799

The motors stop and they never reach the target position. and steppers.run() keeps returning true..
Any idea why? what could cause this sort ofproblem?

Thank you!!

Please post your entire sketch.

If you are using an 8-bit Arduino, like an UNO, Nano, Mini, Leonardo, Micro, MEGA... then this is a problem:

  int temp;
  while (steppers.run())
  {
    temp ++;
    if (temp > 50000)

You can't get to 50000 in a 16-bit signed integer.

Also: You didn't initialize 'temp' so it has random contents.

I think you meant:

  unsigned long temp = 0;

If anyone else wants to do some debugging, this compiles without warnings or errors:

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


AccelStepper steppertheta(AccelStepper::FULL2WIRE, 2, 3);
AccelStepper stepperrho(AccelStepper::FULL2WIRE, 4, 5);
MultiStepper steppers;

const long destinationTheta = 100;
const long destinationRho = 100;
long positions[2];


void setup()
{
  Serial.begin(115200);


  steppers.addStepper(steppertheta);
  steppers.addStepper(stepperrho);
  
  positions[0] = destinationTheta;
  positions[1] = destinationRho;


  steppers.moveTo(positions);
  
  unsigned long temp = 0;
  while (steppers.run())
  {
    temp ++;
    if (temp > 50000)
    {
      Serial.println(destinationTheta);
      Serial.println(destinationRho);
      Serial.println(steppertheta.currentPosition());
      Serial.println(stepperrho.currentPosition());
      temp = 0;
    }
  }
}
void loop() {}
positions[0] = destinationTheta;
positions[1] = destinationRho;

steppers.moveTo(positions);

Based on the above, where do you expect it to move to? Because the destination you specify (positions) is actually the memory address of the first element of the positions array, and NOT the value of the first element of the array.

What you want is:

steppers.moveTo(positions[0]);

RayLivingston:
Based on the above, where do you expect it to move to? Because the destination you specify (positions) is actually the memory address of the first element of the positions array, and NOT the value of the first element of the array.

The 'steppers' object must be a MultiStepper which takes a pointer to an array of longs.

johnwasser:
The 'steppers' object must be a MultiStepper which takes a pointer to an array of longs.

Can't tell that from the snippet....

i checked its working.number increasing

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

#define STEPPER1_DIR_PIN 49
#define STEPPER1_STEP_PIN 47
// The 2 stepper pins
#define STEPPER2_DIR_PIN 53
#define STEPPER2_STEP_PIN 51

// Define some steppers and the pins the will use
AccelStepper stepper1(AccelStepper::DRIVER, STEPPER1_STEP_PIN, STEPPER1_DIR_PIN);
AccelStepper stepper2(AccelStepper::DRIVER, STEPPER2_STEP_PIN, STEPPER2_DIR_PIN);
MultiStepper steppers;

const long destination1 = 1000;
const long destination2 = 1000;
long positions[2];


void setup()
{
  Serial.begin(115200);


  steppers.addStepper(stepper1);
  steppers.addStepper(stepper2);
  
  positions[0] = destination1;
  positions[1] = destination2;


  steppers.moveTo(positions);
  
  unsigned long temp = 0;
  while (steppers.run())
  {
    temp ++;
    if (temp > 50000)
    {
      Serial.println(destination1);
      Serial.println(destination2);
      Serial.println(stepper1.currentPosition());
      Serial.println(stepper2.currentPosition());
      temp = 100;
    }
  }
}
void loop() {}

sharkyenergy:
the output is as follows:

165, 99

329, 198
494, 296
...
...
...
...
7999, 4799
7999, 4799
7999, 4799




The motors stop and they never reach the target position.

I think I can explain why they are stopping one short and possibly why 'run()' is still returning 'true'.

From the way destinationTheta and destinationRho print out you can tell they are 'float' or 'double' variables. It is likely that when destinationTheta was initialized to 8000, the closest valid 'float' number was like 7999.9990, which prints as 8000.00 when rounded but stores in the integer 'positions' as 7999 when truncated. It is also likely that when destinationRho was initialized to 4800, the closest valid 'float' number was like 4799.9990, which prints as 8000.00 when rounded but stores in the integer 'positions' as 4799 when truncated.

As for run() continuing to return 'true': the 'run()' method returns 'true' if the 'distanceToGo()' (targetPosition - currentPosition) on any of the steppers return a non-zero number. It is NOT based on 'currentPosition()' alone. Apparently, according to this comment in MultiStepper.cpp, they can get out of step. Try changing '#if 0' to '#if 1' to enable the work-around.

	// Caution: it has een reported that if any motor is used with acceleration outside of
	// MultiStepper, this code is necessary, you get 
	// strange results where it moves in the wrong direction for a while then 
	// slams back the correct way.
#if 0
	else
	{
	    // Need to call this to clear _stepInterval, _speed and _n, otherwise future calls will fail.
		_steppers[i]->setCurrentPosition(_steppers[i]->currentPosition());
	}
#endif

Im struggling from many days.i tried every thing i could.can anyone help me how to run this multiple steps with different speed in loop.

    //both wheel forward
    stepper1.setMaxSpeed(1000);
    stepper1.setAcceleration(500);
    stepper2.setMaxSpeed(1000);
    stepper2.setAcceleration(500);
    if (digitalRead(buttonPin) == LOW){
     stepper1.moveTo(3200);
     stepper2.moveTo(3200);
     stepper1.run();
     stepper2.run();

    //both wheel backward
    stepper1.setMaxSpeed(800);
    stepper1.setAcceleration(500);
    stepper2.setMaxSpeed(800);
    stepper2.setAcceleration(500);
    if (digitalRead(buttonPin) == LOW){
    stepper1.moveTo(-3200);
    stepper2.moveTo(-3200);
     stepper1.run();
     stepper2.run();

    
    //one wheel forward another backward with slower speed
    stepper1.setMaxSpeed(600);
    stepper1.setAcceleration(300);
    stepper2.setMaxSpeed(600);
    stepper2.setAcceleration(300);
    stepper1.moveTo(3200);
    stepper2.moveTo(-3200);
     stepper1.run();
     stepper2.run();

     
    //one wheel backward and another forward with slower speed
    stepper1.setMaxSpeed(600);
    stepper1.setAcceleration(300);
    stepper2.setMaxSpeed(600);
    stepper2.setAcceleration(300);
    stepper1.moveTo(-3200);
    stepper2.moveTo(3200);
     stepper1.run();
     stepper2.run();

The ‘run()’ function has to be called many times. Calling it once won’t get you to the destination. If you are moving more than one servo you have to call the ‘run()’ function for each servo until all have reached their destination. The function returns ‘true’ if it has not reached the destination so it should work if you change:

     stepper1.run();
     stepper2.run();

to

     while (stepper1.run() || stepper2.run()) {}

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