Problems with a stepper motor and Teensy 3.2

Status
Not open for further replies.

so999

Member
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:

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.
 
Ok. I solved it.
The problem has nothing to do with the timers or the teensy board. As teensy has 3.3V output and my driver has 5V input, I put a transistor which converts the voltage. But the transistor wasn’t very fast and coupled with few resistors, lowered the duty cycle of the signal. At low frequencies this doesn’t matter, but at high frequencies and low duty cycles, the stepper driver simply stops. I was misled by the oscilloscope measurements. But finally I saw that the interrupt still count the pulses so the timer worked fine.
Sorry for the inconvenience!
 
Status
Not open for further replies.
Back
Top