Moving two stepper motor with cnc shield and accelstepper

Hi,
I'have got some troubles with my programme.
All my connexion are good, everything have been test before.
Two NEMA 17 stepper motors are driven by a Shield cnc shield and two a4988.
Both engines must, at each call of the "deplace" function, move a certain number of steps (argument of the function "deplace") multiplied by the "coef" int.
They must move for the same duration for each call, the speed of the one who makes the smallest number of steps therefore adapts to the speed of the one who makes the bigger number in order to arrive at there target position at the same time.

That works when x>y or when x=y. :slight_smile:

But it doesn't work when y>x. :frowning:

Does someone could explain me wath is wrong with my program ? :confused:

//Deux moteurs pas à pas NEMA 17 sont piloté par un cnc Shield et deux a4988.
//Les deux moteurs doivent, à chaque appel de la fonction "deplace", de déplacer d'un certain nombre de pas (argument de la fonction déplace) multiplié par le coef. 
//Ils doivent se déplacer durant la même durée pour chaque appel, la vitesse de celui qui fait le plus petit nombre de pas s'adapte donc à la vitesse de celui qui fait le plus grand nombre.


#include <AccelStepper.h>

AccelStepper Xaxis(1, 2, 5); // pin 2 = step, pin 5 = direction
AccelStepper Yaxis(1, 3, 6); // pin 3 = step, pin 6 = direction
int coef=200; // coef multiplicateur utile pour le programme final


void setup() {
  delay(1000); // Moment s'attente pour observer les moteurs
  Xaxis.setMaxSpeed(1000);
  Yaxis.setMaxSpeed(1000);
  
 deplace(3,6);
 
 deplace(9,3);
 
 deplace(4,1);
  
 deplace(-8,5);

 deplace(3,6);

 deplace(3,-3);
 
 deplace(1,2);
 
 deplace(3,6);

 
  
  }
   


void loop() {  
}

void deplace(int x,int y) {
  Xaxis.move(x*coef);
  Yaxis.move(y*coef);
  if(x>y){
    Xaxis.setSpeed(1000);
    Yaxis.setSpeed(1000/x*y);
    while(Xaxis.distanceToGo()!=0){
    Xaxis. runSpeedToPosition();
    Xaxis.move(Xaxis.distanceToGo());
    Yaxis. runSpeedToPosition();
    Yaxis.move(Yaxis.distanceToGo());
    }
  }
  else if (x=y){
    Xaxis.setSpeed(1000);
    Yaxis.setSpeed(1000);
    while(Xaxis.distanceToGo()!=0){
    Xaxis. runSpeedToPosition();
    Xaxis.move(Xaxis.distanceToGo());
    Yaxis. runSpeedToPosition();
    Yaxis.move(Yaxis.distanceToGo());
    }
  }
  else {
    Xaxis.setSpeed(1000/y*x);
    Yaxis.setSpeed(1000);
    while(Yaxis.distanceToGo()!=0){
    Yaxis. runSpeedToPosition();
    Yaxis.move(Yaxis.distanceToGo());
    Xaxis. runSpeedToPosition();
    Xaxis.move(Xaxis.distanceToGo());
   
    }
  }
  delay(1000);
}

Two NEMA 17 stepper motors

But what color are they? The NEMA 17 standard describes the mounting hole locations. Why do you think that is important for us to know?

void deplace(int x,int y) {
  Xaxis.move(x*coef);
  Yaxis.move(y*coef);
  if(x>y){
    Xaxis.setSpeed(1000);
    Yaxis.setSpeed(1000/x*y);

Is integer division biting you in the posterior? Change 1000 to 1000.0 and see if that makes a difference.

    while(Xaxis.distanceToGo()!=0){
    Xaxis. runSpeedToPosition();
    Xaxis.move(Xaxis.distanceToGo());
    Yaxis. runSpeedToPosition();
    Yaxis.move(Yaxis.distanceToGo());
    }

Do you KNOW what runSpeedToPosition() does?

How many times does it need to be called? How many times does the while loop iterate? Are those answers the same? They should be.

What do your Serial.print() statement tell you is happening? Why not?

Arnaudz:
They must move for the same duration for each call, the speed of the one who makes the smallest number of steps therefore adapts to the speed of the one who makes the bigger number in order to arrive at there target position at the same time.

The AccelStepper library is specifically NOT suitable for that. Either use the MultiStepper version of the library (which does not use acceleration) or write your own code.

...R
Stepper Motor Basics
Simple Stepper Code

Many thanks to you for your help.

Robin2 I found help in using multistepper. But I thought have to use accelstepper Library in order to use multistepper. I'll take a look on it.

PaulS, if you really nead to know, my motors are black :smiley:
I think that setSpeed() take a float argument so I'm not sure the program is going to make a difference between 1000 and 1000.0 But if you really need to know integer division doesn't biting me in the posterior :smiley:

Thank you for the time you took to help me, this problem is solve !

I think that setSpeed() take a float argument so I'm not sure the program is going to make a difference between 1000 and 1000.0 But if you really need to know integer division doesn't biting me in the posterior

Suppose that you decide that you need to step a bit farther.

 deplace(40,60);
    Yaxis.setSpeed(1000/x*y);

What is 1000/(40*60)? If you said anything other than 0, you'd be wrong.

By the way, what happens if you want to move in only one axis?

  deplace(6, 0);
  deplace(0, 3);
    Yaxis.setSpeed(1000/0);

Really NOT a good idea.

Arnaudz:
Robin2 I found help in using multistepper.

I don't know what you mean by that. Do you mean that you have tried the examples and they work?

...R

Yes I did and I've completely change my point of view. Here is the code :

#include <Arduino.h>

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

AccelStepper Xaxis(1, 2, 5); // pin 2 = step, pin 5 = direction
AccelStepper Yaxis(1, 3, 6); // pin 3 = step, pin 6 = direction
int coef=1000; // coef multiplicateur utile pour le programme final

MultiStepper steppers;

void setup()
{
   Xaxis.setMaxSpeed(1000);
  Yaxis.setMaxSpeed(1000);
   
    //add steppers to the MultiStepper object
    steppers.addStepper(Xaxis);
    steppers.addStepper(Yaxis);
    delay(1000); // Moment s'attente pour observer les moteurs
 
  
 
  
 deplace(-8,5);
 deplace(8,-5);
 
}

void loop()
{}

void deplace(int x,int y) {
    int32_t positions[2];        
    positions[0] = x*coef + Xaxis.currentPosition();        
    positions[1] = y*coef + Yaxis.currentPosition();

    steppers.moveTo(positions);

    steppers.runSpeedToPosition();         
}

thanks

Arnaudz:
Yes I did and I've completely change my point of view. Here is the code :

Glad to see you have found a solution.

...R