From 47c6f8a98be4460cfeda61ef83fa31a28d99f3b3 Mon Sep 17 00:00:00 2001 From: InsanityAutomation Date: Sun, 9 Aug 2020 16:43:08 -0400 Subject: [PATCH] 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