Clear ALevents for DC_sync0 and SM3 might have solved the uneven pulse train. Looking better now.

This commit is contained in:
Hakan Bastedt
2024-02-09 17:28:18 +01:00
parent d0433b29cf
commit 6d18c2cb3f
3 changed files with 70 additions and 30 deletions

View File

@@ -21,49 +21,52 @@ StepGen2::StepGen2(TIM_TypeDef *Timer, uint32_t _timerChannel, PinName _stepPin,
startTimer = new HardwareTimer(Timer2);
startTimer->attachInterrupt(irq2);
}
uint32_t cnt = 0;
uint32_t StepGen2::handleStepper(void)
{
if (!enabled)
return updatePos(0);
lcncCycleTime = StepGen2::sync0CycleTime;
lcncCycleTime = StepGen2::sync0CycleTime * 1.0e-6; // was usec, became sec
commandedStepPosition = floor(commandedPosition * stepsPerMM); // Scale position to steps
if (initialStepPosition == commandedStepPosition) // No movement
return updatePos(1);
float approximateFrequency = fabs(initialStepPosition - commandedStepPosition) // We must take at least one step
/ (float)lcncCycleTime; // from here on
if (approximateFrequency > maxAllowedFrequency) // Stay on this position
return 1;
// digitalWrite(dirPin, cnt++ % 2);
float approximateFrequency = fabs(initialStepPosition - commandedStepPosition) // We must take at least one step
/ lcncCycleTime; // from here on
// if (approximateFrequency > maxAllowedFrequency) // Stay on this position
// return 1;
float kTRAJ = (commandedPosition - initialPosition) / float(lcncCycleTime); // Straight line equation
float mTRAJ = initialPosition; // position = kTRAJ x time + mTRAJ
// Operating on incoming positions (not steps)
if (fabs(kTRAJ * lcncCycleTime * stepsPerMM) < 0.01) // Very flat slope
{ //
Tstart = 0.5 * lcncCycleTime; // Just take a step in the middle of the cycle
frequency = 10000; // At some suitable frequency
nSteps = kTRAJ > 0 ? 1 : -1; // Take only one step
float kTRAJ = (commandedPosition - initialPosition) / lcncCycleTime; // Straight line equation
float mTRAJ = initialPosition; // position = kTRAJ x time + mTRAJ
// Operating on incoming positions (not steps)
if (fabs(kTRAJ * lcncCycleTime * stepsPerMM) < 0.01) // Very flat slope
{ //
Tstartf = 0.5 * lcncCycleTime; // Just take a step in the middle of the cycle
frequency = 10000; // At some suitable frequency
nSteps = kTRAJ > 0 ? 1 : -1; // Take only one step
}
else // Regular step train, up or down
{
if (kTRAJ > 0)
Tstart = (ceil(initialPosition * stepsPerMM) - mTRAJ) / kTRAJ;
Tstartf = (ceil(initialPosition * stepsPerMM) / stepsPerMM - mTRAJ) / kTRAJ;
else
Tstart = (floor(initialPosition * stepsPerMM) - mTRAJ) / kTRAJ;
frequency = kTRAJ * stepsPerMM;
Tstartf = (floor(initialPosition * stepsPerMM) / stepsPerMM - mTRAJ) / kTRAJ;
frequency = fabs(kTRAJ * stepsPerMM);
nSteps = commandedStepPosition - initialStepPosition; // sign(nSteps) = direction.
}
updatePos(5);
Tstartu = Tstartf * 1e6; // Was secs, now usecs
startTimer->setOverflow(Tstart + Tjitter, MICROSEC_FORMAT); // All handled by irqs
startTimer->setOverflow(Tstartu + Tjitter, MICROSEC_FORMAT); // All handled by irqs
startTimer->resume();
return 1;
}
void StepGen2::startTimerCB()
{
startTimer->pause(); // Once is enough.
digitalWrite(dirPin, nSteps > 0 ? 1 : -1);
// digitalWrite(dirPin, nSteps > 0 ? 1 : -1);
timerPulseSteps = abs(nSteps);
pulseTimer->setMode(pulseTimerChan, TIMER_OUTPUT_COMPARE_PWM2, stepPin);
pulseTimer->setOverflow(uint32_t(frequency), HERTZ_FORMAT);