diff --git a/R_6.0kOhms_UR_25.0kOhmpm.exp b/R_6.0kOhms_UR_25.0kOhmpm.exp new file mode 100644 index 0000000..a3805c8 Binary files /dev/null and b/R_6.0kOhms_UR_25.0kOhmpm.exp differ diff --git a/exp_generation.py b/exp_generation.py new file mode 100644 index 0000000..0190ceb --- /dev/null +++ b/exp_generation.py @@ -0,0 +1,77 @@ +import numpy + +def make_file (xpoint,ypoint,file_name): + #mm*10 + xpoint = [x*10 for x in xpoint] + ypoint = [y*10 for y in ypoint] + + + #calc delta-x & y for each stitch + dx=[]; dy=[] + for i in range(0,len(xpoint)-1): + dx.append(xpoint[i+1]-xpoint[i]) + dy.append(ypoint[i+1]-ypoint[i]) + + #helps filter out the 10's and 237's, important later + dx = [numpy.round(x) for x in dx] + dy = [numpy.round(y) for y in dy] + + #convert to byte + #1-127 are forward by that amount + #129-255 are backward by that same amount + dxn=[]; dyn=[] + #chr(10) and chr(237) mess things up, FYI + #need to alternate above and below these values to avoid offsets + + xpalt=True; xnalt=True; ypalt=True; ynalt=True; + for stitch in range(0,len(dx)): + if dx[stitch] < 0: + dxn.append(dx[stitch]+256) + if dxn[stitch] == 237: + if xnalt==True: + dxn[stitch]=238 + else: + dxn[stitch]=236 + xnalt=not xnalt + elif dx[stitch] == 10: + if xpalt==True: + dxn.append(11) + else: + dxn.append(9) + xpalt=not xpalt + else: + dxn.append(dx[stitch]) + for stitch in range(0,len(dy)): + if dy[stitch] < 0: + dyn.append(dy[stitch]+256) + if dyn[stitch] == 237: + if ynalt==True: + dyn[stitch]=238 + else: + dyn[stitch]=236 + ynalt=not ynalt + elif dy[stitch] == 10: + if ypalt==True: + dyn.append(11) + else: + dyn.append(9) + ypalt=not ypalt + else: + dyn.append(dy[stitch]) + + + # convert movement number to uint8 format (hexad) + # and merge into one continuous string + string2="" + for stitch in range(0,int(len(dxn)/1)): + string2+=chr(int(dxn[stitch])) + string2+=chr(int(dyn[stitch])) + + + # write data to .exp file + with open(file_name, 'w') as filehandle: + filehandle.write(string2) + + #return values for troubleshooting if needed + return dxn,dyn + diff --git a/main.py b/main.py new file mode 100644 index 0000000..106ff00 --- /dev/null +++ b/main.py @@ -0,0 +1,97 @@ +# RESISTOR FILE GENERATION TOOL (v2) +# +# This program is for creating embroidery files for electrical resistors +# +# Developed in Python 3.8 +# Last edit: May 1, 2021 +# writes to .exp (Melco, Bravo, and Bernina machines) + +import exp_generation +import stitch_gen +import pads +import numpy as np +import math +import matplotlib.pyplot as plt + +################################ +#### user inputs +################################ + +r = 6_000 # desired resistance (Ohms) +rho=25_000 # thread unit resistance (Ohms/m) +gl = 1; #gap length (spacing between rows) (mm) +sl = gl; #stitch length (gap between stitches) (mm) +tgl = 2*gl; #gap length between terminal pads (mm) +pl = 2.5; #pad length (mm) +pad_rows = 3 #terminal pad stitch density (number of stitch rows) + +#making a filename reflective of target resistance and thread unit resistance +#rename to your own filename if you so choose, otherwise leave as is +file_name="".join(["R_",str(round(r/1000,1)),"kOhms_UR_",str(round(rho/1000,1)),"kOhmpm.exp"]) + + +################################ +#### initialize counters, lists, and (x,y) coords +################################ + +tl = 0; #total length of thread used (initialize counter) +xpoint=[]; ypoint=[] #initialize stitch coordinate lists +x = y = 0; #initial coordinates (0,0) + + +################################ +#### pattern calculations +################################ + +len = r/rho; #desired length of thread (ft) +len = len*1000; #length of wire (mm) + +w=(-8*gl+math.sqrt(gl)*math.sqrt(16*gl+24*len+24*tgl+48*pl))/4; #width of resistor +h=w*2/3; #height of the tines + +width_stitches = int((w/2)/gl)+1 +height_stitches = int(h/sl) + +right_bottom_stitches = int((w/2-tgl/2-pl)/sl) + + +################################## +#### Start of stitching process +################################## + +#right terminal pad +right_pad_matrix = pads.pad_right(x,y, xpoint,ypoint,sl,pl,pad_rows) +x=right_pad_matrix[0]; y=right_pad_matrix[1] #(x,y) coords of current stitch +xpoint=right_pad_matrix[2]; ypoint=right_pad_matrix[3] + +# main body of the resistor +main_matrix=stitch_gen.main_block(x,y, xpoint,ypoint,sl,pl,gl,tgl,right_bottom_stitches,width_stitches,height_stitches) + +x=main_matrix[0]; y=main_matrix[1] #(x,y) coords of current stitch + +xpoint=main_matrix[2]; ypoint=main_matrix[3] #list of (x,y) stitches +tl=main_matrix[4] #length of thread used in resistor (no pads) + +#add left terminal pad +main_matrix = pads.pad_left(x,y, xpoint,ypoint,sl,pl,pad_rows) +xpoint=main_matrix[2]; ypoint=main_matrix[3] + + +################################ +#### file generation (convert to .exp) +################################ + +xpoint = np.asarray(xpoint); ypoint = np.asarray(ypoint) + +dx,dy = exp_generation.make_file(xpoint,ypoint,file_name) + + +################################ +#### plotting resistor pattern +################################ + +plt.plot(xpoint,ypoint,'r-',xpoint,ypoint,'x') +plt.ylabel('y coordinate (mm)') +plt.xlabel('x coordinate (mm)') +plt.title('Resistor Pattern') +plt.show() \ No newline at end of file diff --git a/pads.py b/pads.py new file mode 100644 index 0000000..c1f3f09 --- /dev/null +++ b/pads.py @@ -0,0 +1,90 @@ + +# loops to generate the pattern +#right connector, square pad +def pad_right(x,y, xpoint,ypoint,sl,pl,pad_rows): + + #horizontal lines + for i in range(0,pad_rows): + for ii in range(0,pad_rows): + xpoint.append(x) + ypoint.append(y) + x=x+pl/pad_rows; + xpoint.append(x) + ypoint.append(y) + y=y+pl/(pad_rows*2-1); + for ii in range(0,pad_rows): + xpoint.append(x) + ypoint.append(y) + x=x-pl/pad_rows; + xpoint.append(x) + ypoint.append(y) + y=y+pl/(pad_rows*2-1); + #end of the loop to create connector pad (horizontal lines) (right) + y=y-pl/(pad_rows*2-1); #offset last movement + + #vertical lines + for i in range(0,pad_rows): + for ii in range(0,pad_rows): + xpoint.append(x) + ypoint.append(y) + y=y-pl/pad_rows; + xpoint.append(x) + ypoint.append(y) + x=x+pl/(pad_rows*2-1); + for ii in range(0,pad_rows): + xpoint.append(x) + ypoint.append(y) + y=y+pl/pad_rows; + xpoint.append(x) + ypoint.append(y) + x=x+pl/(pad_rows*2-1); + # end of the loop to create connector (vertical lines) (right) + x = x - pl / (pad_rows*2-1); # step back to top of pad + return x, y, xpoint, ypoint +# end of function pads_right + + + + + + +#second terminal pad (left) +def pad_left(x,y, xpoint,ypoint,sl,pl,pad_rows): + # vertical lines + for i in range(0,pad_rows): + for i in range(0,pad_rows): + xpoint.append(x) + ypoint.append(y) + y-=pl/pad_rows; + xpoint.append(x) + ypoint.append(y) + x+=pl/(pad_rows*2-1); + for i in range(0,pad_rows): + xpoint.append(x) + ypoint.append(y) + y+=pl/pad_rows; + xpoint.append(x) + ypoint.append(y) + x+=pl/(pad_rows*2-1); + # end of the loop to create connector (vertical lines) (left) + x-=pl/(pad_rows*2-1); + + #horizontal lines + for i in range(0,pad_rows): + for i in range(0,pad_rows): + xpoint.append(x) + ypoint.append(y) + x-=pl/pad_rows; + xpoint.append(x) + ypoint.append(y) + y-=pl/(pad_rows*2-1); + for i in range(0,pad_rows): + xpoint.append(x) + ypoint.append(y) + x+=pl/pad_rows; + xpoint.append(x) + ypoint.append(y) + y-=pl/(pad_rows*2-1); + # end of the loop to create connector pad (horizontal lines) (left) + y += pl / (pad_rows*2-1); + return x, y, xpoint, ypoint \ No newline at end of file diff --git a/stitch_gen.py b/stitch_gen.py new file mode 100644 index 0000000..11a141b --- /dev/null +++ b/stitch_gen.py @@ -0,0 +1,60 @@ + +def main_block (x,y, xpoint,ypoint,sl,pl,gl,tgl,right_bottom_stitches,width_stitches,height_stitches): + #start of main block + tl=0 + + #move from start to bottom right corner + for i in range(0,right_bottom_stitches): + xpoint.append(x) + ypoint.append(y) + x+=sl; + tl+=sl; + + #step upwards 2 gap lengths + for step in range(0,2): + xpoint.append(x) + ypoint.append(y) + y+=gl; + tl+=gl; + + + #repeating columns for main block + for i in range(0,width_stitches): + for i in range (0,height_stitches): + xpoint.append(x) + ypoint.append(y) + y+=sl; + tl+=sl; + xpoint.append(x) + ypoint.append(y) + x-=gl; + tl+=gl; + for i in range(0,height_stitches): + xpoint.append(x) + ypoint.append(y) + y-=sl; + tl+=sl; + xpoint.append(x) + ypoint.append(y) + x-=gl; + tl+=gl; + #end of while loop, vertical tabs done + + x=x+gl; #cancel x jump of last loop + + for step in range(0,2): + xpoint.append(x) + ypoint.append(y) + y-=gl; #step down to start return path + tl+=gl; + + + #return to left terminal pad + while x < -pl-tgl: + #for i in range(0,left_bottom_stitches): + xpoint.append(x) + ypoint.append(y) + x+=sl; + tl+=sl; + #print(xpoint) + return(x,y, xpoint,ypoint,tl) \ No newline at end of file