Simultaneous movement of two steppers

555 views
Skip to first unread message

Vadim

unread,
Jan 11, 2014, 7:13:08 AM1/11/14
to accels...@googlegroups.com
I operate two steppers by means of buttons. Speed and acceleration are regulated with the help of pots. There is a function of storing of positions. In the kept position I make a transfer by means of such code

  stepper1.setMaxSpeed(SPEEDX);
  stepper1.setAcceleration(ACCELX);
  stepper1.moveTo(savedPosAX);
  stepper1.runToPosition();

Simultaneous movement of two motors in the kept positions is necessary. But this code with blocking and the stepper2 begins movement after the stepper1 will finish the movement.

    stepper1.setMaxSpeed(SPEEDX);
    stepper1.setAcceleration(ACCELX);
    stepper1.moveTo(savedPosAX);
    stepper1.runToPosition();
    stepper2.setMaxSpeed(SPEEDY);
    stepper2.setAcceleration(ACCELY);
    stepper2.moveTo(savedPosAY);
    stepper2.runToPosition();

I tried to replace runToPosition() on run(), but it doesn't work. 
How to provide synchronous movement of two steppers to the kept positions?

Sandy Noble

unread,
Jan 11, 2014, 7:21:49 AM1/11/14
to accels...@googlegroups.com
Have a look at the examples in the code, you will see how AccelStepper is meant to be used in a non-blocking way. RunToPosition blocks, as you've seen.

Two phases:
1) Set target
2) Run motors

eg 
    // phase 1 - set target and speed etc
    stepper1.setMaxSpeed(SPEEDX);
    stepper1.setAcceleration(ACCELX);
    stepper1.moveTo(savedPosAX);
    stepper2.setMaxSpeed(SPEEDY);
    stepper2.setAcceleration(ACCELY);
    stepper2.moveTo(savedPosAY);

    // phase 2 - actually run
    while (stepper1.distanceToGo() != 0 && stepper2.distanceToGo() != 0) 
    {
        stepper1.run();
        stepper2.run();
    }

The paradigm usually involves calling .run() as often as possible, because each call to run can only ever move a maximum of one step, but depending on speed, and need, it will often not move anything at all.

Setting targets to move to is usually done inside conditional if statements. That's the actual logic of your program.

good luck
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/groups/opt_out.



--
Sandy Noble

Vadim

unread,
Jan 11, 2014, 8:39:00 AM1/11/14
to accels...@googlegroups.com
Thanks, Sandy.
Now steppers work at the same time. 
I will try to take now the following step - to force steppers to turn on different distances for at one time

Vadim

unread,
Jan 12, 2014, 4:27:51 AM1/12/14
to accels...@googlegroups.com
I try to force steppers to begin and finish movement  at the same time in the kept positions.
Calculation of speedSynchro for such formula

speedSynchro = SPEEDX*(abs((savedPosAY - savedPosBY)/(savedPosAX - savedPosBX)));

The button Synchro has two modes. One for separate movement of the steppers, the second for the synchronous. speedSynchro is considered when the button in a synchronization mode.

    stepper1.setMaxSpeed(SPEEDX);
    stepper1.setAcceleration(ACCELX);
    stepper1.moveTo(savedPosAX);
    stepper2.setMaxSpeed(speedSynchro);
    stepper2.setAcceleration(ACCELX);
    stepper2.moveTo(savedPosAY);

    while (stepper1.distanceToGo() != 0 ) 
    {
      stepper1.run();
      stepper2.run();
    }

The stepper1 rotates only. Maybe arduino doesn't manage to calculate speedSynchro?
Such code  too the negative

    speedSynchro = SPEEDX*(abs((savedPosAY - savedPosBY)/(savedPosAX - savedPosBX)));
    stepper1.setMaxSpeed(SPEEDX);
    stepper1.setAcceleration(ACCELX);
    stepper1.moveTo(savedPosAX);
    stepper2.setMaxSpeed(speedSynchro);
    stepper2.setAcceleration(ACCELX);
    stepper2.moveTo(savedPosAY);

    while (stepper1.distanceToGo() != 0 ) 
    {
      stepper1.run();
      stepper2.run();
    }

Whether there are any ideas?

Sandy Noble

unread,
Jan 12, 2014, 6:43:36 AM1/12/14
to accels...@googlegroups.com
Debugging arduino isn't easy, but you can try with Serial.println(...). That would let you know if your calculation is actually doing anything.

It is possible that the speedSynchro is just very low, so it looks like it isn't moving. Also possible that your savedPosAY is 0.

Bear in mind: you are modifying the maxSpeed of stepper2 to be a proportion of the maxSpeed of stepper1, but that would only synchronise the motors if they were both moving at maxSpeed. During the acceleration portion of the movement, both accelerate at the same rate, going the same speed, until one of them reaches it's maxSpeed.

The approach I've seen used most often is something like

int SPEEDX = 4000;
long savedPosAX = 1000L;
long savedPosAY = 2000L;
int speedSynchro = 0;

...
// set targets
stepper1.moveTo(savedPosAX);
stepper2.moveTo(savedPosAY);

// calculate the proportion of speed difference
// and run the stepper with the shortest distance
// at the proportionally reduced speed
if (savesPosAX > savedPosAY) {
    speedSynchro = savedPosAY / savedPosAX;
    stepper1.run();
    stepper2.setSpeed(stepper1.speed() * speedSynchro);
    stepper2.runSpeed();
} else {
    speedSynchro = savesPosAX / savedPosAY;
    stepper2.run();
    stepper1.setSpeed(stepper2.speed() * speedSynchro);
    stepper1.runSpeed();
}

(not tested this, but for example).

I wonder if anyone else accepts this as a "done" thing. I haven't tried it recently myself, and I seem to remember it being _not quite as simple as that_ last time I did try it.


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/groups/opt_out.



--
Sandy Noble
Reply all
Reply to author
Forward
0 new messages