From 00f365c30fdd9fde38686c63833dfce061650799 Mon Sep 17 00:00:00 2001 From: bdring Date: Thu, 14 Nov 2024 16:28:38 -0600 Subject: [PATCH 1/6] first cut of adding _selected_tool --- FluidNC/src/GCode.cpp | 19 +++++++++------ FluidNC/src/GCode.h | 1 + FluidNC/src/Parameters.cpp | 10 ++++++-- FluidNC/src/Report.cpp | 2 +- fixture_tests/fixtures/tool_param_test.nc | 28 +++++++++++++++++++++++ 5 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 fixture_tests/fixtures/tool_param_test.nc diff --git a/FluidNC/src/GCode.cpp b/FluidNC/src/GCode.cpp index 196c312c2..11da1b724 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(); } @@ -1611,7 +1612,8 @@ Error gc_execute_line(char* line) { 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 +1621,19 @@ 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.tool = gc_state.selected_tool; + gc_state.current_tool = gc_state.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 +1642,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..799d71eaf 100644 --- a/FluidNC/src/GCode.h +++ b/FluidNC/src/GCode.h @@ -302,6 +302,7 @@ struct parser_state_t { 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..a5d85a6b3 100644 --- a/FluidNC/src/Parameters.cpp +++ b/FluidNC/src/Parameters.cpp @@ -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/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>) From 61987c7b5b41fab2ebc92ae8037262fd68c1b424 Mon Sep 17 00:00:00 2001 From: bdring Date: Thu, 14 Nov 2024 19:33:59 -0600 Subject: [PATCH 2/6] Got rid of gc_state.tool It is now .selected_tool --- FluidNC/src/Channel.cpp | 4 ++-- FluidNC/src/GCode.cpp | 8 ++------ FluidNC/src/GCode.h | 2 +- FluidNC/src/Parameters.cpp | 2 +- FluidNC/src/ToolChangers/atc_manual.cpp | 2 +- 5 files changed, 7 insertions(+), 11 deletions(-) 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 11da1b724..17e6e3bc0 100644 --- a/FluidNC/src/GCode.cpp +++ b/FluidNC/src/GCode.cpp @@ -1605,10 +1605,9 @@ 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 @@ -1621,16 +1620,13 @@ 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; - gc_state.current_tool = gc_state.tool; + 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 diff --git a/FluidNC/src/GCode.h b/FluidNC/src/GCode.h index 799d71eaf..f21acc4fe 100644 --- a/FluidNC/src/GCode.h +++ b/FluidNC/src/GCode.h @@ -300,7 +300,7 @@ struct parser_state_t { float spindle_speed; // RPM float feed_rate; // Millimeters/min - uint32_t tool; // Tracks tool number + //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 diff --git a/FluidNC/src/Parameters.cpp b/FluidNC/src/Parameters.cpp index a5d85a6b3..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; } 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)"); // } From aca92af8fb1f0a0eb9568147224d1d970f27d0b9 Mon Sep 17 00:00:00 2001 From: bdring Date: Thu, 14 Nov 2024 19:37:13 -0600 Subject: [PATCH 3/6] Added some test gcode to fixtures --- fixture_tests/fixtures/current_tool_test.nc | 34 +++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 fixture_tests/fixtures/current_tool_test.nc diff --git a/fixture_tests/fixtures/current_tool_test.nc b/fixture_tests/fixtures/current_tool_test.nc new file mode 100644 index 000000000..fe3bfbbda --- /dev/null +++ b/fixture_tests/fixtures/current_tool_test.nc @@ -0,0 +1,34 @@ +(print, $G) +$G +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>) +(print, T1) +T1 +(print, $G) +$G +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>) +(print, M6) +M6 +(print, $G) +$G +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>) +(print, M6T2) +M6T2 +(print, $G) +$G +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>) +(print, M61Q3) +M61Q3 +(print, $G) +$G +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>) +(print, M6T0) +M6T0 +(print, $G) +$G +(print, Current tool=%d#<_current_tool>) +(print, Selected tool=%d#<_selected_tool>) From 7280f1f75f5cc07bb726a32cb4b0874791534f5f Mon Sep 17 00:00:00 2001 From: bdring Date: Thu, 14 Nov 2024 20:20:43 -0600 Subject: [PATCH 4/6] Test fixture now works with the python script --- fixture_tests/fixtures/current_tool_test.nc | 101 +++++++++++++------- 1 file changed, 67 insertions(+), 34 deletions(-) diff --git a/fixture_tests/fixtures/current_tool_test.nc b/fixture_tests/fixtures/current_tool_test.nc index fe3bfbbda..b991b4eca 100644 --- a/fixture_tests/fixtures/current_tool_test.nc +++ b/fixture_tests/fixtures/current_tool_test.nc @@ -1,34 +1,67 @@ -(print, $G) -$G -(print, Current tool=%d#<_current_tool>) -(print, Selected tool=%d#<_selected_tool>) -(print, T1) -T1 -(print, $G) -$G -(print, Current tool=%d#<_current_tool>) -(print, Selected tool=%d#<_selected_tool>) -(print, M6) -M6 -(print, $G) -$G -(print, Current tool=%d#<_current_tool>) -(print, Selected tool=%d#<_selected_tool>) -(print, M6T2) -M6T2 -(print, $G) -$G -(print, Current tool=%d#<_current_tool>) -(print, Selected tool=%d#<_selected_tool>) -(print, M61Q3) -M61Q3 -(print, $G) -$G -(print, Current tool=%d#<_current_tool>) -(print, Selected tool=%d#<_selected_tool>) -(print, M6T0) -M6T0 -(print, $G) -$G -(print, Current tool=%d#<_current_tool>) -(print, Selected tool=%d#<_selected_tool>) +-> $X +<~ [MSG:INFO: Caution: Unlocked] +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T0 F0 S0] +<- ok +-> (print, Current tool=%d#<_current_tool>) +<- [MSG:INFO: PRINT, Current tool=-1] +<- ok +-> (print, Selected tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Selected tool=0] +<- ok +-> T1 +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T1 F0 S0] +<- ok +-> (print, Current tool=%d#<_current_tool>) +<- [MSG:INFO: PRINT, Current tool=-1] +<- ok +-> (print, Selected tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Selected tool=1] +<- ok +-> M6 +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T1 F0 S0] +<- ok +-> (print, Current tool=%d#<_current_tool>) +<- [MSG:INFO: PRINT, Current tool=1] +<- ok +-> (print, Selected tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Selected tool=1] +<- ok +-> M6T2 +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T2 F0 S0] +<- ok +-> (print, Current tool=%d#<_current_tool>) +<- [MSG:INFO: PRINT, Current tool=2] +<- ok +-> (print, Selected tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Selected tool=2] +<- ok +-> M61Q3 +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T3 F0 S0] +<- ok +-> (print, Current tool=%d#<_current_tool>) +<- [MSG:INFO: PRINT, Current tool=3] +<- ok +-> (print, Selected tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Selected tool=3] +<- ok +-> M6T0 +<- ok +-> $G +<- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T0 F0 S0] +<- ok +-> (print, Current tool=%d#<_current_tool>) +<- [MSG:INFO: PRINT, Current tool=0] +<- ok +-> (print, Selected tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Selected tool=0] +<- ok \ No newline at end of file From e7fd17bc7a4d1e1178e8fd5594275cd7910d1417 Mon Sep 17 00:00:00 2001 From: bdring Date: Fri, 15 Nov 2024 13:27:19 -0600 Subject: [PATCH 5/6] Cleanup --- fixture_tests/fixtures/current_tool_test.nc | 46 ++++++++------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/fixture_tests/fixtures/current_tool_test.nc b/fixture_tests/fixtures/current_tool_test.nc index b991b4eca..11747d57a 100644 --- a/fixture_tests/fixtures/current_tool_test.nc +++ b/fixture_tests/fixtures/current_tool_test.nc @@ -4,64 +4,52 @@ -> $G <- [GC:G0 G54 G17 G21 G90 G94 M5 M9 T0 F0 S0] <- ok --> (print, Current tool=%d#<_current_tool>) -<- [MSG:INFO: PRINT, Current tool=-1] -<- ok --> (print, Selected tool=%d#<_selected_tool>) -<- [MSG:INFO: PRINT, Selected tool=0] +-> (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, Current tool=%d#<_current_tool>) -<- [MSG:INFO: PRINT, Current tool=-1] -<- ok --> (print, Selected tool=%d#<_selected_tool>) -<- [MSG:INFO: PRINT, Selected tool=1] +-> (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, Current tool=%d#<_current_tool>) -<- [MSG:INFO: PRINT, Current tool=1] -<- ok --> (print, Selected tool=%d#<_selected_tool>) -<- [MSG:INFO: PRINT, Selected tool=1] +-> (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, Current tool=%d#<_current_tool>) -<- [MSG:INFO: PRINT, Current tool=2] +-> (print, Cur tool=%d#<_current_tool>, Sel tool=%d#<_selected_tool>) +<- [MSG:INFO: PRINT, Cur tool=2 Sel tool=2] <- ok --> (print, Selected tool=%d#<_selected_tool>) -<- [MSG:INFO: PRINT, Selected tool=2] +-> (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, Current tool=%d#<_current_tool>) -<- [MSG:INFO: PRINT, Current tool=3] -<- ok --> (print, Selected tool=%d#<_selected_tool>) -<- [MSG:INFO: PRINT, Selected tool=3] +-> (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, Current tool=%d#<_current_tool>) -<- [MSG:INFO: PRINT, Current tool=0] -<- ok --> (print, Selected tool=%d#<_selected_tool>) -<- [MSG:INFO: PRINT, Selected tool=0] +-> (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 From 3a07aabe7ff34a9474016b0bf309554358b3b753 Mon Sep 17 00:00:00 2001 From: Mitch Bradley Date: Sat, 16 Nov 2024 17:33:45 -1000 Subject: [PATCH 6/6] Update GCode.h Removed commented-out line --- FluidNC/src/GCode.h | 1 - 1 file changed, 1 deletion(-) diff --git a/FluidNC/src/GCode.h b/FluidNC/src/GCode.h index f21acc4fe..6b96d666e 100644 --- a/FluidNC/src/GCode.h +++ b/FluidNC/src/GCode.h @@ -300,7 +300,6 @@ 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