🐛 Refine FT Motion, I2S Stepping (#26628)

Co-authored-by: Scott Lahteine <thinkyhead@users.noreply.github.com>
This commit is contained in:
narno2202 2024-01-08 05:17:43 +01:00 committed by GitHub
parent 38f483c4a6
commit b106f59eb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 132 additions and 71 deletions

View file

@ -34,6 +34,10 @@
#include <freertos/queue.h> #include <freertos/queue.h>
#include "../../module/stepper.h" #include "../../module/stepper.h"
#if ENABLED(FT_MOTION)
#include "../../module/ft_motion.h"
#endif
#define DMA_BUF_COUNT 8 // number of DMA buffers to store data #define DMA_BUF_COUNT 8 // number of DMA buffers to store data
#define DMA_BUF_LEN 4092 // maximum size in bytes #define DMA_BUF_LEN 4092 // maximum size in bytes
#define I2S_SAMPLE_SIZE 4 // 4 bytes, 32 bits per sample #define I2S_SAMPLE_SIZE 4 // 4 bytes, 32 bits per sample
@ -134,8 +138,8 @@ static void IRAM_ATTR i2s_intr_handler_default(void *arg) {
if (high_priority_task_awoken == pdTRUE) portYIELD_FROM_ISR(); if (high_priority_task_awoken == pdTRUE) portYIELD_FROM_ISR();
// clear interrupt // Clear pending interrupt
I2S0.int_clr.val = I2S0.int_st.val; //clear pending interrupt I2S0.int_clr.val = I2S0.int_st.val;
} }
void stepperTask(void *parameter) { void stepperTask(void *parameter) {
@ -148,29 +152,43 @@ void stepperTask(void *parameter) {
xQueueReceive(dma.queue, &dma.current, portMAX_DELAY); xQueueReceive(dma.queue, &dma.current, portMAX_DELAY);
dma.rw_pos = 0; dma.rw_pos = 0;
const bool using_ftMotion = TERN0(FT_MOTION, ftMotion.cfg.mode);
while (dma.rw_pos < DMA_SAMPLE_COUNT) { while (dma.rw_pos < DMA_SAMPLE_COUNT) {
if (!nextMainISR) {
Stepper::pulse_phase_isr(); #if ENABLED(FT_MOTION)
nextMainISR = Stepper::block_phase_isr();
} if (using_ftMotion) {
#if ENABLED(LIN_ADVANCE) if (!nextMainISR) stepper.ftMotion_stepper();
else if (!nextAdvanceISR) { nextMainISR = 0;
Stepper::advance_isr();
nextAdvanceISR = Stepper::la_interval;
} }
#endif #endif
else
i2s_push_sample();
nextMainISR--; if (!using_ftMotion) {
if (!nextMainISR) {
Stepper::pulse_phase_isr();
nextMainISR = Stepper::block_phase_isr();
}
#if ENABLED(LIN_ADVANCE)
else if (!nextAdvanceISR) {
Stepper::advance_isr();
nextAdvanceISR = Stepper::la_interval;
}
#endif
else
i2s_push_sample();
#if ENABLED(LIN_ADVANCE) nextMainISR--;
if (nextAdvanceISR == Stepper::LA_ADV_NEVER)
nextAdvanceISR = Stepper::la_interval;
if (nextAdvanceISR && nextAdvanceISR != Stepper::LA_ADV_NEVER) #if ENABLED(LIN_ADVANCE)
nextAdvanceISR--; if (nextAdvanceISR == Stepper::LA_ADV_NEVER)
#endif nextAdvanceISR = Stepper::la_interval;
if (nextAdvanceISR && nextAdvanceISR != Stepper::LA_ADV_NEVER)
nextAdvanceISR--;
#endif
}
} }
} }
} }

View file

