DerAndere1 / Marlin

Optimized firmware for RepRap 3D printers based on the Arduino platform. The branch Marlin2ForPipetBot is optimized firmware for cartesian robots (lab robots, also known as liquid handling robots or pipetting robots)
https://derandere.gitlab.io/pipetbot-a8
GNU General Public License v3.0
53 stars 20 forks source link

[FR] 6axis_dev: Customized Gcode for tool change #40

Closed GenaUser closed 3 years ago

GenaUser commented 3 years ago

Could you add possibility to create a custom gcode executed when activating a tool and another when deactivating (identical that the Repetier firmware offers) This allows all axes to be controlled depending on the tools state. For example, to manage a tools changer using a stepper motor through a fourth axis

Thanks in advance for studying this feature.

DerAndere1 commented 3 years ago

I am not sure if I understand. I am not familiar with Repetier firmware. Can you point me to information about the feature of Repetier firmware you are talking about? Marlin already has the option EVENT_GCODE_AFTER_TOOLCHANGE in Configuration_adv.h, see https://github.com/DerAndere1/Marlin/blob/c7b99451e77bcbda696049e244f51dce210552b2/Marlin/Configuration_adv.h#L2002 .

GenaUser commented 3 years ago

The problem with Marlin's "#define EVENT_GCODE_AFTER_TOOLCHANGE" is that the custom Gcode is the same for all tools. But maybe I misunderstood The Repetier firmware allows the possibility to create 2 gcode for each tool: during selection and deselection. In the following example, activating T0 tool moves the « A » axis to its origins (G28 A). Activating T1 tool moves the « A » axis to 12 mm (G1 A12).

Repetier V2 // TOOL_EXTRUDER(name, offx, offy, offz, heater, stepper, resolution, yank, maxSpeed, acceleration, advance, startScript, endScript) TOOL_EXTRUDER(ToolExtruder1, 0, 0, 0, HeaterExtruder1, E1Motor, 1.75, 220.0, 20, 30, 1000, 40, "G28 A", "", &Fan1PWM) TOOL_EXTRUDER(ToolExtruder2, 12, 0, 0, HeaterExtruder2, E2Motor, 1.75, 220.0, 20, 30, 1000, 40, "G1 A12", "", &Fan1PWM)

It is not possible to do the same with a single Gcode, as it seems to be with Marlin. But maybe I'm wrong.

Repetier V1 had specific variables: EXT0_SELECT_COMMANDS = "" EXT0_DESELECT_COMMANDS = "" EXT1_SELECT_COMMANDS = "" EXT1_DESELECT_COMMANDS = ""

Thank you for your interest in my request.

DerAndere1 commented 3 years ago

Lets assume you use my branch 6axis_PR1 (which currently has more bugfixes than 6axis_dev) and your 4th axis drives the tool changer and you have in Configuration.h #define AXIS4_NAME 'A'. As I understand:

To make it work, I propose the following change (untested): The following lines in the file Marlin/src/module/tool_change.cpp:

https://github.com/DerAndere1/Marlin/blob/c7b99451e77bcbda696049e244f51dce210552b2/Marlin/src/module/tool_change.cpp#L1192-L1195

should be changed to

    #ifdef EVENT_GCODE_AFTER_TOOLCHANGE
      if (!no_move && TERN1(DUAL_X_CARRIAGE, dual_x_carriage_mode == DXC_AUTO_PARK_MODE)) {
        #ifdef EVENT_GCODE_AFTER_TOOLCHANGE_T1
          if (new_tool == 1) {
            gcode.process_subcommands_now_P(PSTR(EVENT_GCODE_AFTER_TOOLCHANGE_T1));
          }
          else {
            gcode.process_subcommands_now_P(PSTR(EVENT_GCODE_AFTER_TOOLCHANGE));
          }
        #else
          gcode.process_subcommands_now_P(PSTR(EVENT_GCODE_AFTER_TOOLCHANGE));
        #endif
      }
    #endif

Usage

GenaUser commented 3 years ago

Thanks for your proposition. In theory, this should work. I will try to configure your 6axis_PR1 and then test it on my test bench. It may take a little time while as I am not used to using Marlin. Thank you for your work

DerAndere1 commented 3 years ago

I am actually not sure under what circumstances the EVENT_GCODE_AFTER_TOOLCHANGE is processed in original Marlin since it is not documented well. Maybe it is better to do the following change in Marlin/Marlin/src/module/tool_change.cpp:

https://github.com/DerAndere1/Marlin/blob/c7b99451e77bcbda696049e244f51dce210552b2/Marlin/src/module/tool_change.cpp#L1192-L1195

should be changed to

    #ifdef EVENT_GCODE_TOOLCHANGE_T0
      if ((!no_move) && (new_tool == 0)) {
        gcode.process_subcommands_now_P(PSTR(EVENT_GCODE_TOOLCHANGE_T0));
      }
    #endif

    #ifdef EVENT_GCODE_TOOLCHANGE_T1
      if ((!no_move) && (new_tool == 1)) {
        gcode.process_subcommands_now_P(PSTR(EVENT_GCODE_TOOLCHANGE_T1));
      }
    #endif  

    #ifdef EVENT_GCODE_AFTER_TOOLCHANGE  
      if (!no_move && TERN1(DUAL_X_CARRIAGE, dual_x_carriage_mode == DXC_AUTO_PARK_MODE))
            gcode.process_subcommands_now_P(PSTR(EVENT_GCODE_AFTER_TOOLCHANGE));
    #endif

