Implementing G-code procedures in the firmware

edited March 2017 in General
Hi all,

I have a question I couldn't the answer to with the search function. The thing is that the printer I have to control needs some special procedures for homing, changing the toolheads, calibrating extruders, calibrating the HBP and more.

For example the procedure for homing is as follows:

G28 Y ; Home Y BEFORE X, this is very important
G1 Y-3.0 ; This is needed for the X to be homed, because the magnet that is detected by the hall sensor is on the Y gantry
G28 X ; Home X
G28 Z ; Home Z
G1 Y-10.0 F2400 ; Move to the fysical zero position
G92 X0 Y0 Z0 ; Set the zero position of the printer to the current position

Of course, the printer has to execute all of this code when the user presses "Home all" in simplify3D, or any other gcode sender for that matter. This might be possible by editing some of the definitions in configuration.h (although I doubt it) but it would be way easier if I could kind of "hook" this code to the G28 (home all) command. This is because I have to implement more procedures and digging into the firmware to implement all of them would be really cumbersome while I already have the gcodes corresponding to the procedures.

So my real question is:

Can I replace a command the arduino receives by a series of other commands?


  • Short answer is, yes you can.

    Long answer is to use your cool event system to add your own code. There are 2 events to overwrite default behaviour. If you use dev version it is run before original code and returning true prevents original code from boing run.

    // Allow adding new G and M codes. To implement it create a function
    // bool eventUnhandledGCode(GCode *com)
    // that returns true if it handled the code, otherwise false.
    // Event define would then be
    // #define EVENT_UNHANDLED_G_CODE(c) eventUnhandledGCode(c)
    #define EVENT_UNHANDLED_G_CODE(c) false
    #define EVENT_UNHANDLED_M_CODE(c) false

    Here you need to overwrite

    #define EVENT_UNHANDLED_G_CODE(c) false

    to call the following function

    bool customGCode(GCode *com) {
      switch(com->G) {
      case 28: // send calibration gcode
        // yout homing here
         return false;
      return true;

    for more informations read the header of Events.h on how to add the events or check on github the sample customevent implementations. This allows you to modify the firmware a great way without needing to change our sources, so it stays easy to update later without to redo all changes.
  • Thank you so much! I haven't had the time yet to tinker around. But I will start experimenting now :)
  • Could I use your expertise one more time? Could you tell me how I'd implement the actual gcode in this part:

    case 28: // send calibration gcode
        // yout homing here

    Intuitively I would do it like this:

    case 28: // send calibration gcode
        // etcetera

    But that would give me errors.
  • You always have 2 options. Eithe rcall the function that does the work, e.g. Printer::homeAxis(false,true,false); to home y axis or if that is too complicated inject the gcode directly:

    GCode::executeFString(PSTR("G28 Y0"));

    The onyl command you are not allowed to use is your own, so if case 28: means G28 you can not use G28 in the command as it would call yourself instead making a recusion until firmware crash.

  • Hi Repetier,

    thank you again! I implemented it by changing 28 to 50028. Next, when the board receives a G28 query it automatically sends the corresponding G50028 commands. Works like a charm :)!
Sign In or Register to comment.