# Can AccelStepper accelerate non-linearly?

I am running a Nema 23 motor with an Arduino Uno and a microstepper driver. I am making a torsional vibration demo, which uses a series of springs connected to the motor driveshaft to spin gears. These gears, as changes in shaft speed occur, should begin to weave in- and out-of-phase with one another. In my particular case, I am using three potentiometers, which read 0-1023, to read speed, amplitude, and frequency. The changes in motor speed are where I run into my issues. I need the speed of the stepper to be a sinusoidal function - of the form "Speed - Amplitude x sin(frequency x time)".

Below is some rudimentary code I put together with the AccelStepper library, which does not work:

``````#include <AccelStepper.h>

/* Using a stepper motor,
rotate a vibrational model using gears interconnected by springs
in such a way as to induce torsional vibration, which should be visible by comparing
movement of juxtaposed rotating gears with markers painted on. */

AccelStepper stepper(AccelStepper::DRIVER);
void setEnablePin(int enablePin = 8);

int Microstepping = 8; /* Microstepping factor, as set on the ST-M5045 control board.
Multiplies the number of steps per revolution on the stepper motor.
Reference controller manual for adjustments to this number. */
int Steps = 200; /* Number of steps on your stepper motor.*/
int SPS;
int Amp;
int Freq;
// int SC;
int n = 2;
int i;
int totalSteps = Microstepping * Steps;
int currentPosition();

/* It should be noted that the Microstepping and Steps factors are not necessary, and adjusting the microstepping factor
on the fly will not break the code. However, be aware that changing the microstepping factor by more than one interval
at a time can cause the motor to not keep up with the sudden changes, i.e. losing torque or vibrating/stuttering. */

void setup() {               // THIS RUNS ONCE //

Serial.begin(9600);

// read potentiometers; raw inputs from 0 to 1023 //
SPS = analogRead(0) * 0.5; // MAX steps per second should be <1000. Speeds above 1000 unreliable, according to AccelStepper docs.//
Amp = analogRead(1) / 20;   // MAX AMP ~51//
Freq = analogRead(2) / 170; // MAX FREQ ~12 Hz//

stepper.setMaxSpeed(1000);
stepper.setAcceleration(SPS / 1.5);

int nextStop = totalSteps / Freq;

stepper.moveTo(nextStop);
stepper.run();
Serial.println("INITIALIZED");
}

void loop() {

float spd = SPS + (Amp*sin(Freq*millis()));
stepper.setSpeed(spd);
stepper.run();
Serial.print("speed:  ");
Serial.println(spd);
}
``````

The issue I am having with the above code is that it enters the loop but does not vary the speed, despite the serial output showing me a speed which oscillates about the desired value several times a second.

And now, folks, what you've all been waiting for: The Point.

Does AccelStepper actually support non-linear accelerations not as the result of an error? I cite the following excerpt from the AccelStepper documentation,

`````` /// Sets the maximum permitted speed. The run() function will accelerate
/// up to the speed set by this function.
/// Caution: the maximum speed achievable depends on your processor and clock speed.
/// The default maxSpeed is 1.0 steps per second.
/// \param[in] speed The desired maximum speed in steps per second. Must
/// be > 0. Caution: Speeds that exceed the maximum speed supported by the processor may
/// Result in non-linear accelerations and decelerations.
void    setMaxSpeed(float speed);
``````

which makes the only mention of non-linear acceleration in all of the documentation - in the context of an overflow error.

I've thought of possibly using a modified version of the easydriver example program from reply #3 to this post if AccelStepper isn't capable of doing what I need it to do. Included is also an image of the system (minus the arduino and controller).