Skip to content

Commit

Permalink
Fix selected tool param (#1381)
Browse files Browse the repository at this point in the history
* first cut of adding _selected_tool

* Got rid of gc_state.tool

It is now .selected_tool

* Added some test gcode to fixtures

* Test fixture now works with the python script

* Cleanup

* Update GCode.h

Removed commented-out line

---------

Co-authored-by: Mitch Bradley <[email protected]>
  • Loading branch information
bdring and MitchBradley authored Nov 17, 2024
1 parent 870d88f commit 119a8b4
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 18 deletions.
4 changes: 2 additions & 2 deletions FluidNC/src/Channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 11 additions & 10 deletions FluidNC/src/GCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -1604,32 +1605,31 @@ 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;
}
if (new_spindle) {
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;
Expand All @@ -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 ]:
Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/GCode.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions FluidNC/src/Parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ bool get_numbered_param(ngc_param_id_t id, float& result) {
return true;
}
if (id == 5400) {
result = static_cast<float>(gc_state.tool);
result = static_cast<float>(gc_state.selected_tool);
return true;
}

Expand Down Expand Up @@ -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('.'));
Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/Report.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion FluidNC/src/ToolChangers/atc_manual.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)"); //
}
Expand Down
55 changes: 55 additions & 0 deletions fixture_tests/fixtures/current_tool_test.nc
Original file line number Diff line number Diff line change
@@ -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
28 changes: 28 additions & 0 deletions fixture_tests/fixtures/tool_param_test.nc
Original file line number Diff line number Diff line change
@@ -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>)

0 comments on commit 119a8b4

Please sign in to comment.