Then, in Confguration_adv.h, you don't have to define EVENT_GCODE_AFTER_TOOLCHANGE. You rather do the following in Confguration_adv.h:

https://github.com/DerAndere1/Marlin/blob/620a17e18759dd92ffa39f36116e4bb03e18c200/Marlin/Configuration_adv.h#L2078-L2080

should be changed to

  #if ENABLED(TOOLCHANGE_NO_RETURN)
    //#define EVENT_GCODE_AFTER_TOOLCHANGE "G12X"   // Extra G-code to run after tool-change
  #endif  
  #define EVENT_GCODE_TOOLCHANGE_T0 "G28 A\nG1 A0" // Extra G-code to run while executing tool change command T0
  #define EVENT_GCODE_TOOLCHANGE_T1 "G1 A12" // Extra G-code to run while executing tool change command T1
GenaUser commented 3 years ago

Noted. Thank you

GenaUser commented 3 years ago

Hello, I had compilation errors: "gcode" was not declared in this scope. Compilation error

I worked around the problem by moving #define EVENT_GCODE_AFTER_TOOLCHANGE with a neutral gcode, as a result of

define EVENT_GCODE_TOOLCHANGE_T0 and

define EVENT_GCODE_TOOLCHANGE_T1

Which gives this:

if HAS_MULTI_EXTRUDER

// Z raise distance for tool-change, as needed for some extruders

define TOOLCHANGE_ZRAISE 0 // (mm)

//#define TOOLCHANGE_ZRAISE_BEFORE_RETRACT // Apply raise before swap retraction (if enabled) //#define TOOLCHANGE_NO_RETURN // Never return to previous position on tool-change

if ENABLED(TOOLCHANGE_NO_RETURN)

//#define EVENT_GCODE_AFTER_TOOLCHANGE "G12X"   // Extra G-code to run after tool-change

endif

define EVENT_GCODE_TOOLCHANGE_T0 "G28 A\nG1 A0" // Extra G-code to run while executing tool change command T0

define EVENT_GCODE_TOOLCHANGE_T1 "G1 A12" // Extra G-code to run while executing tool change command T1

define EVENT_GCODE_AFTER_TOOLCHANGE "G21" // Extra G-code to run after tool-change

It's not a clean solution, but it allowed me to do some testing. Your modification works. Custom gcodes are executed correctly when tools change. However, you must first have homing all axes. To ensure the change of head even if the origins are not made, I add "G28 A" and "G28 A12" to the "T0" and T1 "commands of my screen. This can create redundancy, but it does not should not create a problem. I continue my tests. Thank you for this modification which will undoubtedly allow me to use Marlin.

DerAndere1 commented 3 years ago

Custom gcodes are executed correctly when tools change. However, you must first have homing all axes.

With your

#define EVENT_GCODE_TOOLCHANGE_T0 "G28 A\nG1 A0" // Extra G-code to run while executing tool change command T0
#define EVENT_GCODE_TOOLCHANGE_T1 "G1 A12" // Extra G-code to run while executing tool change command T1
#define EVENT_GCODE_AFTER_TOOLCHANGE "G21" // Extra G-code to run after tool-change

if you have a G28 (home all axes) in the start-script of your print , can you have as many T0 and T1 in your print's G-code script as you like? (I am not talking about performing a toolchange from a screen menu) ? Or do you have to send G28 A before each tool change command?

Could you test branch https://github.com/DerAndere1/Marlin/tree/6axis_EVENT_GCODE_TOOLCHANGE and report if it works the same ? it has a cleaner solution (see commit https://github.com/DerAndere1/Marlin/commit/fd6efc3a8b5813c629d637ca84422820caf2b94e)? You can use the same config files as for your previous tests, just remove your #define EVENT_GCODE_AFTER_TOOLCHANGE "G21" // Extra G-code to run after tool-change

GenaUser commented 3 years ago

The "Marlin-6axis_EVENT_GCODE_TOOLCHANGE" branch works correctly in the same way.

With the "G28" present in the script for starting all printing, the "T0" and "T1" commands alone activate the custom gcode. The movement of the fourth axis is considered as for the other axes, as an offset between the heads. This is why they are only functional after homing. In my screen commands, I add "G28 A" to "T0" to have a displacement of the heads even if the axes do not have their origins. This is often the case when you start the printer and you want to change the filaments without initializing everything.

Thank you for your efforts

DerAndere1 commented 3 years ago

How exactly did you add the G28 A to the screen command for tool-change?

GenaUser commented 3 years ago

The screen modification does not concern Marlin, but my TFT35 Bigtreetech touch screen which drives a Rumba32 MKS board. I added a specific menu for the heads with an icon "T0" and "T1" to which I associated my gcodes commands.

Head_menu

I also added a "T" button in the home menu to initialize the "A" axis.

Home_menu

My printer is currently using an 8-bit Rumba+ board with Repetier-Firmware V1. For update board in 32 bits, I compare Repetier V2 and Marlin V2

DerAndere1 commented 3 years ago

Thanks for the insight, I will close this then. You can reopen if there are issues remaining. In case your stepper driver for the A axis is different from A4988, I would love to hear what stepper driver features already work, but only if you have time.

GenaUser commented 3 years ago

At first I had a DRV8825. Now I have installed a TMC5160 SPI. Everything seems to be working fine on my test rig at the moment. Good work!