From 771cead291626113a38f2fbfd0ec634fee7376ac Mon Sep 17 00:00:00 2001 From: Robert Pelnar Date: Mon, 9 Jul 2018 16:44:10 +0200 Subject: [PATCH] TMC2130 gcodes for live motor tunning: TMC_SET_WAVE_X..E (linearity correction) TMC_SET_CHOP_X..E (chopper configuration - 4 args - toff, hstr, hend, tbl) --- Firmware/Marlin_main.cpp | 57 +++++++++++++++++++++++++++++++++++----- Firmware/tmc2130.cpp | 29 +++++++++++--------- Firmware/tmc2130.h | 15 +++++++++++ 3 files changed, 81 insertions(+), 20 deletions(-) diff --git a/Firmware/Marlin_main.cpp b/Firmware/Marlin_main.cpp index 225883bf..6f251d28 100644 --- a/Firmware/Marlin_main.cpp +++ b/Firmware/Marlin_main.cpp @@ -3242,16 +3242,59 @@ void process_commands() } else if (strncmp_P(CMDBUFFER_CURRENT_STRING, PSTR("TMC_"), 4) == 0) { - if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_WAVE_E"), 10) == 0) + if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_WAVE_"), 9) == 0) { - uint8_t fac = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, NULL, 10); - tmc2130_set_wave(E_AXIS, 247, fac); + uint8_t axis = *(CMDBUFFER_CURRENT_STRING + 13); + axis = (axis == 'E')?3:(axis - 'X'); + if (axis < 4) + { + uint8_t fac = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, NULL, 10); + tmc2130_set_wave(axis, 247, fac); + } } - else if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_STEP_E"), 10) == 0) + else if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_STEP_"), 9) == 0) { - uint8_t step = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, NULL, 10); - uint16_t res = tmc2130_get_res(E_AXIS); - tmc2130_goto_step(E_AXIS, step & (4*res - 1), 2, 1000, res); + uint8_t axis = *(CMDBUFFER_CURRENT_STRING + 13); + axis = (axis == 'E')?3:(axis - 'X'); + if (axis < 4) + { + uint8_t step = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, NULL, 10); + uint16_t res = tmc2130_get_res(axis); + tmc2130_goto_step(axis, step & (4*res - 1), 2, 1000, res); + } + } + else if (strncmp_P(CMDBUFFER_CURRENT_STRING + 4, PSTR("SET_CHOP_"), 9) == 0) + { + uint8_t axis = *(CMDBUFFER_CURRENT_STRING + 13); + axis = (axis == 'E')?3:(axis - 'X'); + if (axis < 4) + { + uint8_t chop0 = tmc2130_chopper_config[axis].toff; + uint8_t chop1 = tmc2130_chopper_config[axis].hstr; + uint8_t chop2 = tmc2130_chopper_config[axis].hend; + uint8_t chop3 = tmc2130_chopper_config[axis].tbl; + char* str_end = 0; + if (CMDBUFFER_CURRENT_STRING[14]) + { + chop0 = (uint8_t)strtol(CMDBUFFER_CURRENT_STRING + 14, &str_end, 10) & 15; + if (str_end && *str_end) + { + chop1 = (uint8_t)strtol(str_end, &str_end, 10) & 7; + if (str_end && *str_end) + { + chop2 = (uint8_t)strtol(str_end, &str_end, 10) & 15; + if (str_end && *str_end) + chop3 = (uint8_t)strtol(str_end, &str_end, 10) & 3; + } + } + } + tmc2130_chopper_config[axis].toff = chop0; + tmc2130_chopper_config[axis].hstr = chop1 & 7; + tmc2130_chopper_config[axis].hend = chop2 & 15; + tmc2130_chopper_config[axis].tbl = chop3 & 3; + tmc2130_setup_chopper(axis, tmc2130_mres[axis], tmc2130_current_h[axis], tmc2130_current_r[axis]); + //printf_P(_N("TMC_SET_CHOP_%c %hhd %hhd %hhd %hhd\n"), "xyze"[axis], chop0, chop1, chop2, chop3); + } } } #endif //TMC2130 diff --git a/Firmware/tmc2130.cpp b/Firmware/tmc2130.cpp index dc1af62c..bfc720c6 100644 --- a/Firmware/tmc2130.cpp +++ b/Firmware/tmc2130.cpp @@ -62,6 +62,13 @@ uint8_t tmc2130_home_fsteps[2] = {48, 48}; uint8_t tmc2130_wave_fac[4] = {0, 0, 0, 0}; +tmc2130_chopper_config_t tmc2130_chopper_config[4] = { + {TMC2130_TOFF_XYZ, 5, 1, 2, 0}, + {TMC2130_TOFF_XYZ, 5, 1, 2, 0}, + {TMC2130_TOFF_XYZ, 5, 1, 2, 0}, + {TMC2130_TOFF_E, 5, 1, 2, 0} +}; + bool tmc2130_sg_stop_on_crash = true; uint8_t tmc2130_sg_diag_mask = 0x00; uint8_t tmc2130_sg_crash = 0; @@ -418,13 +425,13 @@ void tmc2130_check_overtemp() void tmc2130_setup_chopper(uint8_t axis, uint8_t mres, uint8_t current_h, uint8_t current_r) { uint8_t intpol = 1; - uint8_t toff = TMC2130_TOFF_XYZ; // toff = 3 (fchop = 27.778kHz) - uint8_t hstrt = 5; //initial 4, modified to 5 - uint8_t hend = 1; + uint8_t toff = tmc2130_chopper_config[axis].toff; // toff = 3 (fchop = 27.778kHz) + uint8_t hstrt = tmc2130_chopper_config[axis].hstr; //initial 4, modified to 5 + uint8_t hend = tmc2130_chopper_config[axis].hend; //original value = 1 uint8_t fd3 = 0; uint8_t rndtf = 0; //random off time uint8_t chm = 0; //spreadCycle - uint8_t tbl = 2; //blanking time + uint8_t tbl = tmc2130_chopper_config[axis].tbl; //blanking time, original value = 2 if (axis == E_AXIS) { #ifdef TMC2130_CNSTOFF_E @@ -434,9 +441,11 @@ void tmc2130_setup_chopper(uint8_t axis, uint8_t mres, uint8_t current_h, uint8_ hend = 0; //sine wave offset chm = 1; // constant off time mod #endif //TMC2130_CNSTOFF_E - toff = TMC2130_TOFF_E; // toff = 3-5 +// toff = TMC2130_TOFF_E; // toff = 3-5 // rndtf = 1; } + DBG(_n("tmc2130_setup_chopper(axis=%hhd, mres=%hhd, curh=%hhd, curr=%hhd\n"), axis, mres, current_h, current_r); + DBG(_n(" toff=%hhd, hstr=%hhd, hend=%hhd, tbl=%hhd\n"), toff, hstrt, hend, tbl); if (current_r <= 31) { tmc2130_wr_CHOPCONF(axis, toff, hstrt, hend, fd3, 0, rndtf, chm, tbl, 1, 0, 0, 0, mres, intpol, 0, 0); @@ -475,10 +484,7 @@ void tmc2130_print_currents() void tmc2130_set_pwm_ampl(uint8_t axis, uint8_t pwm_ampl) { - MYSERIAL.print("tmc2130_set_pwm_ampl "); - MYSERIAL.print((int)axis); - MYSERIAL.print(" "); - MYSERIAL.println((int)pwm_ampl); + DBG(_n("tmc2130_set_pwm_ampl(axis=%hhd, pwm_ampl=%hhd\n"), axis, pwm_ampl); tmc2130_pwm_ampl[axis] = pwm_ampl; if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT)) tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0); @@ -486,10 +492,7 @@ void tmc2130_set_pwm_ampl(uint8_t axis, uint8_t pwm_ampl) void tmc2130_set_pwm_grad(uint8_t axis, uint8_t pwm_grad) { - MYSERIAL.print("tmc2130_set_pwm_grad "); - MYSERIAL.print((int)axis); - MYSERIAL.print(" "); - MYSERIAL.println((int)pwm_grad); + DBG(_n("tmc2130_set_pwm_grad(axis=%hhd, pwm_grad=%hhd\n"), axis, pwm_grad); tmc2130_pwm_grad[axis] = pwm_grad; if (((axis == 0) || (axis == 1)) && (tmc2130_mode == TMC2130_MODE_SILENT)) tmc2130_wr_PWMCONF(axis, tmc2130_pwm_ampl[axis], tmc2130_pwm_grad[axis], tmc2130_pwm_freq[axis], tmc2130_pwm_auto[axis], 0, 0); diff --git a/Firmware/tmc2130.h b/Firmware/tmc2130.h index d5d53111..e3c476dc 100644 --- a/Firmware/tmc2130.h +++ b/Firmware/tmc2130.h @@ -36,6 +36,19 @@ extern uint8_t tmc2130_home_fsteps[2]; extern uint8_t tmc2130_wave_fac[4]; +#pragma pack(push) +#pragma pack(1) +typedef struct +{ + uint8_t toff:4; + uint8_t hstr:3; + uint8_t hend:4; + uint8_t tbl:2; + uint8_t res:3; +} tmc2130_chopper_config_t; +#pragma pack(pop) + +extern tmc2130_chopper_config_t tmc2130_chopper_config[4]; //initialize tmc2130 extern void tmc2130_init(); @@ -55,6 +68,7 @@ extern void tmc2130_sg_meassure_start(uint8_t axis); //stop current stallguard meassuring and report result extern uint16_t tmc2130_sg_meassure_stop(); +extern void tmc2130_setup_chopper(uint8_t axis, uint8_t mres, uint8_t current_h, uint8_t current_r); //set holding current for any axis (M911) extern void tmc2130_set_current_h(uint8_t axis, uint8_t current); @@ -80,6 +94,7 @@ extern bool tmc2130_wait_standstill_xy(int timeout); extern void tmc2130_eeprom_load_config(); extern void tmc2130_eeprom_save_config(); + #pragma pack(push) #pragma pack(1) struct