@ -1309,6 +1309,12 @@
#define HAS_ZV_SHAPING 1 #define HAS_ZV_SHAPING 1
#endif #endif
// FT Motion unified window and batch size
#if ALL(FT_MOTION, FTM_UNIFIED_BWS)
#define FTM_WINDOW_SIZE FTM_BW_SIZE
#define FTM_BATCH_SIZE FTM_BW_SIZE
#endif
// Toolchange Event G-code // Toolchange Event G-code
#if !HAS_MULTI_EXTRUDER || !(defined(EVENT_GCODE_TOOLCHANGE_T0) || defined(EVENT_GCODE_TOOLCHANGE_T1) || defined(EVENT_GCODE_TOOLCHANGE_T2) || defined(EVENT_GCODE_TOOLCHANGE_T3) || defined(EVENT_GCODE_TOOLCHANGE_T4) || defined(EVENT_GCODE_TOOLCHANGE_T5) || defined(EVENT_GCODE_TOOLCHANGE_T6) || defined(EVENT_GCODE_TOOLCHANGE_T7)) #if !HAS_MULTI_EXTRUDER || !(defined(EVENT_GCODE_TOOLCHANGE_T0) || defined(EVENT_GCODE_TOOLCHANGE_T1) || defined(EVENT_GCODE_TOOLCHANGE_T2) || defined(EVENT_GCODE_TOOLCHANGE_T3) || defined(EVENT_GCODE_TOOLCHANGE_T4) || defined(EVENT_GCODE_TOOLCHANGE_T5) || defined(EVENT_GCODE_TOOLCHANGE_T6) || defined(EVENT_GCODE_TOOLCHANGE_T7))
#undef TC_GCODE_USE_GLOBAL_X #undef TC_GCODE_USE_GLOBAL_X

View file

