Skip to content

Commit

Permalink
Use Macros to save and restore machine state
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Lang committed Aug 17, 2024
1 parent 2991888 commit 04360e8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 58 deletions.
94 changes: 42 additions & 52 deletions FluidNC/src/ToolChangers/atc_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,64 +15,67 @@ namespace ATCs {

bool Basic_ATC::tool_change(uint8_t new_tool, bool pre_select, bool set_tool) {
protocol_buffer_synchronize(); // wait for all motion to complete
_macro.erase(); // clear previous gcode
if (pre_select) { // not implemented
_macro.erase(); // clear previous gcode

if (pre_select) { // not implemented
return true;
}

// save current location, so we can return after the tool change.
_macro.addf("#<start_x >= #<_x>");
_macro.addf("#<start_y >= #<_y>");
_macro.addf("#<start_z >= #<_z>");

//save machine states
Macro set_state;
Macro restore_state;
set_state.addf("M9"); // Disable coolant
if (gc_state.modal.coolant.Mist) {
restore_state.addf("M7");
}
if (gc_state.modal.coolant.Flood) {
restore_state.addf("M8");
}
set_state.addf("G21"); // become Metric
if (gc_state.modal.units == Units::Inches) {
restore_state.addf("G20");
}
set_state.addf("G90"); // become absolute
if (gc_state.modal.distance != Distance::Absolute) {
restore_state.addf("G91");
}
set_state.addf("M5"); // turn off the spindle
if (gc_state.modal.spindle != SpindleState::Disable) {
restore_state.addf("M3");
}

_macro.addf(set_state._gcode.c_str());

// set_tool is used to update the current tool and reset the TLO to 0
if (set_tool) {
_prev_tool = new_tool;
_macro.addf("G4P0 0.0");
if (!_have_tool_setter_offset) {
get_ets_offset();
}
_macro.addf(restore_state._gcode.c_str());
_macro.run(nullptr);
return true;
}

//save machine states
bool spindle_was_on = (gc_state.modal.spindle != SpindleState::Disable); // used to restore the spindle state
bool was_inch_mode = (gc_state.modal.units == Units::Inches);
bool was_absolute_mode = (gc_state.modal.distance == Distance::Absolute);
bool mistcoolant_was_on= (gc_state.modal.coolant.Mist);
bool floodcoolant_was_on = (gc_state.modal.coolant.Flood);
// save current location, so we can return after the tool change.
_macro.addf("#<start_x >= #<_x>");
_macro.addf("#<start_y >= #<_y>");
_macro.addf("#<start_z >= #<_z>");

if (mistcoolant_was_on || floodcoolant_was_on) {
_macro.addf("M9");
}
if (was_inch_mode) { // become Metric
_macro.addf("G21");
}
if (!was_absolute_mode) { // become absolute
_macro.addf("G90");
}
if (spindle_was_on) { // turn off the spindle
_macro.addf("M5");
}

try {
if (_prev_tool > 0) {
// return tool
move_to_tool_position(_prev_tool);
_macro.addf(_toolreturn_macro._gcode.c_str()); // use macro with G91 movements or the _tc_tool_* variables to to return tool, operating the ATC using M62 & M63
// ensure the macro didn't change positioning mode
_macro.addf("G90");
_macro.addf("G21");
_macro.addf(set_state._gcode.c_str()); // ensure the previous user macro didn't change modes
}

if (new_tool > 0) {
//pickup tool
move_to_tool_position(_prev_tool);
_macro.addf(_toolpickup_macro._gcode.c_str()); // use macro with G91 movements or the _tc_tool_* variables to to pickup tool, operating the ATC using M62 & M63
// ensure the macro didn't change positioning mode
_macro.addf("G90");
_macro.addf("G21");
_macro.addf(set_state._gcode.c_str()); // ensure the previous user macro didn't change modes
if (!_have_tool_setter_offset) {
get_ets_offset();
}
Expand All @@ -89,21 +92,8 @@ namespace ATCs {
move_to_safe_z();
_macro.addf("G0 X#<start_x>Y#<start_y>");
_macro.addf("G0 Z#<start_z>");
if (was_inch_mode) {
_macro.addf("G20");
}
if (!was_absolute_mode) { // become relative
_macro.addf("G91");
}
if (spindle_was_on) {
_macro.addf("M3"); // spindle should handle spinup delay
}
if (mistcoolant_was_on) {
_macro.addf("M7");
}
if (floodcoolant_was_on) {
_macro.addf("M8");
}

_macro.addf(restore_state._gcode.c_str());
_macro.run(nullptr);

return true;
Expand All @@ -116,9 +106,9 @@ namespace ATCs {
tool_index -= 1;
move_to_safe_z();
_macro.addf("G53 G0 X%0.3f Y%0.3f", _tool_mpos[tool_index][X_AXIS], _tool_mpos[tool_index][Y_AXIS]);
_macro.addf("#<_tc_tool_x >=%0.3f",_tool_mpos[tool_index][X_AXIS]);
_macro.addf("#<_tc_tool_y >=%0.3f",_tool_mpos[tool_index][Y_AXIS]);
_macro.addf("#<_tc_tool_z >=%0.3f",_tool_mpos[tool_index][Z_AXIS]);
_macro.addf("#<_tc_tool_x >=%0.3f", _tool_mpos[tool_index][X_AXIS]);
_macro.addf("#<_tc_tool_y >=%0.3f", _tool_mpos[tool_index][Y_AXIS]);
_macro.addf("#<_tc_tool_z >=%0.3f", _tool_mpos[tool_index][Z_AXIS]);
}

void Basic_ATC::move_to_safe_z() {
Expand All @@ -130,7 +120,7 @@ namespace ATCs {
_macro.addf("G53 G0 X%0.3fY%0.3f", _ets_mpos[0], _ets_mpos[1]);
}

void Basic_ATC::get_ets_offset(){
void Basic_ATC::get_ets_offset() {
ets_probe();
_macro.addf("#<_ets_tool1_z>=[#5063]"); // save the value of the tool1 ETS Z
_have_tool_setter_offset = true;
Expand Down
12 changes: 6 additions & 6 deletions FluidNC/src/ToolChangers/atc_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace ATCs {
const int TOOL_COUNT = 8;

class Basic_ATC : public ATC {
public:
Basic_ATC(const char* name) : ATC(name) {}
Expand All @@ -40,11 +40,11 @@ namespace ATCs {
float _tool_setter_offset = 0.0; // have we established an offset.
float _tool_setter_position[MAX_N_AXIS];

void move_to_safe_z();
void move_over_toolsetter();
void ets_probe();
void get_ets_offset();
void move_to_tool_position(uint8_t tool_index);
void move_to_safe_z();
void move_over_toolsetter();
void ets_probe();
void get_ets_offset();
void move_to_tool_position(uint8_t tool_index);
Macro _macro;
Macro _toolreturn_macro;
Macro _toolpickup_macro;
Expand Down

0 comments on commit 04360e8

Please sign in to comment.