diff options
| author | Franklin Wei <franklin@rockbox.org> | 2019-11-28 15:13:37 -0500 |
|---|---|---|
| committer | Franklin Wei <franklin@rockbox.org> | 2019-11-28 15:13:37 -0500 |
| commit | c2de4a3258c3e37d4b49a41d786eef936262f137 (patch) | |
| tree | 35fef02126cf1daa100efe94953e829539af2944 /src | |
| parent | 04c4ffd24472b66c8ccb43031cd069f81cc09e01 (diff) | |
| download | rastercarve-c2de4a3258c3e37d4b49a41d786eef936262f137.zip rastercarve-c2de4a3258c3e37d4b49a41d786eef936262f137.tar.gz rastercarve-c2de4a3258c3e37d4b49a41d786eef936262f137.tar.bz2 rastercarve-c2de4a3258c3e37d4b49a41d786eef936262f137.tar.xz | |
Some more minor details
Diffstat (limited to 'src')
| -rwxr-xr-x | src/rastercarve.py | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/src/rastercarve.py b/src/rastercarve.py index 880a932..3e94713 100755 --- a/src/rastercarve.py +++ b/src/rastercarve.py @@ -30,6 +30,9 @@ LINEAR_RESOLUTION = .01 # spacing between image samples along a line (inches) #### Image interpolation SUPERSAMPLE = 5 # interpolate the image by this factor (caution: this scales the image by the square of its value) +#### G-Code options +LINE_NOS = True # Generate line "N"umbers + #### Internal stuff - don't mess with this DEG2RAD = math.pi / 180 DEPTH_TO_WIDTH = 2 * math.tan(TOOL_ANGLE / 2 * DEG2RAD) # multiply by this to get the width of a cut @@ -45,6 +48,12 @@ def frange(x, y, jump): def eprint(s): print(s, file=sys.stderr) +line = 1 +def gcode(s): + global line + print(("N%d %s" % (line, s)) if LINE_NOS else s) + line += 1 + pathlen = 0 lastpos = None @@ -56,31 +65,33 @@ def updatePos(pos): pathlen += np.linalg.norm(pos - lastpos) lastpos = pos -is_firstmove = True +# reflect as needed +def transform(x, y): + return x, -y # we will negate the Y axis in all these def move(x, y, z, f = FEEDRATE): - # override feedrate if this is our first move (and a plunge) - global is_firstmove - if is_firstmove: - f = PLUNGE_RATE - is_firstmove = False - print("G1 F%d X%f Y%f Z%f" % (f, x, -y, z)) + x, y = transform(x, y) + gcode("G1 F%d X%f Y%f Z%f" % (f, x, y, z)) updatePos(np.array([x, y, z])) def moveRapid(x, y, z): - print("G0 X%f Y%f Z%f" % (x, -y, z)) + x, y = transform(x, y) + gcode("G0 X%f Y%f Z%f" % (x, y, z)) updatePos(np.array([x, y, z])) def moveSlow(x, y, z): + # we don't want to transform X, Y here move(x, y, z, PLUNGE_RATE) + updatePos(np.array([x, y, z])) def moveRapidXY(x, y): - print("G0 X%f Y%f" % (x, -y)) + x, y = transform(x, y) + gcode("G0 X%f Y%f" % (x, y)) updatePos(np.array([x, y, lastpos[2]])) def moveZ(z, f = PLUNGE_RATE): - print("G1 F%d Z%f" % (f, z)) + gcode("G1 F%d Z%f" % (f, z)) newpos = lastpos newpos[2] = z updatePos(newpos) @@ -131,7 +142,7 @@ def engraveLine(img_interp, img_size, ppi, start, d, step = LINEAR_RESOLUTION): # return last engraved point return v - step * d -def doEngrave(img): +def doEngrave(filename): # check parameter sanity if ( not(0 <= LINE_ANGLE < 90) or not(0 < TOOL_ANGLE < 180) or @@ -147,7 +158,7 @@ def doEngrave(img): eprint("WARNING: Invalid parameter(s).") # invert and convert to grayscale - img = ~cv2.cvtColor(cv2.imread(sys.argv[1]), cv2.COLOR_BGR2GRAY) + img = ~cv2.cvtColor(cv2.imread(filename), cv2.COLOR_BGR2GRAY) orig_h, orig_w = img.shape[:2] @@ -159,7 +170,9 @@ def doEngrave(img): interp_ppi = img_ppi * SUPERSAMPLE # preamble: https://www.instructables.com/id/How-to-write-G-code-basics/ - print("G00 G90 G80 G28 G17 G20 G40 G49\n") + print("( Generated by rastercarve: github.com/built1n/rastercarve )") + print("( Image name: %s )" % (filename)) + gcode("G00 G90 G80 G28 G17 G20 G40 G49\n") d = np.array([math.cos(LINE_ANGLE * DEG2RAD), -math.sin(LINE_ANGLE * DEG2RAD)]) |