Sunday, 23 May 2021

3D Printer control via Gambas

Its easy to write some Gambas code to provide basic control of a 3D printer.

You just need a good G-code reference list.

These notes were born out of a few experiments I recently conducted with my Creality CR-10-V3

My original idea was a stupid one; I wanted to be able to download G-code files to the printers SD card, then instruct it to print. The reason why this is stupid will become clear.

The starting point is to create a new Gambas GUI project, include the component gb.Net, and add a SerialPort object.

Then set the serial properties:-

Name: SerialPort3D ...or whatever

DataBits: Bits8

FlowControl: None

Parity: None

PortName: /dev/ttyUSB0

Speed: 115200

StopBits: Bits1

Then add a nice big TextArea and write some code to read the port;

Public Sub SerialPort3D_Read()
Dim strReceived As String
   
  Try Read #SerialPort3D, strReceived, Lof(SerialPort3D)
  If Error Then
    strReceived = ""
  Else
    TextArea1.Text = TextArea1.Text & strReceived
  Endif

Catch
  Me.Text = Error.Text
End

The next step is to add a routine to send data;

Public Sub SendSerialData(strMessage As String, blnLF As Boolean)

  If blnLF Then
    strMessage &= gb.Lf
  Endif
  SerialPort3D.Begin()
  Write #SerialPort3D, strMessage
  SerialPort3D.Send()
Catch
  Me.Text = Error.Text
End

To keep things simple, I'm going to open the serial port when the program loads and close it when the program closes;

Public Sub Form_Open()

  SerialPort3D.Open()
 
End

Public Sub Form_Close()
 
  SerialPort3D.Close()
 
End

With the printer connected via a suitable cable and having given the Linux user suitable permissions (http://captainbodgit.blogspot.com/2021/05/3d-printing-printer-to-linux-computer.html) running the program now will cause the printer to reset and send data.


Add a few constants to the Gambas code;

Const HOME_PRINT_HEAD As String = "G28"
Const LIST_SD_CARD As String = "M20"
Const INIT_SD_CARD As String = "M21"
Const FIRMWARE_VERSION As String = "M115"
Const LCD_MESSAGE As String = "M117 Controlled by Gambas!"

Then maybe add a few switches to enable the transmission of commands;

Public Sub btnHome_Click()

  SendSerialData(HOME_PRINT_HEAD, True)

End

...

  SendSerialData(LIST_SD_CARD, True)

...

  SendSerialData(LCD_MESSAGE,True)

...

You can even write data to new files on the SD card. Here are a few more constants;

Const WRITE_TO_SD_START As String = "M28"   '"M28 test.gco"
Const WRITE_TO_SD_STOP As String = "M29"
Const DELETE_SD_FILE As String = "M30"

Note that file names must be in the 8.3 format.

However, the problem is this; sliced files (g-code files) are large. The serial link only operates at a Baud rate of 115,200. So it takes a long time to download even the simplest of objects.


conclusions

  1. even though I didn't managed to get optimised data transfer working, I decided that downloading files to the SD card would never be fast enough. Writing data to an Arduino via a simple serial link is never going to be as fast as writing to a USB port on (say) a RaspberryPi...one is a controller, the other is a mini computer
  2. for object files that I can print in a few hours, its better to use Cura to control the printer while it prints. The only downside is that it can make other applications sluggish...such as writing this blog
  3. for objects that take much longer (and for over-night printing) its better to load the file to the printer SD card and run from there.

 

Marlin G-code Reference: https://marlinfw.org/meta/gcode/

 

For more posts like this, click the 3D Printing tab at the top of the page


No comments:

Post a Comment