From 24e6dbcf48af6e778cbb8343fe0b2e84bb0df874 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Wed, 22 Jul 2020 17:07:44 -0400 Subject: [PATCH 01/42] Add New G34 mode for current adjustable drivers TODO : Sanity checks, Finish DAC mode, Test on Rambo --- Marlin/Configuration_adv.h | 19 ++++ Marlin/src/gcode/calibrate/G34_M422.cpp | 145 +++++++++++++++++++++++- Marlin/src/lcd/language/language_en.h | 1 + 3 files changed, 161 insertions(+), 4 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 2712312747..46d43b5ff7 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -3338,6 +3338,25 @@ #define JOY_Z_LIMITS { 4800, 8080-100, 8080+100, 11550 } #endif +/** + * Modern replacement for the Prusa TMC_Z_CALIBRATION + * Adds capability to work with any adjustable current drivers + * Implements as G34 as M915 is deprecated + */ + +//#define MECHANICAL_GANTRY_CALIBRATION +#if ENABLED(MECHANICAL_GANTRY_CALIBRATION) + #define GANTRY_CALIBRATION_CURRENT 600 // Default calibration current in ma + #define GANTRY_CALIBRATION_EXTRA_HEIGHT 15 // Extra distance in mm past Z_###_POS to move + #define GANTRY_CALIBRATION_DIRECTION 1 // Set to 1 for Max or 0 for min + #define GANTRY_CALIBRATION_FEEDRATE 500 // Feedrate for correction move + + //#define GANTRY_CALIBRATION_SAFE_POSITION {X_CENTER, Y_CENTER} // Safe position for nozzle + //#define GANTRY_CALIBRATION_XY_PARK_FEEDRATE 3000 // XY Park Feedrate - MMM + //#define GANTRY_CALIBRATION_COMMANDS_PRE "" + //#define GANTRY_CALIBRATION_COMMANDS_POST "G28" +#endif + /** * MAX7219 Debug Matrix * diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 1fd0754ec7..68b5b1b7b0 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -169,6 +169,7 @@ void GcodeSuite::G34() { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions."); SERIAL_ECHOLNPAIR("\nITERATION: ", int(iteration + 1)); + ui.set_status("\nITERATION: ", int(iteration + 1)); // Initialize minimum value z_measured_min = 100000.0f; @@ -190,7 +191,8 @@ void GcodeSuite::G34() { // current_position.z has been manually altered in the "dirty trick" above. const float z_probed_height = probe.probe_at_point(z_stepper_align.xy[iprobe], raise_after, 0, true, false); if (isnan(z_probed_height)) { - SERIAL_ECHOLNPGM("Probing failed."); + SERIAL_ECHOLNPGM(MSG_LCD_PROBING_FAILED); + ui.set_status_P(MSG_LCD_PROBING_FAILED) err_break = true; break; } @@ -249,6 +251,13 @@ void GcodeSuite::G34() { , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) #endif ); + ui.set_status("\n" + "DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1]) + #if NUM_Z_STEPPER_DRIVERS == 3 + , " Z2-Z3=", ABS(z_measured[1] - z_measured[2]) + , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) + #endif + ); #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) // Check if the applied corrections go in the correct direction. @@ -267,7 +276,8 @@ void GcodeSuite::G34() { // If it's getting worse, stop and throw an error if (last_z_align_level_indicator < z_align_level_indicator * 0.7f) { - SERIAL_ECHOLNPGM("Decreasing accuracy detected."); + SERIAL_ECHOLNPGM(DECREASING_ACCURACY); + ui.set_status_P(DECREASING_ACCURACY); err_break = true; break; } @@ -291,7 +301,8 @@ void GcodeSuite::G34() { // Check for less accuracy compared to last move if (last_z_align_move[zstepper] < z_align_abs * 0.7f) { - SERIAL_ECHOLNPGM("Decreasing accuracy detected."); + SERIAL_ECHOLNPGM(DECREASING_ACCURACY); + ui.set_status_P(DECREASING_ACCURACY); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " last_z_align_move = ", last_z_align_move[zstepper]); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " z_align_abs = ", z_align_abs); adjustment_reverse = !adjustment_reverse; @@ -329,7 +340,11 @@ void GcodeSuite::G34() { if (err_break) break; - if (success_break) { SERIAL_ECHOLNPGM("Target accuracy achieved."); break; } + if (success_break) { + SERIAL_ECHOLNPGM("Target accuracy achieved."); + ui.set_status_P(PGM_P("Target accuracy achieved.")); + break; + } } // for (iteration) @@ -469,4 +484,126 @@ void GcodeSuite::M422() { pos_dest[position_index] = pos; } +#elif ENABLED(MECHANICAL_GANTRY_CALIBRATION) + void GcodeSuite::G34() { + + if (axis_unhomed_error()) return; + + TEMPORARY_SOFT_ENDSTOP_STATE(false); + TEMPORARY_BED_LEVELING_STATE(false); + + #ifdef GANTRY_CALIBRATION_COMMANDS_PRE + gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_PRE)); + #endif + + // Store current motor settings, then apply reduced value + #if DAC_STEPPER_CURRENT + const float target_current = parserfloatval('S', GANTRY_CALIBRATION_CURRENT); + const float previous_current = dac_amps(Z_AXIS, target_current); + else + const int16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); + #endif + + #if HAS_DIGIPOTSS + const uint32_t previous_current = motor_current_setting[Z_AXIS]; + stepper.digipot_current(Z_AXIS, target_current); + #elif HAS_MOTOR_CURRENT_PWM + const uint32_t previous_current = motor_current_setting[Z_AXIS]; + stepper.digipot_current(1, target_current); + #if DAC_STEPPER_CURRENT + dac_current_raw(Z_AXIS, target_current); + #elif ENABLED(HAS_I2C_DIGIPOT) + previous_current = dac_amps(Z_AXIS); + digipot_i2c_set_current(Z_AXIS, target_current) + #elif HAS_TRINAMIC_CONFIG + static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; + #if AXIS_IS_TMC(Z) + previous_current_arr[0] = stepperZ.getMilliamps(); + stepperZ.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z2) + previous_current_arr[1] = stepperZ2.getMilliamps(); + stepperZ2.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z3) + previous_current_arr[2] = stepperZ3.getMilliamps(); + stepperZ3.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z4) + previous_current_arr[3] = stepperZ4.getMilliamps(); + stepperZ4.rms_current(target_current); + #endif + #endif + + // Move XY to safe position + #ifdef GANTRY_CALIBRATION_SAFE_POSITION + xy_pos_t safe_pos = GANTRY_CALIBRATION_SAFE_POSITION; + current_position[X_AXIS] = safe_pos[X_AXIS]; + current_position[Y_AXIS] = safe_pos[Y_AXIS]; + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE); + planner.synchronize(); + #endif + + const uint16_t move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT); + + // Move Z to pounce position + #if GANTRY_CALIBRATION_DIRECTION == 1 + current_position[Z_AXIS] = (Z_MAX_POS - move_distance); + #else + current_position[Z_AXIS] = (Z_MIN_POS + move_distance); + #endif + + do_blocking_move_to(current_position, MMM_TO_MMS(HOMING_FEEDRATE_Z); + planner.synchronize(); + + // Do Final Z move to adjust + #if GANTRY_CALIBRATION_DIRECTION == 1 + current_position[Z_AXIS] = (Z_MAX_POS + move_distance); + #else + current_position[Z_AXIS] = (Z_MIN_POS - move_distance); + #endif + + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE); + planner.synchronize(); + + // Back off end plate, back to normal motion range + #if GANTRY_CALIBRATION_DIRECTION == 1 + current_position[Z_AXIS] = (Z_MAX_POS - move_distance); + #else + current_position[Z_AXIS] = (Z_MIN_POS + move_distance); + #endif + + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE); + planner.synchronize(); + + // Reset current to original values + #if HAS_DIGIPOTSS + stepper.digipot_current(Z_AXIS, previous_current); + #elif HAS_MOTOR_CURRENT_PWM + stepper.digipot_current(1, previous_current); + #if DAC_STEPPER_CURRENT + dac_current_raw(Z_AXIS, previous_current); + #elif ENABLED(HAS_I2C_DIGIPOT) + digipot_i2c_set_current(Z_AXIS, previous_current) + #elif HAS_TRINAMIC_CONFIG + static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; + #if AXIS_IS_TMC(Z) + stepperZ.rms_current(previous_current_arr[0]); + #endif + #if AXIS_IS_TMC(Z2) + stepperZ2.rms_current(previous_current_arr[1]); + #endif + #if AXIS_IS_TMC(Z3) + stepperZ3.rms_current(previous_current_arr[2]); + #endif + #if AXIS_IS_TMC(Z4) + stepperZ4.rms_current(previous_current_arr[3]); + #endif + #endif + + #ifdef GANTRY_CALIBRATION_COMMANDS_POST + gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_POST)); + #endif + } + #endif // Z_STEPPER_AUTO_ALIGN diff --git a/Marlin/src/lcd/language/language_en.h b/Marlin/src/lcd/language/language_en.h index fc9e065abe..f19edc90d9 100644 --- a/Marlin/src/lcd/language/language_en.h +++ b/Marlin/src/lcd/language/language_en.h @@ -68,6 +68,7 @@ namespace Language_en { PROGMEM Language_Str MSG_AUTO_HOME_Z = _UxGT("Home Z"); PROGMEM Language_Str MSG_AUTO_Z_ALIGN = _UxGT("Auto Z-Align"); PROGMEM Language_Str MSG_ASSISTED_TRAMMING = _UxGT("Assisted Tramming"); + PROGMEM Language_Str DECREASING_ACCURACY = _UxGT("Decreasing accuracy detected."); PROGMEM Language_Str MSG_LEVEL_BED_HOMING = _UxGT("Homing XYZ"); PROGMEM Language_Str MSG_LEVEL_BED_WAITING = _UxGT("Click to Begin"); PROGMEM Language_Str MSG_LEVEL_BED_NEXT_POINT = _UxGT("Next Point"); From c67086bdba814f835b817a7924733e5bdc80d2bb Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Fri, 24 Jul 2020 16:09:19 -0400 Subject: [PATCH 02/42] Sanity Checks, gcode calls --- Marlin/src/gcode/gcode.h | 5 +++-- Marlin/src/inc/SanityCheck.h | 24 +++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Marlin/src/gcode/gcode.h b/Marlin/src/gcode/gcode.h index 2427301262..7c3206b20a 100644 --- a/Marlin/src/gcode/gcode.h +++ b/Marlin/src/gcode/gcode.h @@ -463,11 +463,12 @@ private: TERN_(DELTA_AUTO_CALIBRATION, static void G33()); - #if ENABLED(Z_STEPPER_AUTO_ALIGN) + #if ANY(Z_STEPPER_AUTO_ALIGN, MECHANICAL_GANTRY_CALIBRATION) static void G34(); - static void M422(); #endif + TERN_(Z_STEPPER_AUTO_ALIGN, static void M422()); + TERN_(ASSISTED_TRAMMING, static void G35()); TERN_(G38_PROBE_TARGET, static void G38(const int8_t subcode)); diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index 1ee082720d..f077afaeae 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -422,7 +422,7 @@ #elif defined(CHAMBER_HEATER_PIN) #error "CHAMBER_HEATER_PIN is now HEATER_CHAMBER_PIN. Please update your configuration and/or pins." #elif defined(TMC_Z_CALIBRATION) - #error "TMC_Z_CALIBRATION has been deprecated in favor of Z_STEPPER_AUTO_ALIGN. Please update your configuration." + #error "TMC_Z_CALIBRATION has been deprecated in favor of MECHANICAL_GANTRY_CALIBRATION. Please update your configuration." #elif defined(Z_MIN_PROBE_ENDSTOP) #error "Z_MIN_PROBE_ENDSTOP is no longer required. Please remove it from Configuration.h." #elif defined(DUAL_NOZZLE_DUPLICATION_MODE) @@ -2733,6 +2733,28 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #endif #endif +#if ENABLED(MECHANICAL_GANTRY_CALIBRATION) + #if NONE(DAC_STEPPER_CURRENT, HAS_DIGIPOTSS, DAC_STEPPER_CURRENT, HAS_TRINAMIC_CONFIG) + #error "It is highly reccomended to have adjustable current drivers to prevent damage. Disable this line to continue anyway." + #else + #ifndef GANTRY_CALIBRATION_CURRENT + #error "MECHANICAL_GANTRY_CALIBRATION Requires GANTRY_CALIBRATION_CURRENT to be set." + #endif + #ifndef GANTRY_CALIBRATION_EXTRA_HEIGHT + #error "MECHANICAL_GANTRY_CALIBRATION Requires GANTRY_CALIBRATION_EXTRA_HEIGHT to be set." + #endif + #ifndef GANTRY_CALIBRATION_DIRECTION + #error "MECHANICAL_GANTRY_CALIBRATION Requires GANTRY_CALIBRATION_DIRECTION to be set." + #endif + #ifndef GANTRY_CALIBRATION_FEEDRATE + #error "MECHANICAL_GANTRY_CALIBRATION Requires GANTRY_CALIBRATION_FEEDRATE to be set." + #endif + #endif + #if defined(GANTRY_CALIBRATION_SAFE_POSITION) && !defined(GANTRY_CALIBRATION_XY_PARK_FEEDRATE) + #error "GANTRY_CALIBRATION_SAFE_POSITION Requires GANTRY_CALIBRATION_XY_PARK_FEEDRATE to be set." + #endif +#endif + #if ENABLED(PRINTCOUNTER) && DISABLED(EEPROM_SETTINGS) #error "PRINTCOUNTER requires EEPROM_SETTINGS. Please update your Configuration." #endif From 2087e6fbcfa9c7d6f1618f9a12b380bcd13a491e Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Sun, 9 Aug 2020 16:43:08 -0400 Subject: [PATCH 03/42] DIGIPOTSS in motter current array with PWM, stored to eeprom, and G34 tested --- Marlin/src/gcode/calibrate/G34_M422.cpp | 143 +++++++++++++----------- Marlin/src/gcode/gcode.cpp | 2 +- Marlin/src/lcd/menu/menu_motion.cpp | 2 +- Marlin/src/module/settings.cpp | 37 +++++- Marlin/src/module/stepper.cpp | 22 ++-- Marlin/src/module/stepper.h | 8 +- 6 files changed, 130 insertions(+), 84 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 68b5b1b7b0..31555ff0bf 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -22,31 +22,32 @@ #include "../../inc/MarlinConfig.h" -#if ENABLED(Z_STEPPER_AUTO_ALIGN) - -#include "../../feature/z_stepper_align.h" - #include "../gcode.h" #include "../../module/planner.h" #include "../../module/stepper.h" #include "../../module/motion.h" #include "../../module/probe.h" - -#if HAS_MULTI_HOTEND - #include "../../module/tool_change.h" -#endif +#include "../../module/endstops.h" #if HAS_LEVELING #include "../../feature/bedlevel/bedlevel.h" #endif +#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) +#include "../../core/debug_out.h" + +#if ENABLED(Z_STEPPER_AUTO_ALIGN) + +#include "../../feature/z_stepper_align.h" + +#if HAS_MULTI_HOTEND + #include "../../module/tool_change.h" +#endif + #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) #include "../../libs/least_squares_fit.h" #endif -#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) -#include "../../core/debug_out.h" - /** * G34: Z-Stepper automatic alignment * @@ -491,59 +492,25 @@ void GcodeSuite::M422() { TEMPORARY_SOFT_ENDSTOP_STATE(false); TEMPORARY_BED_LEVELING_STATE(false); + TemporaryGlobalEndstopsState unlock_z(false); #ifdef GANTRY_CALIBRATION_COMMANDS_PRE gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_PRE)); #endif - // Store current motor settings, then apply reduced value - #if DAC_STEPPER_CURRENT - const float target_current = parserfloatval('S', GANTRY_CALIBRATION_CURRENT); - const float previous_current = dac_amps(Z_AXIS, target_current); - else - const int16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); - #endif - - #if HAS_DIGIPOTSS - const uint32_t previous_current = motor_current_setting[Z_AXIS]; - stepper.digipot_current(Z_AXIS, target_current); - #elif HAS_MOTOR_CURRENT_PWM - const uint32_t previous_current = motor_current_setting[Z_AXIS]; - stepper.digipot_current(1, target_current); - #if DAC_STEPPER_CURRENT - dac_current_raw(Z_AXIS, target_current); - #elif ENABLED(HAS_I2C_DIGIPOT) - previous_current = dac_amps(Z_AXIS); - digipot_i2c_set_current(Z_AXIS, target_current) - #elif HAS_TRINAMIC_CONFIG - static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; - #if AXIS_IS_TMC(Z) - previous_current_arr[0] = stepperZ.getMilliamps(); - stepperZ.rms_current(target_current); - #endif - #if AXIS_IS_TMC(Z2) - previous_current_arr[1] = stepperZ2.getMilliamps(); - stepperZ2.rms_current(target_current); - #endif - #if AXIS_IS_TMC(Z3) - previous_current_arr[2] = stepperZ3.getMilliamps(); - stepperZ3.rms_current(target_current); - #endif - #if AXIS_IS_TMC(Z4) - previous_current_arr[3] = stepperZ4.getMilliamps(); - stepperZ4.rms_current(target_current); - #endif - #endif + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Sub Commands Processed"); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Parking XY"); // Move XY to safe position #ifdef GANTRY_CALIBRATION_SAFE_POSITION xy_pos_t safe_pos = GANTRY_CALIBRATION_SAFE_POSITION; current_position[X_AXIS] = safe_pos[X_AXIS]; current_position[Y_AXIS] = safe_pos[Y_AXIS]; - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE); + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE)); planner.synchronize(); #endif + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Setting Z Pounce"); const uint16_t move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT); // Move Z to pounce position @@ -553,9 +520,49 @@ void GcodeSuite::M422() { current_position[Z_AXIS] = (Z_MIN_POS + move_distance); #endif - do_blocking_move_to(current_position, MMM_TO_MMS(HOMING_FEEDRATE_Z); + do_blocking_move_to(current_position, MMM_TO_MMS(HOMING_FEEDRATE_Z)); planner.synchronize(); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Reducing Current"); + // Store current motor settings, then apply reduced value + #if HAS_DIGIPOTSS + const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); + const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS]; + stepper.digipot_current(Z_AXIS, target_current); + #elif HAS_MOTOR_CURRENT_PWM + const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); + const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS]; + stepper.digipot_current(1, target_current); + #elif DAC_STEPPER_CURRENT + const float target_current = parser.floatval('S', GANTRY_CALIBRATION_CURRENT); + const float previous_current = dac_amps(Z_AXIS, target_current); + dac_current_raw(Z_AXIS, target_current); + #elif ENABLED(HAS_I2C_DIGIPOT) + const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); + previous_current = dac_amps(Z_AXIS); + digipot_i2c_set_current(Z_AXIS, target_current) + #elif HAS_TRINAMIC_CONFIG + const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); + static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; + #if AXIS_IS_TMC(Z) + previous_current_arr[0] = stepperZ.getMilliamps(); + stepperZ.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z2) + previous_current_arr[1] = stepperZ2.getMilliamps(); + stepperZ2.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z3) + previous_current_arr[2] = stepperZ3.getMilliamps(); + stepperZ3.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z4) + previous_current_arr[3] = stepperZ4.getMilliamps(); + stepperZ4.rms_current(target_current); + #endif + #endif + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Final Z Move"); // Do Final Z move to adjust #if GANTRY_CALIBRATION_DIRECTION == 1 current_position[Z_AXIS] = (Z_MAX_POS + move_distance); @@ -563,9 +570,11 @@ void GcodeSuite::M422() { current_position[Z_AXIS] = (Z_MIN_POS - move_distance); #endif - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE); + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); planner.synchronize(); + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Z Backoff"); // Back off end plate, back to normal motion range #if GANTRY_CALIBRATION_DIRECTION == 1 current_position[Z_AXIS] = (Z_MAX_POS - move_distance); @@ -573,34 +582,38 @@ void GcodeSuite::M422() { current_position[Z_AXIS] = (Z_MIN_POS + move_distance); #endif - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE); + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); planner.synchronize(); + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Restore Current"); // Reset current to original values #if HAS_DIGIPOTSS stepper.digipot_current(Z_AXIS, previous_current); #elif HAS_MOTOR_CURRENT_PWM stepper.digipot_current(1, previous_current); - #if DAC_STEPPER_CURRENT + #elif DAC_STEPPER_CURRENT dac_current_raw(Z_AXIS, previous_current); #elif ENABLED(HAS_I2C_DIGIPOT) digipot_i2c_set_current(Z_AXIS, previous_current) #elif HAS_TRINAMIC_CONFIG static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; #if AXIS_IS_TMC(Z) - stepperZ.rms_current(previous_current_arr[0]); - #endif - #if AXIS_IS_TMC(Z2) - stepperZ2.rms_current(previous_current_arr[1]); - #endif - #if AXIS_IS_TMC(Z3) - stepperZ3.rms_current(previous_current_arr[2]); - #endif - #if AXIS_IS_TMC(Z4) - stepperZ4.rms_current(previous_current_arr[3]); - #endif + stepperZ.rms_current(previous_current_arr[0]); + #endif + #if AXIS_IS_TMC(Z2) + stepperZ2.rms_current(previous_current_arr[1]); + #endif + #if AXIS_IS_TMC(Z3) + stepperZ3.rms_current(previous_current_arr[2]); + #endif + #if AXIS_IS_TMC(Z4) + stepperZ4.rms_current(previous_current_arr[3]); + #endif #endif + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Running Post Commands"); #ifdef GANTRY_CALIBRATION_COMMANDS_POST gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_POST)); #endif diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index c1b6deeadd..25be04dd23 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -335,7 +335,7 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) { case 33: G33(); break; // G33: Delta Auto-Calibration #endif - #if ENABLED(Z_STEPPER_AUTO_ALIGN) + #if ANY(Z_STEPPER_AUTO_ALIGN, MECHANICAL_GANTRY_CALIBRATION) case 34: G34(); break; // G34: Z Stepper automatic alignment using probe #endif diff --git a/Marlin/src/lcd/menu/menu_motion.cpp b/Marlin/src/lcd/menu/menu_motion.cpp index 77ab643cd6..c64e337d6b 100644 --- a/Marlin/src/lcd/menu/menu_motion.cpp +++ b/Marlin/src/lcd/menu/menu_motion.cpp @@ -341,7 +341,7 @@ void menu_motion() { // // Auto Z-Align // - #if ENABLED(Z_STEPPER_AUTO_ALIGN) + #if ANY(Z_STEPPER_AUTO_ALIGN, MECHANICAL_GANTRY_CALIBRATION) GCODES_ITEM(MSG_AUTO_Z_ALIGN, PSTR("G34")); #endif diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index 2c679068b9..f610dbe62b 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -37,7 +37,7 @@ */ // Change EEPROM version if the structure changes -#define EEPROM_VERSION "V81" +#define EEPROM_VERSION "V82" #define EEPROM_OFFSET 100 // Check the integrity of data offsets. @@ -366,7 +366,11 @@ typedef struct SettingsDataStruct { // // HAS_MOTOR_CURRENT_PWM // - uint32_t motor_current_setting[3]; // M907 X Z E + #if HAS_DIGIPOTSS + uint32_t motor_current_setting[COUNT(stepper.motor_current_setting)]; + #else + uint32_t motor_current_setting[3]; // M907 X Z E + #endif // // CNC_COORDINATE_SYSTEMS @@ -1278,7 +1282,7 @@ void MarlinSettings::postprocess() { { _FIELD_TEST(motor_current_setting); - #if HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS EEPROM_WRITE(stepper.motor_current_setting); #else const uint32_t no_current[3] = { 0 }; @@ -2110,13 +2114,19 @@ void MarlinSettings::postprocess() { // Motor Current PWM // { - uint32_t motor_current_setting[3]; + SERIAL_ECHOLN("DIGIPOTS Loading"); + #if HAS_DIGIPOTSS + uint32_t motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; + #else + uint32_t motor_current_setting[3]; // M907 X Z E + #endif _FIELD_TEST(motor_current_setting); EEPROM_READ(motor_current_setting); - #if HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS if (!validating) COPY(stepper.motor_current_setting, motor_current_setting); #endif + SERIAL_ECHOLN("DIGIPOTS Loaded"); } // @@ -2796,6 +2806,16 @@ void MarlinSettings::reset() { stepper.digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q])); #endif + // + // DIGIPOTS + // + SERIAL_ECHOLN("Writing Digipot"); + #if HAS_DIGIPOTSS + static constexpr uint32_t tmp_motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; + LOOP_L_N(q, COUNT(tmp_motor_current_setting)) + stepper.digipot_current(q, tmp_motor_current_setting[q]); + #endif +SERIAL_ECHOLN("Digipot Written"); // // CNC Coordinate System // @@ -3703,6 +3723,13 @@ void MarlinSettings::reset() { , SP_Z_STR, stepper.motor_current_setting[1] , SP_E_STR, stepper.motor_current_setting[2] ); + #elif HASDIGIPOTSS + CONFIG_ECHO_HEADING("Stepper motor currents:"); + CONFIG_ECHO_START(); + LOOP_L_N(q, COUNT(stepper.motor_current_setting)) { + SERIAL_ECHOPAIR_P( "M907 ", axis_codes[q]); + SERIAL_ECHOLN_P(stepper.motor_current_setting[q]); + } #endif /** diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index fab1a61548..0e59549b81 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -83,7 +83,7 @@ Stepper stepper; // Singleton #define BABYSTEPPING_EXTRA_DIR_WAIT -#if HAS_MOTOR_CURRENT_PWM +#if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS bool Stepper::initialized; // = false #endif @@ -144,6 +144,9 @@ Stepper stepper; // Singleton #if HAS_MOTOR_CURRENT_PWM uint32_t Stepper::motor_current_setting[3]; // Initialized by settings.load() +#elif HAS_DIGIPOTSS + constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT; + uint32_t Stepper::motor_current_setting[COUNT(digipot_count)]; // Initialized by settings.load() #endif // private: @@ -2591,7 +2594,7 @@ void Stepper::init() { set_directions(); #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM - TERN_(HAS_MOTOR_CURRENT_PWM, initialized = true); + initialized = true; digipot_init(); #endif } @@ -2971,18 +2974,19 @@ void Stepper::report_positions() { #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM void Stepper::digipot_current(const uint8_t driver, const int16_t current) { + if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1)) + motor_current_setting[driver] = current; // update motor_current_setting + + if (!initialized) return; #if HAS_DIGIPOTSS - + SERIAL_ECHOLNPAIR("Digipotss current ", current); const uint8_t digipot_ch[] = DIGIPOT_CHANNELS; digitalPotWrite(digipot_ch[driver], current); #elif HAS_MOTOR_CURRENT_PWM - if (!initialized) return; - if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1)) - motor_current_setting[driver] = current; // update motor_current_setting #define _WRITE_CURRENT_PWM(P) analogWrite(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE)) switch (driver) { @@ -3021,14 +3025,12 @@ void Stepper::report_positions() { #if HAS_DIGIPOTSS - static const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT; - SPI.begin(); SET_OUTPUT(DIGIPOTSS_PIN); - LOOP_L_N(i, COUNT(digipot_motor_current)) { + LOOP_L_N(i, COUNT(motor_current_setting)) { //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]); - digipot_current(i, digipot_motor_current[i]); + digipot_current(i, motor_current_setting[i]); } #elif HAS_MOTOR_CURRENT_PWM diff --git a/Marlin/src/module/stepper.h b/Marlin/src/module/stepper.h index 3b68553689..51f7c489d7 100644 --- a/Marlin/src/module/stepper.h +++ b/Marlin/src/module/stepper.h @@ -249,8 +249,12 @@ class Stepper { #ifndef PWM_MOTOR_CURRENT #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT #endif - static uint32_t motor_current_setting[3]; static bool initialized; + static uint32_t motor_current_setting[3]; + #elif HAS_DIGIPOTSS + static bool initialized; + static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT; + static uint32_t motor_current_setting[COUNT(digipot_count)]; // Initialized by settings.load() #endif private: @@ -587,7 +591,7 @@ class Stepper { static int32_t _eval_bezier_curve(const uint32_t curr_step); #endif - #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS static void digipot_init(); #endif From 6043f88b3551c3d32f6abe1f433fe6d4328516c9 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Mon, 10 Aug 2020 08:23:07 -0400 Subject: [PATCH 04/42] Update G34_M422.cpp --- Marlin/src/gcode/calibrate/G34_M422.cpp | 36 ++++++++++++++++--------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 31555ff0bf..6884d5ee0a 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -170,7 +170,9 @@ void GcodeSuite::G34() { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions."); SERIAL_ECHOLNPAIR("\nITERATION: ", int(iteration + 1)); - ui.set_status("\nITERATION: ", int(iteration + 1)); + #if HAS_DISPLAY + ui.set_status("\nITERATION: ", int(iteration + 1)); + #endif // Initialize minimum value z_measured_min = 100000.0f; @@ -193,7 +195,9 @@ void GcodeSuite::G34() { const float z_probed_height = probe.probe_at_point(z_stepper_align.xy[iprobe], raise_after, 0, true, false); if (isnan(z_probed_height)) { SERIAL_ECHOLNPGM(MSG_LCD_PROBING_FAILED); - ui.set_status_P(MSG_LCD_PROBING_FAILED) + #if HAS_DISPLAY + ui.set_status_P(MSG_LCD_PROBING_FAILED) + #endif err_break = true; break; } @@ -252,13 +256,15 @@ void GcodeSuite::G34() { , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) #endif ); - ui.set_status("\n" - "DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1]) - #if NUM_Z_STEPPER_DRIVERS == 3 - , " Z2-Z3=", ABS(z_measured[1] - z_measured[2]) - , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) - #endif - ); + #if HAS_DISPLAY + ui.set_status("\n" + "DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1]) + #if NUM_Z_STEPPER_DRIVERS == 3 + , " Z2-Z3=", ABS(z_measured[1] - z_measured[2]) + , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) + #endif + ); + #endif #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) // Check if the applied corrections go in the correct direction. @@ -278,7 +284,9 @@ void GcodeSuite::G34() { // If it's getting worse, stop and throw an error if (last_z_align_level_indicator < z_align_level_indicator * 0.7f) { SERIAL_ECHOLNPGM(DECREASING_ACCURACY); - ui.set_status_P(DECREASING_ACCURACY); + #if HAS_DISPLAY + ui.set_status_P(DECREASING_ACCURACY); + #endif err_break = true; break; } @@ -303,7 +311,9 @@ void GcodeSuite::G34() { // Check for less accuracy compared to last move if (last_z_align_move[zstepper] < z_align_abs * 0.7f) { SERIAL_ECHOLNPGM(DECREASING_ACCURACY); - ui.set_status_P(DECREASING_ACCURACY); + #if HAS_DISPLAY + ui.set_status_P(DECREASING_ACCURACY); + #endif if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " last_z_align_move = ", last_z_align_move[zstepper]); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " z_align_abs = ", z_align_abs); adjustment_reverse = !adjustment_reverse; @@ -343,7 +353,9 @@ void GcodeSuite::G34() { if (success_break) { SERIAL_ECHOLNPGM("Target accuracy achieved."); - ui.set_status_P(PGM_P("Target accuracy achieved.")); + #if HAS_DISPLAY + ui.set_status_P(PGM_P("Target accuracy achieved.")); + #endif break; } From 1c19d9a86aa04f50b47ccf73d4dbe320818bef8f Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Tue, 11 Aug 2020 19:34:23 -0400 Subject: [PATCH 05/42] Add missed lines from working branch --- Marlin/src/gcode/calibrate/G34_M422.cpp | 1 - Marlin/src/inc/SanityCheck.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 6884d5ee0a..6717d37722 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -609,7 +609,6 @@ void GcodeSuite::M422() { #elif ENABLED(HAS_I2C_DIGIPOT) digipot_i2c_set_current(Z_AXIS, previous_current) #elif HAS_TRINAMIC_CONFIG - static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; #if AXIS_IS_TMC(Z) stepperZ.rms_current(previous_current_arr[0]); #endif diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index f077afaeae..e4bee7c813 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -2734,7 +2734,7 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #endif #if ENABLED(MECHANICAL_GANTRY_CALIBRATION) - #if NONE(DAC_STEPPER_CURRENT, HAS_DIGIPOTSS, DAC_STEPPER_CURRENT, HAS_TRINAMIC_CONFIG) + #if NONE(DAC_STEPPER_CURRENT, HAS_DIGIPOTSS, DAC_STEPPER_CURRENT, HAS_TRINAMIC_CONFIG, HAS_MOTOR_CURRENT_PWM) #error "It is highly reccomended to have adjustable current drivers to prevent damage. Disable this line to continue anyway." #else #ifndef GANTRY_CALIBRATION_CURRENT From e79b3281035d8642192e88bcfb0c9a5e0ec8610b Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 12 Aug 2020 17:10:05 -0500 Subject: [PATCH 06/42] Various fixes, cleanup --- Marlin/src/gcode/calibrate/G34_M422.cpp | 64 +++++++++++++------------ Marlin/src/lcd/language/language_en.h | 3 +- Marlin/src/lcd/menu/menu_motion.cpp | 2 +- Marlin/src/module/settings.cpp | 43 +++++++++-------- 4 files changed, 60 insertions(+), 52 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 6717d37722..10ccecc649 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -155,23 +155,28 @@ void GcodeSuite::G34() { z_maxdiff = 0.0f, amplification = z_auto_align_amplification; - // These are needed after the for-loop - uint8_t iteration; - bool err_break = false; - float z_measured_min; - #if DISABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) bool adjustment_reverse = false; #endif - // 'iteration' is declared above and is also used after the for-loop. - // *not* the same as LOOP_L_N(iteration, z_auto_align_iterations) - for (iteration = 0; iteration < z_auto_align_iterations; ++iteration) { + #if HAS_DISPLAY + PGM_P const msg_iteration = GET_TEXT(MSG_ITERATION); + const iter_str_len = strlen_P(iter_str_len); + #endif + + // Final z and iteration values will be used after breaking the loop + float z_measured_min; + uint8_t iteration = 0; + bool err_break = false; // To break out of nested loops + while (iteration < z_auto_align_iterations) { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions."); - SERIAL_ECHOLNPAIR("\nITERATION: ", int(iteration + 1)); + const int iter = iteration + 1; + SERIAL_ECHOLNPAIR("\nIteration: ", iter); #if HAS_DISPLAY - ui.set_status("\nITERATION: ", int(iteration + 1)); + char str[iter_str_len + 2 + 1]; + sprintf_P(str, msg_iteration, iter); + ui.set_status(str); #endif // Initialize minimum value @@ -194,10 +199,8 @@ void GcodeSuite::G34() { // current_position.z has been manually altered in the "dirty trick" above. const float z_probed_height = probe.probe_at_point(z_stepper_align.xy[iprobe], raise_after, 0, true, false); if (isnan(z_probed_height)) { - SERIAL_ECHOLNPGM(MSG_LCD_PROBING_FAILED); - #if HAS_DISPLAY - ui.set_status_P(MSG_LCD_PROBING_FAILED) - #endif + SERIAL_ECHOLNPGM("Probing failed"); + LCD_MESSAGEPGM(MSG_LCD_PROBING_FAILED); err_break = true; break; } @@ -257,13 +260,19 @@ void GcodeSuite::G34() { #endif ); #if HAS_DISPLAY - ui.set_status("\n" - "DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1]) + char msg[30], fstr[16]; + sprintf_P( + PSTR("DIFFERENCE Z1-Z2=%s" + #if NUM_Z_STEPPER_DRIVERS == 3 + " Z2-Z3=%s Z3-Z1=%s" + #endif + ), dtostrf(ABS(z_measured[0] - z_measured[1]), 1, 3, fstr) #if NUM_Z_STEPPER_DRIVERS == 3 - , " Z2-Z3=", ABS(z_measured[1] - z_measured[2]) - , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) + , dtostrf(ABS(z_measured[1] - z_measured[2]), 1, 3, fstr) + , dtostrf(ABS(z_measured[2] - z_measured[0]), 1, 3, fstr) #endif ); + ui.set_status(msg); #endif #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) @@ -283,10 +292,8 @@ void GcodeSuite::G34() { // If it's getting worse, stop and throw an error if (last_z_align_level_indicator < z_align_level_indicator * 0.7f) { - SERIAL_ECHOLNPGM(DECREASING_ACCURACY); - #if HAS_DISPLAY - ui.set_status_P(DECREASING_ACCURACY); - #endif + SERIAL_ECHOLNPGM("Decreasing Accuracy Detected."); + LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY)); err_break = true; break; } @@ -310,10 +317,8 @@ void GcodeSuite::G34() { // Check for less accuracy compared to last move if (last_z_align_move[zstepper] < z_align_abs * 0.7f) { - SERIAL_ECHOLNPGM(DECREASING_ACCURACY); - #if HAS_DISPLAY - ui.set_status_P(DECREASING_ACCURACY); - #endif + SERIAL_ECHOLNPGM("Decreasing Accuracy Detected."); + LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY)); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " last_z_align_move = ", last_z_align_move[zstepper]); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " z_align_abs = ", z_align_abs); adjustment_reverse = !adjustment_reverse; @@ -353,13 +358,12 @@ void GcodeSuite::G34() { if (success_break) { SERIAL_ECHOLNPGM("Target accuracy achieved."); - #if HAS_DISPLAY - ui.set_status_P(PGM_P("Target accuracy achieved.")); - #endif + LCD_MESSAGEPGM(MSG_ACCURACY_ACHIEVED); break; } - } // for (iteration) + iteration++; + } // while (iteration < z_auto_align_iterations) if (err_break) SERIAL_ECHOLNPGM("G34 aborted."); diff --git a/Marlin/src/lcd/language/language_en.h b/Marlin/src/lcd/language/language_en.h index f19edc90d9..2d8fe55fba 100644 --- a/Marlin/src/lcd/language/language_en.h +++ b/Marlin/src/lcd/language/language_en.h @@ -68,7 +68,8 @@ namespace Language_en { PROGMEM Language_Str MSG_AUTO_HOME_Z = _UxGT("Home Z"); PROGMEM Language_Str MSG_AUTO_Z_ALIGN = _UxGT("Auto Z-Align"); PROGMEM Language_Str MSG_ASSISTED_TRAMMING = _UxGT("Assisted Tramming"); - PROGMEM Language_Str DECREASING_ACCURACY = _UxGT("Decreasing accuracy detected."); + PROGMEM Language_Str MSG_DECREASING_ACCURACY = _UxGT("Accuracy Decreasing!"); + PROGMEM Language_Str MSG_ACCURACY_ACHIEVED = _UxGT("Accuracy Achieved"); PROGMEM Language_Str MSG_LEVEL_BED_HOMING = _UxGT("Homing XYZ"); PROGMEM Language_Str MSG_LEVEL_BED_WAITING = _UxGT("Click to Begin"); PROGMEM Language_Str MSG_LEVEL_BED_NEXT_POINT = _UxGT("Next Point"); diff --git a/Marlin/src/lcd/menu/menu_motion.cpp b/Marlin/src/lcd/menu/menu_motion.cpp index c64e337d6b..5a6cab60ee 100644 --- a/Marlin/src/lcd/menu/menu_motion.cpp +++ b/Marlin/src/lcd/menu/menu_motion.cpp @@ -341,7 +341,7 @@ void menu_motion() { // // Auto Z-Align // - #if ANY(Z_STEPPER_AUTO_ALIGN, MECHANICAL_GANTRY_CALIBRATION) + #if EITHER(Z_STEPPER_AUTO_ALIGN, MECHANICAL_GANTRY_CALIBRATION) GCODES_ITEM(MSG_AUTO_Z_ALIGN, PSTR("G34")); #endif diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index f610dbe62b..2273a38527 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -369,7 +369,7 @@ typedef struct SettingsDataStruct { #if HAS_DIGIPOTSS uint32_t motor_current_setting[COUNT(stepper.motor_current_setting)]; #else - uint32_t motor_current_setting[3]; // M907 X Z E + uint32_t motor_current_setting[3]; // M907 X Z E #endif // @@ -2114,19 +2114,19 @@ void MarlinSettings::postprocess() { // Motor Current PWM // { - SERIAL_ECHOLN("DIGIPOTS Loading"); + _FIELD_TEST(motor_current_setting); #if HAS_DIGIPOTSS uint32_t motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; #else - uint32_t motor_current_setting[3]; // M907 X Z E + uint32_t motor_current_setting[3]; #endif - _FIELD_TEST(motor_current_setting); + DEBUG_ECHOLNPGM("DIGIPOTS Loading"); EEPROM_READ(motor_current_setting); + DEBUG_ECHOLNPGM("DIGIPOTS Loaded"); #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS if (!validating) COPY(stepper.motor_current_setting, motor_current_setting); #endif - SERIAL_ECHOLN("DIGIPOTS Loaded"); } // @@ -2809,13 +2809,14 @@ void MarlinSettings::reset() { // // DIGIPOTS // - SERIAL_ECHOLN("Writing Digipot"); #if HAS_DIGIPOTSS static constexpr uint32_t tmp_motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; + DEBUG_ECHOLNPGM("Writing Digipot"); LOOP_L_N(q, COUNT(tmp_motor_current_setting)) stepper.digipot_current(q, tmp_motor_current_setting[q]); + DEBUG_ECHOLNPGM("Digipot Written"); #endif -SERIAL_ECHOLN("Digipot Written"); + // // CNC Coordinate System // @@ -3715,21 +3716,23 @@ SERIAL_ECHOLN("Digipot Written"); #endif #endif - #if HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS CONFIG_ECHO_HEADING("Stepper motor currents:"); CONFIG_ECHO_START(); - SERIAL_ECHOLNPAIR_P( - PSTR(" M907 X"), stepper.motor_current_setting[0] - , SP_Z_STR, stepper.motor_current_setting[1] - , SP_E_STR, stepper.motor_current_setting[2] - ); - #elif HASDIGIPOTSS - CONFIG_ECHO_HEADING("Stepper motor currents:"); - CONFIG_ECHO_START(); - LOOP_L_N(q, COUNT(stepper.motor_current_setting)) { - SERIAL_ECHOPAIR_P( "M907 ", axis_codes[q]); - SERIAL_ECHOLN_P(stepper.motor_current_setting[q]); - } + #if HAS_MOTOR_CURRENT_PWM + SERIAL_ECHOLNPAIR_P( + PSTR(" M907 X"), stepper.motor_current_setting[0] + , SP_Z_STR, stepper.motor_current_setting[1] + , SP_E_STR, stepper.motor_current_setting[2] + ); + #elif HAS_DIGIPOTSS + SERIAL_ECHOPGM(" M907"); + LOOP_L_N(q, COUNT(stepper.motor_current_setting)) { + SERIAL_CHAR(' '); + SERIAL_CHAR(axis_codes[q]); + SERIAL_ECHO(stepper.motor_current_setting[q]); + } + #endif #endif /** From 395d11b06f0b724a81288dd69bd58dc7bcc15b91 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 12 Aug 2020 18:46:55 -0500 Subject: [PATCH 07/42] Stepper current refactor --- Marlin/src/HAL/DUE/fastio/G2_PWM.cpp | 2 +- Marlin/src/HAL/LPC1768/inc/SanityCheck.h | 2 +- Marlin/src/MarlinCore.cpp | 12 ++-- Marlin/src/feature/dac/dac_mcp4728.cpp | 50 ++++++++-------- Marlin/src/feature/dac/dac_mcp4728.h | 25 ++++---- Marlin/src/feature/dac/stepper_dac.cpp | 57 +++++++++---------- Marlin/src/feature/dac/stepper_dac.h | 19 ++++--- Marlin/src/feature/digipot/digipot.h | 12 +++- .../src/feature/digipot/digipot_mcp4018.cpp | 12 ++-- .../src/feature/digipot/digipot_mcp4451.cpp | 8 ++- Marlin/src/gcode/calibrate/G34_M422.cpp | 28 ++++----- .../src/gcode/feature/digipot/M907-M910.cpp | 52 ++++++++--------- Marlin/src/gcode/gcode.cpp | 6 +- Marlin/src/gcode/gcode.h | 12 ++-- Marlin/src/inc/Conditionals_adv.h | 2 +- Marlin/src/inc/Conditionals_post.h | 2 +- Marlin/src/inc/SanityCheck.h | 4 +- Marlin/src/lcd/menu/menu_advanced.cpp | 8 +-- Marlin/src/module/settings.cpp | 18 +++--- Marlin/src/module/stepper.cpp | 49 +++++++--------- Marlin/src/module/stepper.h | 26 +++++---- Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h | 2 +- .../src/pins/teensy2/pins_PRINTRBOARD_REVF.h | 2 +- platformio.ini | 4 +- 24 files changed, 216 insertions(+), 198 deletions(-) diff --git a/Marlin/src/HAL/DUE/fastio/G2_PWM.cpp b/Marlin/src/HAL/DUE/fastio/G2_PWM.cpp index 1682faea66..d9fbabce21 100644 --- a/Marlin/src/HAL/DUE/fastio/G2_PWM.cpp +++ b/Marlin/src/HAL/DUE/fastio/G2_PWM.cpp @@ -154,7 +154,7 @@ void Stepper::digipot_init() { NVIC_SetPriority(PWM_IRQn, NVIC_EncodePriority(0, 10, 0)); // normal priority for PWM module (can stand some jitter on the Vref signals) } -void Stepper::digipot_current(const uint8_t driver, const int16_t current) { +void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) { if (!(PWM->PWM_CH_NUM[0].PWM_CPRD == PWM_PERIOD_US)) digipot_init(); // Init PWM system if needed diff --git a/Marlin/src/HAL/LPC1768/inc/SanityCheck.h b/Marlin/src/HAL/LPC1768/inc/SanityCheck.h index caec347bf5..aeb3010e86 100644 --- a/Marlin/src/HAL/LPC1768/inc/SanityCheck.h +++ b/Marlin/src/HAL/LPC1768/inc/SanityCheck.h @@ -178,7 +178,7 @@ static_assert(DISABLED(BAUD_RATE_GCODE), "BAUD_RATE_GCODE is not yet supported o // // Flag any i2c pin conflicts // -#if ANY(HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT, EXPERIMENTAL_I2CBUS, I2C_POSITION_ENCODERS, PCA9632, I2C_EEPROM) +#if ANY(HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC, EXPERIMENTAL_I2CBUS, I2C_POSITION_ENCODERS, PCA9632, I2C_EEPROM) #define USEDI2CDEV_M 1 // /Wire.cpp #if USEDI2CDEV_M == 0 // P0_27 [D57] (AUX-1) .......... P0_28 [D58] (AUX-1) diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp index 2927a3e40b..7b6d76b87f 100644 --- a/Marlin/src/MarlinCore.cpp +++ b/Marlin/src/MarlinCore.cpp @@ -97,7 +97,7 @@ #include "feature/closedloop.h" #endif -#if HAS_I2C_DIGIPOT +#if HAS_MOTOR_CURRENT_I2C #include "feature/digipot/digipot.h" #endif @@ -125,7 +125,7 @@ #include "module/servo.h" #endif -#if ENABLED(DAC_STEPPER_CURRENT) +#if ENABLED(HAS_MOTOR_CURRENT_DAC) #include "feature/dac/stepper_dac.h" #endif @@ -1070,12 +1070,12 @@ void setup() { SETUP_RUN(enableStepperDrivers()); #endif - #if HAS_I2C_DIGIPOT - SETUP_RUN(digipot_i2c_init()); + #if HAS_MOTOR_CURRENT_I2C + SETUP_RUN(digipot_i2c.init()); #endif - #if ENABLED(DAC_STEPPER_CURRENT) - SETUP_RUN(dac_init()); + #if ENABLED(HAS_MOTOR_CURRENT_DAC) + SETUP_RUN(stepper_dac.init()); #endif #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1 diff --git a/Marlin/src/feature/dac/dac_mcp4728.cpp b/Marlin/src/feature/dac/dac_mcp4728.cpp index 55f10a49ef..81c465cf29 100644 --- a/Marlin/src/feature/dac/dac_mcp4728.cpp +++ b/Marlin/src/feature/dac/dac_mcp4728.cpp @@ -32,16 +32,18 @@ #include "../../inc/MarlinConfig.h" -#if ENABLED(DAC_STEPPER_CURRENT) +#if ENABLED(HAS_MOTOR_CURRENT_DAC) #include "dac_mcp4728.h" -xyze_uint_t mcp4728_values; +MCP4728 mcp4728; + +xyze_uint_t dac_values; /** * Begin I2C, get current values (input register and eeprom) of mcp4728 */ -void mcp4728_init() { +void MCP4728::init() { Wire.begin(); Wire.requestFrom(I2C_ADDRESS(DAC_DEV_ADDRESS), uint8_t(24)); while (Wire.available()) { @@ -50,7 +52,7 @@ void mcp4728_init() { loByte = Wire.read(); if (!(deviceID & 0x08)) - mcp4728_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte); + dac_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte); } } @@ -58,9 +60,9 @@ void mcp4728_init() { * Write input resister value to specified channel using fastwrite method. * Channel : 0-3, Values : 0-4095 */ -uint8_t mcp4728_analogWrite(const uint8_t channel, const uint16_t value) { - mcp4728_values[channel] = value; - return mcp4728_fastWrite(); +uint8_t MCP4728::analogWrite(const uint8_t channel, const uint16_t value) { + dac_values[channel] = value; + return fastWrite(); } /** @@ -68,12 +70,12 @@ uint8_t mcp4728_analogWrite(const uint8_t channel, const uint16_t value) { * This will update both input register and EEPROM value * This will also write current Vref, PowerDown, Gain settings to EEPROM */ -uint8_t mcp4728_eepromWrite() { +uint8_t MCP4728::eepromWrite() { Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS)); Wire.write(SEQWRITE); LOOP_XYZE(i) { - Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[i])); - Wire.write(lowByte(mcp4728_values[i])); + Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(dac_values[i])); + Wire.write(lowByte(dac_values[i])); } return Wire.endTransmission(); } @@ -81,7 +83,7 @@ uint8_t mcp4728_eepromWrite() { /** * Write Voltage reference setting to all input regiters */ -uint8_t mcp4728_setVref_all(const uint8_t value) { +uint8_t MCP4728::setVref_all(const uint8_t value) { Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS)); Wire.write(VREFWRITE | (value ? 0x0F : 0x00)); return Wire.endTransmission(); @@ -89,7 +91,7 @@ uint8_t mcp4728_setVref_all(const uint8_t value) { /** * Write Gain setting to all input regiters */ -uint8_t mcp4728_setGain_all(const uint8_t value) { +uint8_t MCP4728::setGain_all(const uint8_t value) { Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS)); Wire.write(GAINWRITE | (value ? 0x0F : 0x00)); return Wire.endTransmission(); @@ -98,16 +100,16 @@ uint8_t mcp4728_setGain_all(const uint8_t value) { /** * Return Input Register value */ -uint16_t mcp4728_getValue(const uint8_t channel) { return mcp4728_values[channel]; } +uint16_t MCP4728::getValue(const uint8_t channel) { return dac_values[channel]; } #if 0 /** * Steph: Might be useful in the future * Return Vout */ -uint16_t mcp4728_getVout(const uint8_t channel) { +uint16_t MCP4728::getVout(const uint8_t channel) { const uint32_t vref = 2048, - vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096; + vOut = (vref * dac_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096; return _MIN(vOut, defaultVDD); } #endif @@ -115,15 +117,15 @@ uint16_t mcp4728_getVout(const uint8_t channel) { /** * Returns DAC values as a 0-100 percentage of drive strength */ -uint8_t mcp4728_getDrvPct(const uint8_t channel) { return uint8_t(100.0 * mcp4728_values[channel] / (DAC_STEPPER_MAX) + 0.5); } +uint8_t MCP4728::getDrvPct(const uint8_t channel) { return uint8_t(100.0 * dac_values[channel] / (DAC_STEPPER_MAX) + 0.5); } /** * Receives all Drive strengths as 0-100 percent values, updates * DAC Values array and calls fastwrite to update the DAC. */ -void mcp4728_setDrvPct(xyze_uint8_t &pct) { - mcp4728_values *= 0.01 * pct * (DAC_STEPPER_MAX); - mcp4728_fastWrite(); +void MCP4728::setDrvPct(xyze_uint8_t &pct) { + dac_values *= 0.01 * pct * (DAC_STEPPER_MAX); + fastWrite(); } /** @@ -131,11 +133,11 @@ void mcp4728_setDrvPct(xyze_uint8_t &pct) { * DAC Input and PowerDown bits update. * No EEPROM update */ -uint8_t mcp4728_fastWrite() { +uint8_t MCP4728::fastWrite() { Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS)); LOOP_XYZE(i) { - Wire.write(highByte(mcp4728_values[i])); - Wire.write(lowByte(mcp4728_values[i])); + Wire.write(highByte(dac_values[i])); + Wire.write(lowByte(dac_values[i])); } return Wire.endTransmission(); } @@ -143,10 +145,10 @@ uint8_t mcp4728_fastWrite() { /** * Common function for simple general commands */ -uint8_t mcp4728_simpleCommand(const byte simpleCommand) { +uint8_t MCP4728::simpleCommand(const byte simpleCommand) { Wire.beginTransmission(I2C_ADDRESS(GENERALCALL)); Wire.write(simpleCommand); return Wire.endTransmission(); } -#endif // DAC_STEPPER_CURRENT +#endif // HAS_MOTOR_CURRENT_DAC diff --git a/Marlin/src/feature/dac/dac_mcp4728.h b/Marlin/src/feature/dac/dac_mcp4728.h index 6cce77856a..b2872f5bcf 100644 --- a/Marlin/src/feature/dac/dac_mcp4728.h +++ b/Marlin/src/feature/dac/dac_mcp4728.h @@ -65,13 +65,18 @@ // DAC_OR_ADDRESS defined in pins_BOARD.h file #define DAC_DEV_ADDRESS (BASE_ADDR | DAC_OR_ADDRESS) -void mcp4728_init(); -uint8_t mcp4728_analogWrite(const uint8_t channel, const uint16_t value); -uint8_t mcp4728_eepromWrite(); -uint8_t mcp4728_setVref_all(const uint8_t value); -uint8_t mcp4728_setGain_all(const uint8_t value); -uint16_t mcp4728_getValue(const uint8_t channel); -uint8_t mcp4728_fastWrite(); -uint8_t mcp4728_simpleCommand(const byte simpleCommand); -uint8_t mcp4728_getDrvPct(const uint8_t channel); -void mcp4728_setDrvPct(xyze_uint8_t &pct); +class MCP4728 { +public: + static void init(); + static uint8_t analogWrite(const uint8_t channel, const uint16_t value); + static uint8_t eepromWrite(); + static uint8_t setVref_all(const uint8_t value); + static uint8_t setGain_all(const uint8_t value); + static uint16_t getValue(const uint8_t channel); + static uint8_t fastWrite(); + static uint8_t simpleCommand(const byte simpleCommand); + static uint8_t getDrvPct(const uint8_t channel); + static void setDrvPct(xyze_uint8_t &pct); +}; + +extern MCP4728 mcp4728; diff --git a/Marlin/src/feature/dac/stepper_dac.cpp b/Marlin/src/feature/dac/stepper_dac.cpp index 6b140a0025..5f10b4ccfb 100644 --- a/Marlin/src/feature/dac/stepper_dac.cpp +++ b/Marlin/src/feature/dac/stepper_dac.cpp @@ -26,7 +26,7 @@ #include "../../inc/MarlinConfig.h" -#if ENABLED(DAC_STEPPER_CURRENT) +#if ENABLED(HAS_MOTOR_CURRENT_DAC) #include "stepper_dac.h" #include "../../MarlinCore.h" // for SP_X_LBL... @@ -35,56 +35,53 @@ bool dac_present = false; constexpr xyze_uint8_t dac_order = DAC_STEPPER_ORDER; xyze_uint8_t dac_channel_pct = DAC_MOTOR_CURRENT_DEFAULT; -int dac_init() { +StepperDAC stepper_dac; + +int StepperDAC::init() { #if PIN_EXISTS(DAC_DISABLE) OUT_WRITE(DAC_DISABLE_PIN, LOW); // set pin low to enable DAC #endif - mcp4728_init(); + mcp4728.init(); - if (mcp4728_simpleCommand(RESET)) return -1; + if (mcp4728.simpleCommand(RESET)) return -1; dac_present = true; - mcp4728_setVref_all(DAC_STEPPER_VREF); - mcp4728_setGain_all(DAC_STEPPER_GAIN); + mcp4728.setVref_all(DAC_STEPPER_VREF); + mcp4728.setGain_all(DAC_STEPPER_GAIN); - if (mcp4728_getDrvPct(0) < 1 || mcp4728_getDrvPct(1) < 1 || mcp4728_getDrvPct(2) < 1 || mcp4728_getDrvPct(3) < 1 ) { - mcp4728_setDrvPct(dac_channel_pct); - mcp4728_eepromWrite(); + if (mcp4728.getDrvPct(0) < 1 || mcp4728.getDrvPct(1) < 1 || mcp4728.getDrvPct(2) < 1 || mcp4728.getDrvPct(3) < 1 ) { + mcp4728.setDrvPct(dac_channel_pct); + mcp4728.eepromWrite(); } return 0; } -void dac_current_percent(uint8_t channel, float val) { - if (!dac_present) return; - - NOMORE(val, 100); - - mcp4728_analogWrite(dac_order[channel], val * 0.01 * (DAC_STEPPER_MAX)); - mcp4728_simpleCommand(UPDATE); -} - -void dac_current_raw(uint8_t channel, uint16_t val) { +void StepperDAC::set_current_value(const uint8_t channel, uint16_t val) { if (!dac_present) return; NOMORE(val, uint16_t(DAC_STEPPER_MAX)); - mcp4728_analogWrite(dac_order[channel], val); - mcp4728_simpleCommand(UPDATE); + mcp4728.analogWrite(dac_order[channel], val); + mcp4728.simpleCommand(UPDATE); } -static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) * RECIPROCAL(DAC_STEPPER_MAX); } -static float dac_amps(int8_t n) { return mcp4728_getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * 0.125 * RECIPROCAL(DAC_STEPPER_SENSE); } +void StepperDAC::set_current_percent(const uint8_t channel, float val) { + set_current_value(channel, _MIN(val, 100.0f) * (DAC_STEPPER_MAX) / 100.0f); +} -uint8_t dac_current_get_percent(const AxisEnum axis) { return mcp4728_getDrvPct(dac_order[axis]); } -void dac_current_set_percents(xyze_uint8_t &pct) { +static float dac_perc(int8_t n) { return 100.0 * mcp4728.getValue(dac_order[n]) * RECIPROCAL(DAC_STEPPER_MAX); } +static float dac_amps(int8_t n) { return mcp4728.getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * 0.125 * RECIPROCAL(DAC_STEPPER_SENSE); } + +uint8_t StepperDAC::get_current_percent(const AxisEnum axis) { return mcp4728.getDrvPct(dac_order[axis]); } +void StepperDAC::set_current_percents(xyze_uint8_t &pct) { LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]]; - mcp4728_setDrvPct(dac_channel_pct); + mcp4728.setDrvPct(dac_channel_pct); } -void dac_print_values() { +void StepperDAC::print_values() { if (!dac_present) return; SERIAL_ECHO_MSG("Stepper current values in % (Amps):"); SERIAL_ECHO_START(); @@ -94,9 +91,9 @@ void dac_print_values() { SERIAL_ECHOLNPAIR_P(SP_E_LBL, dac_perc(E_AXIS), PSTR(" ("), dac_amps(E_AXIS), PSTR(")")); } -void dac_commit_eeprom() { +void StepperDAC::commit_eeprom() { if (!dac_present) return; - mcp4728_eepromWrite(); + mcp4728.eepromWrite(); } -#endif // DAC_STEPPER_CURRENT +#endif // HAS_MOTOR_CURRENT_DAC diff --git a/Marlin/src/feature/dac/stepper_dac.h b/Marlin/src/feature/dac/stepper_dac.h index 0543b6275e..6836335e98 100644 --- a/Marlin/src/feature/dac/stepper_dac.h +++ b/Marlin/src/feature/dac/stepper_dac.h @@ -27,10 +27,15 @@ #include "dac_mcp4728.h" -int dac_init(); -void dac_current_percent(uint8_t channel, float val); -void dac_current_raw(uint8_t channel, uint16_t val); -void dac_print_values(); -void dac_commit_eeprom(); -uint8_t dac_current_get_percent(AxisEnum axis); -void dac_current_set_percents(xyze_uint8_t &pct); +class StepperDAC { +public: + static int init(); + static void set_current_percent(const uint8_t channel, float val); + static void set_current_value(const uint8_t channel, uint16_t val); + static void print_values(); + static void commit_eeprom(); + static uint8_t get_current_percent(AxisEnum axis); + static void set_current_percents(xyze_uint8_t &pct); +}; + +extern StepperDAC stepper_dac; diff --git a/Marlin/src/feature/digipot/digipot.h b/Marlin/src/feature/digipot/digipot.h index d59f8b930a..c53f8093dd 100644 --- a/Marlin/src/feature/digipot/digipot.h +++ b/Marlin/src/feature/digipot/digipot.h @@ -21,5 +21,13 @@ */ #pragma once -void digipot_i2c_set_current(const uint8_t channel, const float current); -void digipot_i2c_init(); +// +// Header for MCP4018 and MCP4451 current control i2c devices +// +class DigipotI2C { +public: + static void init(); + static void set_current(const uint8_t channel, const float current); +}; + +DigipotI2C digipot_i2c; diff --git a/Marlin/src/feature/digipot/digipot_mcp4018.cpp b/Marlin/src/feature/digipot/digipot_mcp4018.cpp index 35c513a32c..6260185fc3 100644 --- a/Marlin/src/feature/digipot/digipot_mcp4018.cpp +++ b/Marlin/src/feature/digipot/digipot_mcp4018.cpp @@ -24,6 +24,8 @@ #if ENABLED(DIGIPOT_MCP4018) +#include "digipot.h" + #include #include // https://github.com/stawel/SlowSoftI2CMaster @@ -68,7 +70,7 @@ static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = { #endif }; -static void i2c_send(const uint8_t channel, const byte v) { +static void digipot_i2c_send(const uint8_t channel, const byte v) { if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) { pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS_A) << 1) | I2C_WRITE); pots[channel].i2c_write(v); @@ -77,12 +79,12 @@ static void i2c_send(const uint8_t channel, const byte v) { } // This is for the MCP4018 I2C based digipot -void digipot_i2c_set_current(const uint8_t channel, const float current) { +void DigipotI2C::set_current(const uint8_t channel, const float current) { const float ival = _MIN(_MAX(current, 0), float(DIGIPOT_MCP4018_MAX_VALUE)); - i2c_send(channel, current_to_wiper(ival)); + digipot_i2c_send(channel, current_to_wiper(ival)); } -void digipot_i2c_init() { +void DigipotI2C::init() { LOOP_L_N(i, DIGIPOT_I2C_NUM_CHANNELS) pots[i].i2c_init(); // Init currents according to Configuration_adv.h @@ -94,7 +96,7 @@ void digipot_i2c_init() { #endif ; LOOP_L_N(i, COUNT(digipot_motor_current)) - digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i])); + set_current(i, pgm_read_float(&digipot_motor_current[i])); } #endif // DIGIPOT_MCP4018 diff --git a/Marlin/src/feature/digipot/digipot_mcp4451.cpp b/Marlin/src/feature/digipot/digipot_mcp4451.cpp index 0c8ff04a0b..7e6ace49a0 100644 --- a/Marlin/src/feature/digipot/digipot_mcp4451.cpp +++ b/Marlin/src/feature/digipot/digipot_mcp4451.cpp @@ -24,6 +24,8 @@ #if ENABLED(DIGIPOT_MCP4451) +#include "digipot.h" + #include #include @@ -61,7 +63,7 @@ static void digipot_i2c_send(const byte addr, const byte a, const byte b) { } // This is for the MCP4451 I2C based digipot -void digipot_i2c_set_current(const uint8_t channel, const float current) { +void DigipotI2C::set_current(const uint8_t channel, const float current) { // These addresses are specific to Azteeg X3 Pro, can be set to others. // In this case first digipot is at address A0=0, A1=0, second one is at A0=0, A1=1 const byte addr = channel < 4 ? DIGIPOT_I2C_ADDRESS_A : DIGIPOT_I2C_ADDRESS_B; // channel 0-3 vs 4-7 @@ -75,7 +77,7 @@ void digipot_i2c_set_current(const uint8_t channel, const float current) { digipot_i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT))); } -void digipot_i2c_init() { +void DigipotI2C::init() { #if MB(MKS_SBASE) configure_i2c(16); // Set clock_option to 16 ensure I2C is initialized at 400kHz #else @@ -90,7 +92,7 @@ void digipot_i2c_init() { #endif ; LOOP_L_N(i, COUNT(digipot_motor_current)) - digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i])); + set_current(i, pgm_read_float(&digipot_motor_current[i])); } #endif // DIGIPOT_MCP4451 diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 10ccecc649..70768350bb 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -541,22 +541,22 @@ void GcodeSuite::M422() { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Reducing Current"); // Store current motor settings, then apply reduced value - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS]; - stepper.digipot_current(Z_AXIS, target_current); + stepper.set_digipot_current(Z_AXIS, target_current); #elif HAS_MOTOR_CURRENT_PWM const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS]; - stepper.digipot_current(1, target_current); - #elif DAC_STEPPER_CURRENT + stepper.set_digipot_current(1, target_current); + #elif HAS_MOTOR_CURRENT_DAC const float target_current = parser.floatval('S', GANTRY_CALIBRATION_CURRENT); const float previous_current = dac_amps(Z_AXIS, target_current); - dac_current_raw(Z_AXIS, target_current); - #elif ENABLED(HAS_I2C_DIGIPOT) + stepper_dac.set_current_value(Z_AXIS, target_current); + #elif ENABLED(HAS_MOTOR_CURRENT_I2C) const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); previous_current = dac_amps(Z_AXIS); - digipot_i2c_set_current(Z_AXIS, target_current) + digipot_i2c.set_current(Z_AXIS, target_current) #elif HAS_TRINAMIC_CONFIG const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; @@ -604,14 +604,14 @@ void GcodeSuite::M422() { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Restore Current"); // Reset current to original values - #if HAS_DIGIPOTSS - stepper.digipot_current(Z_AXIS, previous_current); + #if HAS_MOTOR_CURRENT_SPI + stepper.set_digipot_current(Z_AXIS, previous_current); #elif HAS_MOTOR_CURRENT_PWM - stepper.digipot_current(1, previous_current); - #elif DAC_STEPPER_CURRENT - dac_current_raw(Z_AXIS, previous_current); - #elif ENABLED(HAS_I2C_DIGIPOT) - digipot_i2c_set_current(Z_AXIS, previous_current) + stepper.set_digipot_current(1, previous_current); + #elif HAS_MOTOR_CURRENT_DAC + stepper_dac.set_current_value(Z_AXIS, previous_current); + #elif ENABLED(HAS_MOTOR_CURRENT_I2C) + digipot_i2c.set_current(Z_AXIS, previous_current) #elif HAS_TRINAMIC_CONFIG #if AXIS_IS_TMC(Z) stepperZ.rms_current(previous_current_arr[0]); diff --git a/Marlin/src/gcode/feature/digipot/M907-M910.cpp b/Marlin/src/gcode/feature/digipot/M907-M910.cpp index 54599a0451..e463666207 100644 --- a/Marlin/src/gcode/feature/digipot/M907-M910.cpp +++ b/Marlin/src/gcode/feature/digipot/M907-M910.cpp @@ -22,19 +22,19 @@ #include "../../../inc/MarlinConfig.h" -#if ANY(HAS_DIGIPOTSS, HAS_MOTOR_CURRENT_PWM, HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT) +#if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC) #include "../../gcode.h" -#if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM +#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM #include "../../../module/stepper.h" #endif -#if HAS_I2C_DIGIPOT +#if HAS_MOTOR_CURRENT_I2C #include "../../../feature/digipot/digipot.h" #endif -#if ENABLED(DAC_STEPPER_CURRENT) +#if ENABLED(HAS_MOTOR_CURRENT_DAC) #include "../../../feature/dac/stepper_dac.h" #endif @@ -42,61 +42,61 @@ * M907: Set digital trimpot motor current using axis codes X, Y, Z, E, B, S */ void GcodeSuite::M907() { - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI - LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper.digipot_current(i, parser.value_int()); - if (parser.seenval('B')) stepper.digipot_current(4, parser.value_int()); - if (parser.seenval('S')) LOOP_LE_N(i, 4) stepper.digipot_current(i, parser.value_int()); + LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper.set_digipot_current(i, parser.value_int()); + if (parser.seenval('B')) stepper.set_digipot_current(4, parser.value_int()); + if (parser.seenval('S')) LOOP_LE_N(i, 4) stepper.set_digipot_current(i, parser.value_int()); #elif HAS_MOTOR_CURRENT_PWM #if ANY_PIN(MOTOR_CURRENT_PWM_X, MOTOR_CURRENT_PWM_Y, MOTOR_CURRENT_PWM_XY) - if (parser.seenval('X') || parser.seenval('Y')) stepper.digipot_current(0, parser.value_int()); + if (parser.seenval('X') || parser.seenval('Y')) stepper.set_digipot_current(0, parser.value_int()); #endif #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z) - if (parser.seenval('Z')) stepper.digipot_current(1, parser.value_int()); + if (parser.seenval('Z')) stepper.set_digipot_current(1, parser.value_int()); #endif #if PIN_EXISTS(MOTOR_CURRENT_PWM_E) - if (parser.seenval('E')) stepper.digipot_current(2, parser.value_int()); + if (parser.seenval('E')) stepper.set_digipot_current(2, parser.value_int()); #endif #endif - #if HAS_I2C_DIGIPOT + #if HAS_MOTOR_CURRENT_I2C // this one uses actual amps in floating point - LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) digipot_i2c_set_current(i, parser.value_float()); + LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) digipot_i2c.set_current(i, parser.value_float()); // Additional extruders use B,C,D for channels 4,5,6. // TODO: Change these parameters because 'E' is used. B? for (uint8_t i = E_AXIS + 1; i < DIGIPOT_I2C_NUM_CHANNELS; i++) - if (parser.seenval('B' + i - (E_AXIS + 1))) digipot_i2c_set_current(i, parser.value_float()); + if (parser.seenval('B' + i - (E_AXIS + 1))) digipot_i2c.set_current(i, parser.value_float()); #endif - #if ENABLED(DAC_STEPPER_CURRENT) + #if ENABLED(HAS_MOTOR_CURRENT_DAC) if (parser.seenval('S')) { const float dac_percent = parser.value_float(); - LOOP_LE_N(i, 4) dac_current_percent(i, dac_percent); + LOOP_LE_N(i, 4) stepper_dac.set_current_percent(i, dac_percent); } - LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) dac_current_percent(i, parser.value_float()); + LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper_dac.set_current_percent(i, parser.value_float()); #endif } -#if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT) +#if EITHER(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC) /** * M908: Control digital trimpot directly (M908 P S) */ void GcodeSuite::M908() { - TERN_(HAS_DIGIPOTSS, stepper.digitalPotWrite(parser.intval('P'), parser.intval('S'))); - TERN_(DAC_STEPPER_CURRENT, dac_current_raw(parser.byteval('P', -1), parser.ushortval('S', 0))); + TERN_(HAS_MOTOR_CURRENT_SPI, stepper.set_digipot_value_spi(parser.intval('P'), parser.intval('S'))); + TERN_(HAS_MOTOR_CURRENT_DAC, stepper_dac.set_current_value(parser.byteval('P', -1), parser.ushortval('S', 0))); } -#endif // HAS_DIGIPOTSS || DAC_STEPPER_CURRENT + #if ENABLED(HAS_MOTOR_CURRENT_DAC) -#if ENABLED(DAC_STEPPER_CURRENT) + void GcodeSuite::M909() { stepper_dac.print_values(); } + void GcodeSuite::M910() { stepper_dac.commit_eeprom(); } - void GcodeSuite::M909() { dac_print_values(); } - void GcodeSuite::M910() { dac_commit_eeprom(); } + #endif // HAS_MOTOR_CURRENT_DAC -#endif // DAC_STEPPER_CURRENT +#endif // HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_DAC -#endif // HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM || HAS_I2C_DIGIPOT || DAC_STEPPER_CURRENT +#endif // HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_I2C || HAS_MOTOR_CURRENT_DAC diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index 25be04dd23..404a902876 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -826,11 +826,11 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) { case 900: M900(); break; // M900: Set advance K factor. #endif - #if ANY(HAS_DIGIPOTSS, HAS_MOTOR_CURRENT_PWM, HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT) + #if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC) case 907: M907(); break; // M907: Set digital trimpot motor current using axis codes. - #if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT) + #if EITHER(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC) case 908: M908(); break; // M908: Control digital trimpot directly. - #if ENABLED(DAC_STEPPER_CURRENT) + #if ENABLED(HAS_MOTOR_CURRENT_DAC) case 909: M909(); break; // M909: Print digipot/DAC current value case 910: M910(); break; // M910: Commit digipot/DAC value to external EEPROM #endif diff --git a/Marlin/src/gcode/gcode.h b/Marlin/src/gcode/gcode.h index 7c3206b20a..baee92ddaa 100644 --- a/Marlin/src/gcode/gcode.h +++ b/Marlin/src/gcode/gcode.h @@ -258,9 +258,9 @@ * M900 - Get or Set Linear Advance K-factor. (Requires LIN_ADVANCE) * M906 - Set or get motor current in milliamps using axis codes X, Y, Z, E. Report values if no axis codes given. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660 or L6470) * M907 - Set digital trimpot motor current using axis codes. (Requires a board with digital trimpots) - * M908 - Control digital trimpot directly. (Requires DAC_STEPPER_CURRENT or DIGIPOTSS_PIN) - * M909 - Print digipot/DAC current value. (Requires DAC_STEPPER_CURRENT) - * M910 - Commit digipot/DAC value to external EEPROM via I2C. (Requires DAC_STEPPER_CURRENT) + * M908 - Control digital trimpot directly. (Requires HAS_MOTOR_CURRENT_DAC or DIGIPOTSS_PIN) + * M909 - Print digipot/DAC current value. (Requires HAS_MOTOR_CURRENT_DAC) + * M910 - Commit digipot/DAC value to external EEPROM via I2C. (Requires HAS_MOTOR_CURRENT_DAC) * M911 - Report stepper driver overtemperature pre-warn condition. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660) * M912 - Clear stepper driver overtemperature pre-warn condition flag. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660) * M913 - Set HYBRID_THRESHOLD speed. (Requires HYBRID_THRESHOLD) @@ -849,11 +849,11 @@ private: static void M918(); #endif - #if ANY(HAS_DIGIPOTSS, HAS_MOTOR_CURRENT_PWM, HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT) + #if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC) static void M907(); - #if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT) + #if EITHER(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC) static void M908(); - #if ENABLED(DAC_STEPPER_CURRENT) + #if ENABLED(HAS_MOTOR_CURRENT_DAC) static void M909(); static void M910(); #endif diff --git a/Marlin/src/inc/Conditionals_adv.h b/Marlin/src/inc/Conditionals_adv.h index 9742967a09..709b32d667 100644 --- a/Marlin/src/inc/Conditionals_adv.h +++ b/Marlin/src/inc/Conditionals_adv.h @@ -130,7 +130,7 @@ #endif #if EITHER(DIGIPOT_MCP4018, DIGIPOT_MCP4451) - #define HAS_I2C_DIGIPOT 1 + #define HAS_MOTOR_CURRENT_I2C 1 #endif // Multiple Z steppers diff --git a/Marlin/src/inc/Conditionals_post.h b/Marlin/src/inc/Conditionals_post.h index 71ae4bb285..b8614dd8b0 100644 --- a/Marlin/src/inc/Conditionals_post.h +++ b/Marlin/src/inc/Conditionals_post.h @@ -1975,7 +1975,7 @@ #define HAS_STEPPER_RESET 1 #endif #if PIN_EXISTS(DIGIPOTSS) - #define HAS_DIGIPOTSS 1 + #define HAS_MOTOR_CURRENT_SPI 1 #endif #if ANY_PIN(MOTOR_CURRENT_PWM_X, MOTOR_CURRENT_PWM_Y, MOTOR_CURRENT_PWM_XY, MOTOR_CURRENT_PWM_Z, MOTOR_CURRENT_PWM_E) #define HAS_MOTOR_CURRENT_PWM 1 diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index e4bee7c813..18f53eaebe 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -2612,7 +2612,7 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal /** * Digipot requirement */ -#if HAS_I2C_DIGIPOT +#if HAS_MOTOR_CURRENT_I2C #if BOTH(DIGIPOT_MCP4018, DIGIPOT_MCP4451) #error "Enable only one of DIGIPOT_MCP4018 or DIGIPOT_MCP4451." #elif !MB(MKS_SBASE) \ @@ -2734,7 +2734,7 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #endif #if ENABLED(MECHANICAL_GANTRY_CALIBRATION) - #if NONE(DAC_STEPPER_CURRENT, HAS_DIGIPOTSS, DAC_STEPPER_CURRENT, HAS_TRINAMIC_CONFIG, HAS_MOTOR_CURRENT_PWM) + #if NONE(HAS_MOTOR_CURRENT_DAC, HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC, HAS_TRINAMIC_CONFIG, HAS_MOTOR_CURRENT_PWM) #error "It is highly reccomended to have adjustable current drivers to prevent damage. Disable this line to continue anyway." #else #ifndef GANTRY_CALIBRATION_CURRENT diff --git a/Marlin/src/lcd/menu/menu_advanced.cpp b/Marlin/src/lcd/menu/menu_advanced.cpp index 9978bc7eab..26d261f2f5 100644 --- a/Marlin/src/lcd/menu/menu_advanced.cpp +++ b/Marlin/src/lcd/menu/menu_advanced.cpp @@ -58,16 +58,16 @@ void menu_tmc(); void menu_backlash(); -#if ENABLED(DAC_STEPPER_CURRENT) +#if ENABLED(HAS_MOTOR_CURRENT_DAC) #include "../../feature/dac/stepper_dac.h" void menu_dac() { static xyze_uint8_t driverPercent; - LOOP_XYZE(i) driverPercent[i] = dac_current_get_percent((AxisEnum)i); + LOOP_XYZE(i) driverPercent[i] = stepper_dac.get_current_percent((AxisEnum)i); START_MENU(); BACK_ITEM(MSG_ADVANCED_SETTINGS); - #define EDIT_DAC_PERCENT(A) EDIT_ITEM(uint8, MSG_DAC_PERCENT_##A, &driverPercent[_AXIS(A)], 0, 100, []{ dac_current_set_percents(driverPercent); }) + #define EDIT_DAC_PERCENT(A) EDIT_ITEM(uint8, MSG_DAC_PERCENT_##A, &driverPercent[_AXIS(A)], 0, 100, []{ stepper_dac.set_current_percents(driverPercent); }) EDIT_DAC_PERCENT(X); EDIT_DAC_PERCENT(Y); EDIT_DAC_PERCENT(Z); @@ -563,7 +563,7 @@ void menu_advanced_settings() { SUBMENU(MSG_BACKLASH, menu_backlash); #endif - #if ENABLED(DAC_STEPPER_CURRENT) + #if ENABLED(HAS_MOTOR_CURRENT_DAC) SUBMENU(MSG_DRIVE_STRENGTH, menu_dac); #endif #if HAS_MOTOR_CURRENT_PWM diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index 2273a38527..35aed6764d 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -366,7 +366,7 @@ typedef struct SettingsDataStruct { // // HAS_MOTOR_CURRENT_PWM // - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI uint32_t motor_current_setting[COUNT(stepper.motor_current_setting)]; #else uint32_t motor_current_setting[3]; // M907 X Z E @@ -1282,7 +1282,7 @@ void MarlinSettings::postprocess() { { _FIELD_TEST(motor_current_setting); - #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI EEPROM_WRITE(stepper.motor_current_setting); #else const uint32_t no_current[3] = { 0 }; @@ -2115,7 +2115,7 @@ void MarlinSettings::postprocess() { // { _FIELD_TEST(motor_current_setting); - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI uint32_t motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; #else uint32_t motor_current_setting[3]; @@ -2123,7 +2123,7 @@ void MarlinSettings::postprocess() { DEBUG_ECHOLNPGM("DIGIPOTS Loading"); EEPROM_READ(motor_current_setting); DEBUG_ECHOLNPGM("DIGIPOTS Loaded"); - #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI if (!validating) COPY(stepper.motor_current_setting, motor_current_setting); #endif @@ -2803,17 +2803,17 @@ void MarlinSettings::reset() { #if HAS_MOTOR_CURRENT_PWM constexpr uint32_t tmp_motor_current_setting[3] = PWM_MOTOR_CURRENT; LOOP_L_N(q, 3) - stepper.digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q])); + stepper.set_digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q])); #endif // // DIGIPOTS // - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI static constexpr uint32_t tmp_motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; DEBUG_ECHOLNPGM("Writing Digipot"); LOOP_L_N(q, COUNT(tmp_motor_current_setting)) - stepper.digipot_current(q, tmp_motor_current_setting[q]); + stepper.set_digipot_current(q, tmp_motor_current_setting[q]); DEBUG_ECHOLNPGM("Digipot Written"); #endif @@ -3716,7 +3716,7 @@ void MarlinSettings::reset() { #endif #endif - #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI CONFIG_ECHO_HEADING("Stepper motor currents:"); CONFIG_ECHO_START(); #if HAS_MOTOR_CURRENT_PWM @@ -3725,7 +3725,7 @@ void MarlinSettings::reset() { , SP_Z_STR, stepper.motor_current_setting[1] , SP_E_STR, stepper.motor_current_setting[2] ); - #elif HAS_DIGIPOTSS + #elif HAS_MOTOR_CURRENT_SPI SERIAL_ECHOPGM(" M907"); LOOP_L_N(q, COUNT(stepper.motor_current_setting)) { SERIAL_CHAR(' '); diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index 0e59549b81..bff91d92f8 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -83,10 +83,6 @@ Stepper stepper; // Singleton #define BABYSTEPPING_EXTRA_DIR_WAIT -#if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS - bool Stepper::initialized; // = false -#endif - #ifdef __AVR__ #include "speed_lookuptable.h" #endif @@ -110,7 +106,7 @@ Stepper stepper; // Singleton #include "../feature/dac/dac_dac084s085.h" #endif -#if HAS_DIGIPOTSS +#if HAS_MOTOR_CURRENT_SPI #include #endif @@ -142,11 +138,12 @@ Stepper stepper; // Singleton bool Stepper::separate_multi_axis = false; #endif -#if HAS_MOTOR_CURRENT_PWM - uint32_t Stepper::motor_current_setting[3]; // Initialized by settings.load() -#elif HAS_DIGIPOTSS - constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT; - uint32_t Stepper::motor_current_setting[COUNT(digipot_count)]; // Initialized by settings.load() +#if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + bool Stepper::initialized; // = false + uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load() + #if HAS_MOTOR_CURRENT_SPI + constexpr uint32_t Stepper::digipot_count[]; + #endif #endif // private: @@ -2593,7 +2590,7 @@ void Stepper::init() { set_directions(); - #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM initialized = true; digipot_init(); #endif @@ -2933,10 +2930,10 @@ void Stepper::report_positions() { * Software-controlled Stepper Motor Current */ -#if HAS_DIGIPOTSS +#if HAS_MOTOR_CURRENT_SPI // From Arduino DigitalPotControl example - void Stepper::digitalPotWrite(const int16_t address, const int16_t value) { + void Stepper::set_digipot_value_spi(const int16_t address, const int16_t value) { WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip SPI.transfer(address); // Send the address and value via SPI SPI.transfer(value); @@ -2944,7 +2941,7 @@ void Stepper::report_positions() { //delay(10); } -#endif // HAS_DIGIPOTSS +#endif // HAS_MOTOR_CURRENT_SPI #if HAS_MOTOR_CURRENT_PWM @@ -2961,7 +2958,7 @@ void Stepper::report_positions() { #if ANY_PIN(MOTOR_CURRENT_PWM_E, MOTOR_CURRENT_PWM_E0, MOTOR_CURRENT_PWM_E1) case 2: #endif - digipot_current(i, motor_current_setting[i]); + set_digipot_current(i, motor_current_setting[i]); default: break; } } @@ -2971,23 +2968,23 @@ void Stepper::report_positions() { #if !MB(PRINTRBOARD_G2) - #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM - void Stepper::digipot_current(const uint8_t driver, const int16_t current) { + void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) { if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1)) motor_current_setting[driver] = current; // update motor_current_setting if (!initialized) return; - #if HAS_DIGIPOTSS - SERIAL_ECHOLNPAIR("Digipotss current ", current); + #if HAS_MOTOR_CURRENT_SPI + + //SERIAL_ECHOLNPAIR("Digipotss current ", current); + const uint8_t digipot_ch[] = DIGIPOT_CHANNELS; - digitalPotWrite(digipot_ch[driver], current); + set_digipot_value_spi(digipot_ch[driver], current); #elif HAS_MOTOR_CURRENT_PWM - - #define _WRITE_CURRENT_PWM(P) analogWrite(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE)) switch (driver) { case 0: @@ -3023,15 +3020,13 @@ void Stepper::report_positions() { void Stepper::digipot_init() { - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI SPI.begin(); SET_OUTPUT(DIGIPOTSS_PIN); - LOOP_L_N(i, COUNT(motor_current_setting)) { - //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]); - digipot_current(i, motor_current_setting[i]); - } + LOOP_L_N(i, COUNT(motor_current_setting)) + set_digipot_current(i, motor_current_setting[i]); #elif HAS_MOTOR_CURRENT_PWM diff --git a/Marlin/src/module/stepper.h b/Marlin/src/module/stepper.h index 51f7c489d7..7e64a1447d 100644 --- a/Marlin/src/module/stepper.h +++ b/Marlin/src/module/stepper.h @@ -245,16 +245,18 @@ class Stepper { static bool separate_multi_axis; #endif - #if HAS_MOTOR_CURRENT_PWM - #ifndef PWM_MOTOR_CURRENT - #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT + #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_PWM + #ifndef PWM_MOTOR_CURRENT + #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT + #endif + #define MOTOR_CURRENT_COUNT 3 + #elif HAS_MOTOR_CURRENT_SPI + static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT; + #define MOTOR_CURRENT_COUNT COUNT(Stepper::digipot_count) #endif static bool initialized; - static uint32_t motor_current_setting[3]; - #elif HAS_DIGIPOTSS - static bool initialized; - static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT; - static uint32_t motor_current_setting[COUNT(digipot_count)]; // Initialized by settings.load() + static uint32_t motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load() #endif private: @@ -466,9 +468,9 @@ class Stepper { // Triggered position of an axis in steps static int32_t triggered_position(const AxisEnum axis); - #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM - static void digitalPotWrite(const int16_t address, const int16_t value); - static void digipot_current(const uint8_t driver, const int16_t current); + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM + static void set_digipot_value_spi(const int16_t address, const int16_t value); + static void set_digipot_current(const uint8_t driver, const int16_t current); #endif #if HAS_MICROSTEPS @@ -591,7 +593,7 @@ class Stepper { static int32_t _eval_bezier_curve(const uint32_t curr_step); #endif - #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI static void digipot_init(); #endif diff --git a/Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h b/Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h index 406cbec816..1428de31a2 100644 --- a/Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h +++ b/Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h @@ -34,7 +34,7 @@ // // I2C based DAC like on the Printrboard REVF -#define DAC_STEPPER_CURRENT +#define HAS_MOTOR_CURRENT_DAC // Channels available for DAC, For Rigidboard there are 4 #define DAC_STEPPER_ORDER { 0, 1, 2, 3 } diff --git a/Marlin/src/pins/teensy2/pins_PRINTRBOARD_REVF.h b/Marlin/src/pins/teensy2/pins_PRINTRBOARD_REVF.h index 83b17b6560..c628fd6dc2 100644 --- a/Marlin/src/pins/teensy2/pins_PRINTRBOARD_REVF.h +++ b/Marlin/src/pins/teensy2/pins_PRINTRBOARD_REVF.h @@ -143,7 +143,7 @@ #endif // NO_EXTRUDRBOARD // Enable control of stepper motor currents with the I2C based MCP4728 DAC used on Printrboard REVF -#define DAC_STEPPER_CURRENT +#define HAS_MOTOR_CURRENT_DAC // Set default drive strength percents if not already defined - X, Y, Z, E axis #ifndef DAC_MOTOR_CURRENT_DEFAULT diff --git a/platformio.ini b/platformio.ini index d0a4cb7b10..f5c63e097e 100644 --- a/platformio.ini +++ b/platformio.ini @@ -199,7 +199,7 @@ HAS_TRINAMIC_CONFIG = TMCStepper@~0.7.1 src_filter=+ + + + + HAS_STEALTHCHOP = src_filter=+ SR_LCD_3W_NL = SailfishLCD=https://github.com/mikeshub/SailfishLCD/archive/master.zip -HAS_I2C_DIGIPOT = SlowSoftI2CMaster +HAS_MOTOR_CURRENT_I2C = SlowSoftI2CMaster src_filter=+ HAS_TMC26X = TMC26XStepper=https://github.com/trinamic/TMC26XStepper/archive/master.zip src_filter=+ @@ -260,7 +260,7 @@ CANCEL_OBJECTS = src_filter=+ + + EXTERNAL_CLOSED_LOOP_CONTROLLER = src_filter=+ + USE_CONTROLLER_FAN = src_filter=+ -DAC_STEPPER_CURRENT = src_filter=+ +HAS_MOTOR_CURRENT_DAC = src_filter=+ DIRECT_STEPPING = src_filter=+ + EMERGENCY_PARSER = src_filter=+ - I2C_POSITION_ENCODERS = src_filter=+ From a3d0fe53cf0f3f1952d1f0c02eccbad0288ea2ef Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 12 Aug 2020 21:44:27 -0500 Subject: [PATCH 08/42] reorder --- Marlin/src/module/settings.cpp | 6 +++--- Marlin/src/module/stepper.cpp | 2 +- Marlin/src/module/stepper.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index 35aed6764d..37959d568d 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -1282,7 +1282,7 @@ void MarlinSettings::postprocess() { { _FIELD_TEST(motor_current_setting); - #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM EEPROM_WRITE(stepper.motor_current_setting); #else const uint32_t no_current[3] = { 0 }; @@ -2123,7 +2123,7 @@ void MarlinSettings::postprocess() { DEBUG_ECHOLNPGM("DIGIPOTS Loading"); EEPROM_READ(motor_current_setting); DEBUG_ECHOLNPGM("DIGIPOTS Loaded"); - #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM if (!validating) COPY(stepper.motor_current_setting, motor_current_setting); #endif @@ -3716,7 +3716,7 @@ void MarlinSettings::reset() { #endif #endif - #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM CONFIG_ECHO_HEADING("Stepper motor currents:"); CONFIG_ECHO_START(); #if HAS_MOTOR_CURRENT_PWM diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index bff91d92f8..7b00355134 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -138,7 +138,7 @@ Stepper stepper; // Singleton bool Stepper::separate_multi_axis = false; #endif -#if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI +#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM bool Stepper::initialized; // = false uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load() #if HAS_MOTOR_CURRENT_SPI diff --git a/Marlin/src/module/stepper.h b/Marlin/src/module/stepper.h index 7e64a1447d..6dc9bbdbe7 100644 --- a/Marlin/src/module/stepper.h +++ b/Marlin/src/module/stepper.h @@ -245,7 +245,7 @@ class Stepper { static bool separate_multi_axis; #endif - #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM #if HAS_MOTOR_CURRENT_PWM #ifndef PWM_MOTOR_CURRENT #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT @@ -593,7 +593,7 @@ class Stepper { static int32_t _eval_bezier_curve(const uint32_t curr_step); #endif - #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM static void digipot_init(); #endif From f11d4968992713955109496cd5a600ef691c2cb4 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 13 Aug 2020 01:33:53 -0500 Subject: [PATCH 09/42] Provide MSG_ITERATION --- Marlin/src/gcode/calibrate/G34_M422.cpp | 17 +++++++++-------- Marlin/src/lcd/language/language_en.h | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 70768350bb..878c44fd0e 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -161,7 +161,7 @@ void GcodeSuite::G34() { #if HAS_DISPLAY PGM_P const msg_iteration = GET_TEXT(MSG_ITERATION); - const iter_str_len = strlen_P(iter_str_len); + const uint8_t iter_str_len = strlen_P(iter_str_len); #endif // Final z and iteration values will be used after breaking the loop @@ -172,7 +172,7 @@ void GcodeSuite::G34() { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions."); const int iter = iteration + 1; - SERIAL_ECHOLNPAIR("\nIteration: ", iter); + SERIAL_ECHOLNPAIR("\nG34 Iteration: ", iter); #if HAS_DISPLAY char str[iter_str_len + 2 + 1]; sprintf_P(str, msg_iteration, iter); @@ -260,16 +260,17 @@ void GcodeSuite::G34() { #endif ); #if HAS_DISPLAY - char msg[30], fstr[16]; + char msg[30], fstr1[16], fstr2[16], fstr3[16]; sprintf_P( - PSTR("DIFFERENCE Z1-Z2=%s" + PSTR("Diffs Z1-Z2=%s" #if NUM_Z_STEPPER_DRIVERS == 3 - " Z2-Z3=%s Z3-Z1=%s" + " Z2-Z3=%s" + " Z3-Z1=%s" #endif - ), dtostrf(ABS(z_measured[0] - z_measured[1]), 1, 3, fstr) + ), dtostrf(ABS(z_measured[0] - z_measured[1]), 1, 3, fstr1) #if NUM_Z_STEPPER_DRIVERS == 3 - , dtostrf(ABS(z_measured[1] - z_measured[2]), 1, 3, fstr) - , dtostrf(ABS(z_measured[2] - z_measured[0]), 1, 3, fstr) + , dtostrf(ABS(z_measured[1] - z_measured[2]), 1, 3, fstr2) + , dtostrf(ABS(z_measured[2] - z_measured[0]), 1, 3, fstr3) #endif ); ui.set_status(msg); diff --git a/Marlin/src/lcd/language/language_en.h b/Marlin/src/lcd/language/language_en.h index 2d8fe55fba..1a92ed10be 100644 --- a/Marlin/src/lcd/language/language_en.h +++ b/Marlin/src/lcd/language/language_en.h @@ -68,6 +68,7 @@ namespace Language_en { PROGMEM Language_Str MSG_AUTO_HOME_Z = _UxGT("Home Z"); PROGMEM Language_Str MSG_AUTO_Z_ALIGN = _UxGT("Auto Z-Align"); PROGMEM Language_Str MSG_ASSISTED_TRAMMING = _UxGT("Assisted Tramming"); + PROGMEM Language_Str MSG_ITERATION = _UxGT("G34 Iteration: %i"); PROGMEM Language_Str MSG_DECREASING_ACCURACY = _UxGT("Accuracy Decreasing!"); PROGMEM Language_Str MSG_ACCURACY_ACHIEVED = _UxGT("Accuracy Achieved"); PROGMEM Language_Str MSG_LEVEL_BED_HOMING = _UxGT("Homing XYZ"); From ae208fa92fa954b901d8c4982bff6fd90e6300bf Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 13 Aug 2020 03:39:59 -0500 Subject: [PATCH 10/42] Clean up G34/M422 --- Marlin/src/gcode/calibrate/G34_M422.cpp | 102 ++++++++++++------------ 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 878c44fd0e..8d1792bace 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -20,26 +20,23 @@ * */ -#include "../../inc/MarlinConfig.h" +#include "../../inc/MarlinConfigPre.h" + +#if ENABLED(Z_STEPPER_AUTO_ALIGN) + +#include "../../feature/z_stepper_align.h" #include "../gcode.h" #include "../../module/planner.h" #include "../../module/stepper.h" #include "../../module/motion.h" #include "../../module/probe.h" -#include "../../module/endstops.h" +#include "../../lcd/ultralcd.h" // for LCD_MESSAGEPGM #if HAS_LEVELING #include "../../feature/bedlevel/bedlevel.h" #endif -#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) -#include "../../core/debug_out.h" - -#if ENABLED(Z_STEPPER_AUTO_ALIGN) - -#include "../../feature/z_stepper_align.h" - #if HAS_MULTI_HOTEND #include "../../module/tool_change.h" #endif @@ -48,6 +45,9 @@ #include "../../libs/least_squares_fit.h" #endif +#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) +#include "../../core/debug_out.h" + /** * G34: Z-Stepper automatic alignment * @@ -503,6 +503,9 @@ void GcodeSuite::M422() { } #elif ENABLED(MECHANICAL_GANTRY_CALIBRATION) + + #include "../../module/endstops.h" + void GcodeSuite::G34() { if (axis_unhomed_error()) return; @@ -513,35 +516,43 @@ void GcodeSuite::M422() { #ifdef GANTRY_CALIBRATION_COMMANDS_PRE gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_PRE)); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Sub Commands Processed"); #endif - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Sub Commands Processed"); - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Parking XY"); - // Move XY to safe position #ifdef GANTRY_CALIBRATION_SAFE_POSITION - xy_pos_t safe_pos = GANTRY_CALIBRATION_SAFE_POSITION; - current_position[X_AXIS] = safe_pos[X_AXIS]; - current_position[Y_AXIS] = safe_pos[Y_AXIS]; - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE)); - planner.synchronize(); + // Move XY to safe position + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Parking XY"); + const xy_pos_t safe_pos = GANTRY_CALIBRATION_SAFE_POSITION; + do_blocking_move_to(safe_pos, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE)); #endif - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Setting Z Pounce"); - const uint16_t move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT); + const uint16_t move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT), + zpounce = ( + #if GANTRY_CALIBRATION_DIRECTION == 1 + (Z_MAX_POS) - move_distance + #else + (Z_MIN_POS) + move_distance + #endif + ), + zgrind = ( + #if GANTRY_CALIBRATION_DIRECTION == 1 + (Z_MAX_POS) + move_distance + #else + (Z_MIN_POS) - move_distance + #endif + ); // Move Z to pounce position - #if GANTRY_CALIBRATION_DIRECTION == 1 - current_position[Z_AXIS] = (Z_MAX_POS - move_distance); - #else - current_position[Z_AXIS] = (Z_MIN_POS + move_distance); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Setting Z Pounce"); + do_blocking_move_to_z(zpounce, MMM_TO_MMS(HOMING_FEEDRATE_Z)); + + // Store current motor settings, then apply reduced value + + #define _REDUCE_CURRENT ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_DAC, HAS_MOTOR_CURRENT_I2C, HAS_TRINAMIC_CONFIG) + #if _REDUCE_CURRENT + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Reducing Current"); #endif - do_blocking_move_to(current_position, MMM_TO_MMS(HOMING_FEEDRATE_Z)); - planner.synchronize(); - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Reducing Current"); - // Store current motor settings, then apply reduced value #if HAS_MOTOR_CURRENT_SPI const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS]; @@ -579,32 +590,20 @@ void GcodeSuite::M422() { #endif #endif - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Final Z Move"); // Do Final Z move to adjust - #if GANTRY_CALIBRATION_DIRECTION == 1 - current_position[Z_AXIS] = (Z_MAX_POS + move_distance); - #else - current_position[Z_AXIS] = (Z_MIN_POS - move_distance); - #endif + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Final Z Move"); + do_blocking_move_to_z(zgrind, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); - planner.synchronize(); - - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Z Backoff"); // Back off end plate, back to normal motion range - #if GANTRY_CALIBRATION_DIRECTION == 1 - current_position[Z_AXIS] = (Z_MAX_POS - move_distance); - #else - current_position[Z_AXIS] = (Z_MIN_POS + move_distance); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Z Backoff"); + do_blocking_move_to_z(zpounce, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); + + // Reset current to original values + + #if _REDUCE_CURRENT + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Restore Current"); #endif - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); - planner.synchronize(); - - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Restore Current"); - // Reset current to original values #if HAS_MOTOR_CURRENT_SPI stepper.set_digipot_current(Z_AXIS, previous_current); #elif HAS_MOTOR_CURRENT_PWM @@ -628,9 +627,8 @@ void GcodeSuite::M422() { #endif #endif - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Running Post Commands"); #ifdef GANTRY_CALIBRATION_COMMANDS_POST + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Running Post Commands"); gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_POST)); #endif } From f6a1d772c2faf4a163bceec1439d1593cd389750 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 13 Aug 2020 17:55:49 -0500 Subject: [PATCH 11/42] helpful patch --- Marlin/src/gcode/calibrate/G34_M422.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 8d1792bace..07e2bee488 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -260,7 +260,10 @@ void GcodeSuite::G34() { #endif ); #if HAS_DISPLAY - char msg[30], fstr1[16], fstr2[16], fstr3[16]; + char msg[30], fstr1[16]; + #if NUM_Z_STEPPER_DRIVERS == 3 + char fstr2[16], fstr3[16]; + #endif sprintf_P( PSTR("Diffs Z1-Z2=%s" #if NUM_Z_STEPPER_DRIVERS == 3 @@ -294,7 +297,7 @@ void GcodeSuite::G34() { // If it's getting worse, stop and throw an error if (last_z_align_level_indicator < z_align_level_indicator * 0.7f) { SERIAL_ECHOLNPGM("Decreasing Accuracy Detected."); - LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY)); + LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY); err_break = true; break; } @@ -319,7 +322,7 @@ void GcodeSuite::G34() { // Check for less accuracy compared to last move if (last_z_align_move[zstepper] < z_align_abs * 0.7f) { SERIAL_ECHOLNPGM("Decreasing Accuracy Detected."); - LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY)); + LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " last_z_align_move = ", last_z_align_move[zstepper]); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " z_align_abs = ", z_align_abs); adjustment_reverse = !adjustment_reverse; From b4dbf606b7eb52f77d28f54991e174e81db1929e Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 13 Aug 2020 20:27:55 -0500 Subject: [PATCH 12/42] Fix sprintf_P call --- Marlin/src/gcode/calibrate/G34_M422.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 07e2bee488..eeb18a9ca8 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -260,11 +260,13 @@ void GcodeSuite::G34() { #endif ); #if HAS_DISPLAY - char msg[30], fstr1[16]; - #if NUM_Z_STEPPER_DRIVERS == 3 - char fstr2[16], fstr3[16]; + char fstr1[10]; + #if NUM_Z_STEPPER_DRIVERS == 2 + char msg[6 + (6 + 5) * 1 + 1]; + #else + char msg[6 + (6 + 5) * 3 + 1], fstr2[10], fstr3[10]; #endif - sprintf_P( + sprintf_P(msg, PSTR("Diffs Z1-Z2=%s" #if NUM_Z_STEPPER_DRIVERS == 3 " Z2-Z3=%s" From d4ab268bbff1ec1395b13e96ca66ac52683c55f5 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 13 Aug 2020 20:43:13 -0500 Subject: [PATCH 13/42] =?UTF-8?q?=E2=80=A6and=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Marlin/src/gcode/calibrate/G34_M422.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index eeb18a9ca8..e0b7965a2f 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -161,7 +161,7 @@ void GcodeSuite::G34() { #if HAS_DISPLAY PGM_P const msg_iteration = GET_TEXT(MSG_ITERATION); - const uint8_t iter_str_len = strlen_P(iter_str_len); + const uint8_t iter_str_len = strlen_P(msg_iteration); #endif // Final z and iteration values will be used after breaking the loop From f7c56bc5e4f5ba33d909c726cf491328c007ba95 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Thu, 17 Sep 2020 11:16:01 -0400 Subject: [PATCH 14/42] Fix compile errors --- Marlin/src/gcode/calibrate/G34_M422.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index e0b7965a2f..c9645c0d32 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -22,10 +22,6 @@ #include "../../inc/MarlinConfigPre.h" -#if ENABLED(Z_STEPPER_AUTO_ALIGN) - -#include "../../feature/z_stepper_align.h" - #include "../gcode.h" #include "../../module/planner.h" #include "../../module/stepper.h" @@ -41,13 +37,20 @@ #include "../../module/tool_change.h" #endif -#if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) - #include "../../libs/least_squares_fit.h" -#endif + #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) #include "../../core/debug_out.h" +#if ENABLED(Z_STEPPER_AUTO_ALIGN) + + +#include "../../feature/z_stepper_align.h" + +#if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) + #include "../../libs/least_squares_fit.h" +#endif + /** * G34: Z-Stepper automatic alignment * @@ -513,7 +516,7 @@ void GcodeSuite::M422() { void GcodeSuite::G34() { - if (axis_unhomed_error()) return; + if (homing_needed()) return; TEMPORARY_SOFT_ENDSTOP_STATE(false); TEMPORARY_BED_LEVELING_STATE(false); From 7255ca0bc6a8087f976c11943b87768212d37ab5 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Sat, 19 Sep 2020 00:02:06 -0400 Subject: [PATCH 15/42] Update Configuration_adv.h --- Marlin/Configuration_adv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 8343d5647e..7aad2393e9 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -3358,7 +3358,7 @@ //#define GANTRY_CALIBRATION_SAFE_POSITION {X_CENTER, Y_CENTER} // Safe position for nozzle //#define GANTRY_CALIBRATION_XY_PARK_FEEDRATE 3000 // XY Park Feedrate - MMM //#define GANTRY_CALIBRATION_COMMANDS_PRE "" - //#define GANTRY_CALIBRATION_COMMANDS_POST "G28" + //#define GANTRY_CALIBRATION_COMMANDS_POST "G28" // G28 is highly recommended here as position is likely no longer accurate. #endif /** From ffd7062e219a5e19a1d4e630cfb6c1fb87f5ad56 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Sat, 19 Sep 2020 00:14:10 -0400 Subject: [PATCH 16/42] Prevent min move from going into overflow sending Z to 65k --- Marlin/src/gcode/calibrate/G34_M422.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index c9645c0d32..8e501326b6 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -534,7 +534,7 @@ void GcodeSuite::M422() { do_blocking_move_to(safe_pos, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE)); #endif - const uint16_t move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT), + const float move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT), zpounce = ( #if GANTRY_CALIBRATION_DIRECTION == 1 (Z_MAX_POS) - move_distance From e61bbe1499c5fa5f3ccf7fce9f9632acdc4966de Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Sat, 19 Sep 2020 00:27:23 -0400 Subject: [PATCH 17/42] Enable Post G28 by default as its usually necessary --- Marlin/Configuration_adv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 7aad2393e9..80d46a5b49 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -3358,7 +3358,7 @@ //#define GANTRY_CALIBRATION_SAFE_POSITION {X_CENTER, Y_CENTER} // Safe position for nozzle //#define GANTRY_CALIBRATION_XY_PARK_FEEDRATE 3000 // XY Park Feedrate - MMM //#define GANTRY_CALIBRATION_COMMANDS_PRE "" - //#define GANTRY_CALIBRATION_COMMANDS_POST "G28" // G28 is highly recommended here as position is likely no longer accurate. + #define GANTRY_CALIBRATION_COMMANDS_POST "G28" // G28 is highly recommended here as position is likely no longer accurate. #endif /** From e74b9776e5a2b7f1e85682d0c1889a46aab99642 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Wed, 22 Jul 2020 17:07:44 -0400 Subject: [PATCH 18/42] Add New G34 mode for current adjustable drivers TODO : Sanity checks, Finish DAC mode, Test on Rambo --- Marlin/Configuration_adv.h | 19 ++++ Marlin/src/gcode/calibrate/G34_M422.cpp | 145 +++++++++++++++++++++++- Marlin/src/lcd/language/language_en.h | 1 + 3 files changed, 161 insertions(+), 4 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 65cbd21a94..9d37f2daf7 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -3344,6 +3344,25 @@ //#define JOYSTICK_DEBUG #endif +/** + * Modern replacement for the Prusa TMC_Z_CALIBRATION + * Adds capability to work with any adjustable current drivers + * Implements as G34 as M915 is deprecated + */ + +//#define MECHANICAL_GANTRY_CALIBRATION +#if ENABLED(MECHANICAL_GANTRY_CALIBRATION) + #define GANTRY_CALIBRATION_CURRENT 600 // Default calibration current in ma + #define GANTRY_CALIBRATION_EXTRA_HEIGHT 15 // Extra distance in mm past Z_###_POS to move + #define GANTRY_CALIBRATION_DIRECTION 1 // Set to 1 for Max or 0 for min + #define GANTRY_CALIBRATION_FEEDRATE 500 // Feedrate for correction move + + //#define GANTRY_CALIBRATION_SAFE_POSITION {X_CENTER, Y_CENTER} // Safe position for nozzle + //#define GANTRY_CALIBRATION_XY_PARK_FEEDRATE 3000 // XY Park Feedrate - MMM + //#define GANTRY_CALIBRATION_COMMANDS_PRE "" + //#define GANTRY_CALIBRATION_COMMANDS_POST "G28" +#endif + /** * MAX7219 Debug Matrix * diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 1fd0754ec7..68b5b1b7b0 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -169,6 +169,7 @@ void GcodeSuite::G34() { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions."); SERIAL_ECHOLNPAIR("\nITERATION: ", int(iteration + 1)); + ui.set_status("\nITERATION: ", int(iteration + 1)); // Initialize minimum value z_measured_min = 100000.0f; @@ -190,7 +191,8 @@ void GcodeSuite::G34() { // current_position.z has been manually altered in the "dirty trick" above. const float z_probed_height = probe.probe_at_point(z_stepper_align.xy[iprobe], raise_after, 0, true, false); if (isnan(z_probed_height)) { - SERIAL_ECHOLNPGM("Probing failed."); + SERIAL_ECHOLNPGM(MSG_LCD_PROBING_FAILED); + ui.set_status_P(MSG_LCD_PROBING_FAILED) err_break = true; break; } @@ -249,6 +251,13 @@ void GcodeSuite::G34() { , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) #endif ); + ui.set_status("\n" + "DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1]) + #if NUM_Z_STEPPER_DRIVERS == 3 + , " Z2-Z3=", ABS(z_measured[1] - z_measured[2]) + , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) + #endif + ); #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) // Check if the applied corrections go in the correct direction. @@ -267,7 +276,8 @@ void GcodeSuite::G34() { // If it's getting worse, stop and throw an error if (last_z_align_level_indicator < z_align_level_indicator * 0.7f) { - SERIAL_ECHOLNPGM("Decreasing accuracy detected."); + SERIAL_ECHOLNPGM(DECREASING_ACCURACY); + ui.set_status_P(DECREASING_ACCURACY); err_break = true; break; } @@ -291,7 +301,8 @@ void GcodeSuite::G34() { // Check for less accuracy compared to last move if (last_z_align_move[zstepper] < z_align_abs * 0.7f) { - SERIAL_ECHOLNPGM("Decreasing accuracy detected."); + SERIAL_ECHOLNPGM(DECREASING_ACCURACY); + ui.set_status_P(DECREASING_ACCURACY); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " last_z_align_move = ", last_z_align_move[zstepper]); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " z_align_abs = ", z_align_abs); adjustment_reverse = !adjustment_reverse; @@ -329,7 +340,11 @@ void GcodeSuite::G34() { if (err_break) break; - if (success_break) { SERIAL_ECHOLNPGM("Target accuracy achieved."); break; } + if (success_break) { + SERIAL_ECHOLNPGM("Target accuracy achieved."); + ui.set_status_P(PGM_P("Target accuracy achieved.")); + break; + } } // for (iteration) @@ -469,4 +484,126 @@ void GcodeSuite::M422() { pos_dest[position_index] = pos; } +#elif ENABLED(MECHANICAL_GANTRY_CALIBRATION) + void GcodeSuite::G34() { + + if (axis_unhomed_error()) return; + + TEMPORARY_SOFT_ENDSTOP_STATE(false); + TEMPORARY_BED_LEVELING_STATE(false); + + #ifdef GANTRY_CALIBRATION_COMMANDS_PRE + gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_PRE)); + #endif + + // Store current motor settings, then apply reduced value + #if DAC_STEPPER_CURRENT + const float target_current = parserfloatval('S', GANTRY_CALIBRATION_CURRENT); + const float previous_current = dac_amps(Z_AXIS, target_current); + else + const int16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); + #endif + + #if HAS_DIGIPOTSS + const uint32_t previous_current = motor_current_setting[Z_AXIS]; + stepper.digipot_current(Z_AXIS, target_current); + #elif HAS_MOTOR_CURRENT_PWM + const uint32_t previous_current = motor_current_setting[Z_AXIS]; + stepper.digipot_current(1, target_current); + #if DAC_STEPPER_CURRENT + dac_current_raw(Z_AXIS, target_current); + #elif ENABLED(HAS_I2C_DIGIPOT) + previous_current = dac_amps(Z_AXIS); + digipot_i2c_set_current(Z_AXIS, target_current) + #elif HAS_TRINAMIC_CONFIG + static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; + #if AXIS_IS_TMC(Z) + previous_current_arr[0] = stepperZ.getMilliamps(); + stepperZ.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z2) + previous_current_arr[1] = stepperZ2.getMilliamps(); + stepperZ2.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z3) + previous_current_arr[2] = stepperZ3.getMilliamps(); + stepperZ3.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z4) + previous_current_arr[3] = stepperZ4.getMilliamps(); + stepperZ4.rms_current(target_current); + #endif + #endif + + // Move XY to safe position + #ifdef GANTRY_CALIBRATION_SAFE_POSITION + xy_pos_t safe_pos = GANTRY_CALIBRATION_SAFE_POSITION; + current_position[X_AXIS] = safe_pos[X_AXIS]; + current_position[Y_AXIS] = safe_pos[Y_AXIS]; + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE); + planner.synchronize(); + #endif + + const uint16_t move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT); + + // Move Z to pounce position + #if GANTRY_CALIBRATION_DIRECTION == 1 + current_position[Z_AXIS] = (Z_MAX_POS - move_distance); + #else + current_position[Z_AXIS] = (Z_MIN_POS + move_distance); + #endif + + do_blocking_move_to(current_position, MMM_TO_MMS(HOMING_FEEDRATE_Z); + planner.synchronize(); + + // Do Final Z move to adjust + #if GANTRY_CALIBRATION_DIRECTION == 1 + current_position[Z_AXIS] = (Z_MAX_POS + move_distance); + #else + current_position[Z_AXIS] = (Z_MIN_POS - move_distance); + #endif + + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE); + planner.synchronize(); + + // Back off end plate, back to normal motion range + #if GANTRY_CALIBRATION_DIRECTION == 1 + current_position[Z_AXIS] = (Z_MAX_POS - move_distance); + #else + current_position[Z_AXIS] = (Z_MIN_POS + move_distance); + #endif + + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE); + planner.synchronize(); + + // Reset current to original values + #if HAS_DIGIPOTSS + stepper.digipot_current(Z_AXIS, previous_current); + #elif HAS_MOTOR_CURRENT_PWM + stepper.digipot_current(1, previous_current); + #if DAC_STEPPER_CURRENT + dac_current_raw(Z_AXIS, previous_current); + #elif ENABLED(HAS_I2C_DIGIPOT) + digipot_i2c_set_current(Z_AXIS, previous_current) + #elif HAS_TRINAMIC_CONFIG + static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; + #if AXIS_IS_TMC(Z) + stepperZ.rms_current(previous_current_arr[0]); + #endif + #if AXIS_IS_TMC(Z2) + stepperZ2.rms_current(previous_current_arr[1]); + #endif + #if AXIS_IS_TMC(Z3) + stepperZ3.rms_current(previous_current_arr[2]); + #endif + #if AXIS_IS_TMC(Z4) + stepperZ4.rms_current(previous_current_arr[3]); + #endif + #endif + + #ifdef GANTRY_CALIBRATION_COMMANDS_POST + gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_POST)); + #endif + } + #endif // Z_STEPPER_AUTO_ALIGN diff --git a/Marlin/src/lcd/language/language_en.h b/Marlin/src/lcd/language/language_en.h index 04c58fd22a..a3a1d476f0 100644 --- a/Marlin/src/lcd/language/language_en.h +++ b/Marlin/src/lcd/language/language_en.h @@ -68,6 +68,7 @@ namespace Language_en { PROGMEM Language_Str MSG_AUTO_HOME_Z = _UxGT("Home Z"); PROGMEM Language_Str MSG_AUTO_Z_ALIGN = _UxGT("Auto Z-Align"); PROGMEM Language_Str MSG_ASSISTED_TRAMMING = _UxGT("Assisted Tramming"); + PROGMEM Language_Str DECREASING_ACCURACY = _UxGT("Decreasing accuracy detected."); PROGMEM Language_Str MSG_LEVEL_BED_HOMING = _UxGT("Homing XYZ"); PROGMEM Language_Str MSG_LEVEL_BED_WAITING = _UxGT("Click to Begin"); PROGMEM Language_Str MSG_LEVEL_BED_NEXT_POINT = _UxGT("Next Point"); From ccb2c3bb852ac882cd27afa6b23b66ed1bf463c5 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Fri, 24 Jul 2020 16:09:19 -0400 Subject: [PATCH 19/42] Sanity Checks, gcode calls --- Marlin/src/gcode/gcode.h | 5 +++-- Marlin/src/inc/SanityCheck.h | 24 +++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/Marlin/src/gcode/gcode.h b/Marlin/src/gcode/gcode.h index 085f6b3618..1195950c92 100644 --- a/Marlin/src/gcode/gcode.h +++ b/Marlin/src/gcode/gcode.h @@ -463,11 +463,12 @@ private: TERN_(DELTA_AUTO_CALIBRATION, static void G33()); - #if ENABLED(Z_STEPPER_AUTO_ALIGN) + #if ANY(Z_STEPPER_AUTO_ALIGN, MECHANICAL_GANTRY_CALIBRATION) static void G34(); - static void M422(); #endif + TERN_(Z_STEPPER_AUTO_ALIGN, static void M422()); + TERN_(ASSISTED_TRAMMING, static void G35()); TERN_(G38_PROBE_TARGET, static void G38(const int8_t subcode)); diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index 8d68b33ece..e1c7141dcf 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -422,7 +422,7 @@ #elif defined(CHAMBER_HEATER_PIN) #error "CHAMBER_HEATER_PIN is now HEATER_CHAMBER_PIN. Please update your configuration and/or pins." #elif defined(TMC_Z_CALIBRATION) - #error "TMC_Z_CALIBRATION has been deprecated in favor of Z_STEPPER_AUTO_ALIGN. Please update your configuration." + #error "TMC_Z_CALIBRATION has been deprecated in favor of MECHANICAL_GANTRY_CALIBRATION. Please update your configuration." #elif defined(Z_MIN_PROBE_ENDSTOP) #error "Z_MIN_PROBE_ENDSTOP is no longer required. Please remove it from Configuration.h." #elif defined(DUAL_NOZZLE_DUPLICATION_MODE) @@ -2733,6 +2733,28 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #endif #endif +#if ENABLED(MECHANICAL_GANTRY_CALIBRATION) + #if NONE(DAC_STEPPER_CURRENT, HAS_DIGIPOTSS, DAC_STEPPER_CURRENT, HAS_TRINAMIC_CONFIG) + #error "It is highly reccomended to have adjustable current drivers to prevent damage. Disable this line to continue anyway." + #else + #ifndef GANTRY_CALIBRATION_CURRENT + #error "MECHANICAL_GANTRY_CALIBRATION Requires GANTRY_CALIBRATION_CURRENT to be set." + #endif + #ifndef GANTRY_CALIBRATION_EXTRA_HEIGHT + #error "MECHANICAL_GANTRY_CALIBRATION Requires GANTRY_CALIBRATION_EXTRA_HEIGHT to be set." + #endif + #ifndef GANTRY_CALIBRATION_DIRECTION + #error "MECHANICAL_GANTRY_CALIBRATION Requires GANTRY_CALIBRATION_DIRECTION to be set." + #endif + #ifndef GANTRY_CALIBRATION_FEEDRATE + #error "MECHANICAL_GANTRY_CALIBRATION Requires GANTRY_CALIBRATION_FEEDRATE to be set." + #endif + #endif + #if defined(GANTRY_CALIBRATION_SAFE_POSITION) && !defined(GANTRY_CALIBRATION_XY_PARK_FEEDRATE) + #error "GANTRY_CALIBRATION_SAFE_POSITION Requires GANTRY_CALIBRATION_XY_PARK_FEEDRATE to be set." + #endif +#endif + #if ENABLED(PRINTCOUNTER) && DISABLED(EEPROM_SETTINGS) #error "PRINTCOUNTER requires EEPROM_SETTINGS. Please update your Configuration." #endif From 47c6f8a98be4460cfeda61ef83fa31a28d99f3b3 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Sun, 9 Aug 2020 16:43:08 -0400 Subject: [PATCH 20/42] DIGIPOTSS in motter current array with PWM, stored to eeprom, and G34 tested --- Marlin/src/gcode/calibrate/G34_M422.cpp | 143 +++++++++++++----------- Marlin/src/gcode/gcode.cpp | 2 +- Marlin/src/lcd/menu/menu_motion.cpp | 2 +- Marlin/src/module/settings.cpp | 37 +++++- Marlin/src/module/stepper.cpp | 22 ++-- Marlin/src/module/stepper.h | 8 +- 6 files changed, 130 insertions(+), 84 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 68b5b1b7b0..31555ff0bf 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -22,31 +22,32 @@ #include "../../inc/MarlinConfig.h" -#if ENABLED(Z_STEPPER_AUTO_ALIGN) - -#include "../../feature/z_stepper_align.h" - #include "../gcode.h" #include "../../module/planner.h" #include "../../module/stepper.h" #include "../../module/motion.h" #include "../../module/probe.h" - -#if HAS_MULTI_HOTEND - #include "../../module/tool_change.h" -#endif +#include "../../module/endstops.h" #if HAS_LEVELING #include "../../feature/bedlevel/bedlevel.h" #endif +#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) +#include "../../core/debug_out.h" + +#if ENABLED(Z_STEPPER_AUTO_ALIGN) + +#include "../../feature/z_stepper_align.h" + +#if HAS_MULTI_HOTEND + #include "../../module/tool_change.h" +#endif + #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) #include "../../libs/least_squares_fit.h" #endif -#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) -#include "../../core/debug_out.h" - /** * G34: Z-Stepper automatic alignment * @@ -491,59 +492,25 @@ void GcodeSuite::M422() { TEMPORARY_SOFT_ENDSTOP_STATE(false); TEMPORARY_BED_LEVELING_STATE(false); + TemporaryGlobalEndstopsState unlock_z(false); #ifdef GANTRY_CALIBRATION_COMMANDS_PRE gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_PRE)); #endif - // Store current motor settings, then apply reduced value - #if DAC_STEPPER_CURRENT - const float target_current = parserfloatval('S', GANTRY_CALIBRATION_CURRENT); - const float previous_current = dac_amps(Z_AXIS, target_current); - else - const int16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); - #endif - - #if HAS_DIGIPOTSS - const uint32_t previous_current = motor_current_setting[Z_AXIS]; - stepper.digipot_current(Z_AXIS, target_current); - #elif HAS_MOTOR_CURRENT_PWM - const uint32_t previous_current = motor_current_setting[Z_AXIS]; - stepper.digipot_current(1, target_current); - #if DAC_STEPPER_CURRENT - dac_current_raw(Z_AXIS, target_current); - #elif ENABLED(HAS_I2C_DIGIPOT) - previous_current = dac_amps(Z_AXIS); - digipot_i2c_set_current(Z_AXIS, target_current) - #elif HAS_TRINAMIC_CONFIG - static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; - #if AXIS_IS_TMC(Z) - previous_current_arr[0] = stepperZ.getMilliamps(); - stepperZ.rms_current(target_current); - #endif - #if AXIS_IS_TMC(Z2) - previous_current_arr[1] = stepperZ2.getMilliamps(); - stepperZ2.rms_current(target_current); - #endif - #if AXIS_IS_TMC(Z3) - previous_current_arr[2] = stepperZ3.getMilliamps(); - stepperZ3.rms_current(target_current); - #endif - #if AXIS_IS_TMC(Z4) - previous_current_arr[3] = stepperZ4.getMilliamps(); - stepperZ4.rms_current(target_current); - #endif - #endif + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Sub Commands Processed"); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Parking XY"); // Move XY to safe position #ifdef GANTRY_CALIBRATION_SAFE_POSITION xy_pos_t safe_pos = GANTRY_CALIBRATION_SAFE_POSITION; current_position[X_AXIS] = safe_pos[X_AXIS]; current_position[Y_AXIS] = safe_pos[Y_AXIS]; - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE); + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE)); planner.synchronize(); #endif + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Setting Z Pounce"); const uint16_t move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT); // Move Z to pounce position @@ -553,9 +520,49 @@ void GcodeSuite::M422() { current_position[Z_AXIS] = (Z_MIN_POS + move_distance); #endif - do_blocking_move_to(current_position, MMM_TO_MMS(HOMING_FEEDRATE_Z); + do_blocking_move_to(current_position, MMM_TO_MMS(HOMING_FEEDRATE_Z)); planner.synchronize(); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Reducing Current"); + // Store current motor settings, then apply reduced value + #if HAS_DIGIPOTSS + const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); + const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS]; + stepper.digipot_current(Z_AXIS, target_current); + #elif HAS_MOTOR_CURRENT_PWM + const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); + const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS]; + stepper.digipot_current(1, target_current); + #elif DAC_STEPPER_CURRENT + const float target_current = parser.floatval('S', GANTRY_CALIBRATION_CURRENT); + const float previous_current = dac_amps(Z_AXIS, target_current); + dac_current_raw(Z_AXIS, target_current); + #elif ENABLED(HAS_I2C_DIGIPOT) + const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); + previous_current = dac_amps(Z_AXIS); + digipot_i2c_set_current(Z_AXIS, target_current) + #elif HAS_TRINAMIC_CONFIG + const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); + static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; + #if AXIS_IS_TMC(Z) + previous_current_arr[0] = stepperZ.getMilliamps(); + stepperZ.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z2) + previous_current_arr[1] = stepperZ2.getMilliamps(); + stepperZ2.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z3) + previous_current_arr[2] = stepperZ3.getMilliamps(); + stepperZ3.rms_current(target_current); + #endif + #if AXIS_IS_TMC(Z4) + previous_current_arr[3] = stepperZ4.getMilliamps(); + stepperZ4.rms_current(target_current); + #endif + #endif + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Final Z Move"); // Do Final Z move to adjust #if GANTRY_CALIBRATION_DIRECTION == 1 current_position[Z_AXIS] = (Z_MAX_POS + move_distance); @@ -563,9 +570,11 @@ void GcodeSuite::M422() { current_position[Z_AXIS] = (Z_MIN_POS - move_distance); #endif - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE); + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); planner.synchronize(); + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Z Backoff"); // Back off end plate, back to normal motion range #if GANTRY_CALIBRATION_DIRECTION == 1 current_position[Z_AXIS] = (Z_MAX_POS - move_distance); @@ -573,34 +582,38 @@ void GcodeSuite::M422() { current_position[Z_AXIS] = (Z_MIN_POS + move_distance); #endif - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE); + do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); planner.synchronize(); + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Restore Current"); // Reset current to original values #if HAS_DIGIPOTSS stepper.digipot_current(Z_AXIS, previous_current); #elif HAS_MOTOR_CURRENT_PWM stepper.digipot_current(1, previous_current); - #if DAC_STEPPER_CURRENT + #elif DAC_STEPPER_CURRENT dac_current_raw(Z_AXIS, previous_current); #elif ENABLED(HAS_I2C_DIGIPOT) digipot_i2c_set_current(Z_AXIS, previous_current) #elif HAS_TRINAMIC_CONFIG static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; #if AXIS_IS_TMC(Z) - stepperZ.rms_current(previous_current_arr[0]); - #endif - #if AXIS_IS_TMC(Z2) - stepperZ2.rms_current(previous_current_arr[1]); - #endif - #if AXIS_IS_TMC(Z3) - stepperZ3.rms_current(previous_current_arr[2]); - #endif - #if AXIS_IS_TMC(Z4) - stepperZ4.rms_current(previous_current_arr[3]); - #endif + stepperZ.rms_current(previous_current_arr[0]); + #endif + #if AXIS_IS_TMC(Z2) + stepperZ2.rms_current(previous_current_arr[1]); + #endif + #if AXIS_IS_TMC(Z3) + stepperZ3.rms_current(previous_current_arr[2]); + #endif + #if AXIS_IS_TMC(Z4) + stepperZ4.rms_current(previous_current_arr[3]); + #endif #endif + + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Running Post Commands"); #ifdef GANTRY_CALIBRATION_COMMANDS_POST gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_POST)); #endif diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index 39276bc729..6d1234250d 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -335,7 +335,7 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) { case 33: G33(); break; // G33: Delta Auto-Calibration #endif - #if ENABLED(Z_STEPPER_AUTO_ALIGN) + #if ANY(Z_STEPPER_AUTO_ALIGN, MECHANICAL_GANTRY_CALIBRATION) case 34: G34(); break; // G34: Z Stepper automatic alignment using probe #endif diff --git a/Marlin/src/lcd/menu/menu_motion.cpp b/Marlin/src/lcd/menu/menu_motion.cpp index 9fea0597db..d8631d47ed 100644 --- a/Marlin/src/lcd/menu/menu_motion.cpp +++ b/Marlin/src/lcd/menu/menu_motion.cpp @@ -341,7 +341,7 @@ void menu_motion() { // // Auto Z-Align // - #if ENABLED(Z_STEPPER_AUTO_ALIGN) + #if ANY(Z_STEPPER_AUTO_ALIGN, MECHANICAL_GANTRY_CALIBRATION) GCODES_ITEM(MSG_AUTO_Z_ALIGN, PSTR("G34")); #endif diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index 0fc23099cc..ebd28e8980 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -37,7 +37,7 @@ */ // Change EEPROM version if the structure changes -#define EEPROM_VERSION "V81" +#define EEPROM_VERSION "V82" #define EEPROM_OFFSET 100 // Check the integrity of data offsets. @@ -366,7 +366,11 @@ typedef struct SettingsDataStruct { // // HAS_MOTOR_CURRENT_PWM // - uint32_t motor_current_setting[3]; // M907 X Z E + #if HAS_DIGIPOTSS + uint32_t motor_current_setting[COUNT(stepper.motor_current_setting)]; + #else + uint32_t motor_current_setting[3]; // M907 X Z E + #endif // // CNC_COORDINATE_SYSTEMS @@ -1278,7 +1282,7 @@ void MarlinSettings::postprocess() { { _FIELD_TEST(motor_current_setting); - #if HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS EEPROM_WRITE(stepper.motor_current_setting); #else const uint32_t no_current[3] = { 0 }; @@ -2111,13 +2115,19 @@ void MarlinSettings::postprocess() { // Motor Current PWM // { - uint32_t motor_current_setting[3]; + SERIAL_ECHOLN("DIGIPOTS Loading"); + #if HAS_DIGIPOTSS + uint32_t motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; + #else + uint32_t motor_current_setting[3]; // M907 X Z E + #endif _FIELD_TEST(motor_current_setting); EEPROM_READ(motor_current_setting); - #if HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS if (!validating) COPY(stepper.motor_current_setting, motor_current_setting); #endif + SERIAL_ECHOLN("DIGIPOTS Loaded"); } // @@ -2797,6 +2807,16 @@ void MarlinSettings::reset() { stepper.digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q])); #endif + // + // DIGIPOTS + // + SERIAL_ECHOLN("Writing Digipot"); + #if HAS_DIGIPOTSS + static constexpr uint32_t tmp_motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; + LOOP_L_N(q, COUNT(tmp_motor_current_setting)) + stepper.digipot_current(q, tmp_motor_current_setting[q]); + #endif +SERIAL_ECHOLN("Digipot Written"); // // CNC Coordinate System // @@ -3704,6 +3724,13 @@ void MarlinSettings::reset() { , SP_Z_STR, stepper.motor_current_setting[1] , SP_E_STR, stepper.motor_current_setting[2] ); + #elif HASDIGIPOTSS + CONFIG_ECHO_HEADING("Stepper motor currents:"); + CONFIG_ECHO_START(); + LOOP_L_N(q, COUNT(stepper.motor_current_setting)) { + SERIAL_ECHOPAIR_P( "M907 ", axis_codes[q]); + SERIAL_ECHOLN_P(stepper.motor_current_setting[q]); + } #endif /** diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index 56a75d1b48..ba3162b219 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -83,7 +83,7 @@ Stepper stepper; // Singleton #define BABYSTEPPING_EXTRA_DIR_WAIT -#if HAS_MOTOR_CURRENT_PWM +#if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS bool Stepper::initialized; // = false #endif @@ -144,6 +144,9 @@ Stepper stepper; // Singleton #if HAS_MOTOR_CURRENT_PWM uint32_t Stepper::motor_current_setting[3]; // Initialized by settings.load() +#elif HAS_DIGIPOTSS + constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT; + uint32_t Stepper::motor_current_setting[COUNT(digipot_count)]; // Initialized by settings.load() #endif // private: @@ -2591,7 +2594,7 @@ void Stepper::init() { set_directions(); #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM - TERN_(HAS_MOTOR_CURRENT_PWM, initialized = true); + initialized = true; digipot_init(); #endif } @@ -2971,18 +2974,19 @@ void Stepper::report_positions() { #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM void Stepper::digipot_current(const uint8_t driver, const int16_t current) { + if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1)) + motor_current_setting[driver] = current; // update motor_current_setting + + if (!initialized) return; #if HAS_DIGIPOTSS - + SERIAL_ECHOLNPAIR("Digipotss current ", current); const uint8_t digipot_ch[] = DIGIPOT_CHANNELS; digitalPotWrite(digipot_ch[driver], current); #elif HAS_MOTOR_CURRENT_PWM - if (!initialized) return; - if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1)) - motor_current_setting[driver] = current; // update motor_current_setting #define _WRITE_CURRENT_PWM(P) analogWrite(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE)) switch (driver) { @@ -3021,14 +3025,12 @@ void Stepper::report_positions() { #if HAS_DIGIPOTSS - static const uint8_t digipot_motor_current[] = DIGIPOT_MOTOR_CURRENT; - SPI.begin(); SET_OUTPUT(DIGIPOTSS_PIN); - LOOP_L_N(i, COUNT(digipot_motor_current)) { + LOOP_L_N(i, COUNT(motor_current_setting)) { //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]); - digipot_current(i, digipot_motor_current[i]); + digipot_current(i, motor_current_setting[i]); } #elif HAS_MOTOR_CURRENT_PWM diff --git a/Marlin/src/module/stepper.h b/Marlin/src/module/stepper.h index da38192d8e..5b8f83a5fc 100644 --- a/Marlin/src/module/stepper.h +++ b/Marlin/src/module/stepper.h @@ -249,8 +249,12 @@ class Stepper { #ifndef PWM_MOTOR_CURRENT #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT #endif - static uint32_t motor_current_setting[3]; static bool initialized; + static uint32_t motor_current_setting[3]; + #elif HAS_DIGIPOTSS + static bool initialized; + static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT; + static uint32_t motor_current_setting[COUNT(digipot_count)]; // Initialized by settings.load() #endif // Last-moved extruder, as set when the last movement was fetched from planner @@ -582,7 +586,7 @@ class Stepper { static int32_t _eval_bezier_curve(const uint32_t curr_step); #endif - #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS static void digipot_init(); #endif From ea15d3edab04137c55a404d2f081bba06547e9df Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Mon, 10 Aug 2020 08:23:07 -0400 Subject: [PATCH 21/42] Update G34_M422.cpp --- Marlin/src/gcode/calibrate/G34_M422.cpp | 36 ++++++++++++++++--------- 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 31555ff0bf..6884d5ee0a 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -170,7 +170,9 @@ void GcodeSuite::G34() { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions."); SERIAL_ECHOLNPAIR("\nITERATION: ", int(iteration + 1)); - ui.set_status("\nITERATION: ", int(iteration + 1)); + #if HAS_DISPLAY + ui.set_status("\nITERATION: ", int(iteration + 1)); + #endif // Initialize minimum value z_measured_min = 100000.0f; @@ -193,7 +195,9 @@ void GcodeSuite::G34() { const float z_probed_height = probe.probe_at_point(z_stepper_align.xy[iprobe], raise_after, 0, true, false); if (isnan(z_probed_height)) { SERIAL_ECHOLNPGM(MSG_LCD_PROBING_FAILED); - ui.set_status_P(MSG_LCD_PROBING_FAILED) + #if HAS_DISPLAY + ui.set_status_P(MSG_LCD_PROBING_FAILED) + #endif err_break = true; break; } @@ -252,13 +256,15 @@ void GcodeSuite::G34() { , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) #endif ); - ui.set_status("\n" - "DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1]) - #if NUM_Z_STEPPER_DRIVERS == 3 - , " Z2-Z3=", ABS(z_measured[1] - z_measured[2]) - , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) - #endif - ); + #if HAS_DISPLAY + ui.set_status("\n" + "DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1]) + #if NUM_Z_STEPPER_DRIVERS == 3 + , " Z2-Z3=", ABS(z_measured[1] - z_measured[2]) + , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) + #endif + ); + #endif #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) // Check if the applied corrections go in the correct direction. @@ -278,7 +284,9 @@ void GcodeSuite::G34() { // If it's getting worse, stop and throw an error if (last_z_align_level_indicator < z_align_level_indicator * 0.7f) { SERIAL_ECHOLNPGM(DECREASING_ACCURACY); - ui.set_status_P(DECREASING_ACCURACY); + #if HAS_DISPLAY + ui.set_status_P(DECREASING_ACCURACY); + #endif err_break = true; break; } @@ -303,7 +311,9 @@ void GcodeSuite::G34() { // Check for less accuracy compared to last move if (last_z_align_move[zstepper] < z_align_abs * 0.7f) { SERIAL_ECHOLNPGM(DECREASING_ACCURACY); - ui.set_status_P(DECREASING_ACCURACY); + #if HAS_DISPLAY + ui.set_status_P(DECREASING_ACCURACY); + #endif if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " last_z_align_move = ", last_z_align_move[zstepper]); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " z_align_abs = ", z_align_abs); adjustment_reverse = !adjustment_reverse; @@ -343,7 +353,9 @@ void GcodeSuite::G34() { if (success_break) { SERIAL_ECHOLNPGM("Target accuracy achieved."); - ui.set_status_P(PGM_P("Target accuracy achieved.")); + #if HAS_DISPLAY + ui.set_status_P(PGM_P("Target accuracy achieved.")); + #endif break; } From 1b211947b83bfc2f7742db2d1870643316e2a709 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Tue, 11 Aug 2020 19:34:23 -0400 Subject: [PATCH 22/42] Add missed lines from working branch --- Marlin/src/gcode/calibrate/G34_M422.cpp | 1 - Marlin/src/inc/SanityCheck.h | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 6884d5ee0a..6717d37722 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -609,7 +609,6 @@ void GcodeSuite::M422() { #elif ENABLED(HAS_I2C_DIGIPOT) digipot_i2c_set_current(Z_AXIS, previous_current) #elif HAS_TRINAMIC_CONFIG - static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; #if AXIS_IS_TMC(Z) stepperZ.rms_current(previous_current_arr[0]); #endif diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index e1c7141dcf..ffa773b912 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -2734,7 +2734,7 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #endif #if ENABLED(MECHANICAL_GANTRY_CALIBRATION) - #if NONE(DAC_STEPPER_CURRENT, HAS_DIGIPOTSS, DAC_STEPPER_CURRENT, HAS_TRINAMIC_CONFIG) + #if NONE(DAC_STEPPER_CURRENT, HAS_DIGIPOTSS, DAC_STEPPER_CURRENT, HAS_TRINAMIC_CONFIG, HAS_MOTOR_CURRENT_PWM) #error "It is highly reccomended to have adjustable current drivers to prevent damage. Disable this line to continue anyway." #else #ifndef GANTRY_CALIBRATION_CURRENT From bb6b23891c1a551dd0f19bf757ed068259818302 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 12 Aug 2020 17:10:05 -0500 Subject: [PATCH 23/42] Various fixes, cleanup --- Marlin/src/gcode/calibrate/G34_M422.cpp | 64 +++++++++++++------------ Marlin/src/lcd/language/language_en.h | 3 +- Marlin/src/lcd/menu/menu_motion.cpp | 2 +- Marlin/src/module/settings.cpp | 43 +++++++++-------- 4 files changed, 60 insertions(+), 52 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 6717d37722..10ccecc649 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -155,23 +155,28 @@ void GcodeSuite::G34() { z_maxdiff = 0.0f, amplification = z_auto_align_amplification; - // These are needed after the for-loop - uint8_t iteration; - bool err_break = false; - float z_measured_min; - #if DISABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) bool adjustment_reverse = false; #endif - // 'iteration' is declared above and is also used after the for-loop. - // *not* the same as LOOP_L_N(iteration, z_auto_align_iterations) - for (iteration = 0; iteration < z_auto_align_iterations; ++iteration) { + #if HAS_DISPLAY + PGM_P const msg_iteration = GET_TEXT(MSG_ITERATION); + const iter_str_len = strlen_P(iter_str_len); + #endif + + // Final z and iteration values will be used after breaking the loop + float z_measured_min; + uint8_t iteration = 0; + bool err_break = false; // To break out of nested loops + while (iteration < z_auto_align_iterations) { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions."); - SERIAL_ECHOLNPAIR("\nITERATION: ", int(iteration + 1)); + const int iter = iteration + 1; + SERIAL_ECHOLNPAIR("\nIteration: ", iter); #if HAS_DISPLAY - ui.set_status("\nITERATION: ", int(iteration + 1)); + char str[iter_str_len + 2 + 1]; + sprintf_P(str, msg_iteration, iter); + ui.set_status(str); #endif // Initialize minimum value @@ -194,10 +199,8 @@ void GcodeSuite::G34() { // current_position.z has been manually altered in the "dirty trick" above. const float z_probed_height = probe.probe_at_point(z_stepper_align.xy[iprobe], raise_after, 0, true, false); if (isnan(z_probed_height)) { - SERIAL_ECHOLNPGM(MSG_LCD_PROBING_FAILED); - #if HAS_DISPLAY - ui.set_status_P(MSG_LCD_PROBING_FAILED) - #endif + SERIAL_ECHOLNPGM("Probing failed"); + LCD_MESSAGEPGM(MSG_LCD_PROBING_FAILED); err_break = true; break; } @@ -257,13 +260,19 @@ void GcodeSuite::G34() { #endif ); #if HAS_DISPLAY - ui.set_status("\n" - "DIFFERENCE Z1-Z2=", ABS(z_measured[0] - z_measured[1]) + char msg[30], fstr[16]; + sprintf_P( + PSTR("DIFFERENCE Z1-Z2=%s" + #if NUM_Z_STEPPER_DRIVERS == 3 + " Z2-Z3=%s Z3-Z1=%s" + #endif + ), dtostrf(ABS(z_measured[0] - z_measured[1]), 1, 3, fstr) #if NUM_Z_STEPPER_DRIVERS == 3 - , " Z2-Z3=", ABS(z_measured[1] - z_measured[2]) - , " Z3-Z1=", ABS(z_measured[2] - z_measured[0]) + , dtostrf(ABS(z_measured[1] - z_measured[2]), 1, 3, fstr) + , dtostrf(ABS(z_measured[2] - z_measured[0]), 1, 3, fstr) #endif ); + ui.set_status(msg); #endif #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) @@ -283,10 +292,8 @@ void GcodeSuite::G34() { // If it's getting worse, stop and throw an error if (last_z_align_level_indicator < z_align_level_indicator * 0.7f) { - SERIAL_ECHOLNPGM(DECREASING_ACCURACY); - #if HAS_DISPLAY - ui.set_status_P(DECREASING_ACCURACY); - #endif + SERIAL_ECHOLNPGM("Decreasing Accuracy Detected."); + LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY)); err_break = true; break; } @@ -310,10 +317,8 @@ void GcodeSuite::G34() { // Check for less accuracy compared to last move if (last_z_align_move[zstepper] < z_align_abs * 0.7f) { - SERIAL_ECHOLNPGM(DECREASING_ACCURACY); - #if HAS_DISPLAY - ui.set_status_P(DECREASING_ACCURACY); - #endif + SERIAL_ECHOLNPGM("Decreasing Accuracy Detected."); + LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY)); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " last_z_align_move = ", last_z_align_move[zstepper]); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " z_align_abs = ", z_align_abs); adjustment_reverse = !adjustment_reverse; @@ -353,13 +358,12 @@ void GcodeSuite::G34() { if (success_break) { SERIAL_ECHOLNPGM("Target accuracy achieved."); - #if HAS_DISPLAY - ui.set_status_P(PGM_P("Target accuracy achieved.")); - #endif + LCD_MESSAGEPGM(MSG_ACCURACY_ACHIEVED); break; } - } // for (iteration) + iteration++; + } // while (iteration < z_auto_align_iterations) if (err_break) SERIAL_ECHOLNPGM("G34 aborted."); diff --git a/Marlin/src/lcd/language/language_en.h b/Marlin/src/lcd/language/language_en.h index a3a1d476f0..55590db6d8 100644 --- a/Marlin/src/lcd/language/language_en.h +++ b/Marlin/src/lcd/language/language_en.h @@ -68,7 +68,8 @@ namespace Language_en { PROGMEM Language_Str MSG_AUTO_HOME_Z = _UxGT("Home Z"); PROGMEM Language_Str MSG_AUTO_Z_ALIGN = _UxGT("Auto Z-Align"); PROGMEM Language_Str MSG_ASSISTED_TRAMMING = _UxGT("Assisted Tramming"); - PROGMEM Language_Str DECREASING_ACCURACY = _UxGT("Decreasing accuracy detected."); + PROGMEM Language_Str MSG_DECREASING_ACCURACY = _UxGT("Accuracy Decreasing!"); + PROGMEM Language_Str MSG_ACCURACY_ACHIEVED = _UxGT("Accuracy Achieved"); PROGMEM Language_Str MSG_LEVEL_BED_HOMING = _UxGT("Homing XYZ"); PROGMEM Language_Str MSG_LEVEL_BED_WAITING = _UxGT("Click to Begin"); PROGMEM Language_Str MSG_LEVEL_BED_NEXT_POINT = _UxGT("Next Point"); diff --git a/Marlin/src/lcd/menu/menu_motion.cpp b/Marlin/src/lcd/menu/menu_motion.cpp index d8631d47ed..16b15043e9 100644 --- a/Marlin/src/lcd/menu/menu_motion.cpp +++ b/Marlin/src/lcd/menu/menu_motion.cpp @@ -341,7 +341,7 @@ void menu_motion() { // // Auto Z-Align // - #if ANY(Z_STEPPER_AUTO_ALIGN, MECHANICAL_GANTRY_CALIBRATION) + #if EITHER(Z_STEPPER_AUTO_ALIGN, MECHANICAL_GANTRY_CALIBRATION) GCODES_ITEM(MSG_AUTO_Z_ALIGN, PSTR("G34")); #endif diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index ebd28e8980..efd0596129 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -369,7 +369,7 @@ typedef struct SettingsDataStruct { #if HAS_DIGIPOTSS uint32_t motor_current_setting[COUNT(stepper.motor_current_setting)]; #else - uint32_t motor_current_setting[3]; // M907 X Z E + uint32_t motor_current_setting[3]; // M907 X Z E #endif // @@ -2115,19 +2115,19 @@ void MarlinSettings::postprocess() { // Motor Current PWM // { - SERIAL_ECHOLN("DIGIPOTS Loading"); + _FIELD_TEST(motor_current_setting); #if HAS_DIGIPOTSS uint32_t motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; #else - uint32_t motor_current_setting[3]; // M907 X Z E + uint32_t motor_current_setting[3]; #endif - _FIELD_TEST(motor_current_setting); + DEBUG_ECHOLNPGM("DIGIPOTS Loading"); EEPROM_READ(motor_current_setting); + DEBUG_ECHOLNPGM("DIGIPOTS Loaded"); #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS if (!validating) COPY(stepper.motor_current_setting, motor_current_setting); #endif - SERIAL_ECHOLN("DIGIPOTS Loaded"); } // @@ -2810,13 +2810,14 @@ void MarlinSettings::reset() { // // DIGIPOTS // - SERIAL_ECHOLN("Writing Digipot"); #if HAS_DIGIPOTSS static constexpr uint32_t tmp_motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; + DEBUG_ECHOLNPGM("Writing Digipot"); LOOP_L_N(q, COUNT(tmp_motor_current_setting)) stepper.digipot_current(q, tmp_motor_current_setting[q]); + DEBUG_ECHOLNPGM("Digipot Written"); #endif -SERIAL_ECHOLN("Digipot Written"); + // // CNC Coordinate System // @@ -3716,21 +3717,23 @@ SERIAL_ECHOLN("Digipot Written"); #endif #endif - #if HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS CONFIG_ECHO_HEADING("Stepper motor currents:"); CONFIG_ECHO_START(); - SERIAL_ECHOLNPAIR_P( - PSTR(" M907 X"), stepper.motor_current_setting[0] - , SP_Z_STR, stepper.motor_current_setting[1] - , SP_E_STR, stepper.motor_current_setting[2] - ); - #elif HASDIGIPOTSS - CONFIG_ECHO_HEADING("Stepper motor currents:"); - CONFIG_ECHO_START(); - LOOP_L_N(q, COUNT(stepper.motor_current_setting)) { - SERIAL_ECHOPAIR_P( "M907 ", axis_codes[q]); - SERIAL_ECHOLN_P(stepper.motor_current_setting[q]); - } + #if HAS_MOTOR_CURRENT_PWM + SERIAL_ECHOLNPAIR_P( + PSTR(" M907 X"), stepper.motor_current_setting[0] + , SP_Z_STR, stepper.motor_current_setting[1] + , SP_E_STR, stepper.motor_current_setting[2] + ); + #elif HAS_DIGIPOTSS + SERIAL_ECHOPGM(" M907"); + LOOP_L_N(q, COUNT(stepper.motor_current_setting)) { + SERIAL_CHAR(' '); + SERIAL_CHAR(axis_codes[q]); + SERIAL_ECHO(stepper.motor_current_setting[q]); + } + #endif #endif /** From 70259129576a529d81d16c55641ea3b6a9f58f12 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 12 Aug 2020 18:46:55 -0500 Subject: [PATCH 24/42] Stepper current refactor --- Marlin/src/HAL/DUE/fastio/G2_PWM.cpp | 2 +- Marlin/src/HAL/LPC1768/inc/SanityCheck.h | 2 +- Marlin/src/MarlinCore.cpp | 12 ++-- Marlin/src/feature/dac/dac_mcp4728.cpp | 50 ++++++++-------- Marlin/src/feature/dac/dac_mcp4728.h | 25 ++++---- Marlin/src/feature/dac/stepper_dac.cpp | 57 +++++++++---------- Marlin/src/feature/dac/stepper_dac.h | 19 ++++--- Marlin/src/feature/digipot/digipot.h | 12 +++- .../src/feature/digipot/digipot_mcp4018.cpp | 12 ++-- .../src/feature/digipot/digipot_mcp4451.cpp | 8 ++- Marlin/src/gcode/calibrate/G34_M422.cpp | 28 ++++----- .../src/gcode/feature/digipot/M907-M910.cpp | 52 ++++++++--------- Marlin/src/gcode/gcode.cpp | 6 +- Marlin/src/gcode/gcode.h | 12 ++-- Marlin/src/inc/Conditionals_adv.h | 2 +- Marlin/src/inc/Conditionals_post.h | 2 +- Marlin/src/inc/SanityCheck.h | 4 +- Marlin/src/lcd/menu/menu_advanced.cpp | 8 +-- Marlin/src/module/settings.cpp | 18 +++--- Marlin/src/module/stepper.cpp | 49 +++++++--------- Marlin/src/module/stepper.h | 26 +++++---- Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h | 2 +- .../src/pins/teensy2/pins_PRINTRBOARD_REVF.h | 2 +- platformio.ini | 4 +- 24 files changed, 216 insertions(+), 198 deletions(-) diff --git a/Marlin/src/HAL/DUE/fastio/G2_PWM.cpp b/Marlin/src/HAL/DUE/fastio/G2_PWM.cpp index 1682faea66..d9fbabce21 100644 --- a/Marlin/src/HAL/DUE/fastio/G2_PWM.cpp +++ b/Marlin/src/HAL/DUE/fastio/G2_PWM.cpp @@ -154,7 +154,7 @@ void Stepper::digipot_init() { NVIC_SetPriority(PWM_IRQn, NVIC_EncodePriority(0, 10, 0)); // normal priority for PWM module (can stand some jitter on the Vref signals) } -void Stepper::digipot_current(const uint8_t driver, const int16_t current) { +void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) { if (!(PWM->PWM_CH_NUM[0].PWM_CPRD == PWM_PERIOD_US)) digipot_init(); // Init PWM system if needed diff --git a/Marlin/src/HAL/LPC1768/inc/SanityCheck.h b/Marlin/src/HAL/LPC1768/inc/SanityCheck.h index 3fc1dd801a..e9d2936d41 100644 --- a/Marlin/src/HAL/LPC1768/inc/SanityCheck.h +++ b/Marlin/src/HAL/LPC1768/inc/SanityCheck.h @@ -178,7 +178,7 @@ static_assert(DISABLED(BAUD_RATE_GCODE), "BAUD_RATE_GCODE is not yet supported o // // Flag any i2c pin conflicts // -#if ANY(HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT, EXPERIMENTAL_I2CBUS, I2C_POSITION_ENCODERS, PCA9632, I2C_EEPROM) +#if ANY(HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC, EXPERIMENTAL_I2CBUS, I2C_POSITION_ENCODERS, PCA9632, I2C_EEPROM) #define USEDI2CDEV_M 1 // /Wire.cpp #if USEDI2CDEV_M == 0 // P0_27 [D57] (AUX-1) .......... P0_28 [D58] (AUX-1) diff --git a/Marlin/src/MarlinCore.cpp b/Marlin/src/MarlinCore.cpp index 22935c1460..cb0dd56033 100644 --- a/Marlin/src/MarlinCore.cpp +++ b/Marlin/src/MarlinCore.cpp @@ -97,7 +97,7 @@ #include "feature/closedloop.h" #endif -#if HAS_I2C_DIGIPOT +#if HAS_MOTOR_CURRENT_I2C #include "feature/digipot/digipot.h" #endif @@ -125,7 +125,7 @@ #include "module/servo.h" #endif -#if ENABLED(DAC_STEPPER_CURRENT) +#if ENABLED(HAS_MOTOR_CURRENT_DAC) #include "feature/dac/stepper_dac.h" #endif @@ -1081,12 +1081,12 @@ void setup() { SETUP_RUN(enableStepperDrivers()); #endif - #if HAS_I2C_DIGIPOT - SETUP_RUN(digipot_i2c_init()); + #if HAS_MOTOR_CURRENT_I2C + SETUP_RUN(digipot_i2c.init()); #endif - #if ENABLED(DAC_STEPPER_CURRENT) - SETUP_RUN(dac_init()); + #if ENABLED(HAS_MOTOR_CURRENT_DAC) + SETUP_RUN(stepper_dac.init()); #endif #if EITHER(Z_PROBE_SLED, SOLENOID_PROBE) && HAS_SOLENOID_1 diff --git a/Marlin/src/feature/dac/dac_mcp4728.cpp b/Marlin/src/feature/dac/dac_mcp4728.cpp index 55f10a49ef..81c465cf29 100644 --- a/Marlin/src/feature/dac/dac_mcp4728.cpp +++ b/Marlin/src/feature/dac/dac_mcp4728.cpp @@ -32,16 +32,18 @@ #include "../../inc/MarlinConfig.h" -#if ENABLED(DAC_STEPPER_CURRENT) +#if ENABLED(HAS_MOTOR_CURRENT_DAC) #include "dac_mcp4728.h" -xyze_uint_t mcp4728_values; +MCP4728 mcp4728; + +xyze_uint_t dac_values; /** * Begin I2C, get current values (input register and eeprom) of mcp4728 */ -void mcp4728_init() { +void MCP4728::init() { Wire.begin(); Wire.requestFrom(I2C_ADDRESS(DAC_DEV_ADDRESS), uint8_t(24)); while (Wire.available()) { @@ -50,7 +52,7 @@ void mcp4728_init() { loByte = Wire.read(); if (!(deviceID & 0x08)) - mcp4728_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte); + dac_values[(deviceID & 0x30) >> 4] = word((hiByte & 0x0F), loByte); } } @@ -58,9 +60,9 @@ void mcp4728_init() { * Write input resister value to specified channel using fastwrite method. * Channel : 0-3, Values : 0-4095 */ -uint8_t mcp4728_analogWrite(const uint8_t channel, const uint16_t value) { - mcp4728_values[channel] = value; - return mcp4728_fastWrite(); +uint8_t MCP4728::analogWrite(const uint8_t channel, const uint16_t value) { + dac_values[channel] = value; + return fastWrite(); } /** @@ -68,12 +70,12 @@ uint8_t mcp4728_analogWrite(const uint8_t channel, const uint16_t value) { * This will update both input register and EEPROM value * This will also write current Vref, PowerDown, Gain settings to EEPROM */ -uint8_t mcp4728_eepromWrite() { +uint8_t MCP4728::eepromWrite() { Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS)); Wire.write(SEQWRITE); LOOP_XYZE(i) { - Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(mcp4728_values[i])); - Wire.write(lowByte(mcp4728_values[i])); + Wire.write(DAC_STEPPER_VREF << 7 | DAC_STEPPER_GAIN << 4 | highByte(dac_values[i])); + Wire.write(lowByte(dac_values[i])); } return Wire.endTransmission(); } @@ -81,7 +83,7 @@ uint8_t mcp4728_eepromWrite() { /** * Write Voltage reference setting to all input regiters */ -uint8_t mcp4728_setVref_all(const uint8_t value) { +uint8_t MCP4728::setVref_all(const uint8_t value) { Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS)); Wire.write(VREFWRITE | (value ? 0x0F : 0x00)); return Wire.endTransmission(); @@ -89,7 +91,7 @@ uint8_t mcp4728_setVref_all(const uint8_t value) { /** * Write Gain setting to all input regiters */ -uint8_t mcp4728_setGain_all(const uint8_t value) { +uint8_t MCP4728::setGain_all(const uint8_t value) { Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS)); Wire.write(GAINWRITE | (value ? 0x0F : 0x00)); return Wire.endTransmission(); @@ -98,16 +100,16 @@ uint8_t mcp4728_setGain_all(const uint8_t value) { /** * Return Input Register value */ -uint16_t mcp4728_getValue(const uint8_t channel) { return mcp4728_values[channel]; } +uint16_t MCP4728::getValue(const uint8_t channel) { return dac_values[channel]; } #if 0 /** * Steph: Might be useful in the future * Return Vout */ -uint16_t mcp4728_getVout(const uint8_t channel) { +uint16_t MCP4728::getVout(const uint8_t channel) { const uint32_t vref = 2048, - vOut = (vref * mcp4728_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096; + vOut = (vref * dac_values[channel] * (_DAC_STEPPER_GAIN + 1)) / 4096; return _MIN(vOut, defaultVDD); } #endif @@ -115,15 +117,15 @@ uint16_t mcp4728_getVout(const uint8_t channel) { /** * Returns DAC values as a 0-100 percentage of drive strength */ -uint8_t mcp4728_getDrvPct(const uint8_t channel) { return uint8_t(100.0 * mcp4728_values[channel] / (DAC_STEPPER_MAX) + 0.5); } +uint8_t MCP4728::getDrvPct(const uint8_t channel) { return uint8_t(100.0 * dac_values[channel] / (DAC_STEPPER_MAX) + 0.5); } /** * Receives all Drive strengths as 0-100 percent values, updates * DAC Values array and calls fastwrite to update the DAC. */ -void mcp4728_setDrvPct(xyze_uint8_t &pct) { - mcp4728_values *= 0.01 * pct * (DAC_STEPPER_MAX); - mcp4728_fastWrite(); +void MCP4728::setDrvPct(xyze_uint8_t &pct) { + dac_values *= 0.01 * pct * (DAC_STEPPER_MAX); + fastWrite(); } /** @@ -131,11 +133,11 @@ void mcp4728_setDrvPct(xyze_uint8_t &pct) { * DAC Input and PowerDown bits update. * No EEPROM update */ -uint8_t mcp4728_fastWrite() { +uint8_t MCP4728::fastWrite() { Wire.beginTransmission(I2C_ADDRESS(DAC_DEV_ADDRESS)); LOOP_XYZE(i) { - Wire.write(highByte(mcp4728_values[i])); - Wire.write(lowByte(mcp4728_values[i])); + Wire.write(highByte(dac_values[i])); + Wire.write(lowByte(dac_values[i])); } return Wire.endTransmission(); } @@ -143,10 +145,10 @@ uint8_t mcp4728_fastWrite() { /** * Common function for simple general commands */ -uint8_t mcp4728_simpleCommand(const byte simpleCommand) { +uint8_t MCP4728::simpleCommand(const byte simpleCommand) { Wire.beginTransmission(I2C_ADDRESS(GENERALCALL)); Wire.write(simpleCommand); return Wire.endTransmission(); } -#endif // DAC_STEPPER_CURRENT +#endif // HAS_MOTOR_CURRENT_DAC diff --git a/Marlin/src/feature/dac/dac_mcp4728.h b/Marlin/src/feature/dac/dac_mcp4728.h index 6cce77856a..b2872f5bcf 100644 --- a/Marlin/src/feature/dac/dac_mcp4728.h +++ b/Marlin/src/feature/dac/dac_mcp4728.h @@ -65,13 +65,18 @@ // DAC_OR_ADDRESS defined in pins_BOARD.h file #define DAC_DEV_ADDRESS (BASE_ADDR | DAC_OR_ADDRESS) -void mcp4728_init(); -uint8_t mcp4728_analogWrite(const uint8_t channel, const uint16_t value); -uint8_t mcp4728_eepromWrite(); -uint8_t mcp4728_setVref_all(const uint8_t value); -uint8_t mcp4728_setGain_all(const uint8_t value); -uint16_t mcp4728_getValue(const uint8_t channel); -uint8_t mcp4728_fastWrite(); -uint8_t mcp4728_simpleCommand(const byte simpleCommand); -uint8_t mcp4728_getDrvPct(const uint8_t channel); -void mcp4728_setDrvPct(xyze_uint8_t &pct); +class MCP4728 { +public: + static void init(); + static uint8_t analogWrite(const uint8_t channel, const uint16_t value); + static uint8_t eepromWrite(); + static uint8_t setVref_all(const uint8_t value); + static uint8_t setGain_all(const uint8_t value); + static uint16_t getValue(const uint8_t channel); + static uint8_t fastWrite(); + static uint8_t simpleCommand(const byte simpleCommand); + static uint8_t getDrvPct(const uint8_t channel); + static void setDrvPct(xyze_uint8_t &pct); +}; + +extern MCP4728 mcp4728; diff --git a/Marlin/src/feature/dac/stepper_dac.cpp b/Marlin/src/feature/dac/stepper_dac.cpp index 6b140a0025..5f10b4ccfb 100644 --- a/Marlin/src/feature/dac/stepper_dac.cpp +++ b/Marlin/src/feature/dac/stepper_dac.cpp @@ -26,7 +26,7 @@ #include "../../inc/MarlinConfig.h" -#if ENABLED(DAC_STEPPER_CURRENT) +#if ENABLED(HAS_MOTOR_CURRENT_DAC) #include "stepper_dac.h" #include "../../MarlinCore.h" // for SP_X_LBL... @@ -35,56 +35,53 @@ bool dac_present = false; constexpr xyze_uint8_t dac_order = DAC_STEPPER_ORDER; xyze_uint8_t dac_channel_pct = DAC_MOTOR_CURRENT_DEFAULT; -int dac_init() { +StepperDAC stepper_dac; + +int StepperDAC::init() { #if PIN_EXISTS(DAC_DISABLE) OUT_WRITE(DAC_DISABLE_PIN, LOW); // set pin low to enable DAC #endif - mcp4728_init(); + mcp4728.init(); - if (mcp4728_simpleCommand(RESET)) return -1; + if (mcp4728.simpleCommand(RESET)) return -1; dac_present = true; - mcp4728_setVref_all(DAC_STEPPER_VREF); - mcp4728_setGain_all(DAC_STEPPER_GAIN); + mcp4728.setVref_all(DAC_STEPPER_VREF); + mcp4728.setGain_all(DAC_STEPPER_GAIN); - if (mcp4728_getDrvPct(0) < 1 || mcp4728_getDrvPct(1) < 1 || mcp4728_getDrvPct(2) < 1 || mcp4728_getDrvPct(3) < 1 ) { - mcp4728_setDrvPct(dac_channel_pct); - mcp4728_eepromWrite(); + if (mcp4728.getDrvPct(0) < 1 || mcp4728.getDrvPct(1) < 1 || mcp4728.getDrvPct(2) < 1 || mcp4728.getDrvPct(3) < 1 ) { + mcp4728.setDrvPct(dac_channel_pct); + mcp4728.eepromWrite(); } return 0; } -void dac_current_percent(uint8_t channel, float val) { - if (!dac_present) return; - - NOMORE(val, 100); - - mcp4728_analogWrite(dac_order[channel], val * 0.01 * (DAC_STEPPER_MAX)); - mcp4728_simpleCommand(UPDATE); -} - -void dac_current_raw(uint8_t channel, uint16_t val) { +void StepperDAC::set_current_value(const uint8_t channel, uint16_t val) { if (!dac_present) return; NOMORE(val, uint16_t(DAC_STEPPER_MAX)); - mcp4728_analogWrite(dac_order[channel], val); - mcp4728_simpleCommand(UPDATE); + mcp4728.analogWrite(dac_order[channel], val); + mcp4728.simpleCommand(UPDATE); } -static float dac_perc(int8_t n) { return 100.0 * mcp4728_getValue(dac_order[n]) * RECIPROCAL(DAC_STEPPER_MAX); } -static float dac_amps(int8_t n) { return mcp4728_getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * 0.125 * RECIPROCAL(DAC_STEPPER_SENSE); } +void StepperDAC::set_current_percent(const uint8_t channel, float val) { + set_current_value(channel, _MIN(val, 100.0f) * (DAC_STEPPER_MAX) / 100.0f); +} -uint8_t dac_current_get_percent(const AxisEnum axis) { return mcp4728_getDrvPct(dac_order[axis]); } -void dac_current_set_percents(xyze_uint8_t &pct) { +static float dac_perc(int8_t n) { return 100.0 * mcp4728.getValue(dac_order[n]) * RECIPROCAL(DAC_STEPPER_MAX); } +static float dac_amps(int8_t n) { return mcp4728.getDrvPct(dac_order[n]) * (DAC_STEPPER_MAX) * 0.125 * RECIPROCAL(DAC_STEPPER_SENSE); } + +uint8_t StepperDAC::get_current_percent(const AxisEnum axis) { return mcp4728.getDrvPct(dac_order[axis]); } +void StepperDAC::set_current_percents(xyze_uint8_t &pct) { LOOP_XYZE(i) dac_channel_pct[i] = pct[dac_order[i]]; - mcp4728_setDrvPct(dac_channel_pct); + mcp4728.setDrvPct(dac_channel_pct); } -void dac_print_values() { +void StepperDAC::print_values() { if (!dac_present) return; SERIAL_ECHO_MSG("Stepper current values in % (Amps):"); SERIAL_ECHO_START(); @@ -94,9 +91,9 @@ void dac_print_values() { SERIAL_ECHOLNPAIR_P(SP_E_LBL, dac_perc(E_AXIS), PSTR(" ("), dac_amps(E_AXIS), PSTR(")")); } -void dac_commit_eeprom() { +void StepperDAC::commit_eeprom() { if (!dac_present) return; - mcp4728_eepromWrite(); + mcp4728.eepromWrite(); } -#endif // DAC_STEPPER_CURRENT +#endif // HAS_MOTOR_CURRENT_DAC diff --git a/Marlin/src/feature/dac/stepper_dac.h b/Marlin/src/feature/dac/stepper_dac.h index 0543b6275e..6836335e98 100644 --- a/Marlin/src/feature/dac/stepper_dac.h +++ b/Marlin/src/feature/dac/stepper_dac.h @@ -27,10 +27,15 @@ #include "dac_mcp4728.h" -int dac_init(); -void dac_current_percent(uint8_t channel, float val); -void dac_current_raw(uint8_t channel, uint16_t val); -void dac_print_values(); -void dac_commit_eeprom(); -uint8_t dac_current_get_percent(AxisEnum axis); -void dac_current_set_percents(xyze_uint8_t &pct); +class StepperDAC { +public: + static int init(); + static void set_current_percent(const uint8_t channel, float val); + static void set_current_value(const uint8_t channel, uint16_t val); + static void print_values(); + static void commit_eeprom(); + static uint8_t get_current_percent(AxisEnum axis); + static void set_current_percents(xyze_uint8_t &pct); +}; + +extern StepperDAC stepper_dac; diff --git a/Marlin/src/feature/digipot/digipot.h b/Marlin/src/feature/digipot/digipot.h index d59f8b930a..c53f8093dd 100644 --- a/Marlin/src/feature/digipot/digipot.h +++ b/Marlin/src/feature/digipot/digipot.h @@ -21,5 +21,13 @@ */ #pragma once -void digipot_i2c_set_current(const uint8_t channel, const float current); -void digipot_i2c_init(); +// +// Header for MCP4018 and MCP4451 current control i2c devices +// +class DigipotI2C { +public: + static void init(); + static void set_current(const uint8_t channel, const float current); +}; + +DigipotI2C digipot_i2c; diff --git a/Marlin/src/feature/digipot/digipot_mcp4018.cpp b/Marlin/src/feature/digipot/digipot_mcp4018.cpp index 35c513a32c..6260185fc3 100644 --- a/Marlin/src/feature/digipot/digipot_mcp4018.cpp +++ b/Marlin/src/feature/digipot/digipot_mcp4018.cpp @@ -24,6 +24,8 @@ #if ENABLED(DIGIPOT_MCP4018) +#include "digipot.h" + #include #include // https://github.com/stawel/SlowSoftI2CMaster @@ -68,7 +70,7 @@ static SlowSoftI2CMaster pots[DIGIPOT_I2C_NUM_CHANNELS] = { #endif }; -static void i2c_send(const uint8_t channel, const byte v) { +static void digipot_i2c_send(const uint8_t channel, const byte v) { if (WITHIN(channel, 0, DIGIPOT_I2C_NUM_CHANNELS - 1)) { pots[channel].i2c_start(((DIGIPOT_I2C_ADDRESS_A) << 1) | I2C_WRITE); pots[channel].i2c_write(v); @@ -77,12 +79,12 @@ static void i2c_send(const uint8_t channel, const byte v) { } // This is for the MCP4018 I2C based digipot -void digipot_i2c_set_current(const uint8_t channel, const float current) { +void DigipotI2C::set_current(const uint8_t channel, const float current) { const float ival = _MIN(_MAX(current, 0), float(DIGIPOT_MCP4018_MAX_VALUE)); - i2c_send(channel, current_to_wiper(ival)); + digipot_i2c_send(channel, current_to_wiper(ival)); } -void digipot_i2c_init() { +void DigipotI2C::init() { LOOP_L_N(i, DIGIPOT_I2C_NUM_CHANNELS) pots[i].i2c_init(); // Init currents according to Configuration_adv.h @@ -94,7 +96,7 @@ void digipot_i2c_init() { #endif ; LOOP_L_N(i, COUNT(digipot_motor_current)) - digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i])); + set_current(i, pgm_read_float(&digipot_motor_current[i])); } #endif // DIGIPOT_MCP4018 diff --git a/Marlin/src/feature/digipot/digipot_mcp4451.cpp b/Marlin/src/feature/digipot/digipot_mcp4451.cpp index 0c8ff04a0b..7e6ace49a0 100644 --- a/Marlin/src/feature/digipot/digipot_mcp4451.cpp +++ b/Marlin/src/feature/digipot/digipot_mcp4451.cpp @@ -24,6 +24,8 @@ #if ENABLED(DIGIPOT_MCP4451) +#include "digipot.h" + #include #include @@ -61,7 +63,7 @@ static void digipot_i2c_send(const byte addr, const byte a, const byte b) { } // This is for the MCP4451 I2C based digipot -void digipot_i2c_set_current(const uint8_t channel, const float current) { +void DigipotI2C::set_current(const uint8_t channel, const float current) { // These addresses are specific to Azteeg X3 Pro, can be set to others. // In this case first digipot is at address A0=0, A1=0, second one is at A0=0, A1=1 const byte addr = channel < 4 ? DIGIPOT_I2C_ADDRESS_A : DIGIPOT_I2C_ADDRESS_B; // channel 0-3 vs 4-7 @@ -75,7 +77,7 @@ void digipot_i2c_set_current(const uint8_t channel, const float current) { digipot_i2c_send(addr, addresses[channel & 0x3], current_to_wiper(_MIN(float(_MAX(current, 0)), DIGIPOT_I2C_MAX_CURRENT))); } -void digipot_i2c_init() { +void DigipotI2C::init() { #if MB(MKS_SBASE) configure_i2c(16); // Set clock_option to 16 ensure I2C is initialized at 400kHz #else @@ -90,7 +92,7 @@ void digipot_i2c_init() { #endif ; LOOP_L_N(i, COUNT(digipot_motor_current)) - digipot_i2c_set_current(i, pgm_read_float(&digipot_motor_current[i])); + set_current(i, pgm_read_float(&digipot_motor_current[i])); } #endif // DIGIPOT_MCP4451 diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 10ccecc649..70768350bb 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -541,22 +541,22 @@ void GcodeSuite::M422() { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Reducing Current"); // Store current motor settings, then apply reduced value - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS]; - stepper.digipot_current(Z_AXIS, target_current); + stepper.set_digipot_current(Z_AXIS, target_current); #elif HAS_MOTOR_CURRENT_PWM const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS]; - stepper.digipot_current(1, target_current); - #elif DAC_STEPPER_CURRENT + stepper.set_digipot_current(1, target_current); + #elif HAS_MOTOR_CURRENT_DAC const float target_current = parser.floatval('S', GANTRY_CALIBRATION_CURRENT); const float previous_current = dac_amps(Z_AXIS, target_current); - dac_current_raw(Z_AXIS, target_current); - #elif ENABLED(HAS_I2C_DIGIPOT) + stepper_dac.set_current_value(Z_AXIS, target_current); + #elif ENABLED(HAS_MOTOR_CURRENT_I2C) const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); previous_current = dac_amps(Z_AXIS); - digipot_i2c_set_current(Z_AXIS, target_current) + digipot_i2c.set_current(Z_AXIS, target_current) #elif HAS_TRINAMIC_CONFIG const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); static uint16_t previous_current_arr[NUM_Z_STEPPER_DRIVERS]; @@ -604,14 +604,14 @@ void GcodeSuite::M422() { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Restore Current"); // Reset current to original values - #if HAS_DIGIPOTSS - stepper.digipot_current(Z_AXIS, previous_current); + #if HAS_MOTOR_CURRENT_SPI + stepper.set_digipot_current(Z_AXIS, previous_current); #elif HAS_MOTOR_CURRENT_PWM - stepper.digipot_current(1, previous_current); - #elif DAC_STEPPER_CURRENT - dac_current_raw(Z_AXIS, previous_current); - #elif ENABLED(HAS_I2C_DIGIPOT) - digipot_i2c_set_current(Z_AXIS, previous_current) + stepper.set_digipot_current(1, previous_current); + #elif HAS_MOTOR_CURRENT_DAC + stepper_dac.set_current_value(Z_AXIS, previous_current); + #elif ENABLED(HAS_MOTOR_CURRENT_I2C) + digipot_i2c.set_current(Z_AXIS, previous_current) #elif HAS_TRINAMIC_CONFIG #if AXIS_IS_TMC(Z) stepperZ.rms_current(previous_current_arr[0]); diff --git a/Marlin/src/gcode/feature/digipot/M907-M910.cpp b/Marlin/src/gcode/feature/digipot/M907-M910.cpp index 54599a0451..e463666207 100644 --- a/Marlin/src/gcode/feature/digipot/M907-M910.cpp +++ b/Marlin/src/gcode/feature/digipot/M907-M910.cpp @@ -22,19 +22,19 @@ #include "../../../inc/MarlinConfig.h" -#if ANY(HAS_DIGIPOTSS, HAS_MOTOR_CURRENT_PWM, HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT) +#if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC) #include "../../gcode.h" -#if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM +#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM #include "../../../module/stepper.h" #endif -#if HAS_I2C_DIGIPOT +#if HAS_MOTOR_CURRENT_I2C #include "../../../feature/digipot/digipot.h" #endif -#if ENABLED(DAC_STEPPER_CURRENT) +#if ENABLED(HAS_MOTOR_CURRENT_DAC) #include "../../../feature/dac/stepper_dac.h" #endif @@ -42,61 +42,61 @@ * M907: Set digital trimpot motor current using axis codes X, Y, Z, E, B, S */ void GcodeSuite::M907() { - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI - LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper.digipot_current(i, parser.value_int()); - if (parser.seenval('B')) stepper.digipot_current(4, parser.value_int()); - if (parser.seenval('S')) LOOP_LE_N(i, 4) stepper.digipot_current(i, parser.value_int()); + LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper.set_digipot_current(i, parser.value_int()); + if (parser.seenval('B')) stepper.set_digipot_current(4, parser.value_int()); + if (parser.seenval('S')) LOOP_LE_N(i, 4) stepper.set_digipot_current(i, parser.value_int()); #elif HAS_MOTOR_CURRENT_PWM #if ANY_PIN(MOTOR_CURRENT_PWM_X, MOTOR_CURRENT_PWM_Y, MOTOR_CURRENT_PWM_XY) - if (parser.seenval('X') || parser.seenval('Y')) stepper.digipot_current(0, parser.value_int()); + if (parser.seenval('X') || parser.seenval('Y')) stepper.set_digipot_current(0, parser.value_int()); #endif #if PIN_EXISTS(MOTOR_CURRENT_PWM_Z) - if (parser.seenval('Z')) stepper.digipot_current(1, parser.value_int()); + if (parser.seenval('Z')) stepper.set_digipot_current(1, parser.value_int()); #endif #if PIN_EXISTS(MOTOR_CURRENT_PWM_E) - if (parser.seenval('E')) stepper.digipot_current(2, parser.value_int()); + if (parser.seenval('E')) stepper.set_digipot_current(2, parser.value_int()); #endif #endif - #if HAS_I2C_DIGIPOT + #if HAS_MOTOR_CURRENT_I2C // this one uses actual amps in floating point - LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) digipot_i2c_set_current(i, parser.value_float()); + LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) digipot_i2c.set_current(i, parser.value_float()); // Additional extruders use B,C,D for channels 4,5,6. // TODO: Change these parameters because 'E' is used. B? for (uint8_t i = E_AXIS + 1; i < DIGIPOT_I2C_NUM_CHANNELS; i++) - if (parser.seenval('B' + i - (E_AXIS + 1))) digipot_i2c_set_current(i, parser.value_float()); + if (parser.seenval('B' + i - (E_AXIS + 1))) digipot_i2c.set_current(i, parser.value_float()); #endif - #if ENABLED(DAC_STEPPER_CURRENT) + #if ENABLED(HAS_MOTOR_CURRENT_DAC) if (parser.seenval('S')) { const float dac_percent = parser.value_float(); - LOOP_LE_N(i, 4) dac_current_percent(i, dac_percent); + LOOP_LE_N(i, 4) stepper_dac.set_current_percent(i, dac_percent); } - LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) dac_current_percent(i, parser.value_float()); + LOOP_XYZE(i) if (parser.seenval(axis_codes[i])) stepper_dac.set_current_percent(i, parser.value_float()); #endif } -#if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT) +#if EITHER(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC) /** * M908: Control digital trimpot directly (M908 P S) */ void GcodeSuite::M908() { - TERN_(HAS_DIGIPOTSS, stepper.digitalPotWrite(parser.intval('P'), parser.intval('S'))); - TERN_(DAC_STEPPER_CURRENT, dac_current_raw(parser.byteval('P', -1), parser.ushortval('S', 0))); + TERN_(HAS_MOTOR_CURRENT_SPI, stepper.set_digipot_value_spi(parser.intval('P'), parser.intval('S'))); + TERN_(HAS_MOTOR_CURRENT_DAC, stepper_dac.set_current_value(parser.byteval('P', -1), parser.ushortval('S', 0))); } -#endif // HAS_DIGIPOTSS || DAC_STEPPER_CURRENT + #if ENABLED(HAS_MOTOR_CURRENT_DAC) -#if ENABLED(DAC_STEPPER_CURRENT) + void GcodeSuite::M909() { stepper_dac.print_values(); } + void GcodeSuite::M910() { stepper_dac.commit_eeprom(); } - void GcodeSuite::M909() { dac_print_values(); } - void GcodeSuite::M910() { dac_commit_eeprom(); } + #endif // HAS_MOTOR_CURRENT_DAC -#endif // DAC_STEPPER_CURRENT +#endif // HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_DAC -#endif // HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM || HAS_I2C_DIGIPOT || DAC_STEPPER_CURRENT +#endif // HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_I2C || HAS_MOTOR_CURRENT_DAC diff --git a/Marlin/src/gcode/gcode.cpp b/Marlin/src/gcode/gcode.cpp index 6d1234250d..870ddadad5 100644 --- a/Marlin/src/gcode/gcode.cpp +++ b/Marlin/src/gcode/gcode.cpp @@ -826,11 +826,11 @@ void GcodeSuite::process_parsed_command(const bool no_ok/*=false*/) { case 900: M900(); break; // M900: Set advance K factor. #endif - #if ANY(HAS_DIGIPOTSS, HAS_MOTOR_CURRENT_PWM, HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT) + #if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC) case 907: M907(); break; // M907: Set digital trimpot motor current using axis codes. - #if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT) + #if EITHER(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC) case 908: M908(); break; // M908: Control digital trimpot directly. - #if ENABLED(DAC_STEPPER_CURRENT) + #if ENABLED(HAS_MOTOR_CURRENT_DAC) case 909: M909(); break; // M909: Print digipot/DAC current value case 910: M910(); break; // M910: Commit digipot/DAC value to external EEPROM #endif diff --git a/Marlin/src/gcode/gcode.h b/Marlin/src/gcode/gcode.h index 1195950c92..ad1c0d55bb 100644 --- a/Marlin/src/gcode/gcode.h +++ b/Marlin/src/gcode/gcode.h @@ -258,9 +258,9 @@ * M900 - Get or Set Linear Advance K-factor. (Requires LIN_ADVANCE) * M906 - Set or get motor current in milliamps using axis codes X, Y, Z, E. Report values if no axis codes given. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660 or L6470) * M907 - Set digital trimpot motor current using axis codes. (Requires a board with digital trimpots) - * M908 - Control digital trimpot directly. (Requires DAC_STEPPER_CURRENT or DIGIPOTSS_PIN) - * M909 - Print digipot/DAC current value. (Requires DAC_STEPPER_CURRENT) - * M910 - Commit digipot/DAC value to external EEPROM via I2C. (Requires DAC_STEPPER_CURRENT) + * M908 - Control digital trimpot directly. (Requires HAS_MOTOR_CURRENT_DAC or DIGIPOTSS_PIN) + * M909 - Print digipot/DAC current value. (Requires HAS_MOTOR_CURRENT_DAC) + * M910 - Commit digipot/DAC value to external EEPROM via I2C. (Requires HAS_MOTOR_CURRENT_DAC) * M911 - Report stepper driver overtemperature pre-warn condition. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660) * M912 - Clear stepper driver overtemperature pre-warn condition flag. (Requires at least one _DRIVER_TYPE defined as TMC2130/2160/5130/5160/2208/2209/2660) * M913 - Set HYBRID_THRESHOLD speed. (Requires HYBRID_THRESHOLD) @@ -847,11 +847,11 @@ private: static void M918(); #endif - #if ANY(HAS_DIGIPOTSS, HAS_MOTOR_CURRENT_PWM, HAS_I2C_DIGIPOT, DAC_STEPPER_CURRENT) + #if ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_I2C, HAS_MOTOR_CURRENT_DAC) static void M907(); - #if EITHER(HAS_DIGIPOTSS, DAC_STEPPER_CURRENT) + #if EITHER(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC) static void M908(); - #if ENABLED(DAC_STEPPER_CURRENT) + #if ENABLED(HAS_MOTOR_CURRENT_DAC) static void M909(); static void M910(); #endif diff --git a/Marlin/src/inc/Conditionals_adv.h b/Marlin/src/inc/Conditionals_adv.h index 73e034d05f..a793ed90bf 100644 --- a/Marlin/src/inc/Conditionals_adv.h +++ b/Marlin/src/inc/Conditionals_adv.h @@ -130,7 +130,7 @@ #endif #if EITHER(DIGIPOT_MCP4018, DIGIPOT_MCP4451) - #define HAS_I2C_DIGIPOT 1 + #define HAS_MOTOR_CURRENT_I2C 1 #endif // Multiple Z steppers diff --git a/Marlin/src/inc/Conditionals_post.h b/Marlin/src/inc/Conditionals_post.h index c9a29b351c..0313c3d036 100644 --- a/Marlin/src/inc/Conditionals_post.h +++ b/Marlin/src/inc/Conditionals_post.h @@ -1972,7 +1972,7 @@ #define HAS_STEPPER_RESET 1 #endif #if PIN_EXISTS(DIGIPOTSS) - #define HAS_DIGIPOTSS 1 + #define HAS_MOTOR_CURRENT_SPI 1 #endif #if ANY_PIN(MOTOR_CURRENT_PWM_X, MOTOR_CURRENT_PWM_Y, MOTOR_CURRENT_PWM_XY, MOTOR_CURRENT_PWM_Z, MOTOR_CURRENT_PWM_E) #define HAS_MOTOR_CURRENT_PWM 1 diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index ffa773b912..78e6d4acf4 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -2612,7 +2612,7 @@ static_assert(hbm[Z_AXIS] >= 0, "HOMING_BUMP_MM.Z must be greater than or equal /** * Digipot requirement */ -#if HAS_I2C_DIGIPOT +#if HAS_MOTOR_CURRENT_I2C #if BOTH(DIGIPOT_MCP4018, DIGIPOT_MCP4451) #error "Enable only one of DIGIPOT_MCP4018 or DIGIPOT_MCP4451." #elif !MB(MKS_SBASE) \ @@ -2734,7 +2734,7 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #endif #if ENABLED(MECHANICAL_GANTRY_CALIBRATION) - #if NONE(DAC_STEPPER_CURRENT, HAS_DIGIPOTSS, DAC_STEPPER_CURRENT, HAS_TRINAMIC_CONFIG, HAS_MOTOR_CURRENT_PWM) + #if NONE(HAS_MOTOR_CURRENT_DAC, HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC, HAS_TRINAMIC_CONFIG, HAS_MOTOR_CURRENT_PWM) #error "It is highly reccomended to have adjustable current drivers to prevent damage. Disable this line to continue anyway." #else #ifndef GANTRY_CALIBRATION_CURRENT diff --git a/Marlin/src/lcd/menu/menu_advanced.cpp b/Marlin/src/lcd/menu/menu_advanced.cpp index 5bca89141f..fc9d8c5bd6 100644 --- a/Marlin/src/lcd/menu/menu_advanced.cpp +++ b/Marlin/src/lcd/menu/menu_advanced.cpp @@ -58,16 +58,16 @@ void menu_tmc(); void menu_backlash(); -#if ENABLED(DAC_STEPPER_CURRENT) +#if ENABLED(HAS_MOTOR_CURRENT_DAC) #include "../../feature/dac/stepper_dac.h" void menu_dac() { static xyze_uint8_t driverPercent; - LOOP_XYZE(i) driverPercent[i] = dac_current_get_percent((AxisEnum)i); + LOOP_XYZE(i) driverPercent[i] = stepper_dac.get_current_percent((AxisEnum)i); START_MENU(); BACK_ITEM(MSG_ADVANCED_SETTINGS); - #define EDIT_DAC_PERCENT(A) EDIT_ITEM(uint8, MSG_DAC_PERCENT_##A, &driverPercent[_AXIS(A)], 0, 100, []{ dac_current_set_percents(driverPercent); }) + #define EDIT_DAC_PERCENT(A) EDIT_ITEM(uint8, MSG_DAC_PERCENT_##A, &driverPercent[_AXIS(A)], 0, 100, []{ stepper_dac.set_current_percents(driverPercent); }) EDIT_DAC_PERCENT(X); EDIT_DAC_PERCENT(Y); EDIT_DAC_PERCENT(Z); @@ -563,7 +563,7 @@ void menu_advanced_settings() { SUBMENU(MSG_BACKLASH, menu_backlash); #endif - #if ENABLED(DAC_STEPPER_CURRENT) + #if ENABLED(HAS_MOTOR_CURRENT_DAC) SUBMENU(MSG_DRIVE_STRENGTH, menu_dac); #endif #if HAS_MOTOR_CURRENT_PWM diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index efd0596129..29ac1504bd 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -366,7 +366,7 @@ typedef struct SettingsDataStruct { // // HAS_MOTOR_CURRENT_PWM // - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI uint32_t motor_current_setting[COUNT(stepper.motor_current_setting)]; #else uint32_t motor_current_setting[3]; // M907 X Z E @@ -1282,7 +1282,7 @@ void MarlinSettings::postprocess() { { _FIELD_TEST(motor_current_setting); - #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI EEPROM_WRITE(stepper.motor_current_setting); #else const uint32_t no_current[3] = { 0 }; @@ -2116,7 +2116,7 @@ void MarlinSettings::postprocess() { // { _FIELD_TEST(motor_current_setting); - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI uint32_t motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; #else uint32_t motor_current_setting[3]; @@ -2124,7 +2124,7 @@ void MarlinSettings::postprocess() { DEBUG_ECHOLNPGM("DIGIPOTS Loading"); EEPROM_READ(motor_current_setting); DEBUG_ECHOLNPGM("DIGIPOTS Loaded"); - #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI if (!validating) COPY(stepper.motor_current_setting, motor_current_setting); #endif @@ -2804,17 +2804,17 @@ void MarlinSettings::reset() { #if HAS_MOTOR_CURRENT_PWM constexpr uint32_t tmp_motor_current_setting[3] = PWM_MOTOR_CURRENT; LOOP_L_N(q, 3) - stepper.digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q])); + stepper.set_digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q])); #endif // // DIGIPOTS // - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI static constexpr uint32_t tmp_motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; DEBUG_ECHOLNPGM("Writing Digipot"); LOOP_L_N(q, COUNT(tmp_motor_current_setting)) - stepper.digipot_current(q, tmp_motor_current_setting[q]); + stepper.set_digipot_current(q, tmp_motor_current_setting[q]); DEBUG_ECHOLNPGM("Digipot Written"); #endif @@ -3717,7 +3717,7 @@ void MarlinSettings::reset() { #endif #endif - #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI CONFIG_ECHO_HEADING("Stepper motor currents:"); CONFIG_ECHO_START(); #if HAS_MOTOR_CURRENT_PWM @@ -3726,7 +3726,7 @@ void MarlinSettings::reset() { , SP_Z_STR, stepper.motor_current_setting[1] , SP_E_STR, stepper.motor_current_setting[2] ); - #elif HAS_DIGIPOTSS + #elif HAS_MOTOR_CURRENT_SPI SERIAL_ECHOPGM(" M907"); LOOP_L_N(q, COUNT(stepper.motor_current_setting)) { SERIAL_CHAR(' '); diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index ba3162b219..c2646b6989 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -83,10 +83,6 @@ Stepper stepper; // Singleton #define BABYSTEPPING_EXTRA_DIR_WAIT -#if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS - bool Stepper::initialized; // = false -#endif - #ifdef __AVR__ #include "speed_lookuptable.h" #endif @@ -110,7 +106,7 @@ Stepper stepper; // Singleton #include "../feature/dac/dac_dac084s085.h" #endif -#if HAS_DIGIPOTSS +#if HAS_MOTOR_CURRENT_SPI #include #endif @@ -142,11 +138,12 @@ Stepper stepper; // Singleton bool Stepper::separate_multi_axis = false; #endif -#if HAS_MOTOR_CURRENT_PWM - uint32_t Stepper::motor_current_setting[3]; // Initialized by settings.load() -#elif HAS_DIGIPOTSS - constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT; - uint32_t Stepper::motor_current_setting[COUNT(digipot_count)]; // Initialized by settings.load() +#if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + bool Stepper::initialized; // = false + uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load() + #if HAS_MOTOR_CURRENT_SPI + constexpr uint32_t Stepper::digipot_count[]; + #endif #endif // private: @@ -2593,7 +2590,7 @@ void Stepper::init() { set_directions(); - #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM initialized = true; digipot_init(); #endif @@ -2933,10 +2930,10 @@ void Stepper::report_positions() { * Software-controlled Stepper Motor Current */ -#if HAS_DIGIPOTSS +#if HAS_MOTOR_CURRENT_SPI // From Arduino DigitalPotControl example - void Stepper::digitalPotWrite(const int16_t address, const int16_t value) { + void Stepper::set_digipot_value_spi(const int16_t address, const int16_t value) { WRITE(DIGIPOTSS_PIN, LOW); // Take the SS pin low to select the chip SPI.transfer(address); // Send the address and value via SPI SPI.transfer(value); @@ -2944,7 +2941,7 @@ void Stepper::report_positions() { //delay(10); } -#endif // HAS_DIGIPOTSS +#endif // HAS_MOTOR_CURRENT_SPI #if HAS_MOTOR_CURRENT_PWM @@ -2961,7 +2958,7 @@ void Stepper::report_positions() { #if ANY_PIN(MOTOR_CURRENT_PWM_E, MOTOR_CURRENT_PWM_E0, MOTOR_CURRENT_PWM_E1) case 2: #endif - digipot_current(i, motor_current_setting[i]); + set_digipot_current(i, motor_current_setting[i]); default: break; } } @@ -2971,23 +2968,23 @@ void Stepper::report_positions() { #if !MB(PRINTRBOARD_G2) - #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM - void Stepper::digipot_current(const uint8_t driver, const int16_t current) { + void Stepper::set_digipot_current(const uint8_t driver, const int16_t current) { if (WITHIN(driver, 0, COUNT(motor_current_setting) - 1)) motor_current_setting[driver] = current; // update motor_current_setting if (!initialized) return; - #if HAS_DIGIPOTSS - SERIAL_ECHOLNPAIR("Digipotss current ", current); + #if HAS_MOTOR_CURRENT_SPI + + //SERIAL_ECHOLNPAIR("Digipotss current ", current); + const uint8_t digipot_ch[] = DIGIPOT_CHANNELS; - digitalPotWrite(digipot_ch[driver], current); + set_digipot_value_spi(digipot_ch[driver], current); #elif HAS_MOTOR_CURRENT_PWM - - #define _WRITE_CURRENT_PWM(P) analogWrite(pin_t(MOTOR_CURRENT_PWM_## P ##_PIN), 255L * current / (MOTOR_CURRENT_PWM_RANGE)) switch (driver) { case 0: @@ -3023,15 +3020,13 @@ void Stepper::report_positions() { void Stepper::digipot_init() { - #if HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_SPI SPI.begin(); SET_OUTPUT(DIGIPOTSS_PIN); - LOOP_L_N(i, COUNT(motor_current_setting)) { - //digitalPotWrite(digipot_ch[i], digipot_motor_current[i]); - digipot_current(i, motor_current_setting[i]); - } + LOOP_L_N(i, COUNT(motor_current_setting)) + set_digipot_current(i, motor_current_setting[i]); #elif HAS_MOTOR_CURRENT_PWM diff --git a/Marlin/src/module/stepper.h b/Marlin/src/module/stepper.h index 5b8f83a5fc..2fae151d91 100644 --- a/Marlin/src/module/stepper.h +++ b/Marlin/src/module/stepper.h @@ -245,16 +245,18 @@ class Stepper { static bool separate_multi_axis; #endif - #if HAS_MOTOR_CURRENT_PWM - #ifndef PWM_MOTOR_CURRENT - #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT + #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_PWM + #ifndef PWM_MOTOR_CURRENT + #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT + #endif + #define MOTOR_CURRENT_COUNT 3 + #elif HAS_MOTOR_CURRENT_SPI + static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT; + #define MOTOR_CURRENT_COUNT COUNT(Stepper::digipot_count) #endif static bool initialized; - static uint32_t motor_current_setting[3]; - #elif HAS_DIGIPOTSS - static bool initialized; - static constexpr uint32_t digipot_count[] = DIGIPOT_MOTOR_CURRENT; - static uint32_t motor_current_setting[COUNT(digipot_count)]; // Initialized by settings.load() + static uint32_t motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load() #endif // Last-moved extruder, as set when the last movement was fetched from planner @@ -461,9 +463,9 @@ class Stepper { // Triggered position of an axis in steps static int32_t triggered_position(const AxisEnum axis); - #if HAS_DIGIPOTSS || HAS_MOTOR_CURRENT_PWM - static void digitalPotWrite(const int16_t address, const int16_t value); - static void digipot_current(const uint8_t driver, const int16_t current); + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM + static void set_digipot_value_spi(const int16_t address, const int16_t value); + static void set_digipot_current(const uint8_t driver, const int16_t current); #endif #if HAS_MICROSTEPS @@ -586,7 +588,7 @@ class Stepper { static int32_t _eval_bezier_curve(const uint32_t curr_step); #endif - #if HAS_MOTOR_CURRENT_PWM || HAS_DIGIPOTSS + #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI static void digipot_init(); #endif diff --git a/Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h b/Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h index 406cbec816..1428de31a2 100644 --- a/Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h +++ b/Marlin/src/pins/ramps/pins_RIGIDBOARD_V2.h @@ -34,7 +34,7 @@ // // I2C based DAC like on the Printrboard REVF -#define DAC_STEPPER_CURRENT +#define HAS_MOTOR_CURRENT_DAC // Channels available for DAC, For Rigidboard there are 4 #define DAC_STEPPER_ORDER { 0, 1, 2, 3 } diff --git a/Marlin/src/pins/teensy2/pins_PRINTRBOARD_REVF.h b/Marlin/src/pins/teensy2/pins_PRINTRBOARD_REVF.h index 3fb8c7f259..ba933e73bc 100644 --- a/Marlin/src/pins/teensy2/pins_PRINTRBOARD_REVF.h +++ b/Marlin/src/pins/teensy2/pins_PRINTRBOARD_REVF.h @@ -143,7 +143,7 @@ #endif // NO_EXTRUDRBOARD // Enable control of stepper motor currents with the I2C based MCP4728 DAC used on Printrboard REVF -#define DAC_STEPPER_CURRENT +#define HAS_MOTOR_CURRENT_DAC // Set default drive strength percents if not already defined - X, Y, Z, E axis #ifndef DAC_MOTOR_CURRENT_DEFAULT diff --git a/platformio.ini b/platformio.ini index f8ec029de3..060bd73380 100644 --- a/platformio.ini +++ b/platformio.ini @@ -199,7 +199,7 @@ HAS_TRINAMIC_CONFIG = TMCStepper@~0.7.1 src_filter=+ + + + + HAS_STEALTHCHOP = src_filter=+ SR_LCD_3W_NL = SailfishLCD=https://github.com/mikeshub/SailfishLCD/archive/master.zip -HAS_I2C_DIGIPOT = SlowSoftI2CMaster +HAS_MOTOR_CURRENT_I2C = SlowSoftI2CMaster src_filter=+ HAS_TMC26X = TMC26XStepper=https://github.com/trinamic/TMC26XStepper/archive/master.zip src_filter=+ @@ -260,7 +260,7 @@ CANCEL_OBJECTS = src_filter=+ + + EXTERNAL_CLOSED_LOOP_CONTROLLER = src_filter=+ + USE_CONTROLLER_FAN = src_filter=+ -DAC_STEPPER_CURRENT = src_filter=+ +HAS_MOTOR_CURRENT_DAC = src_filter=+ DIRECT_STEPPING = src_filter=+ + EMERGENCY_PARSER = src_filter=+ - I2C_POSITION_ENCODERS = src_filter=+ From 682b0b440dfb50b9d3e3fd0db6755a7f568b2885 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Wed, 12 Aug 2020 21:44:27 -0500 Subject: [PATCH 25/42] reorder --- Marlin/src/module/settings.cpp | 6 +++--- Marlin/src/module/stepper.cpp | 2 +- Marlin/src/module/stepper.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index 29ac1504bd..29ebce1e7d 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -1282,7 +1282,7 @@ void MarlinSettings::postprocess() { { _FIELD_TEST(motor_current_setting); - #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM EEPROM_WRITE(stepper.motor_current_setting); #else const uint32_t no_current[3] = { 0 }; @@ -2124,7 +2124,7 @@ void MarlinSettings::postprocess() { DEBUG_ECHOLNPGM("DIGIPOTS Loading"); EEPROM_READ(motor_current_setting); DEBUG_ECHOLNPGM("DIGIPOTS Loaded"); - #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM if (!validating) COPY(stepper.motor_current_setting, motor_current_setting); #endif @@ -3717,7 +3717,7 @@ void MarlinSettings::reset() { #endif #endif - #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM CONFIG_ECHO_HEADING("Stepper motor currents:"); CONFIG_ECHO_START(); #if HAS_MOTOR_CURRENT_PWM diff --git a/Marlin/src/module/stepper.cpp b/Marlin/src/module/stepper.cpp index c2646b6989..8a5de4b608 100644 --- a/Marlin/src/module/stepper.cpp +++ b/Marlin/src/module/stepper.cpp @@ -138,7 +138,7 @@ Stepper stepper; // Singleton bool Stepper::separate_multi_axis = false; #endif -#if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI +#if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM bool Stepper::initialized; // = false uint32_t Stepper::motor_current_setting[MOTOR_CURRENT_COUNT]; // Initialized by settings.load() #if HAS_MOTOR_CURRENT_SPI diff --git a/Marlin/src/module/stepper.h b/Marlin/src/module/stepper.h index 2fae151d91..572c3f3f9b 100644 --- a/Marlin/src/module/stepper.h +++ b/Marlin/src/module/stepper.h @@ -245,7 +245,7 @@ class Stepper { static bool separate_multi_axis; #endif - #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM #if HAS_MOTOR_CURRENT_PWM #ifndef PWM_MOTOR_CURRENT #define PWM_MOTOR_CURRENT DEFAULT_PWM_MOTOR_CURRENT @@ -588,7 +588,7 @@ class Stepper { static int32_t _eval_bezier_curve(const uint32_t curr_step); #endif - #if HAS_MOTOR_CURRENT_PWM || HAS_MOTOR_CURRENT_SPI + #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM static void digipot_init(); #endif From a55cef1165c0a367534bfd9121ca7a3019f47509 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 13 Aug 2020 01:33:53 -0500 Subject: [PATCH 26/42] Provide MSG_ITERATION --- Marlin/src/gcode/calibrate/G34_M422.cpp | 17 +++++++++-------- Marlin/src/lcd/language/language_en.h | 1 + 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 70768350bb..878c44fd0e 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -161,7 +161,7 @@ void GcodeSuite::G34() { #if HAS_DISPLAY PGM_P const msg_iteration = GET_TEXT(MSG_ITERATION); - const iter_str_len = strlen_P(iter_str_len); + const uint8_t iter_str_len = strlen_P(iter_str_len); #endif // Final z and iteration values will be used after breaking the loop @@ -172,7 +172,7 @@ void GcodeSuite::G34() { if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("> probing all positions."); const int iter = iteration + 1; - SERIAL_ECHOLNPAIR("\nIteration: ", iter); + SERIAL_ECHOLNPAIR("\nG34 Iteration: ", iter); #if HAS_DISPLAY char str[iter_str_len + 2 + 1]; sprintf_P(str, msg_iteration, iter); @@ -260,16 +260,17 @@ void GcodeSuite::G34() { #endif ); #if HAS_DISPLAY - char msg[30], fstr[16]; + char msg[30], fstr1[16], fstr2[16], fstr3[16]; sprintf_P( - PSTR("DIFFERENCE Z1-Z2=%s" + PSTR("Diffs Z1-Z2=%s" #if NUM_Z_STEPPER_DRIVERS == 3 - " Z2-Z3=%s Z3-Z1=%s" + " Z2-Z3=%s" + " Z3-Z1=%s" #endif - ), dtostrf(ABS(z_measured[0] - z_measured[1]), 1, 3, fstr) + ), dtostrf(ABS(z_measured[0] - z_measured[1]), 1, 3, fstr1) #if NUM_Z_STEPPER_DRIVERS == 3 - , dtostrf(ABS(z_measured[1] - z_measured[2]), 1, 3, fstr) - , dtostrf(ABS(z_measured[2] - z_measured[0]), 1, 3, fstr) + , dtostrf(ABS(z_measured[1] - z_measured[2]), 1, 3, fstr2) + , dtostrf(ABS(z_measured[2] - z_measured[0]), 1, 3, fstr3) #endif ); ui.set_status(msg); diff --git a/Marlin/src/lcd/language/language_en.h b/Marlin/src/lcd/language/language_en.h index 55590db6d8..e2bd20ea09 100644 --- a/Marlin/src/lcd/language/language_en.h +++ b/Marlin/src/lcd/language/language_en.h @@ -68,6 +68,7 @@ namespace Language_en { PROGMEM Language_Str MSG_AUTO_HOME_Z = _UxGT("Home Z"); PROGMEM Language_Str MSG_AUTO_Z_ALIGN = _UxGT("Auto Z-Align"); PROGMEM Language_Str MSG_ASSISTED_TRAMMING = _UxGT("Assisted Tramming"); + PROGMEM Language_Str MSG_ITERATION = _UxGT("G34 Iteration: %i"); PROGMEM Language_Str MSG_DECREASING_ACCURACY = _UxGT("Accuracy Decreasing!"); PROGMEM Language_Str MSG_ACCURACY_ACHIEVED = _UxGT("Accuracy Achieved"); PROGMEM Language_Str MSG_LEVEL_BED_HOMING = _UxGT("Homing XYZ"); From ce2874b73224f858a1f25660f1f3775b5f6c220a Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 13 Aug 2020 03:39:59 -0500 Subject: [PATCH 27/42] Clean up G34/M422 --- Marlin/src/gcode/calibrate/G34_M422.cpp | 102 ++++++++++++------------ 1 file changed, 50 insertions(+), 52 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 878c44fd0e..8d1792bace 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -20,26 +20,23 @@ * */ -#include "../../inc/MarlinConfig.h" +#include "../../inc/MarlinConfigPre.h" + +#if ENABLED(Z_STEPPER_AUTO_ALIGN) + +#include "../../feature/z_stepper_align.h" #include "../gcode.h" #include "../../module/planner.h" #include "../../module/stepper.h" #include "../../module/motion.h" #include "../../module/probe.h" -#include "../../module/endstops.h" +#include "../../lcd/ultralcd.h" // for LCD_MESSAGEPGM #if HAS_LEVELING #include "../../feature/bedlevel/bedlevel.h" #endif -#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) -#include "../../core/debug_out.h" - -#if ENABLED(Z_STEPPER_AUTO_ALIGN) - -#include "../../feature/z_stepper_align.h" - #if HAS_MULTI_HOTEND #include "../../module/tool_change.h" #endif @@ -48,6 +45,9 @@ #include "../../libs/least_squares_fit.h" #endif +#define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) +#include "../../core/debug_out.h" + /** * G34: Z-Stepper automatic alignment * @@ -503,6 +503,9 @@ void GcodeSuite::M422() { } #elif ENABLED(MECHANICAL_GANTRY_CALIBRATION) + + #include "../../module/endstops.h" + void GcodeSuite::G34() { if (axis_unhomed_error()) return; @@ -513,35 +516,43 @@ void GcodeSuite::M422() { #ifdef GANTRY_CALIBRATION_COMMANDS_PRE gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_PRE)); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Sub Commands Processed"); #endif - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Sub Commands Processed"); - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Parking XY"); - // Move XY to safe position #ifdef GANTRY_CALIBRATION_SAFE_POSITION - xy_pos_t safe_pos = GANTRY_CALIBRATION_SAFE_POSITION; - current_position[X_AXIS] = safe_pos[X_AXIS]; - current_position[Y_AXIS] = safe_pos[Y_AXIS]; - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE)); - planner.synchronize(); + // Move XY to safe position + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Parking XY"); + const xy_pos_t safe_pos = GANTRY_CALIBRATION_SAFE_POSITION; + do_blocking_move_to(safe_pos, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE)); #endif - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Setting Z Pounce"); - const uint16_t move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT); + const uint16_t move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT), + zpounce = ( + #if GANTRY_CALIBRATION_DIRECTION == 1 + (Z_MAX_POS) - move_distance + #else + (Z_MIN_POS) + move_distance + #endif + ), + zgrind = ( + #if GANTRY_CALIBRATION_DIRECTION == 1 + (Z_MAX_POS) + move_distance + #else + (Z_MIN_POS) - move_distance + #endif + ); // Move Z to pounce position - #if GANTRY_CALIBRATION_DIRECTION == 1 - current_position[Z_AXIS] = (Z_MAX_POS - move_distance); - #else - current_position[Z_AXIS] = (Z_MIN_POS + move_distance); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Setting Z Pounce"); + do_blocking_move_to_z(zpounce, MMM_TO_MMS(HOMING_FEEDRATE_Z)); + + // Store current motor settings, then apply reduced value + + #define _REDUCE_CURRENT ANY(HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_PWM, HAS_MOTOR_CURRENT_DAC, HAS_MOTOR_CURRENT_I2C, HAS_TRINAMIC_CONFIG) + #if _REDUCE_CURRENT + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Reducing Current"); #endif - do_blocking_move_to(current_position, MMM_TO_MMS(HOMING_FEEDRATE_Z)); - planner.synchronize(); - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Reducing Current"); - // Store current motor settings, then apply reduced value #if HAS_MOTOR_CURRENT_SPI const uint16_t target_current = parser.intval('S', GANTRY_CALIBRATION_CURRENT); const uint32_t previous_current = stepper.motor_current_setting[Z_AXIS]; @@ -579,32 +590,20 @@ void GcodeSuite::M422() { #endif #endif - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Final Z Move"); // Do Final Z move to adjust - #if GANTRY_CALIBRATION_DIRECTION == 1 - current_position[Z_AXIS] = (Z_MAX_POS + move_distance); - #else - current_position[Z_AXIS] = (Z_MIN_POS - move_distance); - #endif + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Final Z Move"); + do_blocking_move_to_z(zgrind, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); - planner.synchronize(); - - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Z Backoff"); // Back off end plate, back to normal motion range - #if GANTRY_CALIBRATION_DIRECTION == 1 - current_position[Z_AXIS] = (Z_MAX_POS - move_distance); - #else - current_position[Z_AXIS] = (Z_MIN_POS + move_distance); + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Z Backoff"); + do_blocking_move_to_z(zpounce, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); + + // Reset current to original values + + #if _REDUCE_CURRENT + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Restore Current"); #endif - do_blocking_move_to(current_position, MMM_TO_MMS(GANTRY_CALIBRATION_FEEDRATE)); - planner.synchronize(); - - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Restore Current"); - // Reset current to original values #if HAS_MOTOR_CURRENT_SPI stepper.set_digipot_current(Z_AXIS, previous_current); #elif HAS_MOTOR_CURRENT_PWM @@ -628,9 +627,8 @@ void GcodeSuite::M422() { #endif #endif - - if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Running Post Commands"); #ifdef GANTRY_CALIBRATION_COMMANDS_POST + if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Running Post Commands"); gcode.process_subcommands_now_P(PSTR(GANTRY_CALIBRATION_COMMANDS_POST)); #endif } From f0c531abb6e2ab189c5b22186a289caf7bee74ba Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 13 Aug 2020 17:55:49 -0500 Subject: [PATCH 28/42] helpful patch --- Marlin/src/gcode/calibrate/G34_M422.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 8d1792bace..07e2bee488 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -260,7 +260,10 @@ void GcodeSuite::G34() { #endif ); #if HAS_DISPLAY - char msg[30], fstr1[16], fstr2[16], fstr3[16]; + char msg[30], fstr1[16]; + #if NUM_Z_STEPPER_DRIVERS == 3 + char fstr2[16], fstr3[16]; + #endif sprintf_P( PSTR("Diffs Z1-Z2=%s" #if NUM_Z_STEPPER_DRIVERS == 3 @@ -294,7 +297,7 @@ void GcodeSuite::G34() { // If it's getting worse, stop and throw an error if (last_z_align_level_indicator < z_align_level_indicator * 0.7f) { SERIAL_ECHOLNPGM("Decreasing Accuracy Detected."); - LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY)); + LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY); err_break = true; break; } @@ -319,7 +322,7 @@ void GcodeSuite::G34() { // Check for less accuracy compared to last move if (last_z_align_move[zstepper] < z_align_abs * 0.7f) { SERIAL_ECHOLNPGM("Decreasing Accuracy Detected."); - LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY)); + LCD_MESSAGEPGM(MSG_DECREASING_ACCURACY); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " last_z_align_move = ", last_z_align_move[zstepper]); if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPAIR("> Z", int(zstepper + 1), " z_align_abs = ", z_align_abs); adjustment_reverse = !adjustment_reverse; From 3130fd9532b3f2b3afde9847017c7200f7996ad5 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 13 Aug 2020 20:27:55 -0500 Subject: [PATCH 29/42] Fix sprintf_P call --- Marlin/src/gcode/calibrate/G34_M422.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 07e2bee488..eeb18a9ca8 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -260,11 +260,13 @@ void GcodeSuite::G34() { #endif ); #if HAS_DISPLAY - char msg[30], fstr1[16]; - #if NUM_Z_STEPPER_DRIVERS == 3 - char fstr2[16], fstr3[16]; + char fstr1[10]; + #if NUM_Z_STEPPER_DRIVERS == 2 + char msg[6 + (6 + 5) * 1 + 1]; + #else + char msg[6 + (6 + 5) * 3 + 1], fstr2[10], fstr3[10]; #endif - sprintf_P( + sprintf_P(msg, PSTR("Diffs Z1-Z2=%s" #if NUM_Z_STEPPER_DRIVERS == 3 " Z2-Z3=%s" From 5d74ace99f2704d6a7fc41fc93aebe45aabf0302 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Thu, 13 Aug 2020 20:43:13 -0500 Subject: [PATCH 30/42] =?UTF-8?q?=E2=80=A6and=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Marlin/src/gcode/calibrate/G34_M422.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index eeb18a9ca8..e0b7965a2f 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -161,7 +161,7 @@ void GcodeSuite::G34() { #if HAS_DISPLAY PGM_P const msg_iteration = GET_TEXT(MSG_ITERATION); - const uint8_t iter_str_len = strlen_P(iter_str_len); + const uint8_t iter_str_len = strlen_P(msg_iteration); #endif // Final z and iteration values will be used after breaking the loop From 050cef7b48699c9593c8cd9e5b9e4522c49fdd00 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Thu, 17 Sep 2020 11:16:01 -0400 Subject: [PATCH 31/42] Fix compile errors --- Marlin/src/gcode/calibrate/G34_M422.cpp | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index e0b7965a2f..c9645c0d32 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -22,10 +22,6 @@ #include "../../inc/MarlinConfigPre.h" -#if ENABLED(Z_STEPPER_AUTO_ALIGN) - -#include "../../feature/z_stepper_align.h" - #include "../gcode.h" #include "../../module/planner.h" #include "../../module/stepper.h" @@ -41,13 +37,20 @@ #include "../../module/tool_change.h" #endif -#if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) - #include "../../libs/least_squares_fit.h" -#endif + #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) #include "../../core/debug_out.h" +#if ENABLED(Z_STEPPER_AUTO_ALIGN) + + +#include "../../feature/z_stepper_align.h" + +#if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) + #include "../../libs/least_squares_fit.h" +#endif + /** * G34: Z-Stepper automatic alignment * @@ -513,7 +516,7 @@ void GcodeSuite::M422() { void GcodeSuite::G34() { - if (axis_unhomed_error()) return; + if (homing_needed()) return; TEMPORARY_SOFT_ENDSTOP_STATE(false); TEMPORARY_BED_LEVELING_STATE(false); From 27c99f74ad8c1c4b3a0a97ad2dfc500f792e0426 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Sat, 19 Sep 2020 00:02:06 -0400 Subject: [PATCH 32/42] Update Configuration_adv.h --- Marlin/Configuration_adv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index 9d37f2daf7..bcab1e09b0 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -3360,7 +3360,7 @@ //#define GANTRY_CALIBRATION_SAFE_POSITION {X_CENTER, Y_CENTER} // Safe position for nozzle //#define GANTRY_CALIBRATION_XY_PARK_FEEDRATE 3000 // XY Park Feedrate - MMM //#define GANTRY_CALIBRATION_COMMANDS_PRE "" - //#define GANTRY_CALIBRATION_COMMANDS_POST "G28" + //#define GANTRY_CALIBRATION_COMMANDS_POST "G28" // G28 is highly recommended here as position is likely no longer accurate. #endif /** From a0f0af5da812b5f71715f754a5c5413602067255 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Sat, 19 Sep 2020 00:14:10 -0400 Subject: [PATCH 33/42] Prevent min move from going into overflow sending Z to 65k --- Marlin/src/gcode/calibrate/G34_M422.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index c9645c0d32..8e501326b6 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -534,7 +534,7 @@ void GcodeSuite::M422() { do_blocking_move_to(safe_pos, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE)); #endif - const uint16_t move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT), + const float move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT), zpounce = ( #if GANTRY_CALIBRATION_DIRECTION == 1 (Z_MAX_POS) - move_distance From 3d8bfb036df58caaf998d802f14809656a7fb5ab Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Sat, 19 Sep 2020 00:27:23 -0400 Subject: [PATCH 34/42] Enable Post G28 by default as its usually necessary --- Marlin/Configuration_adv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index bcab1e09b0..e6dfa9840e 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -3360,7 +3360,7 @@ //#define GANTRY_CALIBRATION_SAFE_POSITION {X_CENTER, Y_CENTER} // Safe position for nozzle //#define GANTRY_CALIBRATION_XY_PARK_FEEDRATE 3000 // XY Park Feedrate - MMM //#define GANTRY_CALIBRATION_COMMANDS_PRE "" - //#define GANTRY_CALIBRATION_COMMANDS_POST "G28" // G28 is highly recommended here as position is likely no longer accurate. + #define GANTRY_CALIBRATION_COMMANDS_POST "G28" // G28 is highly recommended here as position is likely no longer accurate. #endif /** From 80e2ab6ed654f485374312977faf0033503f6ed2 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Oct 2020 12:12:36 -0500 Subject: [PATCH 35/42] Update Configuration_adv.h --- Marlin/Configuration_adv.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index e6dfa9840e..e7ae561ed3 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -3360,7 +3360,7 @@ //#define GANTRY_CALIBRATION_SAFE_POSITION {X_CENTER, Y_CENTER} // Safe position for nozzle //#define GANTRY_CALIBRATION_XY_PARK_FEEDRATE 3000 // XY Park Feedrate - MMM //#define GANTRY_CALIBRATION_COMMANDS_PRE "" - #define GANTRY_CALIBRATION_COMMANDS_POST "G28" // G28 is highly recommended here as position is likely no longer accurate. + #define GANTRY_CALIBRATION_COMMANDS_POST "G28" // G28 highly recommended to ensure an accurate position #endif /** From 0cb0df6bee75ae201c912364fcea8d838a9f3807 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Oct 2020 12:17:07 -0500 Subject: [PATCH 36/42] Update G34_M422.cpp --- Marlin/src/gcode/calibrate/G34_M422.cpp | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 8e501326b6..250a0eb1b0 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -534,21 +534,10 @@ void GcodeSuite::M422() { do_blocking_move_to(safe_pos, MMM_TO_MMS(GANTRY_CALIBRATION_XY_PARK_FEEDRATE)); #endif + constexpr float dir = (GANTRY_CALIBRATION_DIRECTION == 1) ? -1 : 1; const float move_distance = parser.intval('Z', GANTRY_CALIBRATION_EXTRA_HEIGHT), - zpounce = ( - #if GANTRY_CALIBRATION_DIRECTION == 1 - (Z_MAX_POS) - move_distance - #else - (Z_MIN_POS) + move_distance - #endif - ), - zgrind = ( - #if GANTRY_CALIBRATION_DIRECTION == 1 - (Z_MAX_POS) + move_distance - #else - (Z_MIN_POS) - move_distance - #endif - ); + zpounce = (Z_MIN_POS) + (dir * move_distance), + zgrind = (Z_MAX_POS) - (dir * move_distance); // Move Z to pounce position if (DEBUGGING(LEVELING)) DEBUG_ECHOLNPGM("Setting Z Pounce"); From 87f6e91f5197857fa23a3ff82b4057873020d731 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Oct 2020 12:27:46 -0500 Subject: [PATCH 37/42] Use MOTOR_CURRENT_COUNT --- Marlin/src/module/settings.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Marlin/src/module/settings.cpp b/Marlin/src/module/settings.cpp index 29ebce1e7d..b5733f77de 100644 --- a/Marlin/src/module/settings.cpp +++ b/Marlin/src/module/settings.cpp @@ -366,11 +366,10 @@ typedef struct SettingsDataStruct { // // HAS_MOTOR_CURRENT_PWM // - #if HAS_MOTOR_CURRENT_SPI - uint32_t motor_current_setting[COUNT(stepper.motor_current_setting)]; - #else - uint32_t motor_current_setting[3]; // M907 X Z E + #ifndef MOTOR_CURRENT_COUNT + #define MOTOR_CURRENT_COUNT 3 #endif + uint32_t motor_current_setting[MOTOR_CURRENT_COUNT]; // M907 X Z E // // CNC_COORDINATE_SYSTEMS @@ -1285,7 +1284,7 @@ void MarlinSettings::postprocess() { #if HAS_MOTOR_CURRENT_SPI || HAS_MOTOR_CURRENT_PWM EEPROM_WRITE(stepper.motor_current_setting); #else - const uint32_t no_current[3] = { 0 }; + const uint32_t no_current[MOTOR_CURRENT_COUNT] = { 0 }; EEPROM_WRITE(no_current); #endif } @@ -2116,11 +2115,11 @@ void MarlinSettings::postprocess() { // { _FIELD_TEST(motor_current_setting); - #if HAS_MOTOR_CURRENT_SPI - uint32_t motor_current_setting[] = DIGIPOT_MOTOR_CURRENT; - #else - uint32_t motor_current_setting[3]; - #endif + uint32_t motor_current_setting[MOTOR_CURRENT_COUNT] + #if HAS_MOTOR_CURRENT_SPI + = DIGIPOT_MOTOR_CURRENT + #endif + ; DEBUG_ECHOLNPGM("DIGIPOTS Loading"); EEPROM_READ(motor_current_setting); DEBUG_ECHOLNPGM("DIGIPOTS Loaded"); @@ -2802,8 +2801,8 @@ void MarlinSettings::reset() { // #if HAS_MOTOR_CURRENT_PWM - constexpr uint32_t tmp_motor_current_setting[3] = PWM_MOTOR_CURRENT; - LOOP_L_N(q, 3) + constexpr uint32_t tmp_motor_current_setting[MOTOR_CURRENT_COUNT] = PWM_MOTOR_CURRENT; + LOOP_L_N(q, MOTOR_CURRENT_COUNT) stepper.set_digipot_current(q, (stepper.motor_current_setting[q] = tmp_motor_current_setting[q])); #endif @@ -3728,7 +3727,7 @@ void MarlinSettings::reset() { ); #elif HAS_MOTOR_CURRENT_SPI SERIAL_ECHOPGM(" M907"); - LOOP_L_N(q, COUNT(stepper.motor_current_setting)) { + LOOP_L_N(q, MOTOR_CURRENT_COUNT) { SERIAL_CHAR(' '); SERIAL_CHAR(axis_codes[q]); SERIAL_ECHO(stepper.motor_current_setting[q]); From 8e39965b2d20eec0a63ffa2c643524ff008b98cd Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Oct 2020 12:31:17 -0500 Subject: [PATCH 38/42] Add a CI test --- Marlin/Configuration_adv.h | 8 ++++---- buildroot/tests/DUE-tests | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Marlin/Configuration_adv.h b/Marlin/Configuration_adv.h index e7ae561ed3..dbd3f67ee9 100644 --- a/Marlin/Configuration_adv.h +++ b/Marlin/Configuration_adv.h @@ -3345,11 +3345,11 @@ #endif /** - * Modern replacement for the Prusa TMC_Z_CALIBRATION - * Adds capability to work with any adjustable current drivers - * Implements as G34 as M915 is deprecated + * Mechanical Gantry Calibration + * Modern replacement for the Prusa TMC_Z_CALIBRATION. + * Adds capability to work with any adjustable current drivers. + * Implemented as G34 because M915 is deprecated. */ - //#define MECHANICAL_GANTRY_CALIBRATION #if ENABLED(MECHANICAL_GANTRY_CALIBRATION) #define GANTRY_CALIBRATION_CURRENT 600 // Default calibration current in ma diff --git a/buildroot/tests/DUE-tests b/buildroot/tests/DUE-tests index 7f488f6126..be0c21ab9b 100755 --- a/buildroot/tests/DUE-tests +++ b/buildroot/tests/DUE-tests @@ -13,7 +13,7 @@ opt_set TEMP_SENSOR_0 -2 opt_set TEMP_SENSOR_BED 2 opt_set GRID_MAX_POINTS_X 16 opt_set FANMUX0_PIN 53 -opt_enable S_CURVE_ACCELERATION EEPROM_SETTINGS GCODE_MACROS \ +opt_enable S_CURVE_ACCELERATION EEPROM_SETTINGS GCODE_MACROS MECHANICAL_GANTRY_CALIBRATION \ FIX_MOUNTED_PROBE Z_SAFE_HOMING CODEPENDENT_XY_HOMING ASSISTED_TRAMMING \ EEPROM_SETTINGS SDSUPPORT BINARY_FILE_TRANSFER \ BLINKM PCA9533 PCA9632 RGB_LED RGB_LED_R_PIN RGB_LED_G_PIN RGB_LED_B_PIN LED_CONTROL_MENU \ From 00413994ddb7c07cd52295c28d4ee72d238c1c85 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Oct 2020 12:32:19 -0500 Subject: [PATCH 39/42] Fix wrapper on G34_M422.cpp --- Marlin/src/gcode/calibrate/G34_M422.cpp | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 250a0eb1b0..a944de2954 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -22,6 +22,8 @@ #include "../../inc/MarlinConfigPre.h" +#if ENABLED(Z_STEPPER_AUTO_ALIGN) + #include "../gcode.h" #include "../../module/planner.h" #include "../../module/stepper.h" @@ -37,14 +39,9 @@ #include "../../module/tool_change.h" #endif - - #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) #include "../../core/debug_out.h" -#if ENABLED(Z_STEPPER_AUTO_ALIGN) - - #include "../../feature/z_stepper_align.h" #if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) From 109e8f88ce03c7018f5a0046dd727d3766ef7f21 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Oct 2020 12:34:08 -0500 Subject: [PATCH 40/42] ibid --- Marlin/src/gcode/calibrate/G34_M422.cpp | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index a944de2954..64a38b744b 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -25,6 +25,8 @@ #if ENABLED(Z_STEPPER_AUTO_ALIGN) #include "../gcode.h" +#include "../../feature/z_stepper_align.h" + #include "../../module/planner.h" #include "../../module/stepper.h" #include "../../module/motion.h" @@ -35,19 +37,13 @@ #include "../../feature/bedlevel/bedlevel.h" #endif -#if HAS_MULTI_HOTEND - #include "../../module/tool_change.h" +#if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) + #include "../../libs/least_squares_fit.h" #endif #define DEBUG_OUT ENABLED(DEBUG_LEVELING_FEATURE) #include "../../core/debug_out.h" -#include "../../feature/z_stepper_align.h" - -#if ENABLED(Z_STEPPER_ALIGN_KNOWN_STEPPER_POSITIONS) - #include "../../libs/least_squares_fit.h" -#endif - /** * G34: Z-Stepper automatic alignment * From a5eefa9ffa59930d2d5f945b27292bcb3cbead1d Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Oct 2020 12:36:35 -0500 Subject: [PATCH 41/42] Move test --- Marlin/src/inc/SanityCheck.h | 2 +- buildroot/tests/DUE-tests | 2 +- buildroot/tests/LPC1769-tests | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Marlin/src/inc/SanityCheck.h b/Marlin/src/inc/SanityCheck.h index 78e6d4acf4..7e714821fe 100644 --- a/Marlin/src/inc/SanityCheck.h +++ b/Marlin/src/inc/SanityCheck.h @@ -2735,7 +2735,7 @@ static_assert( _ARR_TEST(3,0) && _ARR_TEST(3,1) && _ARR_TEST(3,2) #if ENABLED(MECHANICAL_GANTRY_CALIBRATION) #if NONE(HAS_MOTOR_CURRENT_DAC, HAS_MOTOR_CURRENT_SPI, HAS_MOTOR_CURRENT_DAC, HAS_TRINAMIC_CONFIG, HAS_MOTOR_CURRENT_PWM) - #error "It is highly reccomended to have adjustable current drivers to prevent damage. Disable this line to continue anyway." + #error "It is highly recommended to have adjustable current drivers to prevent damage. Disable this line to continue anyway." #else #ifndef GANTRY_CALIBRATION_CURRENT #error "MECHANICAL_GANTRY_CALIBRATION Requires GANTRY_CALIBRATION_CURRENT to be set." diff --git a/buildroot/tests/DUE-tests b/buildroot/tests/DUE-tests index be0c21ab9b..7f488f6126 100755 --- a/buildroot/tests/DUE-tests +++ b/buildroot/tests/DUE-tests @@ -13,7 +13,7 @@ opt_set TEMP_SENSOR_0 -2 opt_set TEMP_SENSOR_BED 2 opt_set GRID_MAX_POINTS_X 16 opt_set FANMUX0_PIN 53 -opt_enable S_CURVE_ACCELERATION EEPROM_SETTINGS GCODE_MACROS MECHANICAL_GANTRY_CALIBRATION \ +opt_enable S_CURVE_ACCELERATION EEPROM_SETTINGS GCODE_MACROS \ FIX_MOUNTED_PROBE Z_SAFE_HOMING CODEPENDENT_XY_HOMING ASSISTED_TRAMMING \ EEPROM_SETTINGS SDSUPPORT BINARY_FILE_TRANSFER \ BLINKM PCA9533 PCA9632 RGB_LED RGB_LED_R_PIN RGB_LED_G_PIN RGB_LED_B_PIN LED_CONTROL_MENU \ diff --git a/buildroot/tests/LPC1769-tests b/buildroot/tests/LPC1769-tests index 9c7a1ba10e..14875f063a 100755 --- a/buildroot/tests/LPC1769-tests +++ b/buildroot/tests/LPC1769-tests @@ -47,7 +47,7 @@ opt_set MOTHERBOARD BOARD_COHESION3D_REMIX opt_set X_DRIVER_TYPE TMC2130 opt_set Y_DRIVER_TYPE TMC2130 opt_set Z_DRIVER_TYPE TMC2130 -opt_enable AUTO_BED_LEVELING_BILINEAR EEPROM_SETTINGS EEPROM_CHITCHAT \ +opt_enable AUTO_BED_LEVELING_BILINEAR EEPROM_SETTINGS EEPROM_CHITCHAT MECHANICAL_GANTRY_CALIBRATION \ TMC_USE_SW_SPI MONITOR_DRIVER_STATUS STEALTHCHOP_XY STEALTHCHOP_Z HYBRID_THRESHOLD \ SENSORLESS_PROBING Z_SAFE_HOMING X_STALL_SENSITIVITY Y_STALL_SENSITIVITY Z_STALL_SENSITIVITY TMC_DEBUG \ EXPERIMENTAL_I2CBUS From ff3901903464c72549eb2a702a19361325945865 Mon Sep 17 00:00:00 2001 From: Scott Lahteine Date: Sat, 10 Oct 2020 12:40:08 -0500 Subject: [PATCH 42/42] Update G34_M422.cpp --- Marlin/src/gcode/calibrate/G34_M422.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Marlin/src/gcode/calibrate/G34_M422.cpp b/Marlin/src/gcode/calibrate/G34_M422.cpp index 64a38b744b..8a72bc2bfe 100644 --- a/Marlin/src/gcode/calibrate/G34_M422.cpp +++ b/Marlin/src/gcode/calibrate/G34_M422.cpp @@ -24,9 +24,9 @@ #if ENABLED(Z_STEPPER_AUTO_ALIGN) -#include "../gcode.h" #include "../../feature/z_stepper_align.h" +#include "../gcode.h" #include "../../module/planner.h" #include "../../module/stepper.h" #include "../../module/motion.h"