going to a particular position with constant speed using accelStepper in arduino

Diwakar picture Diwakar · Jun 29, 2014 · Viewed 9.5k times · Source

I've been trying to make a simple two axis cnc. I am just in the beginning phase to I just wanted to check out the accelStepper library. With the help of constantSpeed and Bounce sketch example I got some idea and wrote the following code.

    #include <AccelStepper.h>
    AccelStepper stepperX(4, 4, 5, 6, 7);
    AccelStepper stepperY(4, 8, 9, 10, 11);
    unsigned int X = 800;
    unsigned int Y = 600;

    void setup(){
      stepperX.setMaxSpeed(200);
      stepperX.setSpeed(10);
      stepperY.setMaxSpeed(200);
      stepperY.setSpeed(10);
      stepperX.move(200);
      stepperY.move(200);
   }

   void loop(){
      stepperX.runSpeed();
      stepperY.runSpeed();
   }

When I upload this sketch the motor does not run. But when I change runSpeed() to run() and add setAcceleration() the motor then runs but with acceleration. I don't want acceleration. So, is there any way to do this without acceleration.

Answer

spring picture spring · Jun 29, 2014

From the docs (the only difference between move and moveTo is that the former does a relative move):

void AccelStepper::moveTo ( long absolute )

Set the target position. The run() function will try to move the motor (at most one step per call) from the current position to the target position set by the most recent call to this function. Caution: moveTo() also recalculates the speed for the next step. If you are trying to use constant speed movements, you should call setSpeed() after calling moveTo().

Key line (as in pie): If you are trying to use constant speed movements, you should call setSpeed() after calling moveTo().

But having been there (making an XY plotter, 2 axis CNC), save yourself much grief and just use grbl. It handles all the coordinated axis control, gcode parsing, etc.