Merge remote-tracking branch 'upstream/edge' into feature/e-endstop
[clinton/Smoothieware.git] / src / libs / Kernel.cpp
index 6bd2f7a..0538cb4 100644 (file)
 #include "modules/communication/GcodeDispatch.h"
 #include "modules/robot/Planner.h"
 #include "modules/robot/Robot.h"
-#include "modules/robot/Stepper.h"
 #include "modules/robot/Conveyor.h"
 #include "StepperMotor.h"
 #include "BaseSolution.h"
 #include "EndstopsPublicAccess.h"
+#include "Configurator.h"
+#include "SimpleShell.h"
+
+#include "platform_memory.h"
 
 #include <malloc.h>
 #include <array>
@@ -37,9 +40,9 @@
 
 #define base_stepping_frequency_checksum            CHECKSUM("base_stepping_frequency")
 #define microseconds_per_step_pulse_checksum        CHECKSUM("microseconds_per_step_pulse")
-#define acceleration_ticks_per_second_checksum      CHECKSUM("acceleration_ticks_per_second")
 #define disable_leds_checksum                       CHECKSUM("leds_disable")
 #define grbl_mode_checksum                          CHECKSUM("grbl_mode")
+#define ok_per_line_checksum                        CHECKSUM("ok_per_line")
 
 Kernel* Kernel::instance;
 
