Well it works, network-wise. But there are those extra peaks that have been since the start. Let's see if I can remove them. Welcome StepGen3

This commit is contained in:
Hakan Bastedt
2024-03-21 16:28:55 +01:00
parent 8f26a10224
commit 30a9f10c7b
2 changed files with 19 additions and 17 deletions

View File

@@ -21,12 +21,13 @@ public:
uint32_t pulseTimerChan; uint32_t pulseTimerChan;
HardwareTimer *startTimer; // Use timers 10,11,13,14 HardwareTimer *startTimer; // Use timers 10,11,13,14
uint8_t dirPin; uint8_t dirPin;
PinName dirPinName;
PinName stepPin; PinName stepPin;
uint32_t Tjitter = 400; // Longest time from IRQ to handling in handleStepper, unit is microseconds uint32_t Tjitter = 400; // Longest time from IRQ to handling in handleStepper, unit is microseconds
uint64_t dbg; uint64_t dbg;
const uint16_t t2 = 5; // DIR is ahead of PUL with at least 5 usecs const uint16_t t2 = 5; // DIR is ahead of PUL with at least 5 usecs
const uint16_t t3 = 3; // Pulse width at least 2.5 usecs const uint16_t t3 = 5; // Pulse width at least 2.5 usecs
const uint16_t t4 = 3; // Low level width not less than 2.5 usecs const uint16_t t4 = 5; // Low level width not less than 2.5 usecs
const float maxAllowedFrequency = 1000000 / float(t3 + t4) * 0.9; // 150 kHz for now const float maxAllowedFrequency = 1000000 / float(t3 + t4) * 0.9; // 150 kHz for now
public: public:

View File

@@ -18,6 +18,7 @@ StepGen2::StepGen2(TIM_TypeDef *Timer, uint32_t _timerChannel, PinName _stepPin,
enabled = 0; enabled = 0;
dirPin = _dirPin; dirPin = _dirPin;
dirPinName = digitalPinToPinName(dirPin);
stepPin = _stepPin; stepPin = _stepPin;
pulseTimerChan = _timerChannel; pulseTimerChan = _timerChannel;
pulseTimer = new HardwareTimer(Timer); pulseTimer = new HardwareTimer(Timer);
@@ -36,13 +37,11 @@ uint32_t StepGen2::handleStepper(uint64_t irqTime, uint16_t nLoops)
return updatePos(0); return updatePos(0);
commandedStepPosition = floor(commandedPosition * stepsPerMM); // Scale position to steps commandedStepPosition = floor(commandedPosition * stepsPerMM); // Scale position to steps
#if 0 nSteps = commandedStepPosition - initialStepPosition;
if (initialStepPosition == commandedStepPosition) // No movement if (nSteps == 0) // No movement
{ {
return updatePos(1); return updatePos(1);
} }
#endif
nSteps = commandedStepPosition - initialStepPosition;
lcncCycleTime = nLoops * StepGen2::sync0CycleTime * 1.0e-9; // nLoops is there in case we missed an ethercat cycle. secs lcncCycleTime = nLoops * StepGen2::sync0CycleTime * 1.0e-9; // nLoops is there in case we missed an ethercat cycle. secs
if (abs(nSteps) < 0) // Some small number if (abs(nSteps) < 0) // Some small number
@@ -81,20 +80,22 @@ uint32_t StepGen2::handleStepper(uint64_t irqTime, uint16_t nLoops)
void StepGen2::startTimerCB() void StepGen2::startTimerCB()
{ {
startTimer->pause(); // Once is enough. startTimer->pause(); // Once is enough.
digitalWriteFast(digitalPinToPinName(dirPin), nSteps < 0 ? HIGH : LOW); // nSteps negative => decrease, HIGH digitalWriteFast(dirPinName, nSteps < 0 ? HIGH : LOW); // nSteps negative => decrease, HIGH
// There will be a short break here for t2 usecs, in the future. // There will be a short break here for t2 usecs, in the future.
timerEndPosition += nSteps; timerEndPosition += nSteps;
pulseTimer->setMode(pulseTimerChan, TIMER_OUTPUT_COMPARE_PWM2, stepPin); pulseTimer->setMode(pulseTimerChan, TIMER_OUTPUT_COMPARE_PWM2, stepPin);
pulseTimer->setOverflow(timerFrequency, HERTZ_FORMAT); pulseTimer->setOverflow(timerFrequency, HERTZ_FORMAT);
pulseTimer->setCaptureCompare(pulseTimerChan, 50, PERCENT_COMPARE_FORMAT); // pulseTimer->setCaptureCompare(pulseTimerChan, 50, PERCENT_COMPARE_FORMAT);
pulseTimer->setCaptureCompare(pulseTimerChan, t3, MICROSEC_COMPARE_FORMAT);
pulseTimer->refresh(); pulseTimer->refresh();
pulseTimer->resume(); pulseTimer->resume();
} }
void StepGen2::pulseTimerCB() void StepGen2::pulseTimerCB()
{ {
int16_t dir = digitalReadFast(digitalPinToPinName(dirPin)); int16_t dir = digitalReadFast(dirPinName); //
if (dir == HIGH) if (dir == HIGH) // The step just taken
timerPosition--; timerPosition--;
else else
timerPosition++; timerPosition++;
@@ -104,9 +105,9 @@ void StepGen2::pulseTimerCB()
else else
{ {
if (diffPosition < 0 && dir == LOW) // Change direction. Should not end up here, but alas if (diffPosition < 0 && dir == LOW) // Change direction. Should not end up here, but alas
digitalWriteFast(digitalPinToPinName(dirPin), HIGH); // Normal is to be HIGH when decreasing digitalWriteFast(dirPinName, HIGH); // Normal is to be HIGH when decreasing
if (diffPosition > 0 && dir == HIGH) // Change direction. Should not end up here, but alas if (diffPosition > 0 && dir == HIGH) // Change direction. Should not end up here, but alas
digitalWriteFast(digitalPinToPinName(dirPin), LOW); // Normal is to be LOW when increasing digitalWriteFast(dirPinName, LOW); // Normal is to be LOW when increasing
// Normally nothing is needed // Normally nothing is needed
} }
} }