Skip to content

Commit

Permalink
Use uint8 instead of float for backlash correction
Browse files Browse the repository at this point in the history
- Saves 3 bytes of SRAM.
- Consistent with fan speed.
  • Loading branch information
marcio-ao committed Mar 29, 2019
1 parent 565eb23 commit e725331
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
13 changes: 7 additions & 6 deletions Marlin/src/gcode/calibrate/G425.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
#define HAS_Y_CENTER BOTH(CALIBRATION_MEASURE_FRONT, CALIBRATION_MEASURE_BACK)

#if ENABLED(BACKLASH_GCODE)
extern float backlash_distance_mm[], backlash_correction, backlash_smoothing_mm;
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 };
Expand Down Expand Up @@ -446,7 +447,7 @@ inline void calibrate_backlash(measurements_t &m, const float uncertainty) {

{
// New scope for TEMPORARY_BACKLASH_CORRECTION
TEMPORARY_BACKLASH_CORRECTION(0.0f);
TEMPORARY_BACKLASH_CORRECTION(all_off);
TEMPORARY_BACKLASH_SMOOTHING(0.0f);

probe_sides(m, uncertainty);
Expand Down Expand Up @@ -478,7 +479,7 @@ inline void calibrate_backlash(measurements_t &m, const float uncertainty) {

{
// New scope for TEMPORARY_BACKLASH_CORRECTION
TEMPORARY_BACKLASH_CORRECTION(1.0f);
TEMPORARY_BACKLASH_CORRECTION(all_on);
TEMPORARY_BACKLASH_SMOOTHING(0.0f);
move_to(
X_AXIS, current_position[X_AXIS] + 3,
Expand Down Expand Up @@ -513,7 +514,7 @@ inline void update_measurements(measurements_t &m, const AxisEnum axis) {
* - Call calibrate_backlash() beforehand for best accuracy
*/
inline void calibrate_toolhead(measurements_t &m, const float uncertainty, const uint8_t extruder) {
TEMPORARY_BACKLASH_CORRECTION(1.0f);
TEMPORARY_BACKLASH_CORRECTION(all_on);
TEMPORARY_BACKLASH_SMOOTHING(0.0f);

#if HOTENDS > 1
Expand Down Expand Up @@ -556,7 +557,7 @@ inline void calibrate_toolhead(measurements_t &m, const float uncertainty, const
* uncertainty in - How far away from the object to begin probing
*/
inline void calibrate_all_toolheads(measurements_t &m, const float uncertainty) {
TEMPORARY_BACKLASH_CORRECTION(1.0f);
TEMPORARY_BACKLASH_CORRECTION(all_on);
TEMPORARY_BACKLASH_SMOOTHING(0.0f);

HOTEND_LOOP() calibrate_toolhead(m, uncertainty, e);
Expand Down Expand Up @@ -588,7 +589,7 @@ inline void calibrate_all() {
reset_hotend_offsets();
#endif

TEMPORARY_BACKLASH_CORRECTION(1.0f);
TEMPORARY_BACKLASH_CORRECTION(all_on);
TEMPORARY_BACKLASH_SMOOTHING(0.0f);

// Do a fast and rough calibration of the toolheads
Expand Down
8 changes: 4 additions & 4 deletions Marlin/src/gcode/calibrate/M425.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

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

float backlash_distance_mm[XYZ] = BACKLASH_DISTANCE_MM,
backlash_correction = BACKLASH_CORRECTION;
float backlash_distance_mm[XYZ] = BACKLASH_DISTANCE_MM;
uint8_t backlash_correction = BACKLASH_CORRECTION * all_on;

#ifdef BACKLASH_SMOOTHING_MM
float backlash_smoothing_mm = BACKLASH_SMOOTHING_MM;
Expand Down Expand Up @@ -74,7 +74,7 @@ void GcodeSuite::M425() {

if (parser.seen('F')) {
planner.synchronize();
backlash_correction = MAX(0, MIN(1.0, parser.value_linear_units()));
backlash_correction = MAX(0, MIN(1.0, parser.value_float())) * all_on;
noArgs = false;
}

Expand All @@ -90,7 +90,7 @@ void GcodeSuite::M425() {
SERIAL_ECHOPGM("Backlash correction is ");
if (!backlash_correction) SERIAL_ECHOPGM("in");
SERIAL_ECHOLNPGM("active:");
SERIAL_ECHOPAIR(" Correction Amount/Fade-out: F", backlash_correction);
SERIAL_ECHOPAIR(" Correction Amount/Fade-out: F", float(ui8_to_percent(backlash_correction))/100);
SERIAL_ECHOLNPGM(" (F1.0 = full, F0.0 = none)");
SERIAL_ECHOPGM(" Backlash Distance (mm): ");
LOOP_XYZ(a) {
Expand Down
7 changes: 4 additions & 3 deletions Marlin/src/lcd/extensible_ui/ui_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
#include "ui_api.h"

#if ENABLED(BACKLASH_GCODE)
extern float backlash_distance_mm[XYZ], backlash_correction;
extern float backlash_distance_mm[XYZ];
extern uint8_t backlash_correction;
#ifdef BACKLASH_SMOOTHING_MM
extern float backlash_smoothing_mm;
#endif
Expand Down Expand Up @@ -686,8 +687,8 @@ namespace ExtUI {
void setAxisBacklash_mm(const float value, const axis_t axis)
{ backlash_distance_mm[axis] = clamp(value,0,5); }

float getBacklashCorrection_percent() { return backlash_correction * 100; }
void setBacklashCorrection_percent(const float value) { backlash_correction = clamp(value, 0, 100) / 100.0f; }
float getBacklashCorrection_percent() { return ui8_to_percent(backlash_correction); }
void setBacklashCorrection_percent(const float value) { backlash_correction = map(clamp(value, 0, 100), 0, 100, 0, 255); }

#ifdef BACKLASH_SMOOTHING_MM
float getBacklashSmoothing_mm() { return backlash_smoothing_mm; }
Expand Down
9 changes: 6 additions & 3 deletions Marlin/src/module/planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1572,13 +1572,14 @@ void Planner::synchronize() {
*/
#if ENABLED(BACKLASH_COMPENSATION)
#if ENABLED(BACKLASH_GCODE)
extern float backlash_distance_mm[], backlash_correction;
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,
backlash_correction = BACKLASH_CORRECTION;
constexpr uint8_t backlash_correction = BACKLASH_CORRECTION * 255;
#ifdef BACKLASH_SMOOTHING_MM
constexpr float backlash_smoothing_mm = BACKLASH_SMOOTHING_MM;
#endif
Expand Down Expand Up @@ -1612,13 +1613,15 @@ void Planner::synchronize() {
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] += backlash_correction * (reversing ? -1.0f : 1.0f) * backlash_distance_mm[axis] * planner.settings.axis_steps_per_mm[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];
Expand Down

0 comments on commit e725331

Please sign in to comment.