Hello,
I am using the AccelStepper.h library to control a linear stepper motor in Arduino. I plotted both my acceleration and speed in terms of mm/s/s and m/s, respectively. I found an interesting result for the acceleration...it was constant. This doesn't make too much sense to me as I would have figured it would show accelerating/decelerating as the derivative of speed. Attached is my code and a figure of the output. I am printing the data to the serial plotter every 200 ms. Does anyone have an idea why acceleration is constant?
Thanks!
k
#include <AccelStepper.h>
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 2, 3);
#define STEPS_PER_REV 200
#define MICROSTEPS_PER_STEP 32
#define pitch 5
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Important Variable to change
int speed_desired = 4; // mm per s
int cycles = 10; // number of cycles to run
int distance_desired = 4; // stroke in mm
float motor_hz_desired = 0.0; // hz for motor
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
float x = 1.0; // keeps track of cycles. Inititially at 1.
float milmets = 0.0;
float current_milmets = 0.0;
float rev_per_mm = 1.0 / pitch;
float RPMS = speed_desired * rev_per_mm * 60.0;
float hertz = speed_desired / (distance_desired * 2.0);
float steps_per_mm = rev_per_mm * (MICROSTEPS_PER_STEP)*STEPS_PER_REV;
int steps_per_mm_int = static_cast<int>(steps_per_mm); //1280
float speed_acheived = speed_desired * steps_per_mm; //steps per second, 5120
float milliseconds_per_cycle = ((distance_desired * 2) / speed_desired) * 1000;
int stroke = distance_desired * steps_per_mm_int; //5120
void setup() {
stepper.setMinPulseWidth(20);
stepper.setMaxSpeed(speed_acheived); // steps/s
stepper.setAcceleration(speed_acheived * 2); // steps/s/s
Serial.begin(9600);
}
void loop() {
stepper.enableOutputs();
TEST_motion(stroke);
stepper.disableOutputs();
Serial.println("TEST COMPLETE.");
delay(500);
abort();
}
// This function moves the motor forward and backward for a set amount of cycles. 1 cycle = foward+backward
void TEST_motion(int stroke) {
static unsigned long lastTime;
while (x <= cycles) {
if (stepper.distanceToGo() == 0) { // switches the position
stroke = -stroke;
stepper.moveTo(stroke);
x = x + 0.5;
lastTime = millis();
}
stepper.run();
if (millis() - lastTime >= 200) {
Serial.print(stepper.currentPosition() / steps_per_mm);
Serial.print(" ");
Serial.print(stepper.speed() / steps_per_mm);
Serial.print(" ");
Serial.println(stepper.acceleration() / steps_per_mm);
lastTime = millis();
}
}
}

