Go Down

Topic: optimize code for stepping stepper motor (Read 419 times) previous topic - next topic

titous

hi everyone.  I was hoping you all could help me in optimizing some stepper code (and learn something new!).  the advantage of optimizing it a bit further is that i can potentially run more steppers at the same time.  since upgrading to the due, i'm not sure of how to fully optimize.  here is what i'm working with:

Code: [Select]
// function responsible for stepping (throwing step pin HIGH/LOW)
void Engine::step(void) {
   
  unsigned long _time = micros(); // i'm guessing this could be optimized, but how?
 
  // handles the case of time overflow, which occurs at about 70 minutes for micros;
  // so if _time overflows (goes back to 0) and our last step time is huge, let's reset it
  if (_time < _lastStepTime) {
    _lastStepTime = 0;
  }

  // responsible for stepping high
  if (_time >= _lastStepTime + _currentSpeed && !_stepCycle) { // only step HIGH after _currentSpeed has passed
    if (_stepActive) {
      digitalWriteDirect(_stepPin, HIGH); // step HIGH
    }
    _stepCycle = true;
    _lastStepTime = _time;

    // increment our absolute position based on direction traveled
    if (_dir) {
      _absPos++;
    } else {
      _absPos--;
    }
  }

  // responsible for stepping low
  if (_time >= _lastStepTime + _lowStepWait && _stepCycle) { // we can step LOW after a short period and if we have already stepped HIGH
    if (_stepActive) {
      digitalWriteDirect(_stepPin, LOW); // step LOW
    }
    _stepCycle = false;
    _lastStepTime = _time;
  }
}


i'm aware that on the Uno there is a much faster way of checking micros() directly from the registry through TCNT1; i wonder if we can do this for the Due? 

how about any further optimization?  i'm guessing there isn't any other blaring change.. any help is much appreciated!

Go Up