Direction output to dirPin.

This commit is contained in:
Hakan Bastedt
2024-02-16 11:45:06 +01:00
parent 2b2be4f63d
commit 30dc44d5e6
2 changed files with 17 additions and 20 deletions

View File

@@ -12,10 +12,10 @@ public:
volatile uint32_t timerFrequency; volatile uint32_t timerFrequency;
public: public:
volatile float Tstartf; // Starting delay in secs volatile float Tstartf; // Starting delay in secs
volatile uint32_t Tstartu; // Starting delay in usecs volatile uint32_t Tstartu; // Starting delay in usecs
volatile float Tpulses; // Time it takes to do pulses. Debug volatile float Tpulses; // Time it takes to do pulses. Debug
const float maxAllowedFrequency = 100000; // 100 kHz for now
HardwareTimer *pulseTimer; HardwareTimer *pulseTimer;
uint32_t pulseTimerChan; uint32_t pulseTimerChan;
HardwareTimer *startTimer; // 10,11,13,14 HardwareTimer *startTimer; // 10,11,13,14
@@ -23,6 +23,10 @@ public:
PinName stepPin; PinName stepPin;
const uint32_t Tjitter = 500; // Time unit is microseconds const uint32_t Tjitter = 500; // Time 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 t3 = 3; // Pulse width at least 2.5 usecs
const uint16_t t4 = 3; // Low level width not less than 2.5 usecs
const float maxAllowedFrequency = 1000000 / float(t3 + t4) * 0.9; // 150 kHz for now
public: public:
volatile double_t commandedPosition; // End position when this cycle is completed volatile double_t commandedPosition; // End position when this cycle is completed

View File

@@ -29,34 +29,26 @@ StepGen2::StepGen2(TIM_TypeDef *Timer, uint32_t _timerChannel, PinName _stepPin,
extern volatile uint32_t cnt; extern volatile uint32_t cnt;
uint32_t StepGen2::handleStepper(uint64_t irqTime) uint32_t StepGen2::handleStepper(uint64_t irqTime)
{ {
digitalWrite(dirPin, cnt++ % 2);
if (!enabled) if (!enabled)
return updatePos(0); return updatePos(0);
lcncCycleTime = StepGen2::sync0CycleTime * 1.0e-6; // was usec, became sec lcncCycleTime = StepGen2::sync0CycleTime * 1.0e-6; // was usec, became sec
commandedStepPosition = floor(commandedPosition * stepsPerMM); // Scale position to steps commandedStepPosition = floor(commandedPosition * stepsPerMM); // Scale position to steps
if (initialStepPosition == commandedStepPosition) // No movement if (initialStepPosition == commandedStepPosition) // No movement
return 1; return updatePos(1);
float approximateFrequency = fabs(initialStepPosition - commandedStepPosition) // We must take at least one step nSteps = commandedStepPosition - initialStepPosition;
/ lcncCycleTime; // from here on
// if (approximateFrequency > maxAllowedFrequency) // Stay on this position
// return 1;
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
nSteps = commandedStepPosition - initialStepPosition; //
if (abs(nSteps) < 1000) // Some small number if (abs(nSteps) < 1000) // Some small number
{ // {
frequency = (abs(nSteps) + 1) / lcncCycleTime; frequency = (abs(nSteps) + 1) / lcncCycleTime;
Tpulses = abs(nSteps) / frequency; Tpulses = abs(nSteps) / frequency;
Tstartf = (lcncCycleTime - Tpulses) / 2.0; Tstartf = (lcncCycleTime - Tpulses) / 2.0;
} }
else // Regular step train, up or down else // Regular step train, up or down
{ {
float kTRAJ = (commandedPosition - initialPosition) / lcncCycleTime; // Straight line equation. position = kTRAJ x time + mTRAJ
float mTRAJ = initialPosition; // Operating on incoming positions (not steps)
if (kTRAJ > 0) if (kTRAJ > 0)
Tstartf = (float(initialStepPosition + 1) / float(stepsPerMM) - mTRAJ) / kTRAJ; Tstartf = (float(initialStepPosition + 1) / float(stepsPerMM) - mTRAJ) / kTRAJ;
else else
@@ -69,15 +61,17 @@ uint32_t StepGen2::handleStepper(uint64_t irqTime)
dbg = timeSinceISR; dbg = timeSinceISR;
Tstartu = Tjitter + uint32_t(Tstartf * 1e6) - timeSinceISR; // Have already wasted some time since the irq. Tstartu = Tjitter + uint32_t(Tstartf * 1e6) - timeSinceISR; // Have already wasted some time since the irq.
timerFrequency = uint32_t(frequency); timerFrequency = uint32_t(ceil(frequency));
startTimer->setOverflow(Tstartu, MICROSEC_FORMAT); // All handled by irqs startTimer->setOverflow(Tstartu, MICROSEC_FORMAT); // All handled by irqs
startTimer->resume(); startTimer->resume();
return 1; return 1;
} }
void StepGen2::startTimerCB() void StepGen2::startTimerCB()
{ {
digitalWrite(dirPin, cnt++ % 2);
startTimer->pause(); // Once is enough. startTimer->pause(); // Once is enough.
digitalWrite(dirPin, nSteps > 0 ? HIGH : LOW);
// There will be a short break here for t2 usecs, in the future.
timerPulseSteps = abs(nSteps); timerPulseSteps = abs(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);
@@ -90,7 +84,6 @@ void StepGen2::pulseTimerCB()
if (timerPulseSteps == 0) if (timerPulseSteps == 0)
{ {
pulseTimer->pause(); pulseTimer->pause();
digitalWrite(dirPin, cnt++ % 2);
} }
} }