From 703bbfb03a90d0405a785d0420af6e79a48b8e77 Mon Sep 17 00:00:00 2001 From: Hakan Bastedt Date: Mon, 1 Jan 2024 15:27:29 +0100 Subject: [PATCH] wip, stepper doesn't run --- Firmware/src/Stepper.cpp | 2 +- Firmware/src/main.cpp | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Firmware/src/Stepper.cpp b/Firmware/src/Stepper.cpp index 518bf0a..27f8888 100755 --- a/Firmware/src/Stepper.cpp +++ b/Firmware/src/Stepper.cpp @@ -226,7 +226,7 @@ static void MX_TIM1_Init(void) sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; - if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) + if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK) { Error_Handler(); } diff --git a/Firmware/src/main.cpp b/Firmware/src/main.cpp index 45693c9..a74d0e8 100755 --- a/Firmware/src/main.cpp +++ b/Firmware/src/main.cpp @@ -57,7 +57,7 @@ void cb_get_inputs(void) // Set Master inputs, slave outputs, last operation nFires++; PreviousEncoderCounterValue = 0; } - Obj.DiffT = sync0CycleTime; + // Obj.DiffT = sync0CycleTime; int64_t pos = unwrap_encoder(TIM2->CNT, &PreviousEncoderCounterValue); double CurPos = pos * PosScaleRes; @@ -102,7 +102,7 @@ static esc_cfg_t config = .esc_hw_eep_handler = NULL, .esc_check_dc_handler = dc_checker, }; - +#define STEPPER_DIR PA12 void setup(void) { Serial1.begin(115200); @@ -117,6 +117,7 @@ void setup(void) ecat_slv_init(&config); attachInterrupt(digitalPinToInterrupt(INDEX_PIN), indexPulse, RISING); // Always when Index triggered + pinMode(STEPPER_DIR, OUTPUT); } void loop(void) @@ -160,11 +161,12 @@ volatile int32_t actualPosition = 0; volatile int32_t requestedPosition; volatile uint32_t pulsesToGo = 0; volatile byte forwardDirection = 0; // 1 if going forward -#define STEPPER_DIR PA12 -// #define STEPPER_STEP PA11 // Set in StepperSetup + +volatile uint32_t syncCounts = 0; void sync0Handler(void) { +#if 0 // Update the actual position actualPosition += pulsesToGo; Obj.StepGenOut1.ActualPosition = actualPosition; @@ -172,9 +174,16 @@ void sync0Handler(void) requestedPosition = Obj.StepGenIn1.CommandedPosition; // Get the diff and the direction pulsesToGo = requestedPosition - actualPosition; - forwardDirection = pulsesToGo > 0 ? 1 : 0; +#else + syncCounts++; + pulsesToGo = syncCounts % 2 ? 4 : 8; +#endif + + // forwardDirection = pulsesToGo > 0 ? 1 : 0; + forwardDirection = syncCounts % 2 ? 1 : 0; // Set direction pin digitalWrite(STEPPER_DIR, forwardDirection); // I think one should really wait a bit when changed + Obj.DiffT = forwardDirection; // Make the pulses using hardware timer makePulses(sync0CycleTime / 1000, pulsesToGo); }