Added M500, M501, M503 to save certain values to a config-override file that gets...
authorJim Morris <morris@wolfman.com>
Fri, 13 Dec 2013 07:34:32 +0000 (23:34 -0800)
committerJim Morris <morris@wolfman.com>
Fri, 13 Dec 2013 07:37:24 +0000 (23:37 -0800)
Currently M206, M92, M301 are saved when M500 is used, M503 just displays current overriden settings

src/libs/Kernel.h
src/main.cpp
src/modules/communication/GcodeDispatch.cpp
src/modules/robot/Robot.cpp
src/modules/tools/endstops/Endstops.cpp
src/modules/tools/endstops/Endstops.h
src/modules/tools/extruder/Extruder.cpp
src/modules/tools/temperaturecontrol/TemperatureControl.cpp
src/modules/utils/player/Player.cpp

index 220650c..c8f0fc4 100644 (file)
@@ -34,6 +34,7 @@ class Kernel {
     public:
         Kernel();
         static Kernel* instance; // the Singleton instance of Kernel usable anywhere
+        const char* config_override_filename(){ return "/sd/config-override"; }
 
         void add_module(Module* module);
         void register_for_event(_EVENT_ENUM id_event, Module* module);
index 1de239e..01b40c8 100644 (file)
@@ -137,6 +137,24 @@ int main() {
         leds[3]= sdok?1:0; // 4th led inidicates sdcard is available (TODO maye should indicate config was found)
     }
 
+    if(sdok) {
+        // load config override file if present
+        // NOTE only Mxxx commands that set values should be put in this file. The file is generated by M500
+        FILE *fp= fopen(kernel->config_override_filename(), "r");
+        if(fp != NULL) {
+            char buf[132];
+            kernel->streams->printf("Loading config override file: %s...\n", kernel->config_override_filename());
+            while(fgets(buf, sizeof buf, fp) != NULL) {
+                kernel->streams->printf("  %s", buf);
+                if(buf[0] == ';') continue; // skip the comments
+                struct SerialMessage message= {&(StreamOutput::NullStream), buf};
+                kernel->call_event(ON_CONSOLE_LINE_RECEIVED, &message);
+            }
+            kernel->streams->printf("config override file executed\n");
+            fclose(fp);
+        }
+    }
+
     uint16_t cnt= 0;
     // Main loop
     while(1){
index bbbaf1d..b5e4da1 100644 (file)
@@ -15,6 +15,7 @@ using std::string;
 #include "modules/robot/Conveyor.h"
 #include "libs/SerialMessage.h"
 #include "libs/StreamOutput.h"
+#include "libs/AppendFileStream.h"
 
 GcodeDispatch::GcodeDispatch(){}
 
@@ -116,7 +117,26 @@ void GcodeDispatch::on_console_line_received(void * line){
                         continue;
                     }
 
-    //                 printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m);
+                    if(gcode->has_m && gcode->m == 500) { // M500 save volatile settings to config-override
+                        // delete the existing file
+                        remove(kernel->config_override_filename());
+                        // replace stream with one that appends to config-override file
+                        gcode->stream= new AppendFileStream(kernel->config_override_filename());
+                        // dispatch the M500
+                        this->kernel->call_event(ON_GCODE_RECEIVED, gcode );
+                        delete gcode->stream;
+                        delete gcode;
+                        new_message.stream->printf("Settings Stored to %s\r\nok\r\n", kernel->config_override_filename());
+                        continue;
+                    }
+                    if(gcode->has_m && gcode->m == 501) { // M501 deletes config-override so everything defaults to what is in config
+                        remove(kernel->config_override_filename());
+                        new_message.stream->printf("config override file deleted %s\r\nok\r\n", kernel->config_override_filename());
+                        delete gcode;
+                        continue;
+                    }
+
+                    //printf("dispatch %p: '%s' G%d M%d...", gcode, gcode->command.c_str(), gcode->g, gcode->m);
                     //Dispatch message!
                     this->kernel->call_event(ON_GCODE_RECEIVED, gcode );
                     if (gcode->add_nl)
index b4652e1..9be6420 100644 (file)
@@ -208,9 +208,9 @@ void Robot::on_gcode_received(void * argument){
            }
        }
    }else if( gcode->has_m){
-     switch( gcode->m ){
+        double steps[3];
+        switch( gcode->m ){
             case 92: // M92 - set steps per mm
-                double steps[3];
                 this->arm_solution->get_steps_per_millimeter(steps);
                 if (gcode->has_letter('X'))
                     steps[0] = this->to_millimeters(gcode->get_value('X'));
@@ -234,6 +234,7 @@ void Robot::on_gcode_received(void * argument){
                 gcode->add_nl = true;
                 gcode->mark_as_taken();
                 return;
+
             // TODO I'm not sure if the following is safe to do here, or should it go on the block queue?
             // case 204: // M204 Snnn - set acceleration to nnn, NB only Snnn is currently supported
             //     gcode->mark_as_taken();
@@ -264,6 +265,13 @@ void Robot::on_gcode_received(void * argument){
                 this->kernel->conveyor->wait_for_empty_queue();
                 break;
 
+            case 500: // M500 saves some volatile settings to config override file
+            case 503: // M503 just prints the settings
+                this->arm_solution->get_steps_per_millimeter(steps);
+                gcode->stream->printf(";Steps per unit:\nM92 X%1.4f Y%1.4f Z%1.4f\n", steps[0], steps[1], steps[2]);
+                gcode->mark_as_taken();
+                break;
+
             case 665: // M665 set optional arm solution variables based on arm solution
                 gcode->mark_as_taken();
                 // the parameter args could be any letter so try each one
index 8770a0c..55bbead 100644 (file)
 #include "libs/StepperMotor.h"
 #include "wait_api.h" // mbed.h lib
 
-Endstops::Endstops(){
+#define ALPHA_AXIS 0
+#define BETA_AXIS  1
+#define GAMMA_AXIS 2
+
+#define NOT_HOMING 0
+#define MOVING_TO_ORIGIN_FAST 1
+#define MOVING_BACK 2
+#define MOVING_TO_ORIGIN_SLOW 3
+
+#define endstops_module_enable_checksum         CHECKSUM("endstops_enable")
+#define corexy_homing_checksum                  CHECKSUM("corexy_homing")
+#define delta_homing_checksum                   CHECKSUM("delta_homing")
+
+#define alpha_min_endstop_checksum       CHECKSUM("alpha_min_endstop")
+#define beta_min_endstop_checksum        CHECKSUM("beta_min_endstop")
+#define gamma_min_endstop_checksum       CHECKSUM("gamma_min_endstop")
+
+#define alpha_max_endstop_checksum       CHECKSUM("alpha_max_endstop")
+#define beta_max_endstop_checksum        CHECKSUM("beta_max_endstop")
+#define gamma_max_endstop_checksum       CHECKSUM("gamma_max_endstop")
+
+#define alpha_trim_checksum              CHECKSUM("alpha_trim")
+#define beta_trim_checksum               CHECKSUM("beta_trim")
+#define gamma_trim_checksum              CHECKSUM("gamma_trim")
+
+// these values are in steps and should be deprecated
+#define alpha_fast_homing_rate_checksum  CHECKSUM("alpha_fast_homing_rate")
+#define beta_fast_homing_rate_checksum   CHECKSUM("beta_fast_homing_rate")
+#define gamma_fast_homing_rate_checksum  CHECKSUM("gamma_fast_homing_rate")
+
+#define alpha_slow_homing_rate_checksum  CHECKSUM("alpha_slow_homing_rate")
+#define beta_slow_homing_rate_checksum   CHECKSUM("beta_slow_homing_rate")
+#define gamma_slow_homing_rate_checksum  CHECKSUM("gamma_slow_homing_rate")
+
+#define alpha_homing_retract_checksum    CHECKSUM("alpha_homing_retract")
+#define beta_homing_retract_checksum     CHECKSUM("beta_homing_retract")
+#define gamma_homing_retract_checksum    CHECKSUM("gamma_homing_retract")
+#define endstop_debounce_count_checksum  CHECKSUM("endstop_debounce_count")
+
+// same as above but in user friendly mm/s and mm
+#define alpha_fast_homing_rate_mm_checksum  CHECKSUM("alpha_fast_homing_rate_mm_s")
+#define beta_fast_homing_rate_mm_checksum   CHECKSUM("beta_fast_homing_rate_mm_s")
+#define gamma_fast_homing_rate_mm_checksum  CHECKSUM("gamma_fast_homing_rate_mm_s")
+
+#define alpha_slow_homing_rate_mm_checksum  CHECKSUM("alpha_slow_homing_rate_mm_s")
+#define beta_slow_homing_rate_mm_checksum   CHECKSUM("beta_slow_homing_rate_mm_s")
+#define gamma_slow_homing_rate_mm_checksum  CHECKSUM("gamma_slow_homing_rate_mm_s")
+
+#define alpha_homing_retract_mm_checksum    CHECKSUM("alpha_homing_retract_mm")
+#define beta_homing_retract_mm_checksum     CHECKSUM("beta_homing_retract_mm")
+#define gamma_homing_retract_mm_checksum    CHECKSUM("gamma_homing_retract_mm")
+
+#define endstop_debounce_count_checksum  CHECKSUM("endstop_debounce_count")
+
+#define alpha_homing_direction_checksum  CHECKSUM("alpha_homing_direction")
+#define beta_homing_direction_checksum   CHECKSUM("beta_homing_direction")
+#define gamma_homing_direction_checksum  CHECKSUM("gamma_homing_direction")
+#define home_to_max_checksum             CHECKSUM("home_to_max")
+#define home_to_min_checksum             CHECKSUM("home_to_min")
+#define alpha_min_checksum               CHECKSUM("alpha_min")
+#define beta_min_checksum                CHECKSUM("beta_min")
+#define gamma_min_checksum               CHECKSUM("gamma_min")
+
+#define alpha_max_checksum               CHECKSUM("alpha_max")
+#define beta_max_checksum                CHECKSUM("beta_max")
+#define gamma_max_checksum               CHECKSUM("gamma_max")
+
+#define alpha_steps_per_mm_checksum      CHECKSUM("alpha_steps_per_mm")
+#define beta_steps_per_mm_checksum       CHECKSUM("beta_steps_per_mm")
+#define gamma_steps_per_mm_checksum      CHECKSUM("gamma_steps_per_mm")
+
+Endstops::Endstops()
+{
     this->status = NOT_HOMING;
+    home_offset[0] = home_offset[1] = home_offset[2] = 0.0F;
 }
 
-void Endstops::on_module_loaded() {
+void Endstops::on_module_loaded()
+{
     // Do not do anything if not enabled
-    if( this->kernel->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ){ return; }
+    if ( this->kernel->config->value( endstops_module_enable_checksum )->by_default(true)->as_bool() == false ) {
+        return;
+    }
 
     register_for_event(ON_CONFIG_RELOAD);
     this->register_for_event(ON_GCODE_RECEIVED);
@@ -37,7 +113,8 @@ void Endstops::on_module_loaded() {
 }
 
 // Get config
-void Endstops::on_config_reload(void* argument){
+void Endstops::on_config_reload(void *argument)
+{
     this->pins[0].from_string(         this->kernel->config->value(alpha_min_endstop_checksum          )->by_default("nc" )->as_string())->as_input();
     this->pins[1].from_string(         this->kernel->config->value(beta_min_endstop_checksum           )->by_default("nc" )->as_string())->as_input();
     this->pins[2].from_string(         this->kernel->config->value(gamma_min_endstop_checksum          )->by_default("nc" )->as_string())->as_input();
@@ -61,15 +138,15 @@ void Endstops::on_config_reload(void* argument){
     this->retract_steps[2]          =  this->kernel->config->value(gamma_homing_retract_checksum       )->by_default(1600 )->as_number();
 
     // 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]=    this->kernel->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]=    this->kernel->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]=    this->kernel->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]=    this->kernel->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]=    this->kernel->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]=    this->kernel->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]= this->kernel->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]= this->kernel->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]= this->kernel->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] =    this->kernel->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] =    this->kernel->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] =    this->kernel->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] =    this->kernel->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] =    this->kernel->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] =    this->kernel->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] = this->kernel->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] = this->kernel->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] = this->kernel->config->value(gamma_homing_retract_mm_checksum   )->by_default(this->retract_steps[2] / steps_per_mm[2])->as_number() * steps_per_mm[2];
 
     this->debounce_count  = this->kernel->config->value(endstop_debounce_count_checksum    )->by_default(100)->as_number();
 
@@ -84,39 +161,40 @@ void Endstops::on_config_reload(void* argument){
     home_dir                        = get_checksum(this->kernel->config->value(gamma_homing_direction_checksum)->by_default("home_to_min")->as_string());
     this->home_direction[2]         = home_dir != home_to_max_checksum;
 
-    this->homing_position[0]        =  this->home_direction[0]?this->kernel->config->value(alpha_min_checksum)->by_default(0)->as_number():this->kernel->config->value(alpha_max_checksum)->by_default(200)->as_number();
-    this->homing_position[1]        =  this->home_direction[1]?this->kernel->config->value(beta_min_checksum )->by_default(0)->as_number():this->kernel->config->value(beta_max_checksum )->by_default(200)->as_number();;
-    this->homing_position[2]        =  this->home_direction[2]?this->kernel->config->value(gamma_min_checksum)->by_default(0)->as_number():this->kernel->config->value(gamma_max_checksum)->by_default(200)->as_number();;
+    this->homing_position[0]        =  this->home_direction[0] ? this->kernel->config->value(alpha_min_checksum)->by_default(0)->as_number() : this->kernel->config->value(alpha_max_checksum)->by_default(200)->as_number();
+    this->homing_position[1]        =  this->home_direction[1] ? this->kernel->config->value(beta_min_checksum )->by_default(0)->as_number() : this->kernel->config->value(beta_max_checksum )->by_default(200)->as_number();;
+    this->homing_position[2]        =  this->home_direction[2] ? this->kernel->config->value(gamma_min_checksum)->by_default(0)->as_number() : this->kernel->config->value(gamma_max_checksum)->by_default(200)->as_number();;
 
     this->is_corexy                 =  this->kernel->config->value(corexy_homing_checksum)->by_default(false)->as_bool();
     this->is_delta                  =  this->kernel->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
     // 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]= this->kernel->config->value(alpha_trim_checksum )->by_default(0  )->as_number() * steps_per_mm[0] * dirx;
-    this->trim[1]= this->kernel->config->value(beta_trim_checksum  )->by_default(0  )->as_number() * steps_per_mm[1] * diry;
-    this->trim[2]= this->kernel->config->value(gamma_trim_checksum )->by_default(0  )->as_number() * steps_per_mm[2] * dirz;
+    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] = this->kernel->config->value(alpha_trim_checksum )->by_default(0  )->as_number() * steps_per_mm[0] * dirx;
+    this->trim[1] = this->kernel->config->value(beta_trim_checksum  )->by_default(0  )->as_number() * steps_per_mm[1] * diry;
+    this->trim[2] = this->kernel->config->value(gamma_trim_checksum )->by_default(0  )->as_number() * steps_per_mm[2] * dirz;
 }
 
-void Endstops::wait_for_homed(char axes_to_move){
+void Endstops::wait_for_homed(char axes_to_move)
+{
     bool running = true;
-    unsigned int debounce[3] = {0,0,0};
-    while(running){
+    unsigned int debounce[3] = {0, 0, 0};
+    while (running) {
         running = false;
         this->kernel->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 ) {
+        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'] ++;
                         running = true;
-                    } else if ( this->steppers[c - 'X']->moving ){
-                        this->steppers[c - 'X']->move(0,0);
+                    } else if ( this->steppers[c - 'X']->moving ) {
+                        this->steppers[c - 'X']->move(0, 0);
                     }
-                }else{
+                } else {
                     // The endstop was not hit yet
                     running = true;
                     debounce[c - 'X'] = 0;
@@ -127,13 +205,14 @@ void Endstops::wait_for_homed(char axes_to_move){
 }
 
 // this homing works for cartesian and delta printers, not for HBots/CoreXY
-void Endstops::do_homing(char axes_to_move) {
+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 ){
+    for ( char c = 'X'; c <= 'Z'; c++ ) {
+        if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
             this->steppers[c - 'X']->set_speed(this->fast_rates[c - 'X']);
-            this->steppers[c - 'X']->move(this->home_direction[c - 'X'],10000000);
+            this->steppers[c - 'X']->move(this->home_direction[c - 'X'], 10000000);
         }
     }
 
@@ -143,18 +222,18 @@ 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 ){
+    for ( char c = 'X'; c <= 'Z'; c++ ) {
+        if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
             inverted_dir = !this->home_direction[c - 'X'];
             this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']);
-            this->steppers[c - 'X']->move(inverted_dir,this->retract_steps[c - 'X']);
+            this->steppers[c - 'X']->move(inverted_dir, this->retract_steps[c - 'X']);
         }
     }
 
-     // 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 ){
+    // 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 ) {
                 this->kernel->call_event(ON_IDLE);
             }
         }
@@ -162,34 +241,34 @@ 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->steppers[c - 'X']->set_speed(this->slow_rates[c -'X']);
-            this->steppers[c - 'X']->move(this->home_direction[c - 'X'],10000000);
+    for ( char c = 'X'; c <= 'Z'; c++ ) {
+        if ( ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
+            this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']);
+            this->steppers[c - 'X']->move(this->home_direction[c - 'X'], 10000000);
         }
     }
 
     // Wait for all axes to have homed
     this->wait_for_homed(axes_to_move);
 
-    if(this->is_delta) {
+    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 ){
+        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;
+                if (this->trim[c - 'X'] < 0) inverted_dir = !inverted_dir;
                 this->steppers[c - 'X']->set_speed(this->slow_rates[c - 'X']);
-                this->steppers[c - 'X']->move(inverted_dir,this->trim[c - 'X']);
+                this->steppers[c - 'X']->move(inverted_dir, this->trim[c - 'X']);
             }
         }
 
         // Wait for moves to be done
-        for( char c = 'X'; c <= 'Z'; c++ ){
-            if(  ( axes_to_move >> ( c - 'X' ) ) & 1 ){
+        for ( char c = 'X'; c <= 'Z'; c++ ) {
+            if (  ( axes_to_move >> ( c - 'X' ) ) & 1 ) {
                 //this->kernel->streams->printf("axis %c \r\n", c );
-                while( this->steppers[c - 'X']->moving ){
+                while ( this->steppers[c - 'X']->moving ) {
                     this->kernel->call_event(ON_IDLE);
                 }
             }
@@ -204,22 +283,23 @@ void Endstops::do_homing(char axes_to_move) {
 #define Y_AXIS 1
 #define Z_AXIS 2
 
-void Endstops::wait_for_homed_corexy(int axis){
+void Endstops::wait_for_homed_corexy(int axis)
+{
     bool running = true;
-    unsigned int debounce[3] = {0,0,0};
-    while(running){
+    unsigned int debounce[3] = {0, 0, 0};
+    while (running) {
         running = false;
         this->kernel->call_event(ON_IDLE);
-        if( this->pins[axis + (this->home_direction[axis]?0:3)].get() ){
-            if( debounce[axis] < debounce_count ) {
+        if ( this->pins[axis + (this->home_direction[axis] ? 0 : 3)].get() ) {
+            if ( debounce[axis] < debounce_count ) {
                 debounce[axis] ++;
                 running = true;
             } else {
                 // turn both off if running
-                if(this->steppers[X_AXIS]->moving) this->steppers[X_AXIS]->move(0,0);
-                if(this->steppers[Y_AXIS]->moving) this->steppers[Y_AXIS]->move(0,0);
+                if (this->steppers[X_AXIS]->moving) this->steppers[X_AXIS]->move(0, 0);
+                if (this->steppers[Y_AXIS]->moving) this->steppers[Y_AXIS]->move(0, 0);
             }
-        }else{
+        } else {
             // The endstop was not hit yet
             running = true;
             debounce[axis] = 0;
@@ -227,74 +307,57 @@ void Endstops::wait_for_homed_corexy(int axis){
     }
 }
 
-// this homing works for HBots/CoreXY
-void Endstops::do_homing_corexy(char axes_to_move) {
-    // Start moving the axes to the origin
-    if(axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
-        this->status = MOVING_TO_ORIGIN_FAST;
-        this->steppers[X_AXIS]->set_speed(this->fast_rates[X_AXIS]);
-        this->steppers[X_AXIS]->move(this->home_direction[X_AXIS], 10000000);
-        this->steppers[Y_AXIS]->set_speed(this->fast_rates[X_AXIS]);
-        this->steppers[Y_AXIS]->move(this->home_direction[X_AXIS], 10000000);
+void Endstops::corexy_home(int home_axis, bool dirx, bool diry, double fast_rate, double slow_rate, unsigned int retract_steps)
+{
+    this->status = MOVING_TO_ORIGIN_FAST;
+    this->steppers[X_AXIS]->set_speed(fast_rate);
+    this->steppers[X_AXIS]->move(dirx, 10000000);
+    this->steppers[Y_AXIS]->set_speed(fast_rate);
+    this->steppers[Y_AXIS]->move(diry, 10000000);
 
-        // wait for X
-        this->wait_for_homed_corexy(X_AXIS);
+    // wait for primary axis
+    this->wait_for_homed_corexy(home_axis);
 
-        // Move back a small distance
-        this->status = MOVING_BACK;
-        this->steppers[X_AXIS]->set_speed(this->slow_rates[X_AXIS]);
-        this->steppers[X_AXIS]->move(!this->home_direction[X_AXIS], this->retract_steps[X_AXIS]);
-        this->steppers[Y_AXIS]->set_speed(this->slow_rates[X_AXIS]);
-        this->steppers[Y_AXIS]->move(!this->home_direction[X_AXIS], this->retract_steps[X_AXIS]);
-
-        // wait until done
-        while( this->steppers[X_AXIS]->moving ){ this->kernel->call_event(ON_IDLE); }
-        while( this->steppers[Y_AXIS]->moving ){ this->kernel->call_event(ON_IDLE); }
-
-        // Start moving the axes to the origin slowly
-        this->status = MOVING_TO_ORIGIN_SLOW;
-        this->steppers[X_AXIS]->set_speed(this->slow_rates[X_AXIS]);
-        this->steppers[X_AXIS]->move(this->home_direction[X_AXIS], 10000000);
-        this->steppers[Y_AXIS]->set_speed(this->slow_rates[X_AXIS]);
-        this->steppers[Y_AXIS]->move(this->home_direction[X_AXIS], 10000000);
-
-        // wait for X
-        this->wait_for_homed_corexy(X_AXIS);
+    // Move back a small distance
+    this->status = MOVING_BACK;
+    this->steppers[X_AXIS]->set_speed(slow_rate);
+    this->steppers[X_AXIS]->move(!dirx, retract_steps);
+    this->steppers[Y_AXIS]->set_speed(slow_rate);
+    this->steppers[Y_AXIS]->move(!diry, retract_steps);
+
+    // wait until done
+    while ( this->steppers[X_AXIS]->moving ) {
+        this->kernel->call_event(ON_IDLE);
+    }
+    while ( this->steppers[Y_AXIS]->moving ) {
+        this->kernel->call_event(ON_IDLE);
     }
 
-    if(axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
-        this->status = MOVING_TO_ORIGIN_FAST;
-        this->steppers[X_AXIS]->set_speed(this->fast_rates[Y_AXIS]);
-        this->steppers[X_AXIS]->move(this->home_direction[Y_AXIS], 10000000);
-        this->steppers[Y_AXIS]->set_speed(this->fast_rates[Y_AXIS]); // yes I use X_axis speed as they need to go at the same speed
-        this->steppers[Y_AXIS]->move(!this->home_direction[Y_AXIS], 10000000);
+    // Start moving the axes to the origin slowly
+    this->status = MOVING_TO_ORIGIN_SLOW;
+    this->steppers[X_AXIS]->set_speed(slow_rate);
+    this->steppers[X_AXIS]->move(dirx, 10000000);
+    this->steppers[Y_AXIS]->set_speed(slow_rate);
+    this->steppers[Y_AXIS]->move(diry, 10000000);
 
-        // wait for Y
-        this->wait_for_homed_corexy(Y_AXIS);
+    // wait for primary axis
+    this->wait_for_homed_corexy(home_axis);
+}
 
-        // Move back a small distance
-        this->status = MOVING_BACK;
-        this->steppers[X_AXIS]->set_speed(this->slow_rates[Y_AXIS]);
-        this->steppers[X_AXIS]->move(!this->home_direction[Y_AXIS], this->retract_steps[Y_AXIS]);
-        this->steppers[Y_AXIS]->set_speed(this->slow_rates[Y_AXIS]);
-        this->steppers[Y_AXIS]->move(this->home_direction[Y_AXIS], this->retract_steps[Y_AXIS]);
-
-        // wait until done
-        while( this->steppers[X_AXIS]->moving ){ this->kernel->call_event(ON_IDLE); }
-        while( this->steppers[Y_AXIS]->moving ){ this->kernel->call_event(ON_IDLE); }
-
-        // Start moving the axes to the origin slowly
-        this->status = MOVING_TO_ORIGIN_SLOW;
-        this->steppers[X_AXIS]->set_speed(this->slow_rates[Y_AXIS]);
-        this->steppers[X_AXIS]->move(this->home_direction[Y_AXIS], 10000000);
-        this->steppers[Y_AXIS]->set_speed(this->slow_rates[Y_AXIS]);
-        this->steppers[Y_AXIS]->move(!this->home_direction[Y_AXIS], 10000000);
-
-        // wait for Y
-        this->wait_for_homed_corexy(Y_AXIS);
+// this homing works for HBots/CoreXY
+void Endstops::do_homing_corexy(char axes_to_move)
+{
+    // Home Y first so the X limit swicth canbe in a fixed pplace on the frame not on the X Gantry
+    // TODO should really make order configurable
+    if (axes_to_move & 0x02) { // Home Y, which means both X and Y in different directions
+        corexy_home(Y_AXIS, true, false, this->fast_rates[Y_AXIS], this->slow_rates[Y_AXIS], this->retract_steps[Y_AXIS]);
+    }
+
+    if (axes_to_move & 0x01) { // Home X, which means both X and Y in same direction
+        corexy_home(X_AXIS, true, true, this->fast_rates[X_AXIS], this->slow_rates[X_AXIS], this->retract_steps[X_AXIS]);
     }
 
-    if(axes_to_move & 0x04) { // move Z
+    if (axes_to_move & 0x04) { // move Z
         do_homing(0x04); // just home normally for Z
     }
 
@@ -303,13 +366,11 @@ void Endstops::do_homing_corexy(char axes_to_move) {
 }
 
 // Start homing sequences by response to GCode commands
-void Endstops::on_gcode_received(voidargument)
+void Endstops::on_gcode_received(void *argument)
 {
-    Gcode* gcode = static_cast<Gcode*>(argument);
-    if( gcode->has_g)
-    {
-        if( gcode->g == 28 )
-        {
+    Gcode *gcode = static_cast<Gcode *>(argument);
+    if ( gcode->has_g) {
+        if ( gcode->g == 28 ) {
             gcode->mark_as_taken();
             // G28 is received, we have homing to do
 
@@ -319,85 +380,111 @@ void Endstops::on_gcode_received(void* argument)
             // Do we move select axes or all of them
             char axes_to_move = 0;
             // 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') );
+            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 ( 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' ) );
+                }
             }
 
             // Enable the motors
             this->kernel->stepper->turn_enable_pins_on();
 
             // do the actual homing
-            if(is_corexy)
+            if (is_corexy)
                 do_homing_corexy(axes_to_move);
             else
                 do_homing(axes_to_move);
 
-            // Zero the ax(i/e)s position
-            for( char c = 'X'; c <= 'Z'; c++ ){
-                if( ( axes_to_move >> ( c - 'X' ) ) & 1 ){
-
-                    this->kernel->robot->reset_axis_position(this->homing_position[c - 'X'], c - 'X');
+            // Zero the ax(i/e)s position, add in the home offset
+            for ( int c = 0; c <= 2; c++ ) {
+                if ( (axes_to_move >> c)  & 1 ) {
+                    this->kernel->robot->reset_axis_position(this->homing_position[c] + this->home_offset[c], c);
                 }
             }
-
         }
-    }
-    else if (gcode->has_m){
-        switch(gcode->m){
-            case 119:
-                {
-
-                    int px= this->home_direction[0] ? 0 : 3;
-                    int py= this->home_direction[1] ? 1 : 4;
-                    int pz= this->home_direction[2] ? 2 : 5;
-                    const char* mx= this->home_direction[0] ? "min" : "max";
-                    const char* my= this->home_direction[1] ? "min" : "max";
-                    const char* mz= this->home_direction[2] ? "min" : "max";
-
-                    gcode->stream->printf("X %s:%d Y %s:%d Z %s:%d\n", mx, this->pins[px].get(), my, this->pins[py].get(), mz, this->pins[pz].get());
-                    gcode->mark_as_taken();
+    } else if (gcode->has_m) {
+        switch (gcode->m) {
+            case 119: {
+
+                int px = this->home_direction[0] ? 0 : 3;
+                int py = this->home_direction[1] ? 1 : 4;
+                int pz = this->home_direction[2] ? 2 : 5;
+                const char *mx = this->home_direction[0] ? "min" : "max";
+                const char *my = this->home_direction[1] ? "min" : "max";
+                const char *mz = this->home_direction[2] ? "min" : "max";
+
+                gcode->stream->printf("X %s:%d Y %s:%d Z %s:%d\n", mx, this->pins[px].get(), my, this->pins[py].get(), mz, this->pins[pz].get());
+                gcode->mark_as_taken();
+            }
+            break;
+
+            case 206: // M206 - set homing offset
+                if (gcode->has_letter('X')) home_offset[0] = gcode->get_value('X');
+                if (gcode->has_letter('Y')) home_offset[1] = gcode->get_value('Y');
+                if (gcode->has_letter('Z')) home_offset[2] = gcode->get_value('Z');
+                gcode->stream->printf("X %5.3f Y %5.3f Z %5.3f\n", home_offset[0], home_offset[1], home_offset[2]);
+                gcode->mark_as_taken();
+                break;
+
+            case 500: // save settings
+            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) {
+                    double 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->mark_as_taken();
                 break;
 
             case 665: { // M665 - set max gamma/z height
-                    gcode->mark_as_taken();
-                    double gamma_max= this->homing_position[2];
-                    if(gcode->has_letter('Z')) {
-                        this->homing_position[2]= gamma_max= gcode->get_value('Z');
-                    }
-                    gcode->stream->printf("Max Z %8.3f ", gamma_max);
-                    gcode->add_nl = true;
+                gcode->mark_as_taken();
+                double gamma_max = this->homing_position[2];
+                if (gcode->has_letter('Z')) {
+                    this->homing_position[2] = gamma_max = gcode->get_value('Z');
                 }
-                break;
+                gcode->stream->printf("Max Z %8.3f ", gamma_max);
+                gcode->add_nl = true;
+            }
+            break;
 
-            case 206: // M206 - set trim for each axis in mm (TODO to be deprecated)
-            case 666: // M666 - set trim for each axis in 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);
-                    double mm[3];
-                    mm[0]= trim[0]/steps_per_mm[0] * dirx; // convert to mm
-                    mm[1]= trim[1]/steps_per_mm[1] * diry;
-                    mm[2]= trim[2]/steps_per_mm[2] * dirz;
 
-                    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');
+            case 666: { // M666 - set trim for each axis in mm
+                double mm[3];
+                trim2mm(mm);
 
-                    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;
+                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');
 
-                    // 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();
-                }
-                break;
+                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();
+            }
+            break;
 
         }
     }
 }
 
+void Endstops::trim2mm(double *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;
+}
+
dissimilarity index 67%
index 31011eb..f556920 100644 (file)
-/*
-      This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
-      Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
-      Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
-*/
-
-#ifndef ENDSTOPS_MODULE_H
-#define ENDSTOPS_MODULE_H
-
-#include "libs/Module.h"
-#include "libs/Kernel.h"
-#include "modules/communication/utils/Gcode.h"
-#include "libs/StepperMotor.h"
-#include "libs/Pin.h"
-
-#define ALPHA_AXIS 0
-#define BETA_AXIS  1
-#define GAMMA_AXIS 2
-
-#define NOT_HOMING 0
-#define MOVING_TO_ORIGIN_FAST 1
-#define MOVING_BACK 2
-#define MOVING_TO_ORIGIN_SLOW 3
-
-#define endstops_module_enable_checksum         CHECKSUM("endstops_enable")
-#define corexy_homing_checksum                  CHECKSUM("corexy_homing")
-#define delta_homing_checksum                   CHECKSUM("delta_homing")
-
-#define alpha_min_endstop_checksum       CHECKSUM("alpha_min_endstop")
-#define beta_min_endstop_checksum        CHECKSUM("beta_min_endstop")
-#define gamma_min_endstop_checksum       CHECKSUM("gamma_min_endstop")
-
-#define alpha_max_endstop_checksum       CHECKSUM("alpha_max_endstop")
-#define beta_max_endstop_checksum        CHECKSUM("beta_max_endstop")
-#define gamma_max_endstop_checksum       CHECKSUM("gamma_max_endstop")
-
-#define alpha_trim_checksum              CHECKSUM("alpha_trim")
-#define beta_trim_checksum               CHECKSUM("beta_trim")
-#define gamma_trim_checksum              CHECKSUM("gamma_trim")
-
-// these values are in steps and should be deprecated
-#define alpha_fast_homing_rate_checksum  CHECKSUM("alpha_fast_homing_rate")
-#define beta_fast_homing_rate_checksum   CHECKSUM("beta_fast_homing_rate")
-#define gamma_fast_homing_rate_checksum  CHECKSUM("gamma_fast_homing_rate")
-
-#define alpha_slow_homing_rate_checksum  CHECKSUM("alpha_slow_homing_rate")
-#define beta_slow_homing_rate_checksum   CHECKSUM("beta_slow_homing_rate")
-#define gamma_slow_homing_rate_checksum  CHECKSUM("gamma_slow_homing_rate")
-
-#define alpha_homing_retract_checksum    CHECKSUM("alpha_homing_retract")
-#define beta_homing_retract_checksum     CHECKSUM("beta_homing_retract")
-#define gamma_homing_retract_checksum    CHECKSUM("gamma_homing_retract")
-#define endstop_debounce_count_checksum  CHECKSUM("endstop_debounce_count")
-
-// same as above but in user friendly mm/s and mm
-#define alpha_fast_homing_rate_mm_checksum  CHECKSUM("alpha_fast_homing_rate_mm_s")
-#define beta_fast_homing_rate_mm_checksum   CHECKSUM("beta_fast_homing_rate_mm_s")
-#define gamma_fast_homing_rate_mm_checksum  CHECKSUM("gamma_fast_homing_rate_mm_s")
-
-#define alpha_slow_homing_rate_mm_checksum  CHECKSUM("alpha_slow_homing_rate_mm_s")
-#define beta_slow_homing_rate_mm_checksum   CHECKSUM("beta_slow_homing_rate_mm_s")
-#define gamma_slow_homing_rate_mm_checksum  CHECKSUM("gamma_slow_homing_rate_mm_s")
-
-#define alpha_homing_retract_mm_checksum    CHECKSUM("alpha_homing_retract_mm")
-#define beta_homing_retract_mm_checksum     CHECKSUM("beta_homing_retract_mm")
-#define gamma_homing_retract_mm_checksum    CHECKSUM("gamma_homing_retract_mm")
-
-#define endstop_debounce_count_checksum  CHECKSUM("endstop_debounce_count")
-
-#define alpha_homing_direction_checksum  CHECKSUM("alpha_homing_direction")
-#define beta_homing_direction_checksum   CHECKSUM("beta_homing_direction")
-#define gamma_homing_direction_checksum  CHECKSUM("gamma_homing_direction")
-#define home_to_max_checksum             CHECKSUM("home_to_max")
-#define home_to_min_checksum             CHECKSUM("home_to_min")
-#define alpha_min_checksum               CHECKSUM("alpha_min")
-#define beta_min_checksum                CHECKSUM("beta_min")
-#define gamma_min_checksum               CHECKSUM("gamma_min")
-
-#define alpha_max_checksum               CHECKSUM("alpha_max")
-#define beta_max_checksum                CHECKSUM("beta_max")
-#define gamma_max_checksum               CHECKSUM("gamma_max")
-
-#define alpha_steps_per_mm_checksum      CHECKSUM("alpha_steps_per_mm")
-#define beta_steps_per_mm_checksum       CHECKSUM("beta_steps_per_mm")
-#define gamma_steps_per_mm_checksum      CHECKSUM("gamma_steps_per_mm")
-
-class Endstops : public Module{
-    public:
-        Endstops();
-        void on_module_loaded();
-        void on_gcode_received(void* argument);
-        void on_config_reload(void* argument);
-
-    private:
-        void do_homing(char axes_to_move);
-        void do_homing_corexy(char axes_to_move);
-        void wait_for_homed(char axes_to_move);
-        void wait_for_homed_corexy(int axis);
-        double steps_per_mm[3];
-        double homing_position[3];
-        bool home_direction[3];
-        unsigned int  debounce_count;
-        unsigned int  retract_steps[3];
-        int  trim[3];
-        double  fast_rates[3];
-        double  slow_rates[3];
-        Pin           pins[6];
-        StepperMotor* steppers[3];
-        char status;
-        bool is_corexy;
-        bool is_delta;
-};
-
-#endif
+/*
+      This file is part of Smoothie (http://smoothieware.org/). The motion control part is heavily based on Grbl (https://github.com/simen/grbl).
+      Smoothie is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+      Smoothie is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+      You should have received a copy of the GNU General Public License along with Smoothie. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef ENDSTOPS_MODULE_H
+#define ENDSTOPS_MODULE_H
+
+#include "libs/Module.h"
+#include "libs/Kernel.h"
+#include "modules/communication/utils/Gcode.h"
+#include "libs/StepperMotor.h"
+#include "libs/Pin.h"
+
+
+class Endstops : public Module{
+    public:
+        Endstops();
+        void on_module_loaded();
+        void on_gcode_received(void* argument);
+        void on_config_reload(void* argument);
+
+    private:
+        void do_homing(char axes_to_move);
+        void do_homing_corexy(char axes_to_move);
+        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, double fast_rate, double slow_rate, unsigned int retract_steps);
+        void trim2mm(double * mm);
+
+        double steps_per_mm[3];
+        double homing_position[3];
+        float home_offset[3];
+        bool home_direction[3];
+        unsigned int  debounce_count;
+        unsigned int  retract_steps[3];
+        int  trim[3];
+        double  fast_rates[3];
+        double  slow_rates[3];
+        Pin           pins[6];
+        StepperMotor* steppers[3];
+        char status;
+        bool is_corexy;
+        bool is_delta;
+};
+
+#endif
index 9879e59..3e864ef 100644 (file)
@@ -136,14 +136,18 @@ void Extruder::on_gcode_received(void *argument){
             gcode->stream->printf("E:%4.1f ", this->current_position);
             gcode->add_nl = true;
             gcode->mark_as_taken();
-        }
-        if (gcode->m == 92 ){
+
+        }else if (gcode->m == 92 ){
             double spm = this->steps_per_millimeter;
             if (gcode->has_letter('E'))
                 spm = gcode->get_value('E');
             gcode->stream->printf("E:%g ", spm);
             gcode->add_nl = true;
             gcode->mark_as_taken();
+
+        }else if (gcode->m == 500 || gcode->m == 503){// M500 saves some volatile settings to config override file, M503 just prints the settings
+            gcode->stream->printf(";E Steps per mm:\nM92 E%1.4f\n", this->steps_per_millimeter);
+            gcode->mark_as_taken();
         }
     }
 
index 3e2370e..3bdf6a9 100644 (file)
@@ -165,6 +165,10 @@ void TemperatureControl::on_gcode_received(void* argument){
                 this->pool->PIDtuner->begin(this, target, gcode->stream, ncycles);
             }
 
+        } else if (gcode->m == 500 || gcode->m == 503){// M500 saves some volatile settings to config override file, M503 just prints the settings
+            gcode->stream->printf(";PID settings:\nM301 P%1.4f I%1.4f D%1.4f\n", this->p_factor, this->i_factor/this->PIDdt, this->d_factor*this->PIDdt);
+            gcode->mark_as_taken();
+
         } else if( ( gcode->m == this->set_m_code || gcode->m == this->set_and_wait_m_code ) && gcode->has_letter('S') ) {
             gcode->mark_as_taken();
 
index 86190ad..bccee00 100644 (file)
@@ -27,7 +27,7 @@ void Player::on_module_loaded(){
     this->register_for_event(ON_SET_PUBLIC_DATA);
     this->register_for_event(ON_GCODE_RECEIVED);
 
-    this->on_boot_gcode = this->kernel->config->value(on_boot_gcode_checksum)->by_default("/sd/on_boot.gcode -q")->as_string();
+    this->on_boot_gcode = this->kernel->config->value(on_boot_gcode_checksum)->by_default("/sd/on_boot.gcode")->as_string();
     this->on_boot_gcode_enable = this->kernel->config->value(on_boot_gcode_enable_checksum)->by_default(true)->as_bool();
     this->elapsed_secs= 0;
     this->reply_stream= NULL;
@@ -170,7 +170,7 @@ void Player::play_command( string parameters, StreamOutput* stream ){
 
     // Get filename
     this->filename          = this->absolute_from_relative(shift_parameter( parameters ));
-    string options           = shift_parameter( parameters );
+    string options          = shift_parameter( parameters );
 
     this->current_file_handler = fopen( this->filename.c_str(), "r");
     if(this->current_file_handler == NULL){
@@ -274,12 +274,12 @@ void Player::cd_command( string parameters, StreamOutput* stream ){
 
 void Player::on_main_loop(void* argument){
     if( !this->booted ) {
+        this->booted = true;
         if( this->on_boot_gcode_enable ){
             this->play_command(this->on_boot_gcode, this->kernel->serial);
         }else{
             //this->kernel->serial->printf("On boot gcode disabled! skipping...\n");
         }
-        this->booted = true;
     }
 
     if( this->playing_file ){