tests with linuxcnc, DC sync is not the way to go, leaving that

This commit is contained in:
Hakan Bastedt
2024-01-04 11:39:01 +01:00
parent 5978ebec24
commit e6cd5356c9
2 changed files with 18 additions and 13 deletions

View File

@@ -108,7 +108,7 @@ HardwareTimer *MyTim;
volatile uint32_t stepCount = 0, stepPulses = 0; volatile uint32_t stepCount = 0, stepPulses = 0;
volatile double_t actualPosition = 0; volatile double_t actualPosition = 0;
volatile double_t requestedPosition; volatile double_t requestedPosition;
volatile uint32_t pulsesToGo = 0; volatile int32_t pulsesToGo = 0;
volatile byte forwardDirection = 0; // 1 if going forward volatile byte forwardDirection = 0; // 1 if going forward
void TimerStep_CB(void) void TimerStep_CB(void)
@@ -122,7 +122,7 @@ void TimerStep_CB(void)
void makePulses(uint32_t period /* in usecs */, uint32_t pulses /* nr of pulses to do*/) void makePulses(uint32_t period /* in usecs */, uint32_t pulses /* nr of pulses to do*/)
{ {
MyTim->setOverflow(pulses*1000000/period, HERTZ_FORMAT); MyTim->setOverflow(pulses * 1000000 / period, HERTZ_FORMAT);
MyTim->setCaptureCompare(4, 50, PERCENT_COMPARE_FORMAT); // 50 % MyTim->setCaptureCompare(4, 50, PERCENT_COMPARE_FORMAT); // 50 %
stepCount = 0; stepCount = 0;
stepPulses = pulses; stepPulses = pulses;
@@ -189,22 +189,27 @@ void indexPulse(void)
} }
} }
uint64_t now = 0, back_then = 0;
uint32_t deltaT;
void sync0Handler(void) void sync0Handler(void)
{ {
const int32_t stepsPerMM = 1; now = micros();
const int32_t stepsPerMM = 1000;
// Update the actual position // Update the actual position
pulsesToGo = stepsPerMM * Obj.StepGenIn1.CommandedPosition; // Wrong, but test pulsesToGo = stepsPerMM * (Obj.StepGenIn1.CommandedPosition - actualPosition);
Obj.StepGenOut1.ActualPosition = actualPosition; Obj.StepGenOut1.ActualPosition = actualPosition;
// Get new end position // Get new end position
forwardDirection = pulsesToGo > 1 ? 1 : 0; forwardDirection = pulsesToGo > 1 ? 1 : 0;
// Set direction pin // Set direction pin
Obj.DiffT = forwardDirection; deltaT = now - back_then;
digitalWrite(STEPPER_DIR_PIN, forwardDirection); // I think one should really wait a bit when changed Obj.DiffT = deltaT;
// Make the pulses using hardware timer
if (pulsesToGo != 0) if (pulsesToGo != 0)
makePulses(1000 /*sync0CycleTime / 1000*/, abs((int)pulsesToGo)); {
digitalWrite(STEPPER_DIR_PIN, forwardDirection); // I think one should really wait a bit when changed
makePulses(deltaT-20, abs((int)pulsesToGo)); // Make the pulses using hardware timer
}
actualPosition = Obj.StepGenIn1.CommandedPosition; actualPosition = Obj.StepGenIn1.CommandedPosition;
back_then = now;
} }
void ESC_interrupt_enable(uint32_t mask) void ESC_interrupt_enable(uint32_t mask)

View File

@@ -39,7 +39,7 @@ index 5d9943c..261c965 100644
lcecPdoEntTypeFloatIeee lcecPdoEntTypeFloatIeee
} LCEC_PDOENT_TYPE_T; } LCEC_PDOENT_TYPE_T;
diff --git a/src/lcec_generic.c b/src/lcec_generic.c diff --git a/src/lcec_generic.c b/src/lcec_generic.c
index dfddf73..2f58fde 100644 index dfddf73..41a13a9 100644
--- a/src/lcec_generic.c --- a/src/lcec_generic.c
+++ b/src/lcec_generic.c +++ b/src/lcec_generic.c
@@ -26,6 +26,7 @@ hal_s32_t lcec_generic_read_s32(uint8_t *pd, lcec_generic_pin_t *hal_data); @@ -26,6 +26,7 @@ hal_s32_t lcec_generic_read_s32(uint8_t *pd, lcec_generic_pin_t *hal_data);
@@ -86,17 +86,17 @@ index dfddf73..2f58fde 100644
+ +
+ if (hal_data->pdo_bp == 0 && hal_data->bitOffset == 0) { + if (hal_data->pdo_bp == 0 && hal_data->bitOffset == 0) {
+ EC_WRITE_LREAL(&pd[hal_data->pdo_os], fval); + EC_WRITE_LREAL(&pd[hal_data->pdo_os], fval);
+ return;
+ } + }
+ union { + union {
+ double d; + double d;
+ uin64_t u; + uint64_t u;
+ } v; + } v;
+ v.d = fval; + v.d = fval; // Make an equivalent long int for bit operations (don't work on doubles)
+ offset = ((hal_data->pdo_os << 3) | (hal_data->pdo_bp & 0x07)) + hal_data->bitOffset; + offset = ((hal_data->pdo_os << 3) | (hal_data->pdo_bp & 0x07)) + hal_data->bitOffset;
+ for (i=0; i < hal_data->bitLength; i++, offset++) { + for (i=0; i < hal_data->bitLength; i++, offset++) {
+ EC_WRITE_BIT(&pd[offset >> 3], offset & 0x07,v.u & 1); + EC_WRITE_BIT(&pd[offset >> 3], offset & 0x07,v.u & 1);
+ v.u >>= 1; + v.u >>= 1;
+ } + }
+#endif
+} +}
+ +