From 05e1d6083b47c014f3db51d4ac132ef482517ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Corr=C3=AAa=20da=20Silva=20Sanches?= Date: Mon, 18 Jun 2012 11:03:06 -0300 Subject: refactoring homing routine to avoid copy&pasted code --- Sprinter/Sprinter.pde | 134 +++++++++++++++----------------------------------- 1 file changed, 39 insertions(+), 95 deletions(-) diff --git a/Sprinter/Sprinter.pde b/Sprinter/Sprinter.pde index 94aa538..b423c32 100644 --- a/Sprinter/Sprinter.pde +++ b/Sprinter/Sprinter.pde @@ -1150,6 +1150,42 @@ FORCE_INLINE void process_commands() manage_heater(); } break; + +#define X_HOME_BOUNCE 10 +#define Y_HOME_BOUNCE 10 +#define Z_HOME_BOUNCE 4 + +#define HOMING_ROUTINE(coord)\ + if ((coord##_MIN_PIN > -1 && coord##_HOME_DIR==-1) || (coord##_MAX_PIN > -1 && coord##_HOME_DIR==1))\ + {\ + current_position[coord##_AXIS] = -1.5 * coord##_MAX_LENGTH * coord##_HOME_DIR;\ + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\ + destination[coord##_AXIS] = 0;\ + feedrate = homing_feedrate[coord##_AXIS];\ + prepare_move();\ + st_synchronize();\ + \ + current_position[coord##_AXIS] = coord##_HOME_BOUNCE/2 * coord##_HOME_DIR;\ + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\ + destination[coord##_AXIS] = 0;\ + prepare_move();\ + st_synchronize();\ + \ + current_position[coord##_AXIS] = -coord##_HOME_BOUNCE * coord##_HOME_DIR;\ + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\ + destination[coord##_AXIS] = 0;\ + feedrate = homing_feedrate[coord##_AXIS]/2;\ + prepare_move();\ + st_synchronize();\ + \ + current_position[coord##_AXIS] = (coord##_HOME_DIR == -1) ? 0 : coord##_MAX_LENGTH;\ + current_position[coord##_AXIS] += add_homing[coord##_AXIS];\ + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\ + destination[coord##_AXIS] = current_position[coord##_AXIS];\ + feedrate = 0;\ + /*showString(PSTR("HOME " #coord " AXIS\r\n"));*/\ + } + case 28: //G28 Home all Axis one at a time saved_feedrate = feedrate; saved_feedmultiply = feedmultiply; @@ -1169,105 +1205,13 @@ FORCE_INLINE void process_commands() home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))); if((home_all_axis) || (code_seen(axis_codes[X_AXIS]))) - { - if ((X_MIN_PIN > -1 && X_HOME_DIR==-1) || (X_MAX_PIN > -1 && X_HOME_DIR==1)) - { - st_synchronize(); - current_position[X_AXIS] = -1.5 * X_MAX_LENGTH * X_HOME_DIR; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[X_AXIS] = 0; - feedrate = homing_feedrate[X_AXIS]; - prepare_move(); - - st_synchronize(); - current_position[X_AXIS] = 5 * X_HOME_DIR; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[X_AXIS] = 0; - prepare_move(); - - st_synchronize(); - current_position[X_AXIS] = -10 * X_HOME_DIR; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[X_AXIS] = 0; - feedrate = homing_feedrate[X_AXIS]/2 ; - prepare_move(); - st_synchronize(); - - current_position[X_AXIS] = (X_HOME_DIR == -1) ? 0 : X_MAX_LENGTH; - current_position[X_AXIS] += add_homing[0]; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[X_AXIS] = current_position[X_AXIS]; - feedrate = 0; - } - } - //showString(PSTR("HOME X AXIS\r\n")); + HOMING_ROUTINE(X) if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) - { - if ((Y_MIN_PIN > -1 && Y_HOME_DIR==-1) || (Y_MAX_PIN > -1 && Y_HOME_DIR==1)) - { - current_position[Y_AXIS] = -1.5 * Y_MAX_LENGTH * Y_HOME_DIR; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[Y_AXIS] = 0; - feedrate = homing_feedrate[Y_AXIS]; - prepare_move(); - st_synchronize(); - - current_position[Y_AXIS] = 5 * Y_HOME_DIR; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[Y_AXIS] = 0; - prepare_move(); - st_synchronize(); - - current_position[Y_AXIS] = -10 * Y_HOME_DIR; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[Y_AXIS] = 0; - feedrate = homing_feedrate[Y_AXIS]/2; - prepare_move(); - st_synchronize(); - - current_position[Y_AXIS] = (Y_HOME_DIR == -1) ? 0 : Y_MAX_LENGTH; - current_position[Y_AXIS] += add_homing[1]; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[Y_AXIS] = current_position[Y_AXIS]; - feedrate = 0; - } - } - //showString(PSTR("HOME Y AXIS\r\n")); + HOMING_ROUTINE(Y) if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) - { - if ((Z_MIN_PIN > -1 && Z_HOME_DIR==-1) || (Z_MAX_PIN > -1 && Z_HOME_DIR==1)) - { - current_position[Z_AXIS] = -1.5 * Z_MAX_LENGTH * Z_HOME_DIR; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[Z_AXIS] = 0; - feedrate = homing_feedrate[Z_AXIS]; - prepare_move(); - st_synchronize(); - - current_position[Z_AXIS] = 2 * Z_HOME_DIR; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[Z_AXIS] = 0; - prepare_move(); - st_synchronize(); - - current_position[Z_AXIS] = -3 * Z_HOME_DIR; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[Z_AXIS] = 0; - feedrate = homing_feedrate[Z_AXIS]/2; - prepare_move(); - st_synchronize(); - - current_position[Z_AXIS] = (Z_HOME_DIR == -1) ? 0 : Z_MAX_LENGTH; - current_position[Z_AXIS] += add_homing[2]; - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); - destination[Z_AXIS] = current_position[Z_AXIS]; - feedrate = 0; - } - } - - //showString(PSTR("HOME Z AXIS\r\n")); + HOMING_ROUTINE(Z) #ifdef ENDSTOPS_ONLY_FOR_HOMING enable_endstops(false); -- cgit v1.2.1 From 3cc48055c21cb18fd73b963a385c4b093625a9de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felipe=20Corr=C3=AAa=20da=20Silva=20Sanches?= Date: Mon, 18 Jun 2012 13:07:42 -0300 Subject: reimplement homing routine as an inline function --- Sprinter/Sprinter.pde | 103 +++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 39 deletions(-) diff --git a/Sprinter/Sprinter.pde b/Sprinter/Sprinter.pde index b423c32..f1940d9 100644 --- a/Sprinter/Sprinter.pde +++ b/Sprinter/Sprinter.pde @@ -1103,6 +1103,67 @@ FORCE_INLINE bool code_seen(char code) return (strchr_pointer != NULL); //Return True if a character was found } +FORCE_INLINE void homing_routine(char axis) +{ + int min_pin, max_pin, home_dir, max_length, home_bounce; + + switch(axis){ + case X_AXIS: + min_pin = X_MIN_PIN; + max_pin = X_MAX_PIN; + home_dir = X_HOME_DIR; + max_length = X_MAX_LENGTH; + home_bounce = 10; + break; + case Y_AXIS: + min_pin = Y_MIN_PIN; + max_pin = Y_MAX_PIN; + home_dir = Y_HOME_DIR; + max_length = Y_MAX_LENGTH; + home_bounce = 10; + break; + case Z_AXIS: + min_pin = Z_MIN_PIN; + max_pin = Z_MAX_PIN; + home_dir = Z_HOME_DIR; + max_length = Z_MAX_LENGTH; + home_bounce = 4; + break; + default: + //never reached + break; + } + + if ((min_pin > -1 && home_dir==-1) || (max_pin > -1 && home_dir==1)) + { + current_position[axis] = -1.5 * max_length * home_dir; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[axis] = 0; + feedrate = homing_feedrate[axis]; + prepare_move(); + st_synchronize(); + + current_position[axis] = home_bounce/2 * home_dir; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[axis] = 0; + prepare_move(); + st_synchronize(); + + current_position[axis] = -home_bounce * home_dir; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[axis] = 0; + feedrate = homing_feedrate[axis]/2; + prepare_move(); + st_synchronize(); + + current_position[axis] = (home_dir == -1) ? 0 : max_length; + current_position[axis] += add_homing[axis]; + plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]); + destination[axis] = current_position[axis]; + feedrate = 0; + } +} + //------------------------------------------------ // CHECK COMMAND AND CONVERT VALUES //------------------------------------------------ @@ -1150,42 +1211,6 @@ FORCE_INLINE void process_commands() manage_heater(); } break; - -#define X_HOME_BOUNCE 10 -#define Y_HOME_BOUNCE 10 -#define Z_HOME_BOUNCE 4 - -#define HOMING_ROUTINE(coord)\ - if ((coord##_MIN_PIN > -1 && coord##_HOME_DIR==-1) || (coord##_MAX_PIN > -1 && coord##_HOME_DIR==1))\ - {\ - current_position[coord##_AXIS] = -1.5 * coord##_MAX_LENGTH * coord##_HOME_DIR;\ - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\ - destination[coord##_AXIS] = 0;\ - feedrate = homing_feedrate[coord##_AXIS];\ - prepare_move();\ - st_synchronize();\ - \ - current_position[coord##_AXIS] = coord##_HOME_BOUNCE/2 * coord##_HOME_DIR;\ - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\ - destination[coord##_AXIS] = 0;\ - prepare_move();\ - st_synchronize();\ - \ - current_position[coord##_AXIS] = -coord##_HOME_BOUNCE * coord##_HOME_DIR;\ - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\ - destination[coord##_AXIS] = 0;\ - feedrate = homing_feedrate[coord##_AXIS]/2;\ - prepare_move();\ - st_synchronize();\ - \ - current_position[coord##_AXIS] = (coord##_HOME_DIR == -1) ? 0 : coord##_MAX_LENGTH;\ - current_position[coord##_AXIS] += add_homing[coord##_AXIS];\ - plan_set_position(current_position[X_AXIS], current_position[Y_AXIS], current_position[Z_AXIS], current_position[E_AXIS]);\ - destination[coord##_AXIS] = current_position[coord##_AXIS];\ - feedrate = 0;\ - /*showString(PSTR("HOME " #coord " AXIS\r\n"));*/\ - } - case 28: //G28 Home all Axis one at a time saved_feedrate = feedrate; saved_feedmultiply = feedmultiply; @@ -1205,13 +1230,13 @@ FORCE_INLINE void process_commands() home_all_axis = !((code_seen(axis_codes[0])) || (code_seen(axis_codes[1])) || (code_seen(axis_codes[2]))); if((home_all_axis) || (code_seen(axis_codes[X_AXIS]))) - HOMING_ROUTINE(X) + homing_routine(X_AXIS); if((home_all_axis) || (code_seen(axis_codes[Y_AXIS]))) - HOMING_ROUTINE(Y) + homing_routine(Y_AXIS); if((home_all_axis) || (code_seen(axis_codes[Z_AXIS]))) - HOMING_ROUTINE(Z) + homing_routine(Z_AXIS); #ifdef ENDSTOPS_ONLY_FOR_HOMING enable_endstops(false); -- cgit v1.2.1