Close

Streaming G-code Live

A project log for The Disassembler by team Table Flip

It’s getting hard and expensive to get components. We're developing a system to automatically deconstruct old products for recycling/reuse.

johnJohn 06/10/2022 at 17:180 Comments

After deciding to build our device on top of a 3d printer, we needed to understand how a printer works so we can modify it for our own purposes.

In the normal use of a filament 3d printer, first a 3d model is created digitally. Then the digital file is passed through a slicer program which divides it into the layers that the printer will extrude on top of each other. The entire set of printing instructions is written out in G-code and sent to the printer.

Our process needs to work differently:

None of this functionality would work with just sending one G-code file to the printer. So we needed to figure out how to stream the G-code. The live flexibility to change what the printer is doing makes this project much more robust to overcome unexpected hurdles.

Here is a video where we live stream data from the keyboard to the 3d printer. As we build out the rest of the project we’ll change this so the machine learning algorithm will stream this data, not the keyboard. (Although we also plan on keeping the keyboard functionality to give the option for people who want to hand solder or desolder but may not have the physical capabilities).

https://drive.google.com/file/d/1DHYh9G93C_eJTGZmz_B6gO7Thh_OEOvF/view?usp=sharing

We found that marlin firmware is used on many common printers (LulzBot, Průša Research, Creality3D, BIQU, Geeetech, and Ultimaker). By tapping into that firmware, we can provide access to a wide variety of printers, not just one brand.

If the printer is powered on, it will wait for G-CODE commands. It’s expecting this data over the USB port (it may be easier for you to use a USB-to-Serial adapter). All you need to do is connect to the port using a 115200 baud rate, and you should be able to send G-CODE commands that the printer will interpret live.

There are a lot of code commands, but here are the ones we’re starting with.

The most important function we need to perform is to incrementally move the printer head (which is where we’ll mount our soldering iron). With this command we can tell the printer head where to go based on the current position. The “E” stands from extruder; Marlin won’t let you perform anything if the extruder is not heated.

G0/G1 X<value> Y<value> Z<value> E<value>

If instead we want to move the head to an absolute position (not relative like the above) we could use this command

G92 X<value> Y<value> Z<value> E<value>

To perform that auto homing operation, we just need to run

G28

To set the position from relative to absolute, we can use

G90/G91

From that we were able to put together this code, which is how we controlled the 3d printer from the keyboard.

import serial, time, pygame, sys 

ser = serial.Serial('/dev/tty.usbserial-1130', 115200)  # open serial port
pygame.init()
display = pygame.display.set_mode((300, 300))

W_KEY = 119
S_KEY = 115
A_KEY = 97
D_KEY = 100
Q_KEY = 113
E_KEY = 101
I_KEY = 105
K_KEY = 107

X = 0
Y = 0
Z = 0

INC = 1

# autohome 
ser.write('G28'.encode())

while True:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
         
        if event.type == pygame.KEYDOWN:

            if event.key == I_KEY: INC = INC + 1 
            if event.key == K_KEY: INC = INC - 1
            else:
                if event.key == W_KEY: X = X + INC
                elif event.key == S_KEY: X = X - INC
                elif event.key == A_KEY: Y = Y + INC
                elif event.key == D_KEY: Y = Y - INC
                elif event.key == Q_KEY: Z = Z + INC
                elif event.key == E_KEY: Z = Z - INC

                #move printer head 
                ser.write('G0 X{} Y{} Z{} \n'.format(X, Y, Z).encode())
            

            print("X:{}, Y:{}, Z:{}, Step:{}".format(X, Y, Z, INC))`

Discussions