refactor endstops and zprobe to not use config steps/mm but to use live value instead
authorJim Morris <morris@wolfman.com>
Mon, 28 Apr 2014 04:19:13 +0000 (21:19 -0700)
committerJim Morris <morris@wolfman.com>
Mon, 28 Apr 2014 04:45:46 +0000 (21:45 -0700)
clean up endstops ugly code
add I parameter to G32 to set the accuracy

src/libs/Kernel.cpp
src/modules/tools/endstops/Endstops.cpp
src/modules/tools/endstops/Endstops.h
src/modules/tools/zprobe/ZProbe.cpp
src/modules/tools/zprobe/ZProbe.h

index 0d4eab5..7f12953 100644 (file)
 #include "modules/robot/Planner.h"
 #include "modules/robot/Robot.h"
 #include "modules/robot/Stepper.h"
-#include <array>
-
-
-
 #include "modules/robot/Conveyor.h"
-#include "modules/tools/endstops/Endstops.h"
+
 #include <malloc.h>
+#include <array>
 
 #define baud_rate_setting_checksum CHECKSUM("baud_rate")
 #define uart0_checksum             CHECKSUM("uart0")
index 2f21e94..e3f24b5 100644 (file)
@@ -98,6 +98,8 @@
 #define beta_steps_per_mm_checksum       CHECKSUM("beta_steps_per_mm")
 #define gamma_steps_per_mm_checksum      CHECKSUM("gamma_steps_per_mm")
 
