StepperScript Arduino Sketch

This sketch smoothly controls four stepper motors through a fixed pattern. It uses the AccelStepper library to smoothly ramp speeds up and down.

The full code for the sketch is in a single file and can be downloaded as StepperScript.ino, or browsed below.

StepperScript.ino

  1// StepperScript - drive three or four stepper motors through a sequence of poses
  2//
  3// Written in 2018 by Garth Zeglin <garthz@cmu.edu>.  To the extent possible
  4// under law, the author has dedicated all copyright and related and neighboring
  5// rights to this software to the public domain worldwide.  This software is
  6// distributed without any warranty.  You should have received a copy of the CC0
  7// Public Domain Dedication along with this software.  If not, see
  8// <http://creativecommons.org/publicdomain/zero/1.0/>.
  9
 10// This program assumes that:
 11
 12//  1. three or four A4988 stepper motor drivers are connected following the CNC Shield pin conventions.
 13//  2. the AccelStepper library is installed
 14//  3. if the fourth A axis stepper is used, jumpers are installed to connect
 15//     A-DIR to D13 (also SpinDir) and A-STEP to D12 (also SpinEnable).
 16
 17// ================================================================================
 18// Output pin assignments to control the A4988 stepper motor drivers.  The step
 19// inputs are triggered on a rising edge, with a minimum 1 microsecond HIGH and
 20// LOW pulse widths.
 21#define X_AXIS_STEP_PIN 2
 22#define Y_AXIS_STEP_PIN 3
 23#define Z_AXIS_STEP_PIN 4
 24#define A_AXIS_STEP_PIN 12     // requires an optional jumper
 25
 26#define X_AXIS_DIR_PIN 5
 27#define Y_AXIS_DIR_PIN 6
 28#define Z_AXIS_DIR_PIN 7
 29#define A_AXIS_DIR_PIN 13      // requires an optional jumper
 30
 31#define STEPPER_ENABLE_PIN 8  // active-low (i.e. LOW turns on the drivers)
 32
 33// ================================================================================
 34// Import the third-party AccelStepper library.
 35#include <AccelStepper.h>
 36
 37// Declare three AccelStepper objects to manage the output timing.
 38AccelStepper xaxis(AccelStepper::DRIVER, X_AXIS_STEP_PIN, X_AXIS_DIR_PIN);
 39AccelStepper yaxis(AccelStepper::DRIVER, Y_AXIS_STEP_PIN, Y_AXIS_DIR_PIN);
 40AccelStepper zaxis(AccelStepper::DRIVER, Z_AXIS_STEP_PIN, Z_AXIS_DIR_PIN);
 41AccelStepper aaxis(AccelStepper::DRIVER, A_AXIS_STEP_PIN, A_AXIS_DIR_PIN);
 42
 43// ================================================================================
 44/// Configure the hardware once after booting up.  This runs once after powering
 45/// up the board, pressing reset, or connecting to the console serial port.
 46void setup(void)
 47{
 48  // set up the CNC Shield I/O
 49  digitalWrite(STEPPER_ENABLE_PIN, HIGH); // initialize drivers in disabled state
 50  pinMode(STEPPER_ENABLE_PIN, OUTPUT);
 51
 52  xaxis.setMaxSpeed(4000.0);
 53  xaxis.setAcceleration(2000.0);
 54
 55  yaxis.setMaxSpeed(4000.0);
 56  yaxis.setAcceleration(2000.0);
 57
 58  zaxis.setMaxSpeed(4000.0);
 59  zaxis.setAcceleration(2000.0);
 60
 61  aaxis.setMaxSpeed(4000.0);
 62  aaxis.setAcceleration(2000.0);
 63
 64  // set up the serial port for debugging output
 65  Serial.begin(115200);
 66
 67  // enable the drivers, the motors will remain constantly energized
 68  digitalWrite(STEPPER_ENABLE_PIN, LOW);
 69}
 70/****************************************************************/
 71/// Call the ``run()`` function for each stepper driver object, which will
 72/// recalculate speeds and generate step pulses as needed.
 73void poll_steppers(void)
 74
 75{
 76  xaxis.run();
 77  yaxis.run();
 78  zaxis.run();
 79  aaxis.run();
 80}
 81/// Return true if any one of the drivers are still moving.
 82bool is_moving(void)
 83{
 84  return (xaxis.isRunning() || yaxis.isRunning() || zaxis.isRunning() || aaxis.isRunning());
 85}
 86
 87/// Move a relative displacement at the current speed, blocking until the move is done.
 88void move(long x, long y, long z, long a)
 89{
 90  Serial.print("Moving to ");
 91  Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", "); Serial.print(z); Serial.print(", ");
 92  Serial.println(a);
 93
 94  xaxis.move(x);
 95  yaxis.move(y);
 96  zaxis.move(z);
 97  aaxis.move(a);
 98
 99  do {
100    poll_steppers();
101  } while(is_moving());
102}
103
104/****************************************************************/
105/// Run one iteration of the main loop.  The Arduino system will call this
106/// function over and over forever.  This implementation is a script which will
107/// generate a series of movements, waiting for each to complete.
108void loop()
109{
110  Serial.println("Starting the movement script.");
111  move(800, 800, 800, 800);
112  delay(500);
113
114  Serial.println("Returning to home axis by axis.");
115  move(-800, 0, 0, 0);
116  delay(500);
117
118  move(0, -800, 0, 0);
119  delay(500);
120
121  move(0, 0, -800, 0);
122  delay(500);
123
124  move(0, 0, 0, -800);
125  delay(500);
126}
127/****************************************************************/