Backlash cleanup (#13659)

…And save backlash, fil. sensor, ExtUI userdata to EEPROM.
This commit is contained in:
Marcio Teixeira
2019-05-03 22:53:15 -06:00
committed by Scott Lahteine
parent 0181e57417
commit 15357af67c
22 changed files with 645 additions and 293 deletions

View File

@ -37,7 +37,7 @@
*/
// Change EEPROM version if the structure changes
#define EEPROM_VERSION "V65"
#define EEPROM_VERSION "V66"
#define EEPROM_OFFSET 100
// Check the integrity of data offsets.
@ -90,10 +90,16 @@
#include "../feature/pause.h"
#if ENABLED(BACKLASH_COMPENSATION)
#include "../feature/backlash.h"
#endif
#if HAS_FILAMENT_SENSOR
#include "../feature/runout.h"
#endif
#include "../lcd/extensible_ui/ui_api.h"
#if ENABLED(EXTRA_LIN_ADVANCE_K)
extern float saved_extruder_advance_K[EXTRUDERS];
#endif
@ -149,6 +155,7 @@ typedef struct SettingsDataStruct {
// FILAMENT_RUNOUT_SENSOR
//
bool runout_sensor_enabled; // M412 S
float runout_distance_mm; // M412 D
//
// ENABLE_LEVELING_FADE_HEIGHT
@ -298,6 +305,21 @@ typedef struct SettingsDataStruct {
toolchange_settings_t toolchange_settings; // M217 S P R
#endif
//
// BACKLASH_COMPENSATION
//
float backlash_distance_mm[XYZ]; // M425 X Y Z
uint8_t backlash_correction; // M425 F
float backlash_smoothing_mm; // M425 S
//
// EXTENSIBLE_UI
//
#if ENABLED(EXTENSIBLE_UI)
// This is a significant hardware change; don't reserve space when not present
uint8_t extui_data[ExtUI::eeprom_data_size];
#endif
} SettingsData;
//static_assert(sizeof(SettingsData) <= E2END + 1, "EEPROM too small to contain SettingsData!");
@ -372,6 +394,16 @@ void MarlinSettings::postprocess() {
report_current_position();
}
#if ENABLED(PRINTCOUNTER) && ENABLED(EEPROM_SETTINGS)
#include "printcounter.h"
static_assert(
!WITHIN(STATS_EEPROM_ADDRESS, EEPROM_OFFSET, EEPROM_OFFSET + sizeof(SettingsData)) &&
!WITHIN(STATS_EEPROM_ADDRESS + sizeof(printStatistics), EEPROM_OFFSET, EEPROM_OFFSET + sizeof(SettingsData)),
"STATS_EEPROM_ADDRESS collides with EEPROM settings storage."
);
#endif
#if ENABLED(SD_FIRMWARE_UPDATE)
#if ENABLED(EEPROM_SETTINGS)
@ -528,11 +560,18 @@ void MarlinSettings::postprocess() {
//
{
#if HAS_FILAMENT_SENSOR
EEPROM_WRITE(runout.enabled);
const bool &runout_sensor_enabled = runout.enabled;
#else
const bool runout_sensor_enabled = true;
EEPROM_WRITE(runout_sensor_enabled);
const bool runout_sensor_enabled = false;
#endif
#if HAS_FILAMENT_SENSOR && defined(FILAMENT_RUNOUT_DISTANCE_MM)
const float &runout_distance_mm = runout.runout_distance();
#else
const float runout_distance_mm = 0;
#endif
_FIELD_TEST(runout_sensor_enabled);
EEPROM_WRITE(runout_sensor_enabled);
EEPROM_WRITE(runout_distance_mm);
}
//
@ -1118,6 +1157,42 @@ void MarlinSettings::postprocess() {
EEPROM_WRITE(toolchange_settings);
#endif
//
// Backlash Compensation
//
{
#if ENABLED(BACKLASH_COMPENSATION)
const float (&backlash_distance_mm)[XYZ] = backlash.distance_mm;
const uint8_t &backlash_correction = backlash.correction;
#else
const float backlash_distance_mm[XYZ] = { 0 };
const uint8_t backlash_correction = 0;
#endif
#ifdef BACKLASH_SMOOTHING_MM
const float &backlash_smoothing_mm = backlash.smoothing_mm;
#else
const float backlash_smoothing_mm = 3;
#endif
_FIELD_TEST(backlash_distance_mm);
EEPROM_WRITE(backlash_distance_mm[X_AXIS]);
EEPROM_WRITE(backlash_distance_mm[Y_AXIS]);
EEPROM_WRITE(backlash_distance_mm[Z_AXIS]);
EEPROM_WRITE(backlash_correction);
EEPROM_WRITE(backlash_smoothing_mm);
}
//
// Extensible UI User Data
//
#if ENABLED(EXTENSIBLE_UI)
{
char extui_data[ExtUI::eeprom_data_size] = { 0 };
ExtUI::onStoreSettings(extui_data);
_FIELD_TEST(extui_data);
EEPROM_WRITE(extui_data);
}
#endif
//
// Validate CRC and Data Size
//
@ -1148,7 +1223,7 @@ void MarlinSettings::postprocess() {
#endif
#if ENABLED(EXTENSIBLE_UI)
if (!eeprom_error) ExtUI::onStoreSettings();
ExtUI::onConfigurationStoreWritten(!eeprom_error);
#endif
return !eeprom_error;
@ -1264,12 +1339,18 @@ void MarlinSettings::postprocess() {
// Filament Runout Sensor
//
{
_FIELD_TEST(runout_sensor_enabled);
#if HAS_FILAMENT_SENSOR
EEPROM_READ(runout.enabled);
bool &runout_sensor_enabled = runout.enabled;
#else
bool runout_sensor_enabled;
EEPROM_READ(runout_sensor_enabled);
#endif
_FIELD_TEST(runout_sensor_enabled);
EEPROM_READ(runout_sensor_enabled);
float runout_distance_mm;
EEPROM_READ(runout_distance_mm);
#if HAS_FILAMENT_SENSOR && defined(FILAMENT_RUNOUT_DISTANCE_MM)
runout.set_runout_distance(runout_distance_mm);
#endif
}
@ -1851,6 +1932,44 @@ void MarlinSettings::postprocess() {
EEPROM_READ(toolchange_settings);
#endif
//
// Backlash Compensation
//
{
#if ENABLED(BACKLASH_COMPENSATION)
float (&backlash_distance_mm)[XYZ] = backlash.distance_mm;
uint8_t &backlash_correction = backlash.correction;
#else
float backlash_distance_mm[XYZ];
uint8_t backlash_correction;
#endif
#ifdef BACKLASH_SMOOTHING_MM
float &backlash_smoothing_mm = backlash.smoothing_mm;
#else
float backlash_smoothing_mm;
#endif
_FIELD_TEST(backlash_distance_mm);
EEPROM_READ(backlash_distance_mm[X_AXIS]);
EEPROM_READ(backlash_distance_mm[Y_AXIS]);
EEPROM_READ(backlash_distance_mm[Z_AXIS]);
EEPROM_READ(backlash_correction);
EEPROM_READ(backlash_smoothing_mm);
}
//
// Extensible UI User Data
//
#if ENABLED(EXTENSIBLE_UI)
// This is a significant hardware change; don't reserve EEPROM space when not present
{
const char extui_data[ExtUI::eeprom_data_size] = { 0 };
_FIELD_TEST(extui_data);
EEPROM_READ(extui_data);
if(!validating)
ExtUI::onLoadSettings(extui_data);
}
#endif
eeprom_error = size_error(eeprom_index - (EEPROM_OFFSET));
if (eeprom_error) {
DEBUG_ECHO_START();
@ -1921,7 +2040,7 @@ void MarlinSettings::postprocess() {
if (validate()) {
const bool success = _load();
#if ENABLED(EXTENSIBLE_UI)
if (success) ExtUI::onLoadSettings();
ExtUI::onConfigurationStoreRead(success);
#endif
return success;
}
@ -2090,6 +2209,9 @@ void MarlinSettings::reset() {
#if HAS_FILAMENT_SENSOR
runout.enabled = true;
runout.reset();
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
runout.set_runout_distance(FILAMENT_RUNOUT_DISTANCE_MM);
#endif
#endif
//
@ -2108,6 +2230,23 @@ void MarlinSettings::reset() {
toolchange_settings.z_raise = TOOLCHANGE_ZRAISE;
#endif
#if ENABLED(BACKLASH_GCODE)
backlash.correction = (BACKLASH_CORRECTION) * 255;
#ifdef BACKLASH_DISTANCE_MM
constexpr float tmp[XYZ] = BACKLASH_DISTANCE_MM;
backlash.distance_mm[X_AXIS] = tmp[X_AXIS];
backlash.distance_mm[Y_AXIS] = tmp[Y_AXIS];
backlash.distance_mm[Z_AXIS] = tmp[Z_AXIS];
#endif
#ifdef BACKLASH_SMOOTHING_MM
backlash.smoothing_mm = BACKLASH_SMOOTHING_MM;
#endif
#endif
#if ENABLED(EXTENSIBLE_UI)
ExtUI::onFactoryReset();
#endif
//
// Magnetic Parking Extruder
//
@ -3200,6 +3339,31 @@ void MarlinSettings::reset() {
CONFIG_ECHO_START();
M217_report(true);
#endif
#if ENABLED(BACKLASH_GCODE)
CONFIG_ECHO_HEADING("Backlash compensation:");
CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(
" M425 F", backlash.get_correction(),
" X", LINEAR_UNIT(backlash.distance_mm[X_AXIS]),
" Y", LINEAR_UNIT(backlash.distance_mm[Y_AXIS]),
" Z", LINEAR_UNIT(backlash.distance_mm[Z_AXIS])
#ifdef BACKLASH_SMOOTHING_MM
, " S", LINEAR_UNIT(backlash.smoothing_mm)
#endif
);
#endif
#if HAS_FILAMENT_SENSOR
CONFIG_ECHO_HEADING("Filament runout sensor:");
CONFIG_ECHO_START();
SERIAL_ECHOLNPAIR(
" M412 S", int(runout.enabled)
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
, " D", LINEAR_UNIT(runout.runout_distance())
#endif
);
#endif
}
#endif // !DISABLE_M503

View File

@ -92,6 +92,10 @@
#include "../feature/power.h"
#endif
#if ENABLED(BACKLASH_COMPENSATION)
#include "../feature/backlash.h"
#endif
// Delay for delivery of first block to the stepper ISR, if the queue contains 2 or
// fewer movements. The delay is measured in milliseconds, and must be less than 250ms
#define BLOCK_DELAY_FOR_1ST_MOVE 100
@ -1560,94 +1564,6 @@ void Planner::synchronize() {
) idle();
}
/**
* The following implements axis backlash correction. To minimize seams
* on the printed part, the backlash correction only adds steps to the
* current segment (instead of creating a new segment, which causes
* discontinuities and print artifacts).
*
* When BACKLASH_SMOOTHING_MM is enabled and non-zero, the backlash
* correction is spread over multiple segments, smoothing out print
* artifacts even more.
*/
#if ENABLED(BACKLASH_COMPENSATION)
#if ENABLED(BACKLASH_GCODE)
extern float backlash_distance_mm[];
extern uint8_t backlash_correction;
#ifdef BACKLASH_SMOOTHING_MM
extern float backlash_smoothing_mm;
#endif
#else
constexpr float backlash_distance_mm[XYZ] = BACKLASH_DISTANCE_MM,
constexpr uint8_t backlash_correction = BACKLASH_CORRECTION * 255;
#ifdef BACKLASH_SMOOTHING_MM
constexpr float backlash_smoothing_mm = BACKLASH_SMOOTHING_MM;
#endif
#endif
void Planner::add_backlash_correction_steps(const int32_t da, const int32_t db, const int32_t dc, const uint8_t dm, block_t * const block) {
static uint8_t last_direction_bits;
uint8_t changed_dir = last_direction_bits ^ dm;
// Ignore direction change if no steps are taken in that direction
if (da == 0) CBI(changed_dir, X_AXIS);
if (db == 0) CBI(changed_dir, Y_AXIS);
if (dc == 0) CBI(changed_dir, Z_AXIS);
last_direction_bits ^= changed_dir;
if (backlash_correction == 0) return;
#ifdef BACKLASH_SMOOTHING_MM
// The segment proportion is a value greater than 0.0 indicating how much residual_error
// is corrected for in this segment. The contribution is based on segment length and the
// smoothing distance. Since the computation of this proportion involves a floating point
// division, defer computation until needed.
float segment_proportion = 0;
// Residual error carried forward across multiple segments, so correction can be applied
// to segments where there is no direction change.
static int32_t residual_error[XYZ] = { 0 };
#else
// No leftover residual error from segment to segment
int32_t residual_error[XYZ] = { 0 };
// No direction change, no correction.
if (!changed_dir) return;
#endif
const float f_corr = float(backlash_correction) / 255.0f;
LOOP_XYZ(axis) {
if (backlash_distance_mm[axis]) {
const bool reversing = TEST(dm,axis);
// When an axis changes direction, add axis backlash to the residual error
if (TEST(changed_dir, axis))
residual_error[axis] += (reversing ? -f_corr : f_corr) * backlash_distance_mm[axis] * planner.settings.axis_steps_per_mm[axis];
// Decide how much of the residual error to correct in this segment
int32_t error_correction = residual_error[axis];
#ifdef BACKLASH_SMOOTHING_MM
if (error_correction && backlash_smoothing_mm != 0) {
// Take up a portion of the residual_error in this segment, but only when
// the current segment travels in the same direction as the correction
if (reversing == (error_correction < 0)) {
if (segment_proportion == 0)
segment_proportion = MIN(1.0f, block->millimeters / backlash_smoothing_mm);
error_correction = ceil(segment_proportion * error_correction);
}
else
error_correction = 0; // Don't take up any backlash in this segment, as it would subtract steps
}
#endif
// Making a correction reduces the residual error and modifies delta_mm
if (error_correction) {
block->steps[axis] += ABS(error_correction);
residual_error[axis] -= error_correction;
}
}
}
}
#endif // BACKLASH_COMPENSATION
/**
* Planner::_buffer_steps
*
@ -1919,7 +1835,7 @@ bool Planner::_populate_block(block_t * const block, bool split_move,
* should *never* remove steps!
*/
#if ENABLED(BACKLASH_COMPENSATION)
add_backlash_correction_steps(da, db, dc, dm, block);
backlash.add_correction_steps(da, db, dc, dm, block);
#endif
}

View File

@ -338,10 +338,6 @@ class Planner {
volatile static uint32_t block_buffer_runtime_us; //Theoretical block buffer runtime in µs
#endif
#if ENABLED(BACKLASH_COMPENSATION)
static void add_backlash_correction_steps(const int32_t da, const int32_t db, const int32_t dc, const uint8_t dm, block_t * const block);
#endif
public:
/**

View File

@ -29,6 +29,10 @@ Stopwatch print_job_timer; // Global Print Job Timer instance
#else // PRINTCOUNTER
#if ENABLED(EXTENSIBLE_UI)
#include "../lcd/extensible_ui/ui_api.h"
#endif
#include "printcounter.h"
#include "../Marlin.h"
#include "../HAL/shared/persistent_store_api.h"
@ -169,6 +173,10 @@ void PrintCounter::saveStats() {
persistentStore.access_start();
persistentStore.write_data(address + sizeof(uint8_t), (uint8_t*)&data, sizeof(printStatistics));
persistentStore.access_finish();
#if ENABLED(EXTENSIBLE_UI)
ExtUI::onConfigurationStoreWritten(true);
#endif
}
#if HAS_SERVICE_INTERVALS

View File

@ -54,6 +54,10 @@
#include "planner.h"
#endif
#if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
#include "../feature/backlash.h"
#endif
float zprobe_zoffset; // Initialized by settings.load()
#if ENABLED(BLTOUCH)
@ -463,30 +467,6 @@ bool set_probe_deployed(const bool deploy) {
}
#endif
#if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
#if USES_Z_MIN_PROBE_ENDSTOP
#define TEST_PROBE_PIN (READ(Z_MIN_PROBE_PIN) != Z_MIN_PROBE_ENDSTOP_INVERTING)
#else
#define TEST_PROBE_PIN (READ(Z_MIN_PIN) != Z_MIN_ENDSTOP_INVERTING)
#endif
extern float backlash_measured_mm[];
extern uint8_t backlash_measured_num[];
/* Measure Z backlash by raising nozzle in increments until probe deactivates */
static void measure_backlash_with_probe() {
if (backlash_measured_num[Z_AXIS] == 255) return;
float start_height = current_position[Z_AXIS];
while (current_position[Z_AXIS] < (start_height + BACKLASH_MEASUREMENT_LIMIT) && TEST_PROBE_PIN)
do_blocking_move_to_z(current_position[Z_AXIS] + BACKLASH_MEASUREMENT_RESOLUTION, MMM_TO_MMS(BACKLASH_MEASUREMENT_FEEDRATE));
// The backlash from all probe points is averaged, so count the number of measurements
backlash_measured_mm[Z_AXIS] += current_position[Z_AXIS] - start_height;
backlash_measured_num[Z_AXIS]++;
}
#endif
/**
* @brief Used by run_z_probe to do a single Z probe move.
*
@ -643,7 +623,7 @@ static float run_z_probe() {
}
#if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
measure_backlash_with_probe();
backlash.measure_with_probe();
#endif
#if MULTIPLE_PROBING > 2

View File

@ -113,7 +113,7 @@ Stepper stepper; // Singleton
#include "../feature/mixing.h"
#endif
#if FILAMENT_RUNOUT_DISTANCE_MM > 0
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
#include "../feature/runout.h"
#endif
@ -1537,7 +1537,7 @@ uint32_t Stepper::stepper_block_phase_isr() {
// If current block is finished, reset pointer
if (step_events_completed >= step_event_count) {
#if FILAMENT_RUNOUT_DISTANCE_MM > 0
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
runout.block_completed(current_block);
#endif
axis_did_move = 0;