I am trying to ramp up a stepper to 4000 pulses per second (10 RPS), maintain that speed as long as desired, then accel/decel to a new determined speed.
I have tried AccelStepper, but believe it is intended to reach a position and not a constant speed. I have also tried manually ramping the speed (see bottom of code), but not really sure the appropriate method.
With AccelStepper code, the motor constantly spins (slowly) but doesn't ramp up to goal. Is there a way to constantly offset it's "moveTo" & setSpeed so that it abides by its Acceleration Rate, but changes to a new speed? I have attempted this below.
With my manual code approach, the motor ramps up and achieves what appears to be a decently fast speed but I'm not sure how fast it is running at and am not sure how I would ramp to a new speed after X time.
I don't have a lot of experience coding so am open to any suggestions & help.
Thanks
/*
* Notes re RPS Speed.
* 10 RPS * 400 Steps/rev = 4000 pulses for 10 RPS
*/
#include <AccelStepper.h>
#define directionPin 3
#define stepPin 2
#define MS1 4
#define MS2 5
#define MS3 6
#define EN 7
unsigned long curMicros;
unsigned long prevMicros = 0;
unsigned long minPulseSpeed = 500; //shortest micros duration to send a pulse
int currSpeed = 400; //current speed in pulses per second
int goalSpeed = 4000; //goal 10 RPS
unsigned long targetPos = 100000; //perpetually increased position to always move
//OTHER Variables for ALT Loop at end without Library
int goalRPS = 10; //IE 10 RPS = 250 microseconds per pulse
int stepsPerRev = 400;
int targetMicros = (1000000/(goalRPS*stepsPerRev));// how long for each revolution based on desiredRPS
unsigned long prevStepMicros = 0;
unsigned long microsBetweenSteps = 10000; // 1000 micros in milli
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE
void setup()
{
pinMode(MS1, OUTPUT);
pinMode(MS2, OUTPUT);
pinMode(MS3, OUTPUT);
pinMode(EN, OUTPUT);
digitalWrite(MS1, LOW); //Micro. 000 = full 100 = 1/2, 010 = 1/4, 110 = 1/8
digitalWrite(MS2, LOW);
digitalWrite(MS3, LOW);
digitalWrite(directionPin, LOW);
stepper.setMaxSpeed(5000); //slightly lower than goalSpeed
stepper.setSpeed(0);
stepper.setAcceleration(500);
stepper.moveTo(targetPos);
delay(1000);
}
void loop()
{
curMicros = micros();
singleStep();
// singleStepALT(); //Ramp attempt without Library.
}
void singleStep() {
if (currSpeed <= goalSpeed) {
if (curMicros - prevMicros >= minPulseSpeed) {
curMicros = prevMicros;
currSpeed *= 1.1; //increase speed slightly
stepper.setSpeed(currSpeed); //update motor speed
}
}
else if (currSpeed >= goalSpeed){
stepper.move(targetPos + 1000);
stepper.setSpeed(goalSpeed);
}
stepper.run();
}
//RAMP ATTEMPT WITHOUT LIBRARY. Works but not sure Pulse speed
//void singleStepALT() {
// if (curMicros - prevStepMicros >= microsBetweenSteps) {
// prevStepMicros += microsBetweenSteps;
// if (microsBetweenSteps > targetMicros) {
// microsBetweenSteps *= .999;
// }
// digitalWrite(stepPin, HIGH);
// digitalWrite(stepPin, LOW);
// }
//}