Factored Stepgen2, StepGen3 is a copy of Stepgen2
This commit is contained in:
@@ -50,13 +50,4 @@ public:
|
||||
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
|
||||
@@ -1,6 +1,8 @@
|
||||
#include <Arduino.h>
|
||||
#include <stdio.h>
|
||||
#include "StepGen2.h"
|
||||
#include "extend32to64.h"
|
||||
|
||||
extern "C"
|
||||
{
|
||||
#include "esc.h"
|
||||
@@ -120,20 +122,3 @@ uint32_t StepGen2::updatePos(uint32_t i)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
@@ -18,18 +18,19 @@ void indexPulseEncoderCB1(void)
|
||||
Encoder1.indexPulse();
|
||||
}
|
||||
|
||||
#include "StepGen2.h"
|
||||
#include "StepGen3.h"
|
||||
#include "extend32to64.h"
|
||||
// Stepper 1
|
||||
void pulseTimerCallback1(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 startTimerCallback1(void) { Step1.startTimerCB(); }
|
||||
|
||||
// Stepper 2
|
||||
void pulseTimerCallback2(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 startTimerCallback2(void) { Step2.startTimerCB(); }
|
||||
|
||||
@@ -56,7 +57,7 @@ void handleStepper(void)
|
||||
uint32_t t = micros();
|
||||
reallyNowTime = longTime.extendTime(t);
|
||||
timeDiff = 1000 * (reallyNowTime - reallyThenTime);
|
||||
nLoops = round(double(timeDiff) / double(StepGen2::sync0CycleTime));
|
||||
nLoops = round(double(timeDiff) / double(StepGen3::sync0CycleTime));
|
||||
reallyThenTime = reallyNowTime;
|
||||
nLoops=1;
|
||||
Step1.enabled = true;
|
||||
@@ -210,6 +211,6 @@ uint16_t dc_checker(void)
|
||||
{
|
||||
// Indicate we run DC
|
||||
ESCvar.dcsync = 1;
|
||||
StepGen2::sync0CycleTime = ESC_SYNC0cycletime(); // nsecs
|
||||
StepGen3::sync0CycleTime = ESC_SYNC0cycletime(); // nsecs
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user