+#define STEPS_PER_MM(a) (this->steppers[a]->steps_per_mm)
+
 Endstops::Endstops()
 {
     this->status = NOT_HOMING;
@@ -127,39 +129,34 @@ void Endstops::on_module_loaded()
 // Get config
 void Endstops::on_config_reload(void *argument)
 {
-    this->pins[0].from_string(         THEKERNEL->config->value(alpha_min_endstop_checksum          )->by_default("nc" )->as_string())->as_input();
-    this->pins[1].from_string(         THEKERNEL->config->value(beta_min_endstop_checksum           )->by_default("nc" )->as_string())->as_input();
-    this->pins[2].from_string(         THEKERNEL->config->value(gamma_min_endstop_checksum          )->by_default("nc" )->as_string())->as_input();
-    this->pins[3].from_string(         THEKERNEL->config->value(alpha_max_endstop_checksum          )->by_default("nc" )->as_string())->as_input();
-    this->pins[4].from_string(         THEKERNEL->config->value(beta_max_endstop_checksum           )->by_default("nc" )->as_string())->as_input();
-    this->pins[5].from_string(         THEKERNEL->config->value(gamma_max_endstop_checksum          )->by_default("nc" )->as_string())->as_input();
-
-    // we need to know steps per mm for M206, also use them for all settings
-    this->steps_per_mm[0]           =  THEKERNEL->config->value(alpha_steps_per_mm_checksum         )->as_number();
-    this->steps_per_mm[1]           =  THEKERNEL->config->value(beta_steps_per_mm_checksum          )->as_number();
-    this->steps_per_mm[2]           =  THEKERNEL->config->value(gamma_steps_per_mm_checksum         )->as_number();
-
-    //These are the old ones in steps still here for backwards compatibility
-    this->fast_rates[0]             =  THEKERNEL->config->value(alpha_fast_homing_rate_checksum     )->by_default(4000 )->as_number();
-    this->fast_rates[1]             =  THEKERNEL->config->value(beta_fast_homing_rate_checksum      )->by_default(4000 )->as_number();
-    this->fast_rates[2]             =  THEKERNEL->config->value(gamma_fast_homing_rate_checksum     )->by_default(6400 )->as_number();
-    this->slow_rates[0]             =  THEKERNEL->config->value(alpha_slow_homing_rate_checksum     )->by_default(2000 )->as_number();
-    this->slow_rates[1]             =  THEKERNEL->config->value(beta_slow_homing_rate_checksum      )->by_default(2000 )->as_number();
-    this->slow_rates[2]             =  THEKERNEL->config->value(gamma_slow_homing_rate_checksum     )->by_default(3200 )->as_number();
-    this->retract_steps[0]          =  THEKERNEL->config->value(alpha_homing_retract_checksum       )->by_default(400  )->as_number();
-    this->retract_steps[1]          =  THEKERNEL->config->value(beta_homing_retract_checksum        )->by_default(400  )->as_number();
-    this->retract_steps[2]          =  THEKERNEL->config->value(gamma_homing_retract_checksum       )->by_default(1600 )->as_number();
+    this->pins[0].from_string( THEKERNEL->config->value(alpha_min_endstop_checksum          )->by_default("nc" )->as_string())->as_input();
+    this->pins[1].from_string( THEKERNEL->config->value(beta_min_endstop_checksum           )->by_default("nc" )->as_string())->as_input();
+    this->pins[2].from_string( THEKERNEL->config->value(gamma_min_endstop_checksum          )->by_default("nc" )->as_string())->as_input();
+    this->pins[3].from_string( THEKERNEL->config->value(alpha_max_endstop_checksum          )->by_default("nc" )->as_string())->as_input();
+    this->pins[4].from_string( THEKERNEL->config->value(beta_max_endstop_checksum           )->by_default("nc" )->as_string())->as_input();
+    this->pins[5].from_string( THEKERNEL->config->value(gamma_max_endstop_checksum          )->by_default("nc" )->as_string())->as_input();
+
+    // These are the old ones in steps still here for backwards compatibility
+    this->fast_rates[0] =  THEKERNEL->config->value(alpha_fast_homing_rate_checksum     )->by_default(4000 )->as_number() / STEPS_PER_MM(0);
+    this->fast_rates[1] =  THEKERNEL->config->value(beta_fast_homing_rate_checksum      )->by_default(4000 )->as_number() / STEPS_PER_MM(1);
+    this->fast_rates[2] =  THEKERNEL->config->value(gamma_fast_homing_rate_checksum     )->by_default(6400 )->as_number() / STEPS_PER_MM(2);
+    this->slow_rates[0] =  THEKERNEL->config->value(alpha_slow_homing_rate_checksum     )->by_default(2000 )->as_number() / STEPS_PER_MM(0);
+    this->slow_rates[1] =  THEKERNEL->config->value(beta_slow_homing_rate_checksum      )->by_default(2000 )->as_number() / STEPS_PER_MM(1);
+    this->slow_rates[2] =  THEKERNEL->config->value(gamma_slow_homing_rate_checksum     )->by_default(3200 )->as_number() / STEPS_PER_MM(2);
+    this->retract_mm[0] =  THEKERNEL->config->value(alpha_homing_retract_checksum       )->by_default(400  )->as_number() / STEPS_PER_MM(0);
+    this->retract_mm[1] =  THEKERNEL->config->value(beta_homing_retract_checksum        )->by_default(400  )->as_number() / STEPS_PER_MM(1);
+    this->retract_mm[2] =  THEKERNEL->config->value(gamma_homing_retract_checksum       )->by_default(1600 )->as_number() / STEPS_PER_MM(2);
 
     // newer mm based config values override the old ones, convert to steps/mm and steps, defaults to what was set in the older config settings above
-    this->fast_rates[0] =    THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0]  / steps_per_mm[0])->as_number() * steps_per_mm[0];
-    this->fast_rates[1] =    THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum  )->by_default(this->fast_rates[1]  / steps_per_mm[1])->as_number() * steps_per_mm[1];
-    this->fast_rates[2] =    THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2]  / steps_per_mm[2])->as_number() * steps_per_mm[2];
-    this->slow_rates[0] =    THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0]  / steps_per_mm[0])->as_number() * steps_per_mm[0];
-    this->slow_rates[1] =    THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum  )->by_default(this->slow_rates[1]  / steps_per_mm[1])->as_number() * steps_per_mm[1];
-    this->slow_rates[2] =    THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2]  / steps_per_mm[2])->as_number() * steps_per_mm[2];
-    this->retract_steps[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum   )->by_default(this->retract_steps[0] / steps_per_mm[0])->as_number() * steps_per_mm[0];
-    this->retract_steps[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum    )->by_default(this->retract_steps[1] / steps_per_mm[1])->as_number() * steps_per_mm[1];
-    this->retract_steps[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum   )->by_default(this->retract_steps[2] / steps_per_mm[2])->as_number() * steps_per_mm[2];
+    this->fast_rates[0] = THEKERNEL->config->value(alpha_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[0])->as_number();
+    this->fast_rates[1] = THEKERNEL->config->value(beta_fast_homing_rate_mm_checksum  )->by_default(this->fast_rates[1])->as_number();
+    this->fast_rates[2] = THEKERNEL->config->value(gamma_fast_homing_rate_mm_checksum )->by_default(this->fast_rates[2])->as_number();
+    this->slow_rates[0] = THEKERNEL->config->value(alpha_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[0])->as_number();
+    this->slow_rates[1] = THEKERNEL->config->value(beta_slow_homing_rate_mm_checksum  )->by_default(this->slow_rates[1])->as_number();
+    this->slow_rates[2] = THEKERNEL->config->value(gamma_slow_homing_rate_mm_checksum )->by_default(this->slow_rates[2])->as_number();
+    this->retract_mm[0] = THEKERNEL->config->value(alpha_homing_retract_mm_checksum   )->by_default(this->retract_mm[0])->as_number();
+    this->retract_mm[1] = THEKERNEL->config->value(beta_homing_retract_mm_checksum    )->by_default(this->retract_mm[1])->as_number();
+    this->retract_mm[2] = THEKERNEL->config->value(gamma_homing_retract_mm_checksum   )->by_default(this->retract_mm[2])->as_number();
 
     this->debounce_count  = THEKERNEL->config->value(endstop_debounce_count_checksum    )->by_default(100)->as_number();
 
@@ -181,14 +178,14 @@ void Endstops::on_config_reload(void *argument)
     this->is_corexy                 =  THEKERNEL->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
     this->is_delta                  =  THEKERNEL->config->value(delta_homing_checksum)->by_default(false)->as_bool();
 
