AccelStepper not support two stepper motor moving counterclockwise?

279 views
Skip to first unread message

Woody

unread,
May 24, 2023, 3:37:43 AM5/24/23
to accelstepper

When I set two stepper motors to move counterclockwise at the same time, the motor doesn't respond (it doesn't start) , and when I set two stepper motors to move clockwise at the same time, or when a single motor moves counterclockwise, the bug did not appear.

I am using ESP32 with arduino & TMC2209 driver.

As code below showed, 3 motion type work and only 1 type not work.

```
#include <Arduino.h>

#include <AccelStepper.h>
AccelStepper Xaxis(1, 16, 17); // step, direction
AccelStepper Yaxis(1, 25, 26); // step, direction
void setup() {
    int x_circle = 3200;
    int y_circle = 3200;

    Xaxis.setMaxSpeed(x_circle*8);
    Yaxis.setMaxSpeed(y_circle*8);
    Xaxis.setAcceleration(1*x_circle);
    Yaxis.setAcceleration(1*y_circle);

    // work!
//    Xaxis.move(1*x_circle);
//    Yaxis.move(1*y_circle);

    // work!
//    Xaxis.move(1*x_circle);
//    Yaxis.move(-1*y_circle);

    // work!
//    Xaxis.move(-1*x_circle);
//    Yaxis.move(1*y_circle);

    // NOT WORK!
    Xaxis.move(-1*x_circle);
    Yaxis.move(-1*y_circle);
}
void loop() {
    Xaxis.run();
    Yaxis.run();

}

```


Is it a problem with my use or a bug? Please help me analyze it. Thank you all

Woody

unread,
May 24, 2023, 3:44:58 AM5/24/23
to accelstepper
I am using Arduino CNC shield

Geoff Smith

unread,
May 24, 2023, 9:02:49 AM5/24/23
to accels...@googlegroups.com
Reading the documentation, the functions for speed and acceleration require float type variables not int. The move function requires long type variable. Best not to use the same variable for both functions, try making a new long type variable for the move function and change the existing variables to float type. Give that a go first. 

Sent from my iPhone

On 24 May 2023, at 5:45 pm, Woody <edenpho...@gmail.com> wrote:

I am using Arduino CNC shield
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/accelstepper/71cc6809-d8a4-4eec-9406-07bdf56ad3dan%40googlegroups.com.

Woody

unread,
May 25, 2023, 3:55:23 AM5/25/23
to accelstepper

After testing, the problem was not resolved.


1. I modified the speed input to float and the Move input to long, but there is still a problem. And, with the original writing method, C language should be able to complete the correct implicit conversion.

2. I replaced the main control board with ArduinoUNO, and the code was normal, which made me wonder if it was an issue with ESP32 porting.


I have compared the code of the Accelstepper library used by ArduinoUNO and ESP32, and it is completely consistent.

