# Linear move

Hello all,
on this time I have small problem with linear moving.

I have two stepper motors (Nema17) with library #include “BasicStepperDriver.h”. I’m not able to move both at the same time. So I can move them in while/for cycle by 1 step. But…

When I have X1, Y1 (start point) and X2, Y2 (end point) how can I solve the linear moving? The logic for some moving is easy. For example:

[x1 ; y1] = [10 ; 10]
[x2 ; y2] = [50 ; 50]
Will run motor X, motor Y, motor X, motor Y, motor X, …

45° linear is too okay. But when I will have positions like this:
[x1 ; y1] = [10.41 ; 140.11]
[x2 ; y2] = [50.00 ; 21.47]
How to solve it? I think it is something about parametric expression of the vector/line?

I know I need to calibrate motor steps for milimeters. That’s okay. I want some “example code”? For show how to calculate it. Have you something like that?

Thank you very much for your help.

I used the Accelstepper library and the MultiStepper class to do that. It works, only, at slower speeds as MultiStepper does not use acceleration.

You could look at the source code to see how they do those moves.

Use the Bresenham algorithm to move two steppers along an arbitrary linear path. You have to break up the move into single steps. Extension algorithms allow circular or elliptical paths.

The Bresenham algorithm was originally developed for XY pen plotters driven by stepper motors.

kevincolbert:
I have two stepper motors (Nema17) with library #include "BasicStepperDriver.h". I'm not able to move both at the same time. So I can move them in while/for cycle by 1 step. But...

I'm not familiar with that library and you have not posted your program.

If you are using stepper drivers that take step and direction signals it is not that difficult to write your own code without needing any driver. Have a look at these links
Simple Stepper Code
Simple acceleration code.

If you want to make your own co-ordinated movements for a number of motors one way is to work out the interval between steps for the fastest motor (in millisecs or microsecs) and also the intervals for the other motors. Suppose the fast motor needs to move once per millisec (1000 steps per second) and the 2nd motor needs to do one step every 5 mllisecs. Write your code to make the fast motor move and them make one step for the slow motor every 5th step of the fast motor.

...R

groundFungus:
I used the Accelstepper library and the MultiStepper class to do that. It works, only, at slower speeds as MultiStepper does not use acceleration.

You could look at the source code to see how they do those moves.

Thanks everybody for your answers, you are great! I’m in love with AccelStepper library. Thank you. Now I have only one problem. Two axis are moving in same time, but one is faster than second. The second one is delayed. Can you tell me it’s a mistake in code, or in library/physics?

My code:

``````#include <Arduino.h>
#include "BasicStepperDriver.h"
#include "MultiDriver.h"
#include "SyncDriver.h"

// Motor steps per revolution. Most steppers are 200 steps or 1.8 degrees/step
#define MOTOR_STEPS 200
// Target RPM for X axis motor
#define MOTOR_X_RPM 90
// Target RPM for Y axis motor
#define MOTOR_Y_RPM 90

#define MICROSTEPS 16

// Nastavení pinů motorů
#define X_DIR 21
#define X_STEP 15
#define Y_DIR 23
#define Y_STEP 22
#define SLEEP 14

//Uncomment line to use enable/disable functionality
BasicStepperDriver stepper_X(MOTOR_STEPS, X_DIR, X_STEP, SLEEP);
BasicStepperDriver stepper_Y(MOTOR_STEPS, Y_DIR, Y_STEP, SLEEP);

SyncDriver controller(stepper_X, stepper_Y);

void stepper_setup() {
stepper_X.begin(MOTOR_X_RPM, MICROSTEPS);
stepper_Y.begin(MOTOR_Y_RPM, MICROSTEPS);

// if using enable/disable on ENABLE pin (active LOW) instead of SLEEP uncomment next line
stepper_X.setEnableActiveState(LOW);
stepper_Y.setEnableActiveState(LOW);

// energize coils - the motor will hold position
stepper_X.enable();
stepper_Y.enable();
}

void stepper_loop() {
controller.move(250*MICROSTEPS,-1000*MICROSTEPS);
controller.move(-250*MICROSTEPS,1000*MICROSTEPS);
}
``````

kevincolbert:
I’m in love with AccelStepper library.

[…]

My code:

``````#include <Arduino.h>
``````

#include “BasicStepperDriver.h”
#include “MultiDriver.h”
#include “SyncDriver.h”

Also the code you posted is not a valid Arduino program.

…R

PS … When posting code please use the code button </>

``````so your code
looks like this
``````

Robin2:
Also the code you posted is not a valid Arduino program.

The arduino code is valid. I have divided code to files.

the main.ino look like:

``````/** Some magic **/
#include "./steppertest.h";

void setup() {
/** Some magic **/
stepper_setup();
}

void loop() {
/** Some magic **/
stepper_loop();
}
``````

kevincolbert:
The arduino code is valid. I have divided code to files.

I understand the reason for that. I do it myself.

However it's much easier to help with problems when the whole program is in one file.

...R