Hello all,
I'm trying to move four steppers a different number of steps during the same time interval. I.E. when I set stepper A to run 40 steps at 400 steps/second, and stepper B to run 20 steps at 200 steps/second, both motors should complete their motion in 0.1 seconds.
I'm running the code "virtually", i.e. without the steppers actually powered, just to cement my understanding of how the AccelStepper and MultiStepper libraries work.
When I use "setMaxSpeed" in setup and "setSpeed" later in the program, my steppers all run at an extremely slow speed. Using "setMaxSpeed" all the time gets me closer to the result I'm expecting, but seems wrong. Even in this scenario, my movements are not completing in the same time increment, but rather the completion time slows down as one speed goes lower, even though the slow motor has proportionally fewer steps to complete.
Is there something missing in how I'm approach this library? I really can't figure out what I'm doing wrong.
Here's the code I'm using:
Changing "setSpeed" to "setMaxSpeed" at the bottom is getting my "half-working" solution
#include <AccelStepper.h>
#include <MultiStepper.h>
AccelStepper RightFrontWheel(1, 40, 41); // Stepper1
AccelStepper RightBackWheel(1, 20, 21); // Stepper2
AccelStepper LeftFrontWheel(1, 44, 45); // Stepper3
AccelStepper LeftBackWheel(1, 16, 17); // Stepper4
MultiStepper steppers;
long positions[4] = {0,0,0,0};
int wheelSpeed = 400;
float MovementCommand [4] = {-45,-45,45,45};
bool Running = true;
void setup()
{
LeftFrontWheel.setMaxSpeed(wheelSpeed);
RightFrontWheel.setMaxSpeed(wheelSpeed);
RightBackWheel.setMaxSpeed(wheelSpeed);
LeftBackWheel.setMaxSpeed(wheelSpeed);
steppers.addStepper(LeftFrontWheel);
steppers.addStepper(LeftBackWheel);
steppers.addStepper(RightFrontWheel);
steppers.addStepper(RightBackWheel);
Serial.begin(9600);
}
void loop()
{
if (Running)
{
Running = steppers.run();
}
else
{
Serial.println("step complete");
RecalculateIntervals();
CalculateMotions(MovementCommand);
}
}
void RecalculateIntervals()
{
Serial.print("position updates");
Serial.print(" || ");
Serial.print(MovementCommand[0]);
Serial.print(";");
MovementCommand[1] += 1;
Serial.print(MovementCommand[1]);
Serial.print(";");
MovementCommand[2] -= 1;
Serial.print(MovementCommand[2]);
Serial.print(";");
Serial.println(MovementCommand[3]);
}
void CalculateMotions(float Command[4])
{
Running = true;
positions[0] += Command[0];
positions[1] += Command[1];
positions[2] += Command[2];
positions[3] += Command[3];
int LargestStep = 0;
for (int i = 0; i < 4; i++)
{
LargestStep = max(LargestStep,abs(Command[i]));
}
float Speed0 = wheelSpeed*(Command[0]/LargestStep);
float Speed1 = wheelSpeed*(Command[1]/LargestStep);
float Speed2 = wheelSpeed*(Command[2]/LargestStep);
float Speed3 = wheelSpeed*(Command[3]/LargestStep);
steppers.moveTo(positions);
LeftFrontWheel.setSpeed(Speed0);
RightFrontWheel.setSpeed(Speed1);
RightBackWheel.setSpeed(Speed2);
LeftBackWheel.setSpeed(Speed3);
Serial.print("new speeds : ");
Serial.print(Speed0);
Serial.print(";");
Serial.print(Speed1);
Serial.print(";");
Serial.print(Speed2);
Serial.print(";");
Serial.println(Speed3);
}