Hello everybody,
In this video we will look at using Python scripts within the CNC-machine control-software mikroCNC through a laser cutting example. The mikroCNC software is free and can be downloaded from our website.
The laser cutting process will be demonstrated on a redesigned version of an older generation 3D printer. The complete control electronics were replaced on the 3D printer. For control, we now use Audioms Automatika ETH-MCI motion controller based on Ethernet communication with PC and 4 MST-109 microstep drivers for stepper motors.
For more details on this topic, see the following video.
Download the Python code used in the example from the video
or just copy code from bellow:
import gcode
import mikrocnc as mcnc
import fileio as fio
import math
import sys # needed for input parameters
# L -> number of columns in a matrix
# P -> number of rows in a matrix
# Q -> x dimension of one block
# R -> y dimension of one block
#global parameters
xoffset = -33.5
yoffset = -42
feedrate = 700
laserOn = " A0.255\n"
laserOff = " A0.000\n"
# here we store the input parameters of the macro in local variables
cntx = int(sys.argv[1])
cnty = int(sys.argv[2])
sx = int(sys.argv[3])
sy = int(sys.argv[4])
# create a file with custom gcode
with fio.open('laser_test.tap','w') as file:
xlength = cntx*sx
ylength = cnty*sy
# setup
file.writeline('G90\n')
file.writeline('M3 S10000\n')
file.writeline(f'G0X{xoffset}Y{yoffset}' + laserOff)
file.writeline(f'G1F{feedrate}\n')
# trace horizontal lines
file.writeline(f'G1X{xoffset+xlength}' + laserOn)
for xLine in range(cnty):
file.writeline(f'G0Y{yoffset+sy*(xLine+1)}' + laserOff)
file.writeline(f'G1X{xoffset+xlength*((xLine)%2)}' + laserOn)
# trace vertical lines
invert = 1
if(cnty%2):
invert = 0
file.writeline(f'G1Y{yoffset}' + laserOn)
for yLine in range(cntx):
if(invert):
file.writeline(f'G0X{xoffset+sx*(cntx-1-yLine)}' + laserOff)
else:
file.writeline(f'G0X{xoffset+sx*(yLine+1)}' + laserOff)
file.writeline(f'G1Y{yoffset+ylength*((yLine+1)%2)}' + laserOn)
# final part
file.writeline(f'G0X0Y0' + laserOff)
file.writeline('M5\n')
file.writeline('M30\n')
# Write success message in Macro log window
print("Script built successfully")
# load the gcode file into mikroCNC environment
gcode.LoadFile(mcnc.GetMacroPath()+'laser_test.tap')
Audioms Automatika doo Support team

