From 724c6e36b3c7862b2c1a1f3867afeb7f594a23d8 Mon Sep 17 00:00:00 2001 From: bdring Date: Mon, 30 Nov 2020 14:34:34 -0600 Subject: [PATCH] Manual tool change --- Grbl_Esp32/Custom/atari_1020.cpp | 2 +- Grbl_Esp32/Custom/atc.cpp | 24 +++++++++------ Grbl_Esp32/Custom/custom_code_template.cpp | 2 +- Grbl_Esp32/src/GCode.cpp | 35 +++++++++++++++++++--- Grbl_Esp32/src/GCode.h | 3 +- Grbl_Esp32/src/Grbl.h | 2 +- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/Grbl_Esp32/Custom/atari_1020.cpp b/Grbl_Esp32/Custom/atari_1020.cpp index 2d0e9fff..833a5af2 100644 --- a/Grbl_Esp32/Custom/atari_1020.cpp +++ b/Grbl_Esp32/Custom/atari_1020.cpp @@ -212,7 +212,7 @@ void calc_solenoid(float penZ) { A tool (pen) change is done by bumping the carriage against the right edge 3 times per position change. Pen 1-4 is valid range. */ -bool user_tool_change(uint8_t new_tool) { +bool user_tool_change(uint8_t new_tool, bool automatic) { uint8_t move_count; char gcode_line[20]; protocol_buffer_synchronize(); // wait for all previous moves to complete diff --git a/Grbl_Esp32/Custom/atc.cpp b/Grbl_Esp32/Custom/atc.cpp index 115f5375..75983517 100644 --- a/Grbl_Esp32/Custom/atc.cpp +++ b/Grbl_Esp32/Custom/atc.cpp @@ -71,35 +71,41 @@ void user_machine_init() { pinMode(ETS_DUST_OFF, OUTPUT); // the tool setter - tool[ETS_INDEX].mpos[X_AXIS] = 108; + tool[ETS_INDEX].mpos[X_AXIS] = 104; tool[ETS_INDEX].mpos[Y_AXIS] = 292.0; tool[ETS_INDEX].mpos[Z_AXIS] = -60.0; // Mpos before collet face triggers probe - tool[1].mpos[X_AXIS] = 151.0; - tool[1].mpos[Y_AXIS] = 291.0; + tool[1].mpos[X_AXIS] = 146.0; + tool[1].mpos[Y_AXIS] = 292.0; tool[1].mpos[Z_AXIS] = -87.0; - tool[2].mpos[X_AXIS] = 186.0; - tool[2].mpos[Y_AXIS] = 291.0; + tool[2].mpos[X_AXIS] = 181.0; + tool[2].mpos[Y_AXIS] = 292.0; tool[2].mpos[Z_AXIS] = -87.0; - tool[3].mpos[X_AXIS] = 221.0; + tool[3].mpos[X_AXIS] = 216.0; tool[3].mpos[Y_AXIS] = 292.0; tool[3].mpos[Z_AXIS] = -87.0; - tool[4].mpos[X_AXIS] = 256.0; - tool[4].mpos[Y_AXIS] = 291.0; + tool[4].mpos[X_AXIS] = 251.0; + tool[4].mpos[Y_AXIS] = 292.0; tool[4].mpos[Z_AXIS] = -87.0; top_of_z = limitsMaxPosition(Z_AXIS) - homing_pulloff->get(); } -bool user_tool_change(uint8_t new_tool) { +bool user_tool_change(uint8_t new_tool, bool automatic) { bool spindle_was_on = false; bool was_incremental_mode = false; // started in G91 mode uint64_t spindle_spin_delay; // used to make sure spindle has fully spun down and up. float saved_mpos[MAX_N_AXIS] = {}; // the position before the tool change + if (!automatic) { + current_tool = new_tool; + grbl_msg_sendf(CLIENT_ALL, MsgLevel::Info, "Manual tool change to:%d", current_tool); + return true; + } + if (new_tool == current_tool) { // if no change, we are done grbl_msg_sendf(CLIENT_SERIAL, MsgLevel::Info, "ATC existing tool requested:%d", new_tool); return true; diff --git a/Grbl_Esp32/Custom/custom_code_template.cpp b/Grbl_Esp32/Custom/custom_code_template.cpp index 1dd37bc7..f81e0193 100644 --- a/Grbl_Esp32/Custom/custom_code_template.cpp +++ b/Grbl_Esp32/Custom/custom_code_template.cpp @@ -123,7 +123,7 @@ void forward_kinematics(float* position) { user_tool_change() is called when tool change gcode is received, to perform appropriate actions for your machine. */ -bool user_tool_change(uint8_t new_tool) {} +bool user_tool_change(uint8_t new_tool, bool automatic) {} #if defined(MACRO_BUTTON_0_PIN) || defined(MACRO_BUTTON_1_PIN) || defined(MACRO_BUTTON_2_PIN) /* diff --git a/Grbl_Esp32/src/GCode.cpp b/Grbl_Esp32/src/GCode.cpp index d3ad3813..526758f0 100644 --- a/Grbl_Esp32/src/GCode.cpp +++ b/Grbl_Esp32/src/GCode.cpp @@ -516,6 +516,10 @@ Error gc_execute_line(char* line, uint8_t client) { mg_word_bit = ModalGroup::MM9; break; #endif + case 61: // Set Current Tool + gc_block.modal.tool_change = ToolChange::SetTool; + mg_word_bit = ModalGroup::MM6; + break; case 62: gc_block.modal.io_control = IoControl::DigitalOnSync; mg_word_bit = ModalGroup::MM10; @@ -626,7 +630,10 @@ Error gc_execute_line(char* line, uint8_t client) { case 'Q': axis_word_bit = GCodeWord::Q; gc_block.values.q = value; - //grbl_msg_sendf(CLIENT_SERIAL, MSG_LEVEL_INFO, "Q %2.2f", value); + if (gc_block.modal.tool_change == ToolChange::SetTool) { // M61 + gc_state.tool = int_value; + + } break; case 'R': axis_word_bit = GCodeWord::R; @@ -825,6 +832,19 @@ Error gc_execute_line(char* line, uint8_t client) { bit_false(value_words, bit(GCodeWord::E)); bit_false(value_words, bit(GCodeWord::Q)); } + if (gc_block.non_modal_command == NonModal::Dwell) { + if (bit_isfalse(value_words, bit(GCodeWord::P))) { + FAIL(Error::GcodeValueWordMissing); // [P word missing] + } + bit_false(value_words, bit(GCodeWord::P)); + } + if (gc_block.modal.tool_change == ToolChange::SetTool) { + if (bit_isfalse(value_words, bit(GCodeWord::Q))) { + FAIL(Error::GcodeValueWordMissing); // [P word missing] + } + bit_false(value_words, bit(GCodeWord::Q)); + } + // [11. Set active plane ]: N/A switch (gc_block.modal.plane_select) { case Plane::XY: @@ -1252,6 +1272,7 @@ Error gc_execute_line(char* line, uint8_t client) { (bit(GCodeWord::X) | bit(GCodeWord::Y) | bit(GCodeWord::Z) | bit(GCodeWord::A) | bit(GCodeWord::B) | bit(GCodeWord::C))); // Remove axis words. } + if (value_words) { FAIL(Error::GcodeUnusedWords); // [Unused words] } @@ -1350,12 +1371,19 @@ Error gc_execute_line(char* line, uint8_t client) { } // 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; - // [6. Change tool ]: NOT SUPPORTED + // [6. Change tool ] if (gc_block.modal.tool_change == ToolChange::Enable) { - if (!user_tool_change(gc_state.tool)) { // (weak) should be user defined + if (!user_tool_change(gc_state.tool, true)) { // (weak) should be user defined FAIL(Error::ToolChangeError); } } + // [61. Manually set current tool ] + if (gc_block.modal.tool_change == ToolChange::SetTool) { + if (!user_tool_change(gc_state.tool, false)) { // (weak) should be user defined + FAIL(Error::ToolChangeError); + } + } + // [7. Spindle control ]: if (gc_state.modal.spindle != gc_block.modal.spindle) { // Update spindle control and apply spindle speed when enabling it in this block. @@ -1602,7 +1630,6 @@ Error gc_execute_line(char* line, uint8_t client) { } __attribute__((weak)) bool user_tool_change(uint8_t new_tool) { - return true; } diff --git a/Grbl_Esp32/src/GCode.h b/Grbl_Esp32/src/GCode.h index 4b070459..d55ce764 100644 --- a/Grbl_Esp32/src/GCode.h +++ b/Grbl_Esp32/src/GCode.h @@ -44,7 +44,7 @@ enum class ModalGroup : uint8_t { MG12 = 9, // [G54,G55,G56,G57,G58,G59] Coordinate system selection MG13 = 10, // [G61] Control mode MM4 = 11, // [M0,M1,M2,M30] Stopping - MM6 = 14, // [M6] Tool change + MM6 = 14, // [M6, M61] Tool change, Set Current Tool MM7 = 12, // [M3,M4,M5] Spindle turning MM8 = 13, // [M7,M8,M9] Coolant control MM9 = 14, // [M56] Override control @@ -185,6 +185,7 @@ enum class ToolLengthOffset : uint8_t { enum class ToolChange : uint8_t { Disable = 0, Enable = 1, + SetTool = 2, // tell Grbl the current tool }; // Modal Group G12: Active work coordinate system diff --git a/Grbl_Esp32/src/Grbl.h b/Grbl_Esp32/src/Grbl.h index 3334d9ee..f91d9042 100644 --- a/Grbl_Esp32/src/Grbl.h +++ b/Grbl_Esp32/src/Grbl.h @@ -114,4 +114,4 @@ void user_defined_macro(uint8_t index); // Called if USE_M30 is defined void user_m30(); -bool user_tool_change(uint8_t new_tool); // weak +bool user_tool_change(uint8_t new_tool, bool automatic); // weak