Hello,
I wish to implement a ramping procedure at a stepper motor. My approach was to use a timer (analogWrite function) for generating the pulse signal and another timer (an IntervalTimer) to change periodically the frequency of the pulse pin. I thought that this could work as analogWrite uses FTM and IntervalTimer uses PIT so they are independent. Also I need to count the pulses/steps so I attached an interrupt to pulse pin.
In a test program I started the motor from setup function and then after few seconds a testTimer started the ramp down procedure.
This is the test I did:
Obviously this doesn’t work well. When the rampTimer is activated, the motor stops and after a second or two, it continue with the ramp down. If I slow down the motor a bit, the ramping works fine. I would expect that due to the higher speed one or the other interrupt to work badly. So some pulses to be lost or the ramping to be irregular. But the problem is that the motor stops. The pin doesn’t produce pulses in that interval (I checked with an oscilloscope too). I attached a short video.View attachment bad ramp.zip
I will appreciate any help.
I wish to implement a ramping procedure at a stepper motor. My approach was to use a timer (analogWrite function) for generating the pulse signal and another timer (an IntervalTimer) to change periodically the frequency of the pulse pin. I thought that this could work as analogWrite uses FTM and IntervalTimer uses PIT so they are independent. Also I need to count the pulses/steps so I attached an interrupt to pulse pin.
In a test program I started the motor from setup function and then after few seconds a testTimer started the ramp down procedure.
This is the test I did:
Code:
double stepFreq = 100.0L;
volatile double currentFrequency = 0.0L;
volatile double endFrequency = 0.0L;
uint8_t pulsePin = 3;
volatile unsigned long pulseAddress = 0; //a complete rotation is done with totalMicrostepsNumber pulses; this counts the pulses from 0 to totalMicrostepsNumber
unsigned long Npulses = 0;
IntervalTimer rampTimer;
IntervalTimer testTimer;
void setup() {
Serial.begin(9600);
pinMode(pulsePin, OUTPUT);
Npulses = 1920000;
currentFrequency=600000.0;
endFrequency = 519.36;
analogWriteResolution(16);
analogWriteFrequency(pulsePin, currentFrequency);
analogWrite(pulsePin, 20000);
testTimer.begin(testFunc, 7000000);
}
void testFunc()
{
testTimer.end();
rampTimer.begin(changeFrequency, 1000); //starting the ramping down process
}
void changeFrequency()
{
cli();
currentFrequency = currentFrequency - stepFreq;
if (currentFrequency <= endFrequency)
{
rampTimer.end(); //stop ramp up
}
else
{
analogWriteFrequency(pulsePin, currentFrequency);
analogWrite(pulsePin, 32768);
attachInterrupt (pulsePin, countPulses, RISING);
}
sei();
}
void countPulses()
{
cli();
pulseAddress = pulseAddress + 1;
if (pulseAddress >= Npulses)
{
detachInterrupt (pulsePin);
rampTimer.end();
}
sei();
}
void loop() {
}
Obviously this doesn’t work well. When the rampTimer is activated, the motor stops and after a second or two, it continue with the ramp down. If I slow down the motor a bit, the ramping works fine. I would expect that due to the higher speed one or the other interrupt to work badly. So some pulses to be lost or the ramping to be irregular. But the problem is that the motor stops. The pin doesn’t produce pulses in that interval (I checked with an oscilloscope too). I attached a short video.View attachment bad ramp.zip
I will appreciate any help.