Skip to content

Commit

Permalink
Backlash cleanup (#13659)
Browse files Browse the repository at this point in the history
…And save backlash, fil. sensor, ExtUI userdata to EEPROM.
  • Loading branch information
marcio-ao authored and thinkyhead committed May 4, 2019
1 parent 0181e57 commit 15357af
Show file tree
Hide file tree
Showing 22 changed files with 648 additions and 296 deletions.
16 changes: 9 additions & 7 deletions Marlin/src/Marlin.cpp
Expand Up @@ -939,6 +939,15 @@ void setup() {

queue_setup();

// UI must be initialized before EEPROM
// (because EEPROM code calls the UI).
ui.init();
ui.reset_status();

#if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
ui.show_bootscreen();
#endif

#if ENABLED(SDIO_SUPPORT) && SD_DETECT_PIN == -1
// Auto-mount the SD for EEPROM.dat emulation
if (!card.isDetected()) card.initsd();
Expand Down Expand Up @@ -1044,13 +1053,6 @@ void setup() {
fanmux_init();
#endif

ui.init();
ui.reset_status();

#if HAS_SPI_LCD && ENABLED(SHOW_BOOTSCREEN)
ui.show_bootscreen();
#endif

#if ENABLED(MIXING_EXTRUDER)
mixer.init();
#endif
Expand Down
139 changes: 139 additions & 0 deletions Marlin/src/feature/backlash.cpp
@@ -0,0 +1,139 @@
/**
* Marlin 3D Printer Firmware
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

#include "../Marlin.h"

#if ENABLED(BACKLASH_COMPENSATION)

#include "backlash.h"
#include "../module/planner.h"

#if ENABLED(BACKLASH_GCODE)
uint8_t Backlash::correction = (BACKLASH_CORRECTION) * 0xFF;
#ifdef BACKLASH_DISTANCE_MM
float Backlash::distance_mm[XYZ] = BACKLASH_DISTANCE_MM;
#endif
#ifdef BACKLASH_SMOOTHING_MM
float Backlash::smoothing_mm = BACKLASH_SMOOTHING_MM;
#endif
#endif

#if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
float Backlash::measured_mm[XYZ] = { 0 };
uint8_t Backlash::measured_count[XYZ] = { 0 };
#endif

Backlash backlash;

/**
* To minimize seams in the printed part, backlash correction only adds
* steps to the current segment (instead of creating a new segment, which
* causes discontinuities and print artifacts).
*
* With a non-zero BACKLASH_SMOOTHING_MM value the backlash correction is
* spread over multiple segments, smoothing out artifacts even more.
*/

void Backlash::add_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 (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(correction) / 255.0f;

LOOP_XYZ(axis) {
if (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) * 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 && 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 / 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;
}
}
}
}

#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

// Measure Z backlash by raising nozzle in increments until probe deactivates
void Backlash::measure_with_probe() {
if (measured_count[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
measured_mm[Z_AXIS] += current_position[Z_AXIS] - start_height;
measured_count[Z_AXIS]++;
}
#endif

#endif // BACKLASH_COMPENSATION
88 changes: 88 additions & 0 deletions Marlin/src/feature/backlash.h
@@ -0,0 +1,88 @@
/**
* Marlin 3D Printer Firmware
* Copyright (C) 2019 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#pragma once

#include "../inc/MarlinConfigPre.h"
#include "../module/planner.h"

class Backlash {
public:
#if ENABLED(BACKLASH_GCODE)
static uint8_t correction;
#ifdef BACKLASH_DISTANCE_MM
static float distance_mm[XYZ];
#endif
#ifdef BACKLASH_SMOOTHING_MM
static float smoothing_mm;
#endif
static inline void set_correction(const float &v) { correction = MAX(0, MIN(1.0, v)) * all_on; }
static inline float get_correction() { return float(ui8_to_percent(correction)) / 100.0f; }
#elif ENABLED(BACKLASH_COMPENSATION)
static constexpr uint8_t correction = (BACKLASH_CORRECTION) * 0xFF;
#ifdef BACKLASH_DISTANCE_MM
static constexpr float distance_mm[XYZ] = BACKLASH_DISTANCE_MM;
#endif
#ifdef BACKLASH_SMOOTHING_MM
static constexpr float smoothing_mm = BACKLASH_SMOOTHING_MM;
#endif
static inline void set_correction(float) { }
static inline float get_correction() { return float(ui8_to_percent(correction)) / 100.0f; }
#else
static constexpr uint8_t correction = 0;
static inline void set_correction(float) { }
static inline float get_correction() { return 0; }
#endif

#if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
private:
static float measured_mm[XYZ];
static uint8_t measured_count[XYZ];
public:
static void measure_with_probe();
#endif

static inline float get_measurement(const uint8_t e) {
// Return the measurement averaged over all readings
return (
#if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
measured_count[e] > 0 ? measured_mm[e] / measured_count[e] :
#endif
0
);
}

static inline bool has_measurement(const uint8_t e) {
return (false
#if ENABLED(MEASURE_BACKLASH_WHEN_PROBING)
|| (measured_count[e] > 0)
#endif
);
}

static inline bool has_any_measurement() {
return has_measurement(X_AXIS) || has_measurement(Y_AXIS) || has_measurement(Z_AXIS);
}

void add_correction_steps(const int32_t &da, const int32_t &db, const int32_t &dc, const uint8_t dm, block_t * const block);
};

extern Backlash backlash;
2 changes: 1 addition & 1 deletion Marlin/src/feature/runout.cpp
Expand Up @@ -51,7 +51,7 @@ void FilamentSensorBase::filament_present(const uint8_t extruder) {
uint8_t FilamentSensorEncoder::motion_detected;
#endif

#if FILAMENT_RUNOUT_DISTANCE_MM > 0
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
float RunoutResponseDelayed::runout_distance_mm = FILAMENT_RUNOUT_DISTANCE_MM;
volatile float RunoutResponseDelayed::runout_mm_countdown[EXTRUDERS];
#else
Expand Down
18 changes: 12 additions & 6 deletions Marlin/src/feature/runout.h
Expand Up @@ -78,6 +78,11 @@ class TFilamentMonitor : public FilamentMonitorBase {
response.filament_present(extruder);
}

#ifdef FILAMENT_RUNOUT_DISTANCE_MM
static inline float& runout_distance() { return response.runout_distance_mm; }
static inline void set_runout_distance(const float &mm) { response.runout_distance_mm = mm; }
#endif

// Handle a block completion. RunoutResponseDelayed uses this to
// add up the length of filament moved while the filament is out.
static inline void block_completed(const block_t* const b) {
Expand All @@ -90,13 +95,13 @@ class TFilamentMonitor : public FilamentMonitorBase {
// Give the response a chance to update its counter.
static inline void run() {
if (enabled && !filament_ran_out && (IS_SD_PRINTING() || print_job_timer.isRunning() || did_pause_print)) {
#if FILAMENT_RUNOUT_DISTANCE_MM > 0
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
cli(); // Prevent RunoutResponseDelayed::block_completed from accumulating here
#endif
response.run();
sensor.run();
const bool ran_out = response.has_run_out();
#if FILAMENT_RUNOUT_DISTANCE_MM > 0
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
sei();
#endif
if (ran_out) {
Expand Down Expand Up @@ -272,7 +277,7 @@ class FilamentSensorBase {

/********************************* RESPONSE TYPE *********************************/

#if FILAMENT_RUNOUT_DISTANCE_MM > 0
#ifdef FILAMENT_RUNOUT_DISTANCE_MM

// RunoutResponseDelayed triggers a runout event only if the length
// of filament specified by FILAMENT_RUNOUT_DISTANCE_MM has been fed
Expand Down Expand Up @@ -347,11 +352,12 @@ class FilamentSensorBase {
/********************************* TEMPLATE SPECIALIZATION *********************************/

typedef TFilamentMonitor<
#if FILAMENT_RUNOUT_DISTANCE_MM > 0
#ifdef FILAMENT_RUNOUT_DISTANCE_MM
RunoutResponseDelayed,
#if ENABLED(FILAMENT_MOTION_SENSOR)
RunoutResponseDelayed, FilamentSensorEncoder
FilamentSensorEncoder
#else
RunoutResponseDelayed, FilamentSensorSwitch
FilamentSensorSwitch
#endif
#else
RunoutResponseDebounced, FilamentSensorSwitch
Expand Down
24 changes: 10 additions & 14 deletions Marlin/src/gcode/calibrate/G425.cpp
Expand Up @@ -31,6 +31,7 @@
#include "../../module/tool_change.h"
#include "../../module/endstops.h"
#include "../../feature/bedlevel/bedlevel.h"
#include "../../feature/backlash.h"


/**
Expand All @@ -55,11 +56,6 @@
#define HAS_X_CENTER BOTH(CALIBRATION_MEASURE_LEFT, CALIBRATION_MEASURE_RIGHT)
#define HAS_Y_CENTER BOTH(CALIBRATION_MEASURE_FRONT, CALIBRATION_MEASURE_BACK)

#if ENABLED(BACKLASH_GCODE)
extern float backlash_distance_mm[], backlash_smoothing_mm;
extern uint8_t backlash_correction;
#endif

enum side_t : uint8_t { TOP, RIGHT, FRONT, LEFT, BACK, NUM_SIDES };

struct measurements_t {
Expand All @@ -79,13 +75,13 @@ struct measurements_t {
#define TEMPORARY_SOFT_ENDSTOP_STATE(enable) REMEMBER(tes, soft_endstops_enabled, enable);

#if ENABLED(BACKLASH_GCODE)
#define TEMPORARY_BACKLASH_CORRECTION(value) REMEMBER(tbst, backlash_correction, value)
#define TEMPORARY_BACKLASH_CORRECTION(value) REMEMBER(tbst, backlash.correction, value)
#else
#define TEMPORARY_BACKLASH_CORRECTION(value)
#endif

#if ENABLED(BACKLASH_GCODE) && defined(BACKLASH_SMOOTHING_MM)
#define TEMPORARY_BACKLASH_SMOOTHING(value) REMEMBER(tbsm, backlash_smoothing_mm, value)
#define TEMPORARY_BACKLASH_SMOOTHING(value) REMEMBER(tbsm, backlash.smoothing_mm, value)
#else
#define TEMPORARY_BACKLASH_SMOOTHING(value)
#endif
Expand Down Expand Up @@ -454,22 +450,22 @@ inline void calibrate_backlash(measurements_t &m, const float uncertainty) {

#if ENABLED(BACKLASH_GCODE)
#if HAS_X_CENTER
backlash_distance_mm[X_AXIS] = (m.backlash[LEFT] + m.backlash[RIGHT]) / 2;
backlash.distance_mm[X_AXIS] = (m.backlash[LEFT] + m.backlash[RIGHT]) / 2;
#elif ENABLED(CALIBRATION_MEASURE_LEFT)
backlash_distance_mm[X_AXIS] = m.backlash[LEFT];
backlash.distance_mm[X_AXIS] = m.backlash[LEFT];
#elif ENABLED(CALIBRATION_MEASURE_RIGHT)
backlash_distance_mm[X_AXIS] = m.backlash[RIGHT];
backlash.distance_mm[X_AXIS] = m.backlash[RIGHT];
#endif

#if HAS_Y_CENTER
backlash_distance_mm[Y_AXIS] = (m.backlash[FRONT] + m.backlash[BACK]) / 2;
backlash.distance_mm[Y_AXIS] = (m.backlash[FRONT] + m.backlash[BACK]) / 2;
#elif ENABLED(CALIBRATION_MEASURE_FRONT)
backlash_distance_mm[Y_AXIS] = m.backlash[FRONT];
backlash.distance_mm[Y_AXIS] = m.backlash[FRONT];
#elif ENABLED(CALIBRATION_MEASURE_BACK)
backlash_distance_mm[Y_AXIS] = m.backlash[BACK];
backlash.distance_mm[Y_AXIS] = m.backlash[BACK];
#endif

backlash_distance_mm[Z_AXIS] = m.backlash[TOP];
backlash.distance_mm[Z_AXIS] = m.backlash[TOP];
#endif
}

Expand Down

0 comments on commit 15357af

Please sign in to comment.