@ -4123,8 +4123,12 @@ static_assert(_PLUS_TEST(3), "DEFAULT_MAX_ACCELERATION values must be positive."
/** /**
* Fixed-Time Motion limitations * Fixed-Time Motion limitations
*/ */
#if ALL(FT_MOTION, MIXING_EXTRUDER) #if ENABLED(FT_MOTION)
#error "FT_MOTION does not currently support MIXING_EXTRUDER." #if ENABLED(MIXING_EXTRUDER)
#error "FT_MOTION does not currently support MIXING_EXTRUDER."
#elif DISABLED(FTM_UNIFIED_BWS)
#error "FT_MOTION requires FTM_UNIFIED_BWS to be enabled because FBS is not yet implemented."
#endif
#endif #endif
// Multi-Stepping Limit // Multi-Stepping Limit

View file

@ -826,3 +826,10 @@
#if PIN_EXISTS(BEEPER) && ALL(SPEAKER, NO_SPEAKER) #if PIN_EXISTS(BEEPER) && ALL(SPEAKER, NO_SPEAKER)
#warning "The BEEPER cannot produce tones so you can disable SPEAKER." #warning "The BEEPER cannot produce tones so you can disable SPEAKER."
#endif #endif
/**
* Fixed-Time Motion
*/
#if ALL(FT_MOTION, I2S_STEPPER_STREAM)
#warning "FT_MOTION has not been tested with I2S_STEPPER_STREAM."
#endif

View file

@ -55,8 +55,8 @@ FTMotion ftMotion;
ft_config_t FTMotion::cfg; ft_config_t FTMotion::cfg;
bool FTMotion::busy; // = false bool FTMotion::busy; // = false
ft_command_t FTMotion::stepperCmdBuff[FTM_STEPPERCMD_BUFF_SIZE] = {0U}; // Stepper commands buffer. ft_command_t FTMotion::stepperCmdBuff[FTM_STEPPERCMD_BUFF_SIZE] = {0U}; // Stepper commands buffer.
uint32_t FTMotion::stepperCmdBuff_produceIdx = 0, // Index of next stepper command write to the buffer. int32_t FTMotion::stepperCmdBuff_produceIdx = 0, // Index of next stepper command write to the buffer.
FTMotion::stepperCmdBuff_consumeIdx = 0; // Index of next stepper command read from the buffer. FTMotion::stepperCmdBuff_consumeIdx = 0; // Index of next stepper command read from the buffer.
bool FTMotion::sts_stepperBusy = false; // The stepper buffer has items and is in use. bool FTMotion::sts_stepperBusy = false; // The stepper buffer has items and is in use.
@ -123,6 +123,8 @@ uint32_t FTMotion::interpIdx = 0, // Index of current data point b
float FTMotion::e_advanced_z1 = 0.0f; // (ms) Unit delay of advanced extruder position. float FTMotion::e_advanced_z1 = 0.0f; // (ms) Unit delay of advanced extruder position.
#endif #endif
constexpr uint32_t last_batchIdx = (FTM_WINDOW_SIZE) - (FTM_BATCH_SIZE);
//----------------------------------------------------------------- //-----------------------------------------------------------------
// Function definitions. // Function definitions.
//----------------------------------------------------------------- //-----------------------------------------------------------------
@ -145,8 +147,16 @@ void FTMotion::runoutBlock() {
ratio.reset(); ratio.reset();
max_intervals = cfg.modeHasShaper() ? shaper_intervals : 0; max_intervals = cfg.modeHasShaper() ? shaper_intervals : 0;
if (max_intervals <= TERN(FTM_UNIFIED_BWS, FTM_BW_SIZE, min_max_intervals - (FTM_BATCH_SIZE))) max_intervals = min_max_intervals; if (max_intervals <= TERN(FTM_UNIFIED_BWS, FTM_BATCH_SIZE, min_max_intervals - (FTM_BATCH_SIZE)))
max_intervals += TERN(FTM_UNIFIED_BWS, FTM_BW_SIZE, FTM_WINDOW_SIZE) - makeVector_batchIdx; max_intervals = min_max_intervals;
max_intervals += (
#if ENABLED(FTM_UNIFIED_BWS)
FTM_WINDOW_SIZE - makeVector_batchIdx
#else
FTM_WINDOW_SIZE - ((last_batchIdx < (FTM_BATCH_SIZE)) ? 0 : makeVector_batchIdx)
#endif
);
blockProcRdy = blockDataIsRunout = true; blockProcRdy = blockDataIsRunout = true;
runoutEna = blockProcDn = false; runoutEna = blockProcDn = false;
} }
@ -198,7 +208,7 @@ void FTMotion::loop() {
); );
// Shift the time series back in the window // Shift the time series back in the window
#define TSHIFT(A) memcpy(traj.A, &traj.A[FTM_BATCH_SIZE], (FTM_WINDOW_SIZE - FTM_BATCH_SIZE) * sizeof(traj.A[0])) #define TSHIFT(A) memcpy(traj.A, &traj.A[FTM_BATCH_SIZE], last_batchIdx * sizeof(traj.A[0]))
LOGICAL_AXIS_CODE( LOGICAL_AXIS_CODE(
TSHIFT(e), TSHIFT(e),
TSHIFT(x), TSHIFT(y), TSHIFT(z), TSHIFT(x), TSHIFT(y), TSHIFT(z),
@ -219,7 +229,7 @@ void FTMotion::loop() {
&& (interpIdx - interpIdx_z1 < (FTM_STEPS_PER_LOOP)) && (interpIdx - interpIdx_z1 < (FTM_STEPS_PER_LOOP))
) { ) {
convertToSteps(interpIdx); convertToSteps(interpIdx);
if (++interpIdx == TERN(FTM_UNIFIED_BWS, FTM_BW_SIZE, FTM_BATCH_SIZE)) { if (++interpIdx == FTM_BATCH_SIZE) {
batchRdyForInterp = false; batchRdyForInterp = false;
interpIdx = 0; interpIdx = 0;
} }
@ -449,7 +459,7 @@ void FTMotion::reset() {
endPosn_prevBlock.reset(); endPosn_prevBlock.reset();
makeVector_idx = makeVector_idx_z1 = 0; makeVector_idx = makeVector_idx_z1 = 0;
makeVector_batchIdx = 0; makeVector_batchIdx = TERN(FTM_UNIFIED_BWS, 0, _MAX(last_batchIdx, FTM_BATCH_SIZE));
steps.reset(); steps.reset();
interpIdx = interpIdx_z1 = 0; interpIdx = interpIdx_z1 = 0;
@ -464,10 +474,11 @@ void FTMotion::reset() {
} }
// Private functions. // Private functions.
// Auxiliary function to get number of step commands in the buffer. // Auxiliary function to get number of step commands in the buffer.
uint32_t FTMotion::stepperCmdBuffItems() { int32_t FTMotion::stepperCmdBuffItems() {
const uint32_t udiff = stepperCmdBuff_produceIdx - stepperCmdBuff_consumeIdx; const int32_t udiff = stepperCmdBuff_produceIdx - stepperCmdBuff_consumeIdx;
return stepperCmdBuff_produceIdx < stepperCmdBuff_consumeIdx ? (FTM_STEPPERCMD_BUFF_SIZE) + udiff : udiff; return (udiff < 0) ? udiff + (FTM_STEPPERCMD_BUFF_SIZE) : udiff;
} }
// Initializes storage variables before startup. // Initializes storage variables before startup.
@ -677,8 +688,8 @@ void FTMotion::makeVector() {
#endif #endif
// Filled up the queue with regular and shaped steps // Filled up the queue with regular and shaped steps
if (++makeVector_batchIdx == TERN(FTM_UNIFIED_BWS, FTM_BW_SIZE, (FTM_WINDOW_SIZE - FTM_BATCH_SIZE))) { if (++makeVector_batchIdx == FTM_WINDOW_SIZE) {
makeVector_batchIdx = 0; makeVector_batchIdx = last_batchIdx;
batchRdy = true; batchRdy = true;
} }

View file

@ -102,12 +102,11 @@ class FTMotion {
} }
static ft_command_t stepperCmdBuff[FTM_STEPPERCMD_BUFF_SIZE]; // Buffer of stepper commands. static ft_command_t stepperCmdBuff[FTM_STEPPERCMD_BUFF_SIZE]; // Buffer of stepper commands.
static uint32_t stepperCmdBuff_produceIdx, // Index of next stepper command write to the buffer. static int32_t stepperCmdBuff_produceIdx, // Index of next stepper command write to the buffer.
stepperCmdBuff_consumeIdx; // Index of next stepper command read from the buffer. stepperCmdBuff_consumeIdx; // Index of next stepper command read from the buffer.
static bool sts_stepperBusy; // The stepper buffer has items and is in use. static bool sts_stepperBusy; // The stepper buffer has items and is in use.
// Public methods // Public methods
static void init(); static void init();
static void startBlockProc(); // Set controller states to begin processing a block. static void startBlockProc(); // Set controller states to begin processing a block.
@ -153,10 +152,10 @@ class FTMotion {
static uint32_t N1, N2, N3; static uint32_t N1, N2, N3;
static uint32_t max_intervals; static uint32_t max_intervals;
static constexpr uint32_t _ftm_size = TERN(FTM_UNIFIED_BWS, FTM_BW_SIZE, FTM_BATCH_SIZE), #define _DIVCEIL(A,B) (((A) + (B) - 1) / (B))
_ftm_wind = TERN(FTM_UNIFIED_BWS, 2, CEIL((FTM_WINDOW_SIZE) / _ftm_size)), static constexpr uint32_t _ftm_ratio = TERN(FTM_UNIFIED_BWS, 2, _DIVCEIL(FTM_WINDOW_SIZE, FTM_BATCH_SIZE)),
shaper_intervals = _ftm_size * CEIL((FTM_ZMAX) / _ftm_size), shaper_intervals = (FTM_BATCH_SIZE) * _DIVCEIL(FTM_ZMAX, FTM_BATCH_SIZE),
min_max_intervals = _ftm_size * _ftm_wind; min_max_intervals = (FTM_BATCH_SIZE) * _ftm_ratio;
// Make vector variables. // Make vector variables.
static uint32_t makeVector_idx, static uint32_t makeVector_idx,
@ -203,7 +202,7 @@ class FTMotion {
#endif #endif
// Private methods // Private methods
static uint32_t stepperCmdBuffItems(); static int32_t stepperCmdBuffItems();
static void loadBlockData(block_t *const current_block); static void loadBlockData(block_t *const current_block);
static void makeVector(); static void makeVector();
static void convertToSteps(const uint32_t idx); static void convertToSteps(const uint32_t idx);

View file

@ -44,13 +44,8 @@ enum dynFreqMode_t : uint8_t {
#define IS_EI_MODE(N) WITHIN(N, ftMotionMode_EI, ftMotionMode_3HEI) #define IS_EI_MODE(N) WITHIN(N, ftMotionMode_EI, ftMotionMode_3HEI)
#if ENABLED(FTM_UNIFIED_BWS) typedef struct XYZEarray<float, FTM_WINDOW_SIZE> xyze_trajectory_t;
typedef struct XYZEarray<float, FTM_BW_SIZE> xyze_trajectory_t; typedef struct XYZEarray<float, FTM_BATCH_SIZE> xyze_trajectoryMod_t;
typedef struct XYZEarray<float, FTM_BW_SIZE> xyze_trajectoryMod_t;
#else
typedef struct XYZEarray<float, FTM_WINDOW_SIZE> xyze_trajectory_t;
typedef struct XYZEarray<float, FTM_BATCH_SIZE> xyze_trajectoryMod_t;
#endif
enum { enum {
LIST_N(DOUBLE(LOGICAL_AXES), LIST_N(DOUBLE(LOGICAL_AXES),

View file

@ -1508,14 +1508,12 @@ void Stepper::isr() {
#if ENABLED(FT_MOTION) #if ENABLED(FT_MOTION)
if (using_ftMotion) { if (using_ftMotion) {
if (!nextMainISR) { if (!nextMainISR) { // Main ISR is ready to fire during this iteration?
nextMainISR = FTM_MIN_TICKS; nextMainISR = FTM_MIN_TICKS; // Set to minimum interval (a limit on the top speed)
ftMotion_stepper(); ftMotion_stepper(); // Run FTM Stepping
endstops.update();
TERN_(BABYSTEPPING, if (babystep.has_steps()) babystepping_isr());
} }
interval = nextMainISR; interval = nextMainISR; // Interval is either some old nextMainISR or FTM_MIN_TICKS
nextMainISR -= interval; nextMainISR = 0; // For FT Motion fire again ASAP
} }
#endif #endif
@ -3448,7 +3446,8 @@ void Stepper::report_positions() {
// Use one byte to restore one stepper command in the format: // Use one byte to restore one stepper command in the format:
// |X_step|X_direction|Y_step|Y_direction|Z_step|Z_direction|E_step|E_direction| // |X_step|X_direction|Y_step|Y_direction|Z_step|Z_direction|E_step|E_direction|
const ft_command_t command = ftMotion.stepperCmdBuff[ftMotion.stepperCmdBuff_consumeIdx]; const ft_command_t command = ftMotion.stepperCmdBuff[ftMotion.stepperCmdBuff_consumeIdx];
if (++ftMotion.stepperCmdBuff_consumeIdx == (FTM_STEPPERCMD_BUFF_SIZE)) ftMotion.stepperCmdBuff_consumeIdx = 0U; if (++ftMotion.stepperCmdBuff_consumeIdx == (FTM_STEPPERCMD_BUFF_SIZE))
ftMotion.stepperCmdBuff_consumeIdx = 0;
if (abort_current_block) return; if (abort_current_block) return;
@ -3492,6 +3491,8 @@ void Stepper::report_positions() {
U_APPLY_STEP(axis_did_move.u, false), V_APPLY_STEP(axis_did_move.v, false), W_APPLY_STEP(axis_did_move.w, false) U_APPLY_STEP(axis_did_move.u, false), V_APPLY_STEP(axis_did_move.v, false), W_APPLY_STEP(axis_did_move.w, false)
); );
TERN_(I2S_STEPPER_STREAM, i2s_push_sample());
// Begin waiting for the minimum pulse duration // Begin waiting for the minimum pulse duration
START_TIMED_PULSE(); START_TIMED_PULSE();
@ -3533,6 +3534,12 @@ void Stepper::report_positions() {
U_APPLY_STEP(!STEP_STATE_U, false), V_APPLY_STEP(!STEP_STATE_V, false), W_APPLY_STEP(!STEP_STATE_W, false) U_APPLY_STEP(!STEP_STATE_U, false), V_APPLY_STEP(!STEP_STATE_V, false), W_APPLY_STEP(!STEP_STATE_W, false)
); );
// Check endstops on every step
IF_DISABLED(ENDSTOP_INTERRUPTS_FEATURE, endstops.update());
// Also handle babystepping here
TERN_(BABYSTEPPING, if (babystep.has_steps()) babystepping_isr());
} // Stepper::ftMotion_stepper } // Stepper::ftMotion_stepper
void Stepper::ftMotion_blockQueueUpdate() { void Stepper::ftMotion_blockQueueUpdate() {

View file

@ -34,7 +34,9 @@
// //
// I2S (steppers & other output-only pins) // I2S (steppers & other output-only pins)
// //
#define I2S_STEPPER_STREAM #ifndef I2S_STEPPER_STREAM
#define I2S_STEPPER_STREAM
#endif
#if ENABLED(I2S_STEPPER_STREAM) #if ENABLED(I2S_STEPPER_STREAM)
#define I2S_WS 17 #define I2S_WS 17
#define I2S_BCK 22 #define I2S_BCK 22

View file

@ -32,7 +32,9 @@
// //
// I2S (steppers & other output-only pins) // I2S (steppers & other output-only pins)
// //
#define I2S_STEPPER_STREAM #ifndef I2S_STEPPER_STREAM
#define I2S_STEPPER_STREAM
#endif
#if ENABLED(I2S_STEPPER_STREAM) #if ENABLED(I2S_STEPPER_STREAM)
#define I2S_WS 25 #define I2S_WS 25
#define I2S_BCK 26 #define I2S_BCK 26

View file

@ -56,7 +56,9 @@
// //
// Enable I2S stepper stream // Enable I2S stepper stream
// //
#define I2S_STEPPER_STREAM #ifndef I2S_STEPPER_STREAM
#define I2S_STEPPER_STREAM
#endif
#if ENABLED(I2S_STEPPER_STREAM) #if ENABLED(I2S_STEPPER_STREAM)
#define I2S_WS 26 #define I2S_WS 26
#define I2S_BCK 25 #define I2S_BCK 25

View file

@ -85,7 +85,9 @@
// //
// Enable I2S stepper stream // Enable I2S stepper stream
// //
#define I2S_STEPPER_STREAM #ifndef I2S_STEPPER_STREAM
#define I2S_STEPPER_STREAM
#endif
#if ENABLED(I2S_STEPPER_STREAM) #if ENABLED(I2S_STEPPER_STREAM)
#define I2S_WS 26 #define I2S_WS 26
#define I2S_BCK 25 #define I2S_BCK 25

View file

@ -51,7 +51,9 @@
// //
// Enable I2S stepper stream // Enable I2S stepper stream
// //
#define I2S_STEPPER_STREAM #ifndef I2S_STEPPER_STREAM
#define I2S_STEPPER_STREAM
#endif
#if ENABLED(I2S_STEPPER_STREAM) #if ENABLED(I2S_STEPPER_STREAM)
#define I2S_WS 26 #define I2S_WS 26
#define I2S_BCK 25 #define I2S_BCK 25

View file

@ -34,4 +34,6 @@
// //
// I2S (steppers & other output-only pins) // I2S (steppers & other output-only pins)
// //
#define I2S_STEPPER_STREAM #ifndef I2S_STEPPER_STREAM
#define I2S_STEPPER_STREAM
#endif

View file

@ -13,15 +13,17 @@
# Espressif ESP32 # Espressif ESP32
# #
[env:esp32] [env:esp32]
platform = espressif32@2.1.0 platform = espressif32@2.1.0
board = esp32dev platform_packages = espressif/toolchain-xtensa-esp32s3
build_flags = ${common.build_flags} -DCORE_DEBUG_LEVEL=0 board = esp32dev
build_src_filter = ${common.default_src_filter} +<src/HAL/ESP32> build_flags = ${common.build_flags} -DCORE_DEBUG_LEVEL=0 -std=gnu++17
lib_ignore = NativeEthernet build_unflags = -std=gnu11 -std=gnu++11
upload_speed = 500000 build_src_filter = ${common.default_src_filter} +<src/HAL/ESP32>
monitor_speed = 250000 lib_ignore = NativeEthernet
monitor_filters = colorize, time, send_on_enter, log2file, esp32_exception_decoder upload_speed = 500000
#upload_port = marlinesp.local monitor_speed = 250000
monitor_filters = colorize, time, send_on_enter, log2file, esp32_exception_decoder
#upload_port = marlinesp.local
#board_build.flash_mode = qio #board_build.flash_mode = qio
[env:FYSETC_E4] [env:FYSETC_E4]