@@ -75,29 +78,36 @@ Kernel::Kernel(){
 #if MRI_ENABLE != 0
     switch( __mriPlatform_CommUartIndex() ) {
         case 0:
-            this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
+            this->serial = new(AHB0) SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
             break;
         case 1:
-            this->serial = new SerialConsole(  p13,   p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
+            this->serial = new(AHB0) SerialConsole(  p13,   p14, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
             break;
         case 2:
-            this->serial = new SerialConsole(  p28,   p27, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
+            this->serial = new(AHB0) SerialConsole(  p28,   p27, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
             break;
         case 3:
-            this->serial = new SerialConsole(   p9,   p10, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
+            this->serial = new(AHB0) SerialConsole(   p9,   p10, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
             break;
     }
 #endif
     // default
     if(this->serial == NULL) {
-        this->serial = new SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
+        this->serial = new(AHB0) SerialConsole(USBTX, USBRX, this->config->value(uart0_checksum,baud_rate_setting_checksum)->by_default(DEFAULT_SERIAL_BAUD_RATE)->as_number());
     }
 
     //some boards don't have leds.. TOO BAD!
     this->use_leds= !this->config->value( disable_leds_checksum )->by_default(false)->as_bool();
+
+    #ifdef CNC
+    this->grbl_mode= this->config->value( grbl_mode_checksum )->by_default(true)->as_bool();
+    #else
     this->grbl_mode= this->config->value( grbl_mode_checksum )->by_default(false)->as_bool();
+    #endif
+
+    // we exepct ok per line now not per G code, setting this to false will return to the old (incorrect) way of ok per G code
+    this->ok_per_line= this->config->value( ok_per_line_checksum )->by_default(true)->as_bool();
 
-    this->add_module( this->config );
     this->add_module( this->serial );
 
     // HAL stuff
@@ -113,7 +123,6 @@ Kernel::Kernel(){
     NVIC_SetPriority(TIMER1_IRQn, 1);
     NVIC_SetPriority(TIMER2_IRQn, 4);
     NVIC_SetPriority(PendSV_IRQn, 3);
-    NVIC_SetPriority(RIT_IRQn, 3); // we make acceleration tick the same prio as pendsv so it can't be pre-empted by end of block
 
     // Set other priorities lower than the timers
     NVIC_SetPriority(ADC_IRQn, 5);
@@ -134,22 +143,20 @@ Kernel::Kernel(){
 
     // Configure the step ticker
     this->base_stepping_frequency = this->config->value(base_stepping_frequency_checksum)->by_default(100000)->as_number();
-    float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum)->by_default(5)->as_number();
-    this->acceleration_ticks_per_second = THEKERNEL->config->value(acceleration_ticks_per_second_checksum)->by_default(1000)->as_number();
+    float microseconds_per_step_pulse = this->config->value(microseconds_per_step_pulse_checksum)->by_default(1)->as_number();
 
-    // Configure the step ticker ( TODO : shouldnt this go into stepticker's code ? )
-    this->step_ticker->set_reset_delay( microseconds_per_step_pulse );
+    // Configure the step ticker
     this->step_ticker->set_frequency( this->base_stepping_frequency );
-    this->step_ticker->set_acceleration_ticks_per_second(acceleration_ticks_per_second); // must be set after set_frequency
+    this->step_ticker->set_unstep_time( microseconds_per_step_pulse );
 
     // Core modules
     this->add_module( this->gcode_dispatch = new GcodeDispatch() );
     this->add_module( this->robot          = new Robot()         );
-    this->add_module( this->stepper        = new Stepper()       );
     this->add_module( this->conveyor       = new Conveyor()      );
+    this->add_module( this->simpleshell    = new SimpleShell()   );
 
     this->planner = new Planner();
-
+    this->configurator = new Configurator();
 }
 
 // return a GRBL-like query string for serial ?
@@ -159,6 +166,7 @@ std::string Kernel::get_query_string()
     bool homing;
     bool ok = PublicData::get_value(endstops_checksum, get_homing_status_checksum, 0, &homing);
     if(!ok) homing= false;
+    bool running= false;
 
     str.append("<");
     if(halted) {
@@ -167,33 +175,45 @@ std::string Kernel::get_query_string()
         str.append("Home,");
     }else if(feed_hold) {
         str.append("Hold,");
-    }else if(this->conveyor->is_queue_empty()) {
+    }else if(this->conveyor->is_idle()) {
         str.append("Idle,");
     }else{
+        running= true;
         str.append("Run,");
     }
 
-    // get real time current actuator position in mm
-    ActuatorCoordinates current_position{
-        robot->actuators[X_AXIS]->get_current_position(),
-        robot->actuators[Y_AXIS]->get_current_position(),
-        robot->actuators[Z_AXIS]->get_current_position()
-    };
-
-    // get machine position from the actuator position using FK
-    float mpos[3];
-    robot->arm_solution->actuator_to_cartesian(current_position, mpos);
-
-    char buf[64];
-    // machine position
-    size_t n= snprintf(buf, sizeof(buf), "%f,%f,%f,", mpos[0], mpos[1], mpos[2]);
-    str.append("MPos:").append(buf, n);
-
-    // work space position
-    Robot::wcs_t pos= robot->mcs2wcs(mpos);
-    n= snprintf(buf, sizeof(buf), "%f,%f,%f", robot->from_millimeters(std::get<X_AXIS>(pos)), robot->from_millimeters(std::get<Y_AXIS>(pos)), robot->from_millimeters(std::get<Z_AXIS>(pos)));
-    str.append("WPos:").append(buf, n);
-    str.append(">\r\n");
+    if(running) {
+        float mpos[3];
+        robot->get_current_machine_position(mpos);
+        // current_position/mpos includes the compensation transform so we need to get the inverse to get actual position
+        if(robot->compensationTransform) robot->compensationTransform(mpos, true); // get inverse compensation transform
+
+        char buf[128];
+        // machine position
+        size_t n= snprintf(buf, sizeof(buf), "%1.4f,%1.4f,%1.4f,", robot->from_millimeters(mpos[0]), robot->from_millimeters(mpos[1]), robot->from_millimeters(mpos[2]));
+        str.append("MPos:").append(buf, n);
+
+        // work space position
+        Robot::wcs_t pos= robot->mcs2wcs(mpos);
+        n= snprintf(buf, sizeof(buf), "%1.4f,%1.4f,%1.4f", robot->from_millimeters(std::get<X_AXIS>(pos)), robot->from_millimeters(std::get<Y_AXIS>(pos)), robot->from_millimeters(std::get<Z_AXIS>(pos)));
+        str.append("WPos:").append(buf, n);
+        str.append(">\r\n");
+
+    }else{
+        // return the last milestone if idle
+        char buf[128];
+        // machine position
+        Robot::wcs_t mpos= robot->get_axis_position();
+        size_t n= snprintf(buf, sizeof(buf), "%1.4f,%1.4f,%1.4f,", robot->from_millimeters(std::get<X_AXIS>(mpos)), robot->from_millimeters(std::get<Y_AXIS>(mpos)), robot->from_millimeters(std::get<Z_AXIS>(mpos)));
+        str.append("MPos:").append(buf, n);
+
+        // work space position
+        Robot::wcs_t pos= robot->mcs2wcs(mpos);
+        n= snprintf(buf, sizeof(buf), "%1.4f,%1.4f,%1.4f", robot->from_millimeters(std::get<X_AXIS>(pos)), robot->from_millimeters(std::get<Y_AXIS>(pos)), robot->from_millimeters(std::get<Z_AXIS>(pos)));
+        str.append("WPos:").append(buf, n);
+        str.append(">\r\n");
+
+    }
     return str;
 }
 
@@ -209,12 +229,21 @@ void Kernel::register_for_event(_EVENT_ENUM id_event, Module *mod){
 
 // Call a specific event with an argument
 void Kernel::call_event(_EVENT_ENUM id_event, void * argument){
+    bool was_idle= true;
     if(id_event == ON_HALT) {
         this->halted= (argument == nullptr);
+        was_idle= conveyor->is_idle(); // see if we were doing anything like printing
     }
+
+    // send to all registered modules
     for (auto m : hooks[id_event]) {
         (m->*kernel_callback_functions[id_event])(argument);
     }
+
+    if(id_event == ON_HALT && this->halted && !was_idle) {
+        // we need to try to correct current positions if we were running
+        this->robot->reset_position_from_current_actuator_position();
+    }
 }
 
 // These are used by tests to test for various things. basically mocks