diff --git a/FluidNC/src/Channel.cpp b/FluidNC/src/Channel.cpp index 50b8bda4c..1d626beda 100644 --- a/FluidNC/src/Channel.cpp +++ b/FluidNC/src/Channel.cpp @@ -93,11 +93,11 @@ void Channel::autoReportGCodeState() { // Force the compare to succeed if the only change is the motion mode _lastModal.motion = gc_state.modal.motion; } - if (memcmp(&_lastModal, &gc_state.modal, sizeof(_lastModal)) || _lastTool != gc_state.tool || + if (memcmp(&_lastModal, &gc_state.modal, sizeof(_lastModal)) || _lastTool != gc_state.selected_tool || (!motionState() && (_lastSpindleSpeed != gc_state.spindle_speed || _lastFeedRate != gc_state.feed_rate))) { report_gcode_modes(*this); memcpy(&_lastModal, &gc_state.modal, sizeof(_lastModal)); - _lastTool = gc_state.tool; + _lastTool = gc_state.selected_tool; _lastSpindleSpeed = gc_state.spindle_speed; _lastFeedRate = gc_state.feed_rate; } diff --git a/FluidNC/src/GCode.cpp b/FluidNC/src/GCode.cpp index 196c312c2..17e6e3bc0 100644 --- a/FluidNC/src/GCode.cpp +++ b/FluidNC/src/GCode.cpp @@ -69,6 +69,7 @@ void gc_init() { // Load default G54 coordinate system. gc_state.modal = modal_defaults; gc_state.modal.override = config->_start->_deactivateParking ? Override::Disabled : Override::ParkingMotion; + gc_state.current_tool = -1; coords[gc_state.modal.coord_select]->get(gc_state.coord_system); flowcontrol_init(); } @@ -1604,14 +1605,14 @@ Error gc_execute_line(char* line) { pl_data->spindle_speed = gc_state.spindle_speed; // Record data for planner use. } // else { pl_data->spindle_speed = 0.0; } // Initialized as zero already. // [5. Select tool ]: NOT SUPPORTED. Only tracks tool value. - // gc_state.tool = gc_block.values.t; // [M6. Change tool ]: if (gc_block.modal.tool_change == ToolChange::Enable) { - if (gc_state.selected_tool != gc_state.tool) { + if (gc_state.selected_tool != gc_state.current_tool) { bool stopped_spindle = false; // was spindle stopped via the change bool new_spindle = false; // was the spindle changed protocol_buffer_synchronize(); // wait for motion in buffer to finish - Spindles::Spindle::switchSpindle(gc_state.selected_tool, Spindles::SpindleFactory::objects(), spindle, stopped_spindle, new_spindle); + Spindles::Spindle::switchSpindle( + gc_state.selected_tool, Spindles::SpindleFactory::objects(), spindle, stopped_spindle, new_spindle); if (stopped_spindle) { gc_block.modal.spindle = SpindleState::Disable; } @@ -1619,17 +1620,16 @@ Error gc_execute_line(char* line) { gc_state.spindle_speed = 0.0; } spindle->tool_change(gc_state.selected_tool, false, false); - gc_state.tool = gc_state.selected_tool; - report_ovr_counter = 0; // Set to report change immediately + gc_state.current_tool = gc_state.selected_tool; + report_ovr_counter = 0; // Set to report change immediately gc_ovr_changed(); } } if (gc_block.modal.set_tool_number == SetToolNumber::Enable) { gc_state.selected_tool = gc_block.values.q; - gc_state.tool = gc_state.selected_tool; - bool stopped_spindle = false; // was spindle stopped via the change - bool new_spindle = false; // was the spindle changed - protocol_buffer_synchronize(); // wait for motion in buffer to finish + bool stopped_spindle = false; // was spindle stopped via the change + bool new_spindle = false; // was the spindle changed + protocol_buffer_synchronize(); // wait for motion in buffer to finish Spindles::Spindle::switchSpindle(gc_state.selected_tool, Spindles::SpindleFactory::objects(), spindle, stopped_spindle, new_spindle); if (stopped_spindle) { gc_block.modal.spindle = SpindleState::Disable; @@ -1638,7 +1638,8 @@ Error gc_execute_line(char* line) { gc_state.spindle_speed = 0.0; } spindle->tool_change(gc_state.selected_tool, false, true); - report_ovr_counter = 0; // Set to report change immediately + gc_state.current_tool = gc_block.values.q; + report_ovr_counter = 0; // Set to report change immediately gc_ovr_changed(); } // [7. Spindle control ]: diff --git a/FluidNC/src/GCode.h b/FluidNC/src/GCode.h index 6990887f7..6b96d666e 100644 --- a/FluidNC/src/GCode.h +++ b/FluidNC/src/GCode.h @@ -300,8 +300,8 @@ struct parser_state_t { float spindle_speed; // RPM float feed_rate; // Millimeters/min - uint32_t tool; // Tracks tool number uint32_t selected_tool; // tool from T value + int32_t current_tool; // the tool in use. default is -1 int32_t line_number; // Last line number sent float position[MAX_N_AXIS]; // Where the interpreter considers the tool to be at this point in the code diff --git a/FluidNC/src/Parameters.cpp b/FluidNC/src/Parameters.cpp index 3a7e7981a..4b7e74610 100644 --- a/FluidNC/src/Parameters.cpp +++ b/FluidNC/src/Parameters.cpp @@ -162,7 +162,7 @@ bool get_numbered_param(ngc_param_id_t id, float& result) { return true; } if (id == 5400) { - result = static_cast(gc_state.tool); + result = static_cast(gc_state.selected_tool); return true; } @@ -291,10 +291,16 @@ bool get_system_param(const std::string& name, float& result) { result = gc_state.spindle_speed; return true; } - if (sysn == "_current_tool" || sysn == "_selected_tool") { - result = gc_state.tool; + if (sysn == "_selected_tool") { + result = gc_state.selected_tool; return true; } + + if (sysn == "_current_tool") { + result = gc_state.current_tool; + return true; + } + if (sysn == "_vmajor") { std::string version(grbl_version); auto major = version.substr(0, version.find('.')); diff --git a/FluidNC/src/Report.cpp b/FluidNC/src/Report.cpp index c5644b394..4ddb94fba 100644 --- a/FluidNC/src/Report.cpp +++ b/FluidNC/src/Report.cpp @@ -367,7 +367,7 @@ void report_gcode_modes(Channel& channel) { msg << " M56"; } - msg << " T" << gc_state.tool; + msg << " T" << gc_state.selected_tool; int digits = config->_reportInches ? 1 : 0; msg << " F" << std::fixed << std::setprecision(digits) << gc_state.feed_rate; msg << " S" << uint32_t(gc_state.spindle_speed); diff --git a/FluidNC/src/ToolChangers/atc_manual.cpp b/FluidNC/src/ToolChangers/atc_manual.cpp index fc3a3e9db..21d0938c8 100644 --- a/FluidNC/src/ToolChangers/atc_manual.cpp +++ b/FluidNC/src/ToolChangers/atc_manual.cpp @@ -161,7 +161,7 @@ namespace ATCs { void Manual_ATC::reset() { _is_OK = true; _have_tool_setter_offset = false; - _prev_tool = gc_state.tool; // Double check this + _prev_tool = gc_state.selected_tool; // Double check this _macro.addf("G43.1Z0"); // reset the TLO to 0 _macro.addf("(MSG: TLO Z reset to 0)"); // } diff --git a/fixture_tests/fixtures/current_tool_test.nc b/fixture_tests/fixtures/current_tool_test.nc new file mode 100644 index 000000000..11747d57a --- /dev/null +++ b/fixture_tests/fixtures/current_tool_test.nc @@ -0,0 +1,55 @@ +-> $X +<~ [MSG:INFO: Caution: Unlocked] +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T0 F0 S0] +<- ok +-> (print, Cur tool=%d#<_current_tool>, Sel tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Cur tool=-1 Sel tool=0] +<- ok +-> T1 +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T1 F0 S0] +<- ok +-> (print, Cur tool=%d#<_current_tool>, Sel tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Cur tool=-1 Sel tool=1] +<- ok +-> M6 +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T1 F0 S0] +<- ok +-> (print, Cur tool=%d#<_current_tool>, Sel tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Cur tool=1 Sel tool=1] +<- ok +-> M6T2 +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T2 F0 S0] +<- ok +-> (print, Cur tool=%d#<_current_tool>, Sel tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Cur tool=2 Sel tool=2] +<- ok +-> (print, Cur tool=%d#<_current_tool>) +<- [MSG:INFO: PRINT, Cur tool=2] +<- ok +-> (print, Sel tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Sel tool=2] +<- ok +-> M61Q3 +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T3 F0 S0] +<- ok +-> (print, Cur tool=%d#<_current_tool>, Sel tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Cur tool=3 Sel tool=3] +<- ok +-> M6T0 +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T0 F0 S0] +<- ok +-> (print, Cur tool=%d#<_current_tool>, Sel tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Cur tool=0 Sel tool=0] +<- ok \ No newline at end of file diff --git a/fixture_tests/fixtures/tool_param_test.nc b/fixture_tests/fixtures/tool_param_test.nc new file mode 100644 index 000000000..4ee78bfce --- /dev/null +++ b/fixture_tests/fixtures/tool_param_test.nc @@ -0,0 +1,28 @@ +$G +(print, $G) +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>) +T1 +(print, T1) +$G +(print, $G) +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>) +M6 +(print, M6) +$G +(print, $G) +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>) +M6T2 +(print, M6T2) +$G +(print, $G) +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>) +M61Q3 +(print, M61Q3) +$G +(print, $G) +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>)