From 1f97534133befd25332412f9095ffcb1d35a7d05 Mon Sep 17 00:00:00 2001 From: Hakan Bastedt Date: Sat, 20 Jan 2024 12:37:42 +0100 Subject: [PATCH] New pwmCycleTime different from sync0CycleTime --- Firmware/include/StepGen.h | 1 + Firmware/src/StepGen.cpp | 11 ++++++----- Firmware/src/main.cpp | 3 ++- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/Firmware/include/StepGen.h b/Firmware/include/StepGen.h index 4f7fc85..b94fbe2 100755 --- a/Firmware/include/StepGen.h +++ b/Firmware/include/StepGen.h @@ -26,6 +26,7 @@ private: public: static uint32_t sync0CycleTime; + static uint32_t pwmCycleTime; StepGen(TIM_TypeDef *Timer, uint32_t timerChannel, PinName stepPin, uint8_t dirPin, void irq(void)); void reqPos(double_t pos); diff --git a/Firmware/src/StepGen.cpp b/Firmware/src/StepGen.cpp index 52fd1de..c0f37d2 100755 --- a/Firmware/src/StepGen.cpp +++ b/Firmware/src/StepGen.cpp @@ -51,10 +51,10 @@ void StepGen::handleStepper(void) actPos(timerStepPosition / double(stepsPerMM)); double diffPosition = reqPos() - actPos(); - uint64_t fre = abs(diffPosition) * stepsPerMM * 1000000 / double(sync0CycleTime); // Frequency needed + uint64_t fre = abs(diffPosition) * stepsPerMM * 1000000 / double(pwmCycleTime); // Frequency needed if (fre > maxFreq) // Only do maxFre { - double maxDist = maxFreq / stepsPerMM * sync0CycleTime / 1000000.0 * (diffPosition > 0 ? 1 : -1); + double maxDist = maxFreq / stepsPerMM * pwmCycleTime / 1000000.0 * (diffPosition > 0 ? 1 : -1); reqPos(actualPosition + maxDist); } int32_t pulsesAtEndOfCycle = stepsPerMM * reqPos(); // From Turner.hal X:5000 Z:2000 ps/mm @@ -72,7 +72,7 @@ void StepGen::handleStepper(void) digitalWrite(dirPin, sgn); timerStepDirection = steps > 0 ? 1 : -1; timerStepPositionAtEnd = pulsesAtEndOfCycle; // Current Position - double_t freqf = abs(steps) * (1e6 / double(sync0CycleTime)); + double_t freqf = abs(steps) * (1e6 / double(pwmCycleTime)); uint32_t freq = uint32_t(freqf); MyTim->setMode(timerChan, TIMER_OUTPUT_COMPARE_PWM2, stepPin); MyTim->setOverflow(freq, HERTZ_FORMAT); @@ -96,7 +96,7 @@ void StepGen::timerCB() { uint8_t sgn = steps > 0 ? HIGH : LOW; digitalWrite(dirPin, sgn); - double_t freqf = abs(steps) * (1e6 / double(sync0CycleTime)); + double_t freqf = abs(steps) * (1e6 / double(pwmCycleTime)); uint32_t freq = uint32_t(freqf); timerStepDirection = steps > 0 ? 1 : -1; timerStepPositionAtEnd = timerNewEndStepPosition; @@ -120,4 +120,5 @@ void StepGen::setScale(int16_t spm) stepsPerMM = spm; } -uint32_t StepGen::sync0CycleTime = 0; \ No newline at end of file +uint32_t StepGen::sync0CycleTime = 0; +uint32_t StepGen::pwmCycleTime = 0; diff --git a/Firmware/src/main.cpp b/Firmware/src/main.cpp index 3f702dd..5d15905 100755 --- a/Firmware/src/main.cpp +++ b/Firmware/src/main.cpp @@ -176,6 +176,7 @@ uint16_t dc_checker(void) { // Indicate we run DC ESCvar.dcsync = 1; - StepGen::sync0CycleTime = 10*ESC_SYNC0cycletime() / 1000; + StepGen::sync0CycleTime = ESC_SYNC0cycletime() / 1000; // usecs + StepGen::pwmCycleTime = 10 * StepGen::sync0CycleTime; return 0; }