Factored Stepgen2, StepGen3 is a copy of Stepgen2

This commit is contained in:
Hakan Bastedt
2024-03-21 16:45:46 +01:00
parent 30a9f10c7b
commit fa34d81e41
3 changed files with 8 additions and 31 deletions

View File

@@ -50,13 +50,4 @@ public:
uint32_t updatePos(uint32_t i); uint32_t updatePos(uint32_t i);
}; };
class extend32to64
{
public:
int64_t previousTimeValue = 0;
const uint64_t ONE_PERIOD = 4294967296; // almost UINT32_MAX;
const uint64_t HALF_PERIOD = 2147483648; // Half of that
int64_t extendTime(uint32_t in);
};
#endif #endif

View File

@@ -1,6 +1,8 @@
#include <Arduino.h> #include <Arduino.h>
#include <stdio.h> #include <stdio.h>
#include "StepGen2.h" #include "StepGen2.h"
#include "extend32to64.h"
extern "C" extern "C"
{ {
#include "esc.h" #include "esc.h"
@@ -120,20 +122,3 @@ uint32_t StepGen2::updatePos(uint32_t i)
} }
uint32_t StepGen2::sync0CycleTime = 0; uint32_t StepGen2::sync0CycleTime = 0;
// Extend from 32-bit to 64-bit precision
int64_t extend32to64::extendTime(uint32_t in)
{
int64_t c64 = (int64_t)in - HALF_PERIOD; // remove half period to determine (+/-) sign of the wrap
int64_t dif = (c64 - previousTimeValue); // core concept: prev + (current - prev) = current
// wrap difference from -HALF_PERIOD to HALF_PERIOD. modulo prevents differences after the wrap from having an incorrect result
int64_t mod_dif = ((dif + HALF_PERIOD) % ONE_PERIOD) - HALF_PERIOD;
if (dif < int64_t(-HALF_PERIOD))
mod_dif += ONE_PERIOD; // account for mod of negative number behavior in C
int64_t unwrapped = previousTimeValue + mod_dif;
previousTimeValue = unwrapped; // load previous value
return unwrapped + HALF_PERIOD; // remove the shift we applied at the beginning, and return
}

View File

@@ -18,18 +18,19 @@ void indexPulseEncoderCB1(void)
Encoder1.indexPulse(); Encoder1.indexPulse();
} }
#include "StepGen2.h" #include "StepGen3.h"
#include "extend32to64.h"
// Stepper 1 // Stepper 1
void pulseTimerCallback1(void); void pulseTimerCallback1(void);
void startTimerCallback1(void); void startTimerCallback1(void);
StepGen2 Step1(TIM1, 4, PA_11, PA12, pulseTimerCallback1, TIM10, startTimerCallback1); StepGen3 Step1(TIM1, 4, PA_11, PA12, pulseTimerCallback1, TIM10, startTimerCallback1);
void pulseTimerCallback1(void) { Step1.pulseTimerCB(); } void pulseTimerCallback1(void) { Step1.pulseTimerCB(); }
void startTimerCallback1(void) { Step1.startTimerCB(); } void startTimerCallback1(void) { Step1.startTimerCB(); }
// Stepper 2 // Stepper 2
void pulseTimerCallback2(void); void pulseTimerCallback2(void);
void startTimerCallback2(void); void startTimerCallback2(void);
StepGen2 Step2(TIM3, 4, PC_9, PC10, pulseTimerCallback2, TIM11, startTimerCallback2); StepGen3 Step2(TIM3, 4, PC_9, PC10, pulseTimerCallback2, TIM11, startTimerCallback2);
void pulseTimerCallback2(void) { Step2.pulseTimerCB(); } void pulseTimerCallback2(void) { Step2.pulseTimerCB(); }
void startTimerCallback2(void) { Step2.startTimerCB(); } void startTimerCallback2(void) { Step2.startTimerCB(); }
@@ -56,7 +57,7 @@ void handleStepper(void)
uint32_t t = micros(); uint32_t t = micros();
reallyNowTime = longTime.extendTime(t); reallyNowTime = longTime.extendTime(t);
timeDiff = 1000 * (reallyNowTime - reallyThenTime); timeDiff = 1000 * (reallyNowTime - reallyThenTime);
nLoops = round(double(timeDiff) / double(StepGen2::sync0CycleTime)); nLoops = round(double(timeDiff) / double(StepGen3::sync0CycleTime));
reallyThenTime = reallyNowTime; reallyThenTime = reallyNowTime;
nLoops=1; nLoops=1;
Step1.enabled = true; Step1.enabled = true;
@@ -210,6 +211,6 @@ uint16_t dc_checker(void)
{ {
// Indicate we run DC // Indicate we run DC
ESCvar.dcsync = 1; ESCvar.dcsync = 1;
StepGen2::sync0CycleTime = ESC_SYNC0cycletime(); // nsecs StepGen3::sync0CycleTime = ESC_SYNC0cycletime(); // nsecs
return 0; return 0;
} }