So the problem becomes, ESP32 runs my example code (even with the modifications in # 1) and still cannot achieve simultaneous reversal of two stepper motors. I think this is a bug


PS: I captured through an oscilloscope that the PWM and square waves of the step and dir of the two steppers are normal jumps.

gjgsm...@gmail.com

unread,
May 27, 2023, 8:30:39 AM5/27/23
to accelstepper
Your:
     Xaxis.setMaxSpeed(x_circle*8);
    Yaxis.setMaxSpeed(y_circle*8);
is likely too high even for the ESP32. I would set Max Speed to a maximum of say 3000 and vary up or down from there.

Some drivers need a longer pulse width than the library default.
Try adding this to your setup:

void setup() {
    Xaxis.setMinPulseWidth(20);   // Try varying this up or down if necessary.
    Yaxis.setMinPulseWidth(20);
   
    // Rest of your code
}

I have found the need to setMinPulseWidth sometimes when using an ESP32.


Here are a few things to check:

Wiring and Connections: Double-check your wiring, make sure everything is properly connected and no loose or bad connections exist. Both motors should be wired identically.

Power Supply: Both motors moving at the same time require more current. Make sure that your power supply is able to provide enough current for both motors.

Driver Configuration: You're using TMC2209 drivers. Ensure they are properly configured and correctly connected to the ESP32. Also, double-check your driver's current settings, it might not be able to provide enough current when both motors move counterclockwise.

Woody

unread,
May 28, 2023, 4:47:23 AM5/28/23
to accelstepper
Hi, Thanks again for your help! This bug realy blocked my project.

I have done serveral test:
1. Reducing maximum speed and acceleration is not a key factor;

2.Setting PWM pulse width is not a critical factor;

3.Setting the current of TMC2209 to maximum (close to 2A) and adjusting microstep (8 or 16) is not a critical factor;

I am certain that the connection is correct. Please see the following example. I controlled the only variable, which is that when the target position is positive and negative, the expected motor movement cannot be achieved:

```

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

long x_circle = 1600;
long y_circle = 1600;


AccelStepper Xaxis(1, 16, 17); // step, direction
AccelStepper Yaxis(1, 25, 26); // step, direction

MultiStepper steppersControl;  // Create instance of MultiStepper

long gotoposition[2]; // An array to store the target positions for each stepper motor




void setup() {
    Xaxis.setMinPulseWidth(20);   // Try varying this up or down if necessary.
    Yaxis.setMinPulseWidth(20);


    Xaxis.setMaxSpeed(x_circle*1);
Yaxis.setMaxSpeed(y_circle*1);
Xaxis.setAcceleration(0.2*x_circle);
Yaxis.setAcceleration(0.2*y_circle);

    steppersControl.addStepper(Xaxis);
    steppersControl.addStepper(Yaxis);
}
void loop() {
    gotoposition[0] = 1*x_circle;

    // Work! At this point, motor 1 will normally perform the expected alternating forward and reverse movements
    gotoposition[1] = -1;

    // Not work! motor 1 will NOT reverse
//    gotoposition[1] = 0;


    steppersControl.moveTo(gotoposition);
    steppersControl.runSpeedToPosition();
    delay(1000);

    gotoposition[0] = -1*x_circle;
    gotoposition[1] = 0*y_circle;
    steppersControl.moveTo(gotoposition);
    steppersControl.runSpeedToPosition();
    delay(1000);
}
```

I use the ESP32 board wemos_d1_uno32,with Arduino and platformio.

Thank you again for your help. This issue has been bothering me for too long

gjgsm...@gmail.com

unread,
May 28, 2023, 7:48:14 AM5/28/23
to accelstepper
I haven't done anything with MultiStepper but I see a couple of possible issues with your use of the documented example.

Be aware that when your MCU boots the step position for all steppers is '0'. This position never changes while the power is connected or unless the setCurrentPosition() is used.

I am guessing that your loop is actually working but you have two 'gotoposition[1] ' in the first coordinate movement. The difference in position between these two is just 1 step (-1 to 0). You may not even notice that it has moved that one step.

See the code below, I have created some functions for each coordinate movement to make it easier to understand. Experiment by changing the coordinates. This code is obviously untested...

// --------------------------------------------------------------------------------------------
#include <Arduino.h>
#include <AccelStepper.h>
#include <MultiStepper.h>

long x_circle = 1600;
long y_circle = 1600;

AccelStepper Xaxis(1, 16, 17); // step, direction
AccelStepper Yaxis(1, 25, 26); // step, direction

MultiStepper steppersControl;  // Create instance of MultiStepper

long gotoposition[2]; // An array to store the target positions for each stepper motor


firstCoordinateMovement() {
gotoposition[0] = 1*x_circle;
gotoposition[1] = 1*y_circle;
steppersControl.moveTo(gotoposition);
steppersControl.runSpeedToPosition();
}


secondCoordinateMovement() {
gotoposition[0] = 0*x_circle; // Equivalent to step position '0' which is the start position.
gotoposition[1] = 0*y_circle; // Equivalent to step position '0' which is the start position.
steppersControl.moveTo(gotoposition);
steppersControl.runSpeedToPosition();
}


thirdCoordinateMovement() {
gotoposition[0] = -1*x_circle;
gotoposition[1] = 1*y_circle;
steppersControl.moveTo(gotoposition);
steppersControl.runSpeedToPosition();

}


void setup() {

    Xaxis.setMinPulseWidth(20);   // Try varying this up or down if necessary.
    Yaxis.setMinPulseWidth(20);

    Xaxis.setMaxSpeed(x_circle*1.0);
Yaxis.setMaxSpeed(y_circle*1.0);

// Xaxis.setAcceleration(0.2*x_circle);
// Yaxis.setAcceleration(0.2*y_circle);

    steppersControl.addStepper(Xaxis);
    steppersControl.addStepper(Yaxis);
}


void loop() {

firstCoordinateMovement();
    delay(1000);

secondCoordinateMovement();
    delay(1000);

thirdCoordinateMovement()
    delay(1000);

secondCoordinateMovement(); //Moves steppers back to their origin points.
    delay(1000);
}

Woody

unread,
May 28, 2023, 9:48:14 AM5/28/23
to accelstepper
1.After I corrected your code correctly, the running results are still incorrect, so I will continue to discuss based on my code
2.My goal T1 is to use runSpeedToPosition for both motors, which cannot be achieved in practice; Target T2 is motor 1, which is in reverse and not subject to gotoposition
···

    // Work! At this point, motor 1 will normally perform the expected alternating forward and reverse movements
    gotoposition[1] = -1;

    // Not work! motor 1 will NOT reverse
//    gotoposition[1] = 0;
···

3. The strangest problem is that I added the print code and used the oscilloscope to find:
A.multistepper 's moveTo is normal (but I doubt if there is any influence in accelstepper)
B. The step,dir waveform of the oscilloscope is similar and correct.




1.png2.png3.png


T2 not work.mp4
T2 work.mp4

Geoff Smith

unread,
May 28, 2023, 8:23:01 PM5/28/23
to accels...@googlegroups.com
Someone else may be willing to help you but, the only thing I can suggest now is that you read all the Accelstepper documentation and run the Bounce example to verify that your setup is actually working correctly and then run the MultiStepper example, if that runs ok then make modifications from there. The library is not the problem here. Good luck. 

Sent from my iPhone

On 28 May 2023, at 11:48 pm, Woody <edenpho...@gmail.com> wrote:


T2 work.mp4
T2 not work.mp4
1.png
3.png
2.png
2.png
3.png
1.png
Reply all
Reply to author
Forward
0 new messages