I'm trying to linearly accelerate a stepper motor but I'm hard coding the microsecond delay with a count x++ using:
digitalWrite(dirPin,LOW); // Enables the motor to move in a particular direction
for(int x = 0; x < 1000; x++) { // Change this number of pulses for rotation degree
digitalWrite(stepPin,HIGH);
digitalWrite(LEDPin, HIGH);
delayMicroseconds(250); // starts at 250 microsecond pulse
digitalWrite(stepPin,LOW);
digitalWrite(LEDPin, LOW);
delayMicroseconds(250);
}
for(int x = 0; x < 1000; x++) { // Change this number of pulses for rotation degree
digitalWrite(stepPin,HIGH);
digitalWrite(LEDPin, HIGH);
delayMicroseconds(225); // decreases the pulse time by 25 pulse
digitalWrite(stepPin,LOW);
digitalWrite(LEDPin, LOW);
delayMicroseconds(225);
}
I want to reduce the pulse count linearly using some sort of method using a start value and an end value where the pulse lowers by one step per count cycle. So let's say I use one int x = 0; x <8000; x++; for the cycle count but the microsecond delay would be something like int i = 250; i < 50; i--; where the value i is determined by the cycle count to accelerate to 50 microsecond pulses from 250 and then accelerate back from 50 to 250 in 8000 cycles.
I'm sorry if my code doesn't indent properly. I'm still trying to figure this one out. Thanks if you have any suggestions.
As I remeber steppers have 2 parameters, PULL_IN_RATE, resp. PULL_OUT_RATE. They tell how fast You can reach top speed resp. what maximum retardation is from full speed. Myebe worth having a look at.
Thank You so much. All were a great help. I think the acceleration code posted helped me understand. The stepper has high torque and I have about 150 kg being turned at a 75:1 ratio. I only have to turn the turret 1/4 turn left and then back to center after 10 seconds. Then right 1/4 turn and back to center. I'm using a hybrid drive HSB86oH. So as you can imagine the step using 25 microsecond pulse increments caused it to jerk a lot. I'm trying to smooth that by using a linear acceleration and deceleration. Thank you all.
neuralmorph:
So let's say I use one int x = 0; x <8000; x++; for the cycle count but the microsecond delay would be something like int i = 250; i < 50; i--; where the value i is determined by the cycle count to accelerate to 50 microsecond pulses from 250 and then accelerate back from 50 to 250 in 8000 cycles.
OK.. So to accelerate from 250 to 50 you need 200 cycles and to decelerate from 50 to 250 you need another 200 cycles. That leaves 7600 cycles (8000 - (2 * 200)) at full speed.
const byte stepPin = 9;
const byte LEDPin = 13;
const unsigned long MaxPulseLength = 250;
const unsigned long MinPulseLength = 50;
const unsigned AccelerationCount = MaxPulseLength - MinPulseLength;
void move(int steps)
{
int accelerationSteps = AccelerationCount;
int coastingSteps;
if (accelerationSteps * 2 > steps)
{
// No time to get to full speed
accelerationSteps = steps / 2;
coastingSteps = 0;
}
else
coastingSteps = steps - (accelerationSteps * 2);
unsigned long pulseLength = MaxPulseLength; // Start at slowest speed
for (int x = 0; x < accelerationSteps; x++)
{ // Change this number of pulses for rotation degree
digitalWrite(stepPin, HIGH);
digitalWrite(LEDPin, HIGH);
delayMicroseconds(pulseLength);
digitalWrite(stepPin, LOW);
digitalWrite(LEDPin, LOW);
delayMicroseconds(pulseLength);
pulseLength--; // Accelerate
}
// At full speed
for (int x = 0; x < coastingSteps; x++)
{ // Change this number of pulses for rotation degree
digitalWrite(stepPin, HIGH);
digitalWrite(LEDPin, HIGH);
delayMicroseconds(MinPulseLength);
digitalWrite(stepPin, LOW);
digitalWrite(LEDPin, LOW);
delayMicroseconds(MinPulseLength);
}
// Decelerate
for (int x = 0; x < accelerationSteps; x++)
{ // Change this number of pulses for rotation degree
digitalWrite(stepPin, HIGH);
digitalWrite(LEDPin, HIGH);
delayMicroseconds(pulseLength);
digitalWrite(stepPin, LOW);
digitalWrite(LEDPin, LOW);
delayMicroseconds(pulseLength);
pulseLength++; // Decelerate
}
}