-    // endstop trim used by deltas to do soft adjusting, in mm, convert to steps, and negate depending on homing direction
+    // endstop trim used by deltas to do soft adjusting, in mm, negate depending on homing direction
     // eg on a delta homing to max, a negative trim value will move the carriage down, and a positive will move it up
     int dirx = (this->home_direction[0] ? 1 : -1);
     int diry = (this->home_direction[1] ? 1 : -1);
     int dirz = (this->home_direction[2] ? 1 : -1);
-    this->trim[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0  )->as_number() * steps_per_mm[0] * dirx;
-    this->trim[1] = THEKERNEL->config->value(beta_trim_checksum  )->by_default(0  )->as_number() * steps_per_mm[1] * diry;
-    this->trim[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0  )->as_number() * steps_per_mm[2] * dirz;
+    this->trim_mm[0] = THEKERNEL->config->value(alpha_trim_checksum )->by_default(0  )->as_number() * dirx;
+    this->trim_mm[1] = THEKERNEL->config->value(beta_trim_checksum  )->by_default(0  )->as_number() * diry;
+    this->trim_mm[2] = THEKERNEL->config->value(gamma_trim_checksum )->by_default(0  )->as_number() * dirz;
 }
 
 void Endstops::wait_for_homed(char axes_to_move)
@@ -198,19 +195,19 @@ void Endstops::wait_for_homed(char axes_to_move)
     while (running) {
         running = false;
         THEKERNEL->call_event(ON_IDLE);
-        for ( char c = 'X'; c <= 'Z'; c++ ) {
-            if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
-                if ( this->pins[c - 'X' + (this->home_direction[c - 'X'] ? 0 : 3)].get() ) {
-                    if ( debounce[c - 'X'] < debounce_count ) {
-                        debounce[c - 'X'] ++;
+        for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
+            if ( ( axes_to_move >> c ) & 1 ) {
+                if ( this->pins[c + (this->home_direction[c] ? 0 : 3)].get() ) {
+                    if ( debounce[c] < debounce_count ) {
+                        debounce[c]++;
                         running = true;
-                    } else if ( this->steppers[c - 'X']->moving ) {
-                        this->steppers[c - 'X']->move(0, 0);
+                    } else if ( this->steppers[c]->moving ) {
+                        this->steppers[c]->move(0, 0);
                     }
                 } else {
                     // The endstop was not hit yet
                     running = true;
-                    debounce[c - 'X'] = 0;
+                    debounce[c] = 0;
                 }
             }
         }
@@ -222,11 +219,11 @@ void Endstops::do_homing(char axes_to_move)
 {
     // Start moving the axes to the origin
     this->status = MOVING_TO_ORIGIN_FAST;
-    for ( char c = 'X'; c <= 'Z'; c++ ) {
-        if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
-            this->feed_rate[c - 'X']= this->fast_rates[c - 'X'];
-            this->steppers[c - 'X']->set_speed(0);
-            this->steppers[c - 'X']->move(this->home_direction[c - 'X'], 10000000);
+    for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
+        if ( ( axes_to_move >> c) & 1 ) {
+            this->feed_rate[c]= this->fast_rates[c];
+            this->steppers[c]->set_speed(0);
+            this->steppers[c]->move(this->home_direction[c], 10000000);
         }
     }
 
@@ -236,19 +233,19 @@ void Endstops::do_homing(char axes_to_move)
     // Move back a small distance
     this->status = MOVING_BACK;
     bool inverted_dir;
-    for ( char c = 'X'; c <= 'Z'; c++ ) {
-        if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
-            inverted_dir = !this->home_direction[c - 'X'];
-            this->feed_rate[c - 'X']= this->slow_rates[c - 'X'];
-            this->steppers[c - 'X']->set_speed(0);
-            this->steppers[c - 'X']->move(inverted_dir, this->retract_steps[c - 'X']);
+    for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
+        if ( ( axes_to_move >> c ) & 1 ) {
+            inverted_dir = !this->home_direction[c];
+            this->feed_rate[c]= this->slow_rates[c];
+            this->steppers[c]->set_speed(0);
+            this->steppers[c]->move(inverted_dir, this->retract_mm[c]*STEPS_PER_MM(c));
         }
     }
 
     // Wait for moves to be done
-    for ( char c = 'X'; c <= 'Z'; c++ ) {
-        if (  ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
-            while ( this->steppers[c - 'X']->moving ) {
+    for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
+        if (  ( axes_to_move >> c ) & 1 ) {
+            while ( this->steppers[c]->moving ) {
                 THEKERNEL->call_event(ON_IDLE);
             }
         }
@@ -256,11 +253,11 @@ void Endstops::do_homing(char axes_to_move)
 
     // Start moving the axes to the origin slowly
     this->status = MOVING_TO_ORIGIN_SLOW;
-    for ( char c = 'X'; c <= 'Z'; c++ ) {
-        if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
-            this->feed_rate[c - 'X']= this->slow_rates[c - 'X'];
-            this->steppers[c - 'X']->set_speed(0);
-            this->steppers[c - 'X']->move(this->home_direction[c - 'X'], 10000000);
+    for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
+        if ( ( axes_to_move >> c ) & 1 ) {
+            this->feed_rate[c]= this->slow_rates[c];
+            this->steppers[c]->set_speed(0);
+            this->steppers[c]->move(this->home_direction[c], 10000000);
         }
     }
 
@@ -270,22 +267,22 @@ void Endstops::do_homing(char axes_to_move)
     if (this->is_delta) {
         // move for soft trim
         this->status = MOVING_BACK;
-        for ( char c = 'X'; c <= 'Z'; c++ ) {
-            if ( this->trim[c - 'X'] != 0 && ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
-                inverted_dir = !this->home_direction[c - 'X'];
-                // move up or down depending on sign of trim
-                if (this->trim[c - 'X'] < 0) inverted_dir = !inverted_dir;
-                this->feed_rate[c - 'X']= this->slow_rates[c - 'X'];
-                this->steppers[c - 'X']->set_speed(0);
-                this->steppers[c - 'X']->move(inverted_dir, abs(this->trim[c - 'X']));
+        for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
+            if ( this->trim_mm[c] != 0.0F && ( axes_to_move >> c ) & 1 ) {
+                inverted_dir = this->home_direction[c];
+                // move up or down depending on sign of trim, -ive is down away from home
+                if (this->trim_mm[c] < 0) inverted_dir = !inverted_dir;
+                this->feed_rate[c]= this->slow_rates[c];
+                this->steppers[c]->set_speed(0);
+                this->steppers[c]->move(inverted_dir, abs(round(this->trim_mm[c]*STEPS_PER_MM(c))));
             }
         }
 
         // Wait for moves to be done
-        for ( char c = 'X'; c <= 'Z'; c++ ) {
-            if (  ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
+        for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
+            if (  ( axes_to_move >> c ) & 1 ) {
                 //THEKERNEL->streams->printf("axis %c \r\n", c );
-                while ( this->steppers[c - 'X']->moving ) {
+                while ( this->steppers[c]->moving ) {
                     THEKERNEL->call_event(ON_IDLE);
                 }
             }
@@ -411,12 +408,12 @@ void Endstops::do_homing_corexy(char axes_to_move)
     // move individual axis
     if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
         bool dir= this->home_direction[X_AXIS];
-        corexy_home(X_AXIS, dir, dir, this->fast_rates[X_AXIS], this->slow_rates[X_AXIS], this->retract_steps[X_AXIS]);
+        corexy_home(X_AXIS, dir, dir, this->fast_rates[X_AXIS], this->slow_rates[X_AXIS], this->retract_mm[X_AXIS]*STEPS_PER_MM(X_AXIS));
     }
 
     if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
         bool dir= this->home_direction[Y_AXIS];
-        corexy_home(Y_AXIS, dir, !dir, this->fast_rates[Y_AXIS], this->slow_rates[Y_AXIS], this->retract_steps[Y_AXIS]);
+        corexy_home(Y_AXIS, dir, !dir, this->fast_rates[Y_AXIS], this->slow_rates[Y_AXIS], this->retract_mm[Y_AXIS]*STEPS_PER_MM(Y_AXIS));
     }
 
     if (axes_to_move & 0x04) { // move Z
@@ -444,9 +441,9 @@ void Endstops::on_gcode_received(void *argument)
             // only enable homing if the endstop is defined, deltas always home all axis
             bool home_all = this->is_delta || !( gcode->has_letter('X') || gcode->has_letter('Y') || gcode->has_letter('Z') );
 
-            for ( char c = 'X'; c <= 'Z'; c++ ) {
-                if ( (home_all || gcode->has_letter(c)) && this->pins[c - 'X' + (this->home_direction[c - 'X'] ? 0 : 3)].connected() ) {
-                    axes_to_move += ( 1 << (c - 'X' ) );
+            for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
+                if ( (home_all || gcode->has_letter(c+'X')) && this->pins[c + (this->home_direction[c] ? 0 : 3)].connected() ) {
+                    axes_to_move += ( 1 << c );
                 }
             }
 
@@ -460,7 +457,7 @@ void Endstops::on_gcode_received(void *argument)
                 do_homing(axes_to_move);
 
             // Zero the ax(i/e)s position, add in the home offset
-            for ( int c = 0; c <= 2; c++ ) {
+            for ( int c = X_AXIS; c <= Z_AXIS; c++ ) {
                 if ( (axes_to_move >> c)  & 1 ) {
                     THEKERNEL->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c);
                 }
@@ -495,10 +492,8 @@ void Endstops::on_gcode_received(void *argument)
             case 503: // print settings
                 gcode->stream->printf(";Home offset (mm):\nM206 X%1.2f Y%1.2f Z%1.2f\n", home_offset[0], home_offset[1], home_offset[2]);
                 if (is_delta) {
-                    float mm[3];
-                    trim2mm(mm);
-                    gcode->stream->printf(";Trim (mm):\nM666 X%1.2f Y%1.2f Z%1.2f\n", mm[0], mm[1], mm[2]);
-                    gcode->stream->printf(";Max Z\nM665 Z%1.2f\n", this->homing_position[2]);
+                    gcode->stream->printf(";Trim (mm):\nM666 X%1.3f Y%1.3f Z%1.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
+                    gcode->stream->printf(";Max Z\nM665 Z%1.3f\n", this->homing_position[2]);
                 }
                 gcode->mark_as_taken();
                 break;
@@ -515,25 +510,16 @@ void Endstops::on_gcode_received(void *argument)
             break;
 
 
-            case 666: { // M666 - set trim for each axis in mm, NB negative mm and positive steps trim is down
-                float mm[3];
-                trim2mm(mm);
+            case 666:
+                if(this->is_delta) { // M666 - set trim for each axis in mm, NB negative mm trim is down
+                    if (gcode->has_letter('X')) trim_mm[0] = gcode->get_value('X');
+                    if (gcode->has_letter('Y')) trim_mm[1] = gcode->get_value('Y');
+                    if (gcode->has_letter('Z')) trim_mm[2] = gcode->get_value('Z');
 
-                if (gcode->has_letter('X')) mm[0] = gcode->get_value('X');
-                if (gcode->has_letter('Y')) mm[1] = gcode->get_value('Y');
-                if (gcode->has_letter('Z')) mm[2] = gcode->get_value('Z');
-
-                int dirx = (this->home_direction[0] ? 1 : -1);
-                int diry = (this->home_direction[1] ? 1 : -1);
-                int dirz = (this->home_direction[2] ? 1 : -1);
-                trim[0] = lround(mm[0] * steps_per_mm[0]) * dirx; // convert back to steps
-                trim[1] = lround(mm[1] * steps_per_mm[1]) * diry;
-                trim[2] = lround(mm[2] * steps_per_mm[2]) * dirz;
-
-                // print the current trim values in mm and steps
-                gcode->stream->printf("X %5.3f (%d) Y %5.3f (%d) Z %5.3f (%d)\n", mm[0], trim[0], mm[1], trim[1], mm[2], trim[2]);
-                gcode->mark_as_taken();
-            }
+                    // print the current trim values in mm
+                    gcode->stream->printf("X: %5.3f Y: %5.3f Z: %5.3f\n", trim_mm[0], trim_mm[1], trim_mm[2]);
+                    gcode->mark_as_taken();
+                }
             break;
 
             // NOTE this is to test accuracy of lead screws etc.
@@ -563,17 +549,6 @@ void Endstops::on_gcode_received(void *argument)
     }
 }
 
-void Endstops::trim2mm(float *mm)
-{
-    int dirx = (this->home_direction[0] ? 1 : -1);
-    int diry = (this->home_direction[1] ? 1 : -1);
-    int dirz = (this->home_direction[2] ? 1 : -1);
-
-    mm[0] = this->trim[0] / this->steps_per_mm[0] * dirx; // convert to mm
-    mm[1] = this->trim[1] / this->steps_per_mm[1] * diry;
-    mm[2] = this->trim[2] / this->steps_per_mm[2] * dirz;
-}
-
 #define max(a,b) (((a) > (b)) ? (a) : (b))
 // Called periodically to change the speed to match acceleration
 uint32_t Endstops::acceleration_tick(uint32_t dummy)
@@ -585,10 +560,10 @@ uint32_t Endstops::acceleration_tick(uint32_t dummy)
         if( !this->steppers[c]->moving ) continue;
 
         uint32_t current_rate = this->steppers[c]->steps_per_second;
-        uint32_t target_rate = int(floor(this->feed_rate[c]));
+        uint32_t target_rate = int(floor(this->feed_rate[c]*STEPS_PER_MM(c)));
 
         if( current_rate < target_rate ){
-            uint32_t rate_increase = int(floor((THEKERNEL->planner->acceleration/THEKERNEL->stepper->acceleration_ticks_per_second)*this->steps_per_mm[c]));
+            uint32_t rate_increase = int(floor((THEKERNEL->planner->acceleration/THEKERNEL->stepper->acceleration_ticks_per_second)*STEPS_PER_MM(c)));
             current_rate = min( target_rate, current_rate + rate_increase );
         }
         if( current_rate > target_rate ){ current_rate = target_rate; }
index 9aba4e5..e7aeacb 100644 (file)
@@ -29,19 +29,17 @@ class Endstops : public Module{
         void wait_for_homed(char axes_to_move);
         void wait_for_homed_corexy(int axis);
         void corexy_home(int home_axis, bool dirx, bool diry, float fast_rate, float slow_rate, unsigned int retract_steps);
-        void trim2mm(float * mm);
 
-        float steps_per_mm[3];
         float homing_position[3];
         float home_offset[3];
         bool home_direction[3];
         unsigned int  debounce_count;
-        unsigned int  retract_steps[3];
-        int  trim[3];
+        float  retract_mm[3];
+        float  trim_mm[3];
         float  fast_rates[3];
         float  slow_rates[3];
         float  feed_rate[3];
-        Pin           pins[6];
+        Pin    pins[6];
         StepperMotor* steppers[3];
         char status;
         bool is_corexy;
index 243306b..b183c4e 100644 (file)
 #define Y_AXIS 1
 #define Z_AXIS 2
 
+#define STEPS_PER_MM(a) (this->steppers[a]->steps_per_mm)
+#define Z_STEPS_PER_MM STEPS_PER_MM(Z_AXIS)
+
 void ZProbe::on_module_loaded()
 {
     // if the module is disabled -> do nothing
-    this->enabled = THEKERNEL->config->value( zprobe_checksum, enable_checksum )->by_default(false)->as_bool();
-    if( !(this->enabled) ) {
+    if(!THEKERNEL->config->value( zprobe_checksum, enable_checksum )->by_default(false)->as_bool()) {
         // as this module is not needed free up the resource
         delete this;
         return;
@@ -85,12 +87,6 @@ void ZProbe::on_config_reload(void *argument)
     this->steppers[1] = THEKERNEL->robot->beta_stepper_motor;
     this->steppers[2] = THEKERNEL->robot->gamma_stepper_motor;
 
-    // we need to know steps per mm
-    // FIXME we need to get this after config loaded from robot as the config settings can be overriden or trap M92
-    this->steps_per_mm[0] = THEKERNEL->config->value(alpha_steps_per_mm_checksum)->as_number();
-    this->steps_per_mm[1] = THEKERNEL->config->value(beta_steps_per_mm_checksum)->as_number();
-    this->steps_per_mm[2] = THEKERNEL->config->value(gamma_steps_per_mm_checksum)->as_number();
-
     this->slow_feedrate = THEKERNEL->config->value(zprobe_checksum, slow_feedrate_checksum)->by_default(5)->as_number(); // feedrate in mm/sec
     this->fast_feedrate = THEKERNEL->config->value(zprobe_checksum, fast_feedrate_checksum)->by_default(100)->as_number(); // feedrate in mm/sec
 }
@@ -138,23 +134,23 @@ bool ZProbe::run_probe(int& steps, bool fast)
 {
     // Enable the motors
     THEKERNEL->stepper->turn_enable_pins_on();
-    this->current_feedrate = (fast ? this->fast_feedrate : this->slow_feedrate) * this->steps_per_mm[Z_AXIS]; // steps/sec
+    this->current_feedrate = (fast ? this->fast_feedrate : this->slow_feedrate) * Z_STEPS_PER_MM; // steps/sec
 
     // move Z down
     this->running = true;
     this->steppers[Z_AXIS]->set_speed(0); // will be increased by acceleration tick
-    this->steppers[Z_AXIS]->move(true, 1000 * this->steps_per_mm[Z_AXIS]); // always probes down, no more than 1000mm TODO should be 2*maxz
+    this->steppers[Z_AXIS]->move(true, 1000 * Z_STEPS_PER_MM); // always probes down, no more than 1000mm TODO should be 2*maxz
     if(this->is_delta) {
         // for delta need to move all three actuators
         this->steppers[X_AXIS]->set_speed(0);
-        this->steppers[X_AXIS]->move(true, 1000 * this->steps_per_mm[X_AXIS]);
+        this->steppers[X_AXIS]->move(true, 1000 * STEPS_PER_MM(X_AXIS));
         this->steppers[Y_AXIS]->set_speed(0);
-        this->steppers[Y_AXIS]->move(true, 1000 * this->steps_per_mm[Y_AXIS]);
+        this->steppers[Y_AXIS]->move(true, 1000 * STEPS_PER_MM(Y_AXIS));
     }
 
     int s[3];
     bool r = wait_for_probe(s);
-    steps= s[2]; // only need z
+    steps= s[Z_AXIS]; // only need z
     this->running = false;
     return r;
 }
@@ -162,7 +158,7 @@ bool ZProbe::run_probe(int& steps, bool fast)
 bool ZProbe::return_probe(int steps)
 {
     // move probe back to where it was
-    this->current_feedrate = this->fast_feedrate * this->steps_per_mm[Z_AXIS]; // feedrate in steps/sec
+    this->current_feedrate = this->fast_feedrate * Z_STEPS_PER_MM; // feedrate in steps/sec
     bool dir= steps < 0;
     steps= abs(steps);
 
@@ -229,6 +225,8 @@ bool ZProbe::calibrate_delta_endstops(Gcode *gcode)
     float t1x, t1y, t2x, t2y, t3x, t3y;
     std::tie(t1x, t1y, t2x, t2y, t3x, t3y) = getCoordinates(this->probe_radius);
 
+    // TODO get current trim, and continue from that if requested
+
     // zero trim values
     set_trim(0, 0, 0, &(StreamOutput::NullStream));
 
@@ -240,30 +238,28 @@ bool ZProbe::calibrate_delta_endstops(Gcode *gcode)
     if(!run_probe(s, true)) return false;
 
     // how far to move down from home before probe
-    int probestart = s - (this->probe_height*this->steps_per_mm[Z_AXIS]);
-    gcode->stream->printf("Probe start ht is %f mm\n", probestart/this->steps_per_mm[Z_AXIS]);
-
+    int probestart = s - (this->probe_height*Z_STEPS_PER_MM);
+    gcode->stream->printf("Probe start ht is %f mm\n", probestart/Z_STEPS_PER_MM);
 
     // move to start position
     home();
     return_probe(-probestart);
 
-
     gcode->stream->printf("Calibrating Endstops\n");
     // get initial probes
     // probe the base of the X tower
     if(!probe_delta_tower(s, t1x, t1y)) return false;
-    float t1z= s / this->steps_per_mm[Z_AXIS];
+    float t1z= s / Z_STEPS_PER_MM;
     gcode->stream->printf("T1-1 Z:%1.4f C:%d\n", t1z, s);
 
     // probe the base of the Y tower
     if(!probe_delta_tower(s, t2x, t2y)) return false;
-    float t2z= s / this->steps_per_mm[Z_AXIS];
+    float t2z= s / Z_STEPS_PER_MM;
     gcode->stream->printf("T2-1 Z:%1.4f C:%d\n", t2z, s);
 
     // probe the base of the Z tower
     if(!probe_delta_tower(s, t3x, t3y)) return false;
-    float t3z= s / this->steps_per_mm[Z_AXIS];
+    float t3z= s / Z_STEPS_PER_MM;
     gcode->stream->printf("T3-1 Z:%1.4f C:%d\n", t3z, s);
 
     float trimscale= 1.2522F; // empirically determined
@@ -275,28 +271,31 @@ bool ZProbe::calibrate_delta_endstops(Gcode *gcode)
     // set initial trim
     set_trim(trimx, trimy, trimz, gcode->stream);
 
-    for (int i = 1; i <= 4; ++i) {
+    float target= 0.03F;
+    if(gcode->has_letter('I')) target= gcode->get_value('I'); // override default target
+
+    for (int i = 1; i <= 10; ++i) {
         // home and move probe to start position just above the bed
         home();
         return_probe(-probestart);
 
         // probe the base of the X tower
         if(!probe_delta_tower(s, t1x, t1y)) return false;
-        t1z= s / this->steps_per_mm[Z_AXIS];
+        t1z= s / Z_STEPS_PER_MM;
         gcode->stream->printf("T1-2-%d Z:%1.4f C:%d\n", i, t1z, s);
 
         // probe the base of the Y tower
         if(!probe_delta_tower(s, t2x, t2y)) return false;
-        t2z= s / this->steps_per_mm[Z_AXIS];
+        t2z= s / Z_STEPS_PER_MM;
         gcode->stream->printf("T2-2-%d Z:%1.4f C:%d\n", i, t2z, s);
 
         // probe the base of the Z tower
         if(!probe_delta_tower(s, t3x, t3y)) return false;
-        t3z= s / this->steps_per_mm[Z_AXIS];
+        t3z= s / Z_STEPS_PER_MM;
         gcode->stream->printf("T3-2-%d Z:%1.4f C:%d\n", i, t3z, s);
 
         auto mm= std::minmax({t1z, t2z, t3z});
-        if((mm.second-mm.first) < 0.03F) break; // probably as good as it gets, TODO set 0.02 as config value
+        if((mm.second-mm.first) <= target) break; // probably as good as it gets
 
         // set new trim values based on min difference
         min= mm.first;
@@ -318,15 +317,15 @@ bool ZProbe::calibrate_delta_endstops(Gcode *gcode)
     // probe the base of the three towers again to see if we are level
     int dx= 0, dy= 0, dz= 0;
     if(!probe_delta_tower(dx, t1x, t1y)) return false;
-    gcode->stream->printf("T1-final Z:%1.4f C:%d\n", dx / this->steps_per_mm[Z_AXIS], dx);
+    gcode->stream->printf("T1-final Z:%1.4f C:%d\n", dx / Z_STEPS_PER_MM, dx);
     if(!probe_delta_tower(dy, t2x, t2y)) return false;
-    gcode->stream->printf("T2-final Z:%1.4f C:%d\n", dy / this->steps_per_mm[Z_AXIS], dy);
+    gcode->stream->printf("T2-final Z:%1.4f C:%d\n", dy / Z_STEPS_PER_MM, dy);
     if(!probe_delta_tower(dz, t3x, t3y)) return false;
-    gcode->stream->printf("T3-final Z:%1.4f C:%d\n", dz / this->steps_per_mm[Z_AXIS], dz);
+    gcode->stream->printf("T3-final Z:%1.4f C:%d\n", dz / Z_STEPS_PER_MM, dz);
 
     // compare the three and report
     auto mm= std::minmax({dx, dy, dz});
-    gcode->stream->printf("max endstop delta= %f\n", (mm.second-mm.first)/this->steps_per_mm[Z_AXIS]);
+    gcode->stream->printf("max endstop delta= %1.4f\n", (mm.second-mm.first)/Z_STEPS_PER_MM);
 
     return true;
 }
@@ -343,7 +342,7 @@ bool ZProbe::calibrate_delta_radius(Gcode *gcode)
     // find bed, then move to a point 5mm above it
     int s;
     if(!run_probe(s, true)) return false;
-    float bedht= s/this->steps_per_mm[Z_AXIS] - this->probe_height; // distance to move from home to 5mm above bed
+    float bedht= s/Z_STEPS_PER_MM - this->probe_height; // distance to move from home to 5mm above bed
     gcode->stream->printf("Bed ht is %f mm\n", bedht);
 
     home();
@@ -352,15 +351,25 @@ bool ZProbe::calibrate_delta_radius(Gcode *gcode)
     // probe the base of the three towers to get reference point at this Z height
     int dx= 0, dy= 0, dz= 0, dc= 0;
     if(!probe_delta_tower(dx, t1x, t1y)) return false;
-    gcode->stream->printf("T1 Z:%1.3f C:%d\n", dx / this->steps_per_mm[Z_AXIS], dx);
+    gcode->stream->printf("T1 Z:%1.3f C:%d\n", dx / Z_STEPS_PER_MM, dx);
     if(!probe_delta_tower(dy, t2x, t2y)) return false;
-    gcode->stream->printf("T2 Z:%1.3f C:%d\n", dy / this->steps_per_mm[Z_AXIS], dy);
+    gcode->stream->printf("T2 Z:%1.3f C:%d\n", dy / Z_STEPS_PER_MM, dy);
     if(!probe_delta_tower(dz, t3x, t3y)) return false;
-    gcode->stream->printf("T3 Z:%1.3f C:%d\n", dz / this->steps_per_mm[Z_AXIS], dz);
+    gcode->stream->printf("T3 Z:%1.3f C:%d\n", dz / Z_STEPS_PER_MM, dz);
     if(!probe_delta_tower(dc, 0, 0)) return false;
-    gcode->stream->printf("CT Z:%1.3f C:%d\n", dc / this->steps_per_mm[Z_AXIS], dc);
-
-    float cmm= dc / this->steps_per_mm[Z_AXIS];
+    gcode->stream->printf("CT Z:%1.3f C:%d\n", dc / Z_STEPS_PER_MM, dc);
+
+    float target= 0.03F;
+    if(gcode->has_letter('I')) target= gcode->get_value('I'); // override default target
+
+    // See if we are already in range and skip calibration if not needed
+    float cmm= dc / Z_STEPS_PER_MM;
+    float m= dx / Z_STEPS_PER_MM;
+    float d= cmm-m;
+    if(abs(d) <= target) {
+        gcode->stream->printf("Delta Radius already in range: %1.3f\n", d);
+        return true;
+    }
 
     // get current delta radius
     float delta_radius= 0.0F;
@@ -387,10 +396,10 @@ bool ZProbe::calibrate_delta_radius(Gcode *gcode)
         if(!probe_delta_tower(dx, t1x, t1y)) return false;
 
         // now look at the difference and reduce it by adjusting delta radius
-        float m= dx / this->steps_per_mm[Z_AXIS];
-        float d= cmm-m;
+        m= dx / Z_STEPS_PER_MM;
+        d= cmm-m;
         gcode->stream->printf("T1-%d Z:%1.4f C:%d delta: %1.3f\n", i, m, dx, d);
-        if(abs(d) < 0.03F) break; // resolution of success TODO should be in config
+        if(abs(d) <= target) break; // resolution of success
         // increase delta radius to adjust for low center
         // decrease delta radius to adjust for high center
         delta_radius += (d*drinc);
@@ -411,7 +420,7 @@ void ZProbe::on_gcode_received(void *argument)
 
             int steps;
             if(run_probe(steps)) {
-                gcode->stream->printf("Z:%1.4f C:%d\n", steps / this->steps_per_mm[Z_AXIS], steps);
+                gcode->stream->printf("Z:%1.4f C:%d\n", steps / Z_STEPS_PER_MM, steps);
                 // move back to where it started, unless a Z is specified
                 if(gcode->has_letter('Z')) {
                     // set Z to the specified value, and leave probe where it is
@@ -474,7 +483,7 @@ uint32_t ZProbe::acceleration_tick(uint32_t dummy)
         uint32_t target_rate = int(floor(this->current_feedrate));
 
         if( current_rate < target_rate ) {
-            uint32_t rate_increase = int(floor((THEKERNEL->planner->acceleration / THEKERNEL->stepper->acceleration_ticks_per_second) * this->steps_per_mm[c]));
+            uint32_t rate_increase = int(floor((THEKERNEL->planner->acceleration / THEKERNEL->stepper->acceleration_ticks_per_second) * STEPS_PER_MM(c)));
             current_rate = min( target_rate, current_rate + rate_increase );
         }
         if( current_rate > target_rate ) {
index 0c856e5..c4699ff 100644 (file)
@@ -42,12 +42,9 @@ private:
     float          current_feedrate;
     float          slow_feedrate;
     float          fast_feedrate;
-    float          steps_per_mm[3];
-    unsigned int   mcode;
-    bool           enabled;
     StepperMotor  *steppers[3];
     Pin            pin;
-    unsigned int   debounce_count;
+    uint8_t        debounce_count;
     bool           running;
     bool           is_delta;
 };