diff --git a/Marlin/Marlin.h b/Marlin/Marlin.h index c6274056c1..feda4bb06d 100644 --- a/Marlin/Marlin.h +++ b/Marlin/Marlin.h @@ -341,8 +341,7 @@ extern bool axis_homed[3]; // axis[n].is_homed extern bool filament_sensor; //indicates that filament sensor readings should control extrusion extern float filament_width_meas; //holds the filament diameter as accurately measured extern int8_t measurement_delay[]; //ring buffer to delay measurement - extern int delay_index1, delay_index2; //ring buffer index. used by planner, temperature, and main code - extern float delay_dist; //delay distance counter + extern int filwidth_delay_index1, filwidth_delay_index2; //ring buffer index. used by planner, temperature, and main code extern int meas_delay_cm; //delay distance #endif diff --git a/Marlin/Marlin_main.cpp b/Marlin/Marlin_main.cpp index 1deabb4770..863b6515e2 100644 --- a/Marlin/Marlin_main.cpp +++ b/Marlin/Marlin_main.cpp @@ -411,9 +411,8 @@ static uint8_t target_extruder; bool filament_sensor = false; //M405 turns on filament_sensor control, M406 turns it off float filament_width_meas = DEFAULT_MEASURED_FILAMENT_DIA; //Stores the measured filament diameter int8_t measurement_delay[MAX_MEASUREMENT_DELAY + 1]; //ring buffer to delay measurement store extruder factor after subtracting 100 - int delay_index1 = 0; //index into ring buffer - int delay_index2 = -1; //index into ring buffer - set to -1 on startup to indicate ring buffer needs to be initialized - float delay_dist = 0; //delay distance counter + int filwidth_delay_index1 = 0; //index into ring buffer + int filwidth_delay_index2 = -1; //index into ring buffer - set to -1 on startup to indicate ring buffer needs to be initialized int meas_delay_cm = MEASUREMENT_DELAY_CM; //distance delay setting #endif @@ -5428,13 +5427,13 @@ inline void gcode_M400() { st_synchronize(); } if (code_seen('D')) meas_delay_cm = code_value(); NOMORE(meas_delay_cm, MAX_MEASUREMENT_DELAY); - if (delay_index2 == -1) { //initialize the ring buffer if it has not been done since startup + if (filwidth_delay_index2 == -1) { // Initialize the ring buffer if not done since startup int temp_ratio = widthFil_to_size_ratio(); - for (delay_index1 = 0; delay_index1 < (int)COUNT(measurement_delay); ++delay_index1) - measurement_delay[delay_index1] = temp_ratio - 100; //subtract 100 to scale within a signed byte + for (uint8_t i = 0; i < COUNT(measurement_delay); ++i) + measurement_delay[i] = temp_ratio - 100; // Subtract 100 to scale within a signed byte - delay_index1 = delay_index2 = 0; + filwidth_delay_index1 = filwidth_delay_index2 = 0; } filament_sensor = true; diff --git a/Marlin/planner.cpp b/Marlin/planner.cpp index ed3aa78a93..318b5bdb28 100644 --- a/Marlin/planner.cpp +++ b/Marlin/planner.cpp @@ -852,25 +852,34 @@ float junction_deviation = 0.1; block->nominal_rate = ceil(block->step_event_count * inverse_second); // (step/sec) Always > 0 #if ENABLED(FILAMENT_WIDTH_SENSOR) + static float filwidth_e_count = 0, filwidth_delay_dist = 0; + //FMM update ring buffer used for delay with filament measurements - if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM && delay_index2 > -1) { //only for extruder with filament sensor and if ring buffer is initialized + if (extruder == FILAMENT_SENSOR_EXTRUDER_NUM && filwidth_delay_index2 >= 0) { //only for extruder with filament sensor and if ring buffer is initialized - const int MMD = MAX_MEASUREMENT_DELAY + 1, MMD10 = MMD * 10; + const int MMD_CM = MAX_MEASUREMENT_DELAY + 1, MMD_MM = MMD_CM * 10; - delay_dist += delta_mm[E_AXIS]; // increment counter with next move in e axis - while (delay_dist >= MMD10) delay_dist -= MMD10; // loop around the buffer - while (delay_dist < 0) delay_dist += MMD10; + // increment counters with next move in e axis + filwidth_e_count += delta_mm[E_AXIS]; + filwidth_delay_dist += delta_mm[E_AXIS]; - delay_index1 = delay_dist / 10.0; // calculate index - delay_index1 = constrain(delay_index1, 0, MAX_MEASUREMENT_DELAY); // (already constrained above) + // Only get new measurements on forward E movement + if (filwidth_e_count > 0.0001) { - if (delay_index1 != delay_index2) { // moved index - int8_t meas_sample = widthFil_to_size_ratio() - 100; // Subtract 100 to reduce magnitude - to store in a signed char - while (delay_index1 != delay_index2) { - // Increment and loop around buffer - if (++delay_index2 >= MMD) delay_index2 -= MMD; - delay_index2 = constrain(delay_index2, 0, MAX_MEASUREMENT_DELAY); - measurement_delay[delay_index2] = meas_sample; + // Loop the delay distance counter (modulus by the mm length) + while (filwidth_delay_dist >= MMD_MM) filwidth_delay_dist -= MMD_MM; + + // Convert into an index into the measurement array + filwidth_delay_index1 = (int)(filwidth_delay_dist / 10.0 + 0.0001); + + // If the index has changed (must have gone forward)... + if (filwidth_delay_index1 != filwidth_delay_index2) { + filwidth_e_count = 0; // Reset the E movement counter + int8_t meas_sample = widthFil_to_size_ratio() - 100; // Subtract 100 to reduce magnitude - to store in a signed char + do { + filwidth_delay_index2 = (filwidth_delay_index2 + 1) % MMD_CM; // The next unused slot + measurement_delay[filwidth_delay_index2] = meas_sample; // Store the measurement + } while (filwidth_delay_index1 != filwidth_delay_index2); // More slots to fill? } } } diff --git a/Marlin/temperature.cpp b/Marlin/temperature.cpp index 22797a06fb..a6898368ec 100644 --- a/Marlin/temperature.cpp +++ b/Marlin/temperature.cpp @@ -705,7 +705,7 @@ void manage_heater() { // Control the extruder rate based on the width sensor #if ENABLED(FILAMENT_WIDTH_SENSOR) if (filament_sensor) { - meas_shift_index = delay_index1 - meas_delay_cm; + meas_shift_index = filwidth_delay_index1 - meas_delay_cm; if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed // Get the delayed info and add 100 to reconstitute to a percent of