Move two motors in the same time

Hello everyone,
I'm working on a camera slider. A stepper motor translate the camera, and rotates it with another stepper motor in order to track an object with its coordinate known.
The linear movement is generated with a stepper motor NEMA 17 and a A4988 driver. The rotation is generated with a 28byj-48 stepper and its dedicated driver.

The problem is that the movement is jerky because each motor move one step at the time. At each steps of the NEMA 17, I calculate the angle .

Is there a way to let the NEMA 17 running while in the same time calculate the angle and tell the other motor to move without stopping the NEMA 17 ?

Thank you , I hope I am clear enough !

Here's the function that makes the camera moves

 void runandtrack(){
     
    motorSpeed = 5;
    double diff_angle = ((atan2 (dist_y_obj, dist_x_obj) - angle));
    double step_rot = diff_angle * resolution_rot; 
    if (diff_angle >= ( (2 * PI) / stepsPerRevolution2 ))
    {
      angle = atan2 (dist_y_obj, dist_x_obj);
      for (int i = 0; i < step_rot; i ++){
      clockwise();
      }
    }
    digitalWrite(dirPin,LOW);
    digitalWrite(stepPin, HIGH);
    delayMicroseconds(map(speed_x, 0, 100,4000,300));
    digitalWrite(stepPin, LOW);
    delayMicroseconds(map(speed_x, 0, 100,4000,300));
    dist_x_obj = dist_x_obj - resolution_x;
    pas_x = pas_x +1 ;

  }

You need to post the complete program.

What does the function clockwise() do?

Probably the simplest solution is to use the AccelStepper library with its non-blocking run() or runSpeed() functions. If, as the last thing in loop() your code is like this

void loop() {
   // other code

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

then both motors will move at the same time.

...R

Thank's for your answer.
The code is very long and a big part of it is not part of my problem.
The function clockwise makes one step for the 28byj-48 stepper motor.

I've read about Accelstepper and it looks very useful.
However, when I try the example "constant speed" with the NEMA 17 , it is not working. The motor make noises but doesn't move . Any ideas why?

#include <AccelStepper.h>
AccelStepper stepper1(1, 9, 10);

void setup()
{  
   stepper.setMaxSpeed(1000);
   stepper.setSpeed(50);	
}

void loop()
{  
   stepper.runSpeed();
}

It's always worth trying a much slower speed when there are problems.

If you need more help please post a link to the datasheet for the Nema17 motor and tell us what motor power supply you are using (volts and amps).

...R

do your really want the angle, or time between steps

not obvious that path is circular or linear

if the path were linear and 100 ft from the camera, the following table indicates the step, the half angle between steps where the step should be made, the distance along the path from the perpendicular bisector and the difference in distances between steps.

if the object were moving 1 ft/sec, the last column would indicate the time between step once synchronized. Obviously the delays between steps become shorter as the object becomes perpendicular to the camera

code could use such a table, scaling values for speed and distance from camera

 step   0, ang  0.90, x 6365.7
 step   1, ang  2.70, x 2120.5, dx 4245.2
 step   2, ang  4.50, x 1270.6, dx  849.9
 step   3, ang  6.30, x  905.8, dx  364.8
 step   4, ang  8.10, x  702.6, dx  203.2
 step   5, ang  9.90, x  573.0, dx  129.7
 step   6, ang 11.70, x  482.9, dx   90.1
 step   7, ang 13.50, x  416.5, dx   66.4
 step   8, ang 15.30, x  365.5, dx   51.0
 step   9, ang 17.10, x  325.1, dx   40.5
 step  10, ang 18.90, x  292.1, dx   33.0
 step  11, ang 20.70, x  264.6, dx   27.4
 step  12, ang 22.50, x  241.4, dx   23.2
 step  13, ang 24.30, x  221.5, dx   19.9
 step  14, ang 26.10, x  204.1, dx   17.4
 step  15, ang 27.90, x  188.9, dx   15.3
 step  16, ang 29.70, x  175.3, dx   13.5
 step  17, ang 31.50, x  163.2, dx   12.1
 step  18, ang 33.30, x  152.2, dx   10.9
 step  19, ang 35.10, x  142.3, dx    9.9
 step  20, ang 36.90, x  133.2, dx    9.1
 step  21, ang 38.70, x  124.8, dx    8.4
 step  22, ang 40.50, x  117.1, dx    7.7
 step  23, ang 42.30, x  109.9, dx    7.2
 step  24, ang 44.10, x  103.2, dx    6.7
 step  25, ang 45.90, x   96.9, dx    6.3
 step  26, ang 47.70, x   91.0, dx    5.9
 step  27, ang 49.50, x   85.4, dx    5.6
 step  28, ang 51.30, x   80.1, dx    5.3
 step  29, ang 53.10, x   75.1, dx    5.0
 step  30, ang 54.90, x   70.3, dx    4.8
 step  31, ang 56.70, x   65.7, dx    4.6
 step  32, ang 58.50, x   61.3, dx    4.4
 step  33, ang 60.30, x   57.0, dx    4.2
 step  34, ang 62.10, x   52.9, dx    4.1
 step  35, ang 63.90, x   49.0, dx    4.0
 step  36, ang 65.70, x   45.2, dx    3.8
 step  37, ang 67.50, x   41.4, dx    3.7
 step  38, ang 69.30, x   37.8, dx    3.6
 step  39, ang 71.10, x   34.2, dx    3.5
 step  40, ang 72.90, x   30.8, dx    3.5
 step  41, ang 74.70, x   27.4, dx    3.4
 step  42, ang 76.50, x   24.0, dx    3.3
 step  43, ang 78.30, x   20.7, dx    3.3
 step  44, ang 80.10, x   17.5, dx    3.3
 step  45, ang 81.90, x   14.2, dx    3.2
 step  46, ang 83.70, x   11.0, dx    3.2
 step  47, ang 85.50, x    7.9, dx    3.2
 step  48, ang 87.30, x    4.7, dx    3.2
 step  49, ang 89.10, x    1.6, dx    3.1
 step  50, ang 90.90, x   -1.6, dx    3.1

Robin2:
It's always worth trying a much slower speed when there are problems.

Indeed it seems that the speed was too high. But the speed of my motor is not written on my datasheet :

I am supplying it with 12 V on the driver and with 1 amp .

gcjr:
do your really want the angle, or time between steps

not obvious that path is circular or linear

I am not sure to understand well. My path is linear , it's a rail. Is my method with the angle is wrong ?

tangoscar:
I am not sure to understand well. My path is linear , it's a rail.

like a train passing on a track ?

Robin2:
It's always worth trying a much slower speed when there are problems.

I am using my motor with microstepping (sixteenth steps) , so I have adjusted the speed in order to get the right RPM (considering my motor can go to about 600 RPM). So basically I wrote :
stepper.setSpeed(32000); // 200 * 16 * 600 / 60 in order to get in steps/s
Wich works well , the motor turns really fast.
But if I want to run with lower speeds (wich is what I need) , the motor behaves erratically.
Note that my motor was running perfectly slowly without the Accelstepper library.

gcjr:
like a train passing on a track ?

Yes , like this project :

https://www.youtube.com/watch?v=ELlJouMnDrE (not mine)

tangoscar:
So basically I wrote :
stepper.setSpeed(32000); // 200 * 16 * 600 / 60 in order to get in steps/s
Wich works well , the motor turns really fast.
But if I want to run with lower speeds (wich is what I need) , the motor behaves erratically.

Please post the program that works and also the program that behaves erratically. I suspect an error in your maths or the type of variable you are using.

I am supplying it with 12 V on the driver and with 1 amp .

Does that mean that you have adjusted the current limit on the A4988 to 1 amp? If not, then what do you mean?

...R

Robin2:
Does that mean that you have adjusted the current limit on the A4988 to 1 amp? If not, then what do you mean?

yes

Program that I'm trying to get working, just to run the NEMA 17

#include <AccelStepper.h>

AccelStepper stepper(1, 10, 9);


void setup()
{  
   stepper.setMaxSpeed(32000); 
   stepper.setSpeed(693); 
   //trying to have a speed of 0,01 m/s with a radius of 0,007m , so it's 13 RPM. 
   // 13 RPM in sixteenth mode equal to (13 * 16 * 200 / 60) = 693 steps/seconds 
}

void loop()
{  
   stepper.runSpeed();
}

The motor turns but with too much speed and it is not smooth at all .

Program that works well :

const int stepPin = 10; 
const int dirPin = 9;
const int sleep_drv = 8;

int speed_x = 100;
void setup() {
   pinMode(stepPin,OUTPUT); 
   pinMode(dirPin,OUTPUT);
   digitalWrite(sleep_drv,HIGH);
   digitalWrite(dirPin,LOW);


}

void loop() {
    digitalWrite(stepPin, HIGH);
    delay(2);
    digitalWrite(stepPin, LOW);
    delay(2);
}

I don't know why I am not getting the same results

The program that works well is doing 250 steps per second. The AccelStepper program is being asked to do 693 steps per second.

...R

Ok maybe, but when I change to 250 rpm on the one that doesn't work it's still the same :

#include <AccelStepper.h>

AccelStepper stepper(1, 10, 9); // (Type:driver, STEP, DIR)


void setup()
{  
   stepper.setMaxSpeed(32000);
   stepper.setSpeed(250);	
}

void loop()
{  
   stepper.runSpeed();
}

I don't understand why my motor isn't running

tangoscar:
Yes , like this project :

https://www.youtube.com/watch?v=ELlJouMnDrE (not mine)

while the camera is moving laterally at a fixed rate (steps/sec), it looks like the camera angle changes more quickly near the center than at the ends.

so for each lateral position there is some desired angle and step position.

if the lateral rate is fixed, i still suggest that it is the time between angular steps that needs to be determined for each lateral position.

of course stepping may appear jitttery. i think micro-stepping attempts to avoid this. i think driving the coils with PWM at specific rates can pull the motor to the next step more smoothly but i have no experience with doing that.

tangoscar:
I don't understand why my motor isn't running

Neither do I. Have you tried a much slower speed? Always start at a slow speed and work up to a higher speed.

Have you applied microstepping with the AccelStepper trial?

I note that you have this line in the working program and not in the AccelStepper program

digitalWrite(sleep_drv,HIGH);

I also note that you do NOT have the expected

pinMode(sleed_drv, OUTOUT);

...R

Ok I just fond where the problem is. When I wired the "sleep" and "reset" in the pololu driver, everythings working perfectly.