1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-09-03 11:22:38 +02:00

works, except that function "user_tool_change" starts the tool change and it will interfer with further g code running in parallel. tbd

This commit is contained in:
Jens Hauser
2020-07-30 21:20:29 +02:00
parent ed7ec882d3
commit 27ec3504c9
3 changed files with 192 additions and 279 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -1,6 +1,19 @@
/* /*
custom_code_template.cpp (copy and use your machine name) custom_code_template.cpp (4 axis machine)
Part of Grbl_ESP32 Part of Grbl_ESP32
copyright (c) 2020 Jens Hauser. This file was intended for use on the ESP32
CPU. Do not use this with Grbl for atMega328P
//TODO describe this
Tool change function called by G code "T[1..n] M06". Not triggered by G38.2 or ESP3D probe function :-)
first Z probe before tool change. Only executed once, because then we<77>ll know the initial Z height.
// second Z probe after tool change. Now we can compare
//TODO parameterise this
//vTaskDelay (0.5 / portTICK_RATE_MS); // 0.5 Sec.
*/ */
#define DEBUG 0; // do I want debug messages? yes/no #define DEBUG 0; // do I want debug messages? yes/no
@@ -9,358 +22,258 @@
VARIABLES VARIABLES
*/ */
uint8_t AmountOfToolChanges; // Each new tool increases this by 1. Before first tool, it<69>s 0. uint8_t AmountOfToolChanges; // Each new tool increases this by 1. Before first tool, it<69>s 0.
uint8_t current_toolNo, new_toolNo; uint8_t currenttoolNo, newtoolNo;
float firstZPos, newZPos, Zdiff; float firstZPos, newZPos, Zdiff;
static TaskHandle_t zProbeSyncTaskHandle = 0; static TaskHandle_t zProbeSyncTaskHandle = NULL;
// Finite state machine.
uint8_t state; // final state machine
// Finite state machine and sequence of steps
uint8_t tc_state; // tool change (tc) state machine
#define TOOLCHANGE_IDLE 0 // initial state. tool change switched off. set during machine_init() #define TOOLCHANGE_IDLE 0 // initial state. tool change switched off. set during machine_init()
#define TOOLCHANGE_INIT 1 // do some reporting at first. tool change number x and so on #define TOOLCHANGE_INIT 1 // do some reporting at first. initialize G code for this procedure
#define TOOLCHANGE_START 2 // tool change function called by G code "T[1..n] M06". Not triggered by G38.2 or ESP3D probe function :-) #define TOOLCHANGE_START 2 // decide, if first or further tool changes and set next status appropriately
#define TOOLCHANGE_ZPROBE_1a 3 // Z probe #1. Send order to press the Z probe button
// first Z probe before tool change #define TOOLCHANGE_ZPROBE_1b 4 // Z probe #1. After button press
#define TOOLCHANGE_ZPROBE_1a 3 // Z probe #1 (quick). Touch down and order to press the button #define TOOLCHANGE_MANUAL 5 // Go to tool change position
#define TOOLCHANGE_ZPROBE_1b 4 // Z probe #1 (quick). Wait for button press. #define TOOLCHANGE_ZPROBE_2 6 // Z probe #2. Send order to press the Z probe button
#define TOOLCHANGE_MANUAL 5 // go to tool change position
// second Z probe after tool change. Now we can compare
#define TOOLCHANGE_ZPROBE_2a 6 // Z probe #2 (slow ). Touch down and order to press the button
#define TOOLCHANGE_ZPROBE_2b 7 // Z probe #2 (slow ). Wait for button press.
#define TOOLCHANGE_FINISH 99 // tool change finish. do some reporting, clean up, etc. #define TOOLCHANGE_FINISH 99 // tool change finish. do some reporting, clean up, etc.
// declare functions // declare functions
float getCurrentZPos();
float getLastZProbePos(); float getLastZProbePos();
float getG54Zvalue();
int8_t getState();
bool checkState(uint8_t checkState);
void setState(int8_t newState);
void setState(int8_t newState, uint8_t newtool);
#ifdef USE_MACHINE_INIT #ifdef USE_MACHINE_INIT
/* /*
machine_init() is called when Grbl_ESP32 first starts. You can use it to do any machine_init() is called when Grbl_ESP32 first starts. You can use it to do any
special things your machine needs at startup. special things your machine needs at startup.
Prerequisite: add "#define USE_MACHINE_INIT" to your machine.h file Prerequisite: add "#define USE_MACHINE_INIT" to your machine.h file
*/ */
void machine_init() void machine_init()
{ {
// We start with no tool changes yet // We start with 0 tool changes
AmountOfToolChanges=0; AmountOfToolChanges=0;
// unknown at the beginning. But this will change, if the first tool is loaded // unknown at the beginning, maybe there is no special tool loaded. But this will change, if the next tool is loaded
current_toolNo = 0; currenttoolNo = 0;
// Initialize state machine // Initialize state machine
state=TOOLCHANGE_IDLE; tc_state=TOOLCHANGE_IDLE;
// TODO this task runs permanently. Alternative? // TODO this task runs permanently. Alternative?
xTaskCreatePinnedToCore(zProbeSyncTask, // task xTaskCreatePinnedToCore(zProbeSyncTask, // task
"zProbeSyncTask", // name for task "zProbeSyncTask", // name for task
4096, // size of task stack 4096, // size of task stack
NULL, // parameters NULL, // parameters
1, // priority 1, // priority
&zProbeSyncTaskHandle, // handle &zProbeSyncTaskHandle, // handle
0 // core 0 // core
); );
} }
#endif #endif
// return current state machine status
int8_t getState()
{
return state;
}
// check, if given state is the active
bool checkState(uint8_t checkState)
{
if (state == checkState) return 1;
else return 0;
}
// setnew state machine status
void setState(int8_t newState)
{
state = newState;
}
// setnew state machine status
void setState(int8_t newState, uint8_t newtool)
{
setState(newState);
new_toolNo = newtool;
}
// state machine // state machine
void zProbeSyncTask(void* pvParameters) void zProbeSyncTask(void* pvParameters)
{ {
TickType_t xLastWakeTime; TickType_t xLastWakeTime;
const TickType_t xProbeFrequency = 100; // in ticks (typically ms) const TickType_t xProbeFrequency = 100; // in ticks
xLastWakeTime = xTaskGetTickCount(); // Initialise the xLastWakeTime variable with the current time. xLastWakeTime = xTaskGetTickCount(); // Initialise the xLastWakeTime variable with the current time.
for( ;; ) //protocol_buffer_synchronize(); // wait for all previous moves to complete
{
switch ( getState() )
{
//while running
case TOOLCHANGE_INIT: for ( ;; )
#ifdef DEBUG {
grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_INIT. State=%d\r\n", getState()); switch ( tc_state )
#else {
grbl_sendf (CLIENT_ALL, "Tool change procedure started.\r\n");
#endif
// TODO set AmountOfToolChanges to 0 after job finish
AmountOfToolChanges++; // Report tool change amount.
#ifdef DEBUG case TOOLCHANGE_INIT:
grbl_sendf (CLIENT_ALL, "This is the %d. tool change in this job\r\n", AmountOfToolChanges); // TODO set AmountOfToolChanges to 0 after job finish
grbl_sendf (CLIENT_ALL, "Old tool is #%d (0 means unknown), new tool is #%d\r\n", current_toolNo, new_toolNo); // Set amount of tool changes
#endif AmountOfToolChanges++;
// Init. Safe start block. G54, XY plane, mm mode, relative addressing mode #ifdef DEBUG
inputBuffer.push("G49\r\n"); grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_INIT. State=%d\r", tc_state);
//inputBuffer.push("G54\r\n"); grbl_sendf (CLIENT_ALL, "This is the %d. tool change in this job\r", AmountOfToolChanges);
inputBuffer.push("G53\r\n"); grbl_sendf (CLIENT_ALL, "Old tool is #%d (0 means unknown), new tool is #%d\r", currenttoolNo, newtoolNo);
inputBuffer.push("G17 G21 G90\r\n"); #endif
inputBuffer.push("G0 F100\r\n");
// Init. Safe start block. XY plane, mm mode, absolute addressing mode
// inputBuffer.push("G90 G94\r");
// inputBuffer.push("G17 G21\r");
// Switch off spindle // Switch off spindle
inputBuffer.push("M05\r\n"); inputBuffer.push("M05\r");
setState (TOOLCHANGE_START); tc_state = TOOLCHANGE_START;
break; break;
case TOOLCHANGE_START: case TOOLCHANGE_START:
#ifdef DEBUG #ifdef DEBUG
grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_START. State=%d\r\n", getState()); grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_START. State=%d\r", tc_state);
#endif #endif
// nur beim ersten Mal die L<>nge des Fr<46>sers messen. Mit dem Wert alle Weiteren vergleichen und G43.2 berechnen. // Measure firstZPos only once. Then adjust G43.2 by comparing firstZPos and newZPos.
if (AmountOfToolChanges == 1) if (AmountOfToolChanges == 1) // first time
setState(TOOLCHANGE_ZPROBE_1a); tc_state = TOOLCHANGE_ZPROBE_1a; // measure before manual tool change
else else
setState(TOOLCHANGE_MANUAL); tc_state = TOOLCHANGE_MANUAL; // measure after manual tool change
break; break;
// First Z Probe // First Z Probe
case TOOLCHANGE_ZPROBE_1a: case TOOLCHANGE_ZPROBE_1a:
#ifdef DEBUG #ifdef DEBUG
grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_ZPROBE_1a. State=%d\r\n", getState()); grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_ZPROBE_1a. State=%d\r", tc_state);
#endif #endif
// Place spindle directly above button in X/Y and a few mm above Z // Place spindle directly above button in X/Y and at high Z
inputBuffer.push("G53 G0 Z-5\r\n"); inputBuffer.push("G53 G0 Z-5\r");
inputBuffer.push("G53 G0 X-29 Y-410\r\n"); inputBuffer.push("G53 G0 X-29 Y-410\r");
// Z probe, max. 50mm to press button, quick // Z probe
inputBuffer.push("G91 G38.2 Z-110 F500\r\n"); inputBuffer.push("G91 G38.2 Z-100 F500\r");
inputBuffer.push("G4 P1.0\r\n");
setState(TOOLCHANGE_ZPROBE_1b); tc_state = TOOLCHANGE_ZPROBE_1b;
break; break;
case TOOLCHANGE_ZPROBE_1b: // wait for button press case TOOLCHANGE_ZPROBE_1b: // wait for button press
#ifdef DEBUG #ifdef DEBUG
// grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_ZPROBE_1b. State=%d\r\n", getState()); grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_ZPROBE_1b. State=%d\r", tc_state);
#endif #endif
// TODO Error handling. What happens in case the button is not pressed? // wait until we hit Z probe button
if ( probe_get_state() ) // TODO Error handling. What happens in case the button is not pressed?
{ if ( probe_get_state() )
{
if (AmountOfToolChanges == 1)
firstZPos = getLastZProbePos(); // save Z pos for comparison later
// TODO neue Tooll<6C>nge immer gegen das erste Tool, richtig? // hit the probe
if (AmountOfToolChanges == 1) grbl_sendf(CLIENT_ALL, "Button pressed first time. Z probe pos=%4.3f\r", firstZPos);
firstZPos = getLastZProbePos(); // save Z pos for comparison later
// hit the probe inputBuffer.push("G53 G0 Z-5\r");
#ifdef DEBUG
grbl_sendf(CLIENT_ALL, "Button pressed first time, quickly. Z probe pos=%4.3f\r\n", firstZPos);
#endif
inputBuffer.push("G91 G0 Z2\r\n"); tc_state = TOOLCHANGE_MANUAL;
vTaskDelay (xProbeFrequency * 20); // wait until button is released }
setState (TOOLCHANGE_MANUAL); break;
}
break;
// go to manual tool change position // go to manual tool change position
case TOOLCHANGE_MANUAL: case TOOLCHANGE_MANUAL:
#ifdef DEBUG #ifdef DEBUG
grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_MANUAL. State=%d\r\n", getState()); grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_MANUAL. State=%d\r", tc_state);
#endif #endif
// Go to tool change position if ( !probe_get_state() ) // button released now
inputBuffer.push("G53 G0 Z-5\r\n"); {
inputBuffer.push("G53 G0 X-5 Y-210\r\n"); // Go to tool change position
inputBuffer.push("G53 G0 X-5 Y-210\r");
// Hold // Hold
inputBuffer.push("M0\r\n"); inputBuffer.push("M0\r");
setState (TOOLCHANGE_ZPROBE_2a); // Place spindle directly above button in X/Y and a few mm above Z
break; inputBuffer.push("G53 G0 Z-5\r");
inputBuffer.push("G53 G0 X-29 Y-410\r");
case TOOLCHANGE_ZPROBE_2a: // Z probe, max. 50mm to press button, quick
#ifdef DEBUG inputBuffer.push("G91 G38.2 Z-100 F500\r");
grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_ZPROBE_2a. State=%d\r\n", getState());
#endif tc_state = TOOLCHANGE_ZPROBE_2;
}
// Place spindle directly above button in X/Y and a few mm above Z break;
inputBuffer.push("G53 G0 Z-5\r\n");
inputBuffer.push("G53 G0 X-29 Y-410\r\n");
// Z probe, max. 50mm to press button, quick case TOOLCHANGE_ZPROBE_2: // wait for button press
inputBuffer.push("G91 G38.2 Z-110 F500\r\n"); #ifdef DEBUG
inputBuffer.push("G4 P2.0\r\n"); // grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_ZPROBE_2. State=%d\r", tc_state);
#endif
setState(TOOLCHANGE_ZPROBE_2b); // TODO Error handling. What happens in case the button is not pressed?
break; if ( probe_get_state() )
{
newZPos = getLastZProbePos(); // save Z pos for later comparison to firstZPos
case TOOLCHANGE_ZPROBE_2b: // wait for button press // hit the probe
#ifdef DEBUG #ifdef DEBUG
// grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_ZPROBE_2b. State=%d\r\n", getState()); grbl_sendf (CLIENT_ALL, "Button pressed second time. new Z probe pos=%4.3f\r", newZPos);
#endif #endif
// TODO Error handling. What happens in case the button is not pressed? // calculate and send out G43.1 adjustment
if ( probe_get_state() ) char gcode_line[20];
{ sprintf(gcode_line, "G43.1 Z%4.3f\r", newZPos-firstZPos);
newZPos = getLastZProbePos(); // save Z pos for comparison later inputBuffer.push(gcode_line);
grbl_sendf (CLIENT_ALL, gcode_line);
// hit the probe // go up
#ifdef DEBUG inputBuffer.push("G53 G0 Z-5\r");
grbl_sendf (CLIENT_ALL, "Button pressed second time, slowly. new Z probe pos=%4.3f\r\n", newZPos);
#endif
// send out G43.1 adjustment tc_state = TOOLCHANGE_FINISH;
char gcode_line[20]; }
break;
sprintf(gcode_line, "G54 G43.1 Z%4.3f\r\n", newZPos-firstZPos); // That<61>s it
case TOOLCHANGE_FINISH:
#ifdef DEBUG
grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_FINISH. State=%d\r", tc_state);
#endif
inputBuffer.push(gcode_line); // button released, we lift up
grbl_sendf (CLIENT_ALL, gcode_line); if (! probe_get_state() )
{
//vTaskDelay (1 / portTICK_RATE_MS); // 1 sec.
grbl_send (CLIENT_ALL, "Tool change procedure finished.\r");
grbl_send (CLIENT_ALL, "Go to current WCS origin after hold.\r");
inputBuffer.push("G91 G0 Z2\r\n"); // go to current WCS origin. This could be G54, but also another one
setState (TOOLCHANGE_FINISH); inputBuffer.push("G0 X0 Y0\r");
} inputBuffer.push("G0 Z0\r");
break;
// That<61>s it //protocol_buffer_synchronize(); // wait for all previous moves to complete
case TOOLCHANGE_FINISH: }
#ifdef DEBUG
grbl_sendf (CLIENT_ALL, "zProbeSyncTask. TOOLCHANGE_FINISH. State=%d\r\n", getState());
#endif
grbl_send (CLIENT_ALL, "Tool change procedure finished.\r\n"); tc_state = TOOLCHANGE_IDLE;
grbl_send (CLIENT_ALL, "Go to G54 after hold.\r\n"); break;
}
// Hold
//inputBuffer.push("M0\r\n");
// Clean up vTaskDelayUntil(&xLastWakeTime, xProbeFrequency);
//inputBuffer.push("G90\r\n"); }
// Go to G54
// TODO G54 needs to be set before, otherwise bad things could happen
inputBuffer.push("G53 G0 Z-5\r\n");
inputBuffer.push("G54");
inputBuffer.push("G90");
inputBuffer.push("G0 X0 Y0\r\n");
inputBuffer.push("G0 Z0\r\n");
setState (TOOLCHANGE_IDLE);
break;
}
vTaskDelayUntil(&xLastWakeTime, xProbeFrequency);
}
} }
#ifdef USE_TOOL_CHANGE #ifdef USE_TOOL_CHANGE
/* /*
user_tool_change() is called when tool change gcode is received, user_tool_change() is called when tool change gcode is received,
to perform appropriate actions for your machine. to perform appropriate actions for your machine.
Prerequisite: add "#define USE_TOOL_CHANGE" to your machine.h file Prerequisite: add "#define USE_TOOL_CHANGE" to your machine.h file
*/ */
void user_tool_change(uint8_t new_tool) void user_tool_change(uint8_t new_tool)
{ {
setState (TOOLCHANGE_INIT, new_tool); // let<65>s start with the state machine // let<65>s start with the state machine
/* newtoolNo = new_tool;
Prerequisites tc_state = TOOLCHANGE_INIT;
- First tool properly mounted in spindle sleep(2500);
- G54 is set. G54/Z touches the work piece surface
- Reminder: set Autodesk Fusion 360 raw material origin to G54
What happens with every call to this function while ( zProbeSyncTaskHandle != NULL )
{
vTaskDelete( xHandle );
}
+ Switch off spindle //TODO
+ Move to tool change position (not for the first call) // Nach Aufruf dieser Function wird gleich wieder zurc<72>gkegeben in die aufrufende Function.
- Hold (not for the first call) // Ziel: Erst return, wenn wirklich beendet (RTOS!)
return;
- Manual tool change (not for the first call)
- Proceed by un-hold (not for the first call)
- Move to Z probe button
- Quick and then slow Z probe for better precision
- Save Z probe position to "NEW"
- Calculate delta between "NEW" and "OLD"
- Call G43.1 with that delta
- Copy Z probe position "NEW" to "OLD"
- Go to G54
- Hold
- Switch on spindle
- Proceed with job
*/
return;
} }
#endif #endif
// return current Z machine position
float getCurrentZPos()
{
int32_t current_position[N_AXIS]; // copy of current location
float m_pos[N_AXIS]; // machine position in mm
memcpy(current_position, sys_position, sizeof(sys_position)); // get current position in step
system_convert_array_steps_to_mpos(m_pos, sys_position); // convert to millimeters
return m_pos[Z_AXIS];
}
// return last Z probe machine position // return last Z probe machine position
float getLastZProbePos() float getLastZProbePos()
{ {
int32_t lastZPosition[N_AXIS]; // copy of current location int32_t lastZPosition[N_AXIS]; // copy of current location
float m_pos[N_AXIS]; // machine position in mm float m_pos[N_AXIS]; // machine position in mm
char output[200]; char output[200];
memcpy(lastZPosition, sys_probe_position, sizeof(sys_probe_position)); // get current position in step memcpy(lastZPosition, sys_probe_position, sizeof(sys_probe_position)); // get current position in step
system_convert_array_steps_to_mpos(m_pos, lastZPosition); // convert to millimeters system_convert_array_steps_to_mpos(m_pos, lastZPosition); // convert to millimeters
return m_pos[Z_AXIS]; return m_pos[Z_AXIS];
}
// return the stored G54/Z value
float getG54Zvalue()
{
float coord_data[N_AXIS];
settings_read_coord_data(0, coord_data); //0=G54
return coord_data[Z_AXIS];
} }