summaryrefslogtreecommitdiff
path: root/coil_test_board.py
blob: ddab2ae9ef41c96c982fbf30591621ca3cf13490 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/usr/bin/env python3

import math
import itertools
import datetime
import tempfile
import subprocess

import gerbonara.cad.kicad.pcb as pcb
import gerbonara.cad.kicad.footprints as fp
import gerbonara.cad.primitives as cad_pr
import gerbonara.cad.kicad.graphical_primitives as kc_gr

cols = 6
rows = 4

coil_specs = [
        {'n':  1, 's':  True, 't': 1, 'c': 0.20, 'w': 5.00, 'v': 0.40},
        {'n':  2, 's':  True, 't': 1, 'c': 0.20, 'w': 3.00, 'v': 0.40},
        {'n':  3, 's':  True, 't': 1, 'c': 0.20, 'w': 1.50, 'v': 0.40},
        {'n':  5, 's':  True, 't': 1, 'c': 0.20, 'w': 0.80, 'v': 0.40},
        {'n': 10, 's':  True, 't': 1, 'c': 0.20, 'w': 0.50, 'v': 0.40},
        {'n': 25, 's':  True, 't': 1, 'c': 0.15, 'w': 0.25, 'v': 0.40},

        {'n':  1, 's': False, 't': 1, 'c': 0.20, 'w': 5.00, 'v': 0.40},
        {'n':  2, 's': False, 't': 1, 'c': 0.20, 'w': 3.00, 'v': 0.40},
        {'n':  3, 's': False, 't': 1, 'c': 0.20, 'w': 1.50, 'v': 0.40},
        {'n':  5, 's': False, 't': 1, 'c': 0.20, 'w': 0.80, 'v': 0.40},
        {'n': 10, 's': False, 't': 1, 'c': 0.20, 'w': 0.50, 'v': 0.40},
        {'n': 25, 's': False, 't': 1, 'c': 0.15, 'w': 0.25, 'v': 0.40},

        {'n':  1, 's': False, 't': 3, 'c': 0.20, 'w': 5.00, 'v': 0.40},
        {'n':  2, 's': False, 't': 3, 'c': 0.20, 'w': 3.00, 'v': 0.40},
        {'n':  3, 's': False, 't': 2, 'c': 0.20, 'w': 1.50, 'v': 0.40},
        {'n':  5, 's': False, 't': 2, 'c': 0.20, 'w': 0.80, 'v': 0.40},
        {'n': 10, 's': False, 't': 3, 'c': 0.20, 'w': 0.50, 'v': 0.40},
        {'n': 25, 's': False, 't': 2, 'c': 0.15, 'w': 0.25, 'v': 0.40},

        {'n':  1, 's': False, 't': 5, 'c': 0.20, 'w': 5.00, 'v': 0.40},
        {'n':  2, 's': False, 't': 5, 'c': 0.20, 'w': 3.00, 'v': 0.40},
        {'n':  3, 's': False, 't': 4, 'c': 0.20, 'w': 1.50, 'v': 0.40},
        {'n':  5, 's': False, 't': 3, 'c': 0.20, 'w': 0.80, 'v': 0.40},
        {'n': 10, 's': False, 't': 7, 'c': 0.20, 'w': 0.50, 'v': 0.40},
        {'n': 25, 's': False, 't': 7, 'c': 0.15, 'w': 0.25, 'v': 0.40},
]

version_string = 'v1.0'
coil_border = 7 # mm
cut_gap = 3 # mm
tooling_border = 10 # mm
vscore_extra = 10 # mm
mouse_bite_width = 8 # mm
mouse_bite_hole_dia = 0.5
mouse_bite_hole_spacing = 0.3
hole_offset = 5
hole_dia = 3.2
coil_dia = 35 # mm
pad_offset = 2 # mm
pad_dia = 2.0 # mm
pad_length = 3.5 # mm
pad_drill = 1.1 # mm
pad_pitch = 2.54 # mm
v_cuts = False # FIXME DEBUG
mouse_bites = False # FIXME DEBUG
coil_pitch = coil_dia + coil_border*2 + cut_gap

total_width = coil_pitch*cols + 2*tooling_border + cut_gap
total_height = coil_pitch*rows + 2*tooling_border + cut_gap

tile_width = tile_height = coil_dia + 2*coil_border
drawing_text_size = 2.0

print(f'Calculated board size: {total_width:.2f} * {total_height:.2f} mm')
print(f'Tile size: {tile_height:.2f} * {tile_height:.2f} mm')

x0, y0 = 100, 100

xy = pcb.XYCoord
b = pcb.Board.empty_board(page=pcb.PageSettings(page_format='A2'))

b.add(kc_gr.Rectangle(xy(x0, y0), xy(x0+total_width, y0+total_height), layer='Edge.Cuts', stroke=pcb.Stroke(width=0.15)))

def do_line(x0, y0, x1, y1, off_x=0, off_y=0):
    b.add(kc_gr.Line(xy(x0+off_x, y0+off_y),
                     xy(x1+off_x, y1+off_y),
                     layer='Edge.Cuts', stroke=pcb.Stroke(width=0.15)))

if v_cuts:
    for y in range(rows):
        for off_y in [0, tile_height]:
            y_pos = y0 + tooling_border + cut_gap + off_y + y*coil_pitch
            do_line(x0 - vscore_extra, y_pos, x0 + total_width + vscore_extra, y_pos)
            b.add(kc_gr.Text(text='V-score',
                             at=pcb.AtPos(x0 + total_width + vscore_extra + drawing_text_size/2, y_pos, 0),
                             layer=kc_gr.TextLayer('Edge.Cuts'),
                             effects=pcb.TextEffect(
                                 font=pcb.FontSpec(size=xy(drawing_text_size, drawing_text_size),
                                                   thickness=drawing_text_size/10),
                                 justify=pcb.Justify(h=pcb.Atom.left))))


    for x in range(cols):
        for off_x in [0, tile_width]:
            x_pos = x0 + tooling_border + cut_gap + off_x + x*coil_pitch
            do_line(x_pos, y0 - vscore_extra, x_pos, y0 + total_height + vscore_extra)
            b.add(kc_gr.Text(text='V-score',
                             at=pcb.AtPos(x_pos, y0 + total_height + vscore_extra + drawing_text_size/2, 90),
                             layer=kc_gr.TextLayer('Edge.Cuts'),
                             effects=pcb.TextEffect(
                                 font=pcb.FontSpec(size=xy(drawing_text_size, drawing_text_size),
                                                   thickness=drawing_text_size/10),
                                 justify=pcb.Justify(h=pcb.Atom.right))))

def draw_corner(x0, y0, spokes):
    right, top, left, bottom = [True if c.lower() in 'y1' else False for c in spokes]

    l = (tile_width - mouse_bite_width)/2 - cut_gap/2

    if right:
        do_line(cut_gap/2, -cut_gap/2, cut_gap/2 + l, -cut_gap/2, x0, y0)
        do_line(cut_gap/2, cut_gap/2, cut_gap/2 + l, cut_gap/2, x0, y0)
        b.add(kc_gr.Arc(start=xy(x0+cut_gap/2+l, y0-cut_gap/2),
                        end=xy(x0+cut_gap/2+l, y0+cut_gap/2),
                        center=xy(x0+cut_gap/2+l, y0),
                        layer='Edge.Cuts',
                        stroke=pcb.Stroke(width=0.15)))

    else:
        do_line(cut_gap/2, -cut_gap/2, cut_gap/2, cut_gap/2, x0, y0)

    if left:
        do_line(-cut_gap/2, -cut_gap/2, -cut_gap/2 - l, -cut_gap/2, x0, y0)
        do_line(-cut_gap/2, cut_gap/2, -cut_gap/2 - l, cut_gap/2, x0, y0)
        b.add(kc_gr.Arc(end=xy(x0-cut_gap/2-l, y0-cut_gap/2),
                        start=xy(x0-cut_gap/2-l, y0+cut_gap/2),
                        center=xy(x0-cut_gap/2-l, y0),
                        layer='Edge.Cuts',
                        stroke=pcb.Stroke(width=0.15)))

    else:
        do_line(-cut_gap/2, -cut_gap/2, -cut_gap/2, cut_gap/2, x0, y0)

    if bottom:
        do_line(-cut_gap/2, cut_gap/2, -cut_gap/2, cut_gap/2 + l, x0, y0)
        do_line(cut_gap/2, cut_gap/2, cut_gap/2, cut_gap/2 + l, x0, y0)
        b.add(kc_gr.Arc(end=xy(x0-cut_gap/2, y0+cut_gap/2+l),
                        start=xy(x0+cut_gap/2, y0+cut_gap/2+l),
                        center=xy(x0, y0+cut_gap/2+l),
                        layer='Edge.Cuts',
                        stroke=pcb.Stroke(width=0.15)))

    else:
        do_line(-cut_gap/2, cut_gap/2, cut_gap/2, cut_gap/2, x0, y0)

    if top:
        do_line(-cut_gap/2, -cut_gap/2, -cut_gap/2, -cut_gap/2 - l, x0, y0)
        do_line(cut_gap/2, -cut_gap/2, cut_gap/2, -cut_gap/2 - l, x0, y0)
        b.add(kc_gr.Arc(start=xy(x0-cut_gap/2, y0-cut_gap/2-l),
                        end=xy(x0+cut_gap/2, y0-cut_gap/2-l),
                        center=xy(x0, y0-cut_gap/2-l),
                        layer='Edge.Cuts',
                        stroke=pcb.Stroke(width=0.15)))
    else:

        do_line(-cut_gap/2, -cut_gap/2, cut_gap/2, -cut_gap/2, x0, y0)


def make_mouse_bite(x, y, rot=0, width=mouse_bite_width, hole_dia=mouse_bite_hole_dia, hole_spacing=mouse_bite_hole_spacing, **kwargs):

    pitch = hole_dia + hole_spacing
    num_holes = int(math.floor((width - hole_spacing) / pitch))
    
    actual_spacing = (width - num_holes*hole_dia) / (num_holes + 1)
    
    f = fp.Footprint(name='mouse_bite', _version=None, generator=None, at=fp.AtPos(x, y, rot), **kwargs)
    for i in range(num_holes):
        f.pads.append(fp.Pad(
            number='1',
            type=fp.Atom.np_thru_hole,
            shape=fp.Atom.circle,
            at=fp.AtPos(-width/2 + actual_spacing + i*pitch + hole_dia/2, 0, 0),
            size=xy(hole_dia, hole_dia),
            drill=fp.Drill(diameter=hole_dia),
            footprint=f))
    return f


def make_hole(x, y, dia, **kwargs):
    f = fp.Footprint(name='hole', _version=None, generator=None, at=fp.AtPos(x, y, 0), **kwargs)
    f.pads.append(fp.Pad(
        number='1',
        type=fp.Atom.np_thru_hole,
        shape=fp.Atom.circle,
        at=fp.AtPos(0, 0, 0),
        size=xy(dia, dia),
        drill=fp.Drill(diameter=dia),
        footprint=f))
    return f


def make_pads(x, y, rot, n, pad_dia, pad_length, drill, pitch, **kwargs):
    f = fp.Footprint(name=f'conn_gen_01x{n}', _version=None, generator=None, at=fp.AtPos(x, y, rot), **kwargs)

    for i in range(n):
        f.pads.append(fp.Pad(
            number=str(i+1),
            type=fp.Atom.thru_hole,
            shape=fp.Atom.oval,
            at=fp.AtPos(-pitch*(n-1)/2 + i*pitch, 0, rot),
            size=xy(pad_dia, pad_length),
            drill=fp.Drill(diameter=drill),
            footprint=f))

    return f


corner_x0 = x0 + tooling_border + cut_gap/2
corner_y0 = y0 + tooling_border + cut_gap/2
corner_x1 = x0 + total_width - tooling_border - cut_gap/2
corner_y1 = y0 + total_height - tooling_border - cut_gap/2

# Corners
draw_corner(corner_x0, corner_y0, 'YNNY')
draw_corner(corner_x0, corner_y1, 'YYNN')
draw_corner(corner_x1, corner_y0, 'NNYY')
draw_corner(corner_x1, corner_y1, 'NYYN')

# T junctions
for x in range(1, cols):
    draw_corner(corner_x0 + x*coil_pitch, corner_y0, 'YNYY')
    draw_corner(corner_x0 + x*coil_pitch, corner_y1, 'YYYN')

for y in range(1, rows):
    draw_corner(corner_x0, corner_y0 + y*coil_pitch, 'YYNY')
    draw_corner(corner_x1, corner_y0 + y*coil_pitch, 'NYYY')

# X Junctions
for x in range(1, cols):
    for y in range(1, rows):
        draw_corner(corner_x0 + x*coil_pitch, corner_y0 + y*coil_pitch, 'YYYY')

# Mouse bites
if mouse_bites:
    for x in range(0, cols):
        for y in range(0, rows):
            tile_x0 = x0 + tooling_border + cut_gap + x*coil_pitch
            tile_y0 = y0 + tooling_border + cut_gap + y*coil_pitch

            b.add(make_mouse_bite(tile_x0 + tile_width/2, tile_y0, 0))
            b.add(make_mouse_bite(tile_x0 + tile_width/2, tile_y0 + tile_height, 0))
            b.add(make_mouse_bite(tile_x0, tile_y0 + tile_height/2, 90))
            b.add(make_mouse_bite(tile_x0 + tile_width, tile_y0 + tile_height/2, 90))

# Mounting holes
for x in range(0, cols):
    for y in range(0, rows):
        tile_x0 = x0 + tooling_border + cut_gap + x*coil_pitch + tile_width/2
        tile_y0 = y0 + tooling_border + cut_gap + y*coil_pitch + tile_height/2

        dx = tile_width/2 - hole_offset
        dy = tile_height/2 - hole_offset
        b.add(make_hole(tile_x0 - dx, tile_y0 - dy, hole_dia))
        b.add(make_hole(tile_x0 - dx, tile_y0 + dy, hole_dia))
        b.add(make_hole(tile_x0 + dx, tile_y0 - dy, hole_dia))
        b.add(make_hole(tile_x0 + dx, tile_y0 + dy, hole_dia))

# border graphics
c = 3
for layer in ['F.SilkS', 'B.SilkS']:
    b.add(kc_gr.Rectangle(start=xy(x0, y0), end=xy(x0+c, y0+total_height), layer=layer, stroke=pcb.Stroke(width=0),
                          fill=kc_gr.FillMode(pcb.Atom.solid)))
    b.add(kc_gr.Rectangle(start=xy(x0, y0), end=xy(x0+total_width, y0+c), layer=layer, stroke=pcb.Stroke(width=0),
                          fill=kc_gr.FillMode(pcb.Atom.solid)))
    b.add(kc_gr.Rectangle(start=xy(x0+total_width-c, y0), end=xy(x0+total_width, y0+total_height), layer=layer, stroke=pcb.Stroke(width=0),
                          fill=kc_gr.FillMode(pcb.Atom.solid)))
    b.add(kc_gr.Rectangle(start=xy(x0, y0+total_height-c), end=xy(x0+total_width, y0+total_height), layer=layer, stroke=pcb.Stroke(width=0),
                          fill=kc_gr.FillMode(pcb.Atom.solid)))

a = 3
timestamp = datetime.datetime.now().strftime('%Y-%m-%d')
b.add(kc_gr.Text(text=f'Planar inductor test panel {version_string} {timestamp} © 2023 Jan Götte, FG KOM, TU Darmstadt',
                 at=pcb.AtPos(x0 + c + a/3, y0 + c + a/3),
                 layer=kc_gr.TextLayer('F.SilkS'),
                 effects=pcb.TextEffect(
                     font=pcb.FontSpec(size=xy(a, a),
                                       thickness=a/5),
                     justify=pcb.Justify(h=pcb.Atom.left, v=pcb.Atom.top))))

for index, ((y, x), spec) in enumerate(zip(itertools.product(range(rows), range(cols)), coil_specs), start=1):
    pass
    with tempfile.NamedTemporaryFile(suffix='.kicad_mod') as f:
        tile_x0 = x0 + tooling_border + cut_gap + x*coil_pitch + tile_width/2
        tile_y0 = y0 + tooling_border + cut_gap + y*coil_pitch + tile_height/2

        for key, alias in {
                'inner_diameter': 'id',
                'outer_diameter': 'od',
                'trace_width': 'w',
                'turns': 'n',
                'twists': 't',
                'clearance': 'c',
                'single_layer': 's',
                'via_drill': 'v'}.items():
            if alias in spec:
                spec[key] = spec.pop(alias)

        if 'via_diameter' not in spec:
            spec['via_diameter'] = spec['trace_width']
        
        if 'inner_diameter' not in spec:
            spec['inner_diameter'] = 15

        if 'outer_diameter' not in spec:
            spec['outer_diameter'] = 35

        args = ['python', '-m', 'twisted_coil_gen_twolayer', '--no-keepout-zone']
        for k, v in spec.items():
            if not isinstance(v, bool) or v:
                args.append('--' + k.replace('_', '-'))
                if v is not True:
                    args.append(str(v))
        args.append(f.name)
        subprocess.run(args, check=True)

        coil = fp.Footprint.open_mod(f.name)
        coil.at = fp.AtPos(tile_x0, tile_y0, 0)
        b.add(coil)

        t = [f'n={spec["turns"]}',
             f'{spec["twists"]} twists',
             f'w={spec["trace_width"]:.2f}mm']
        if spec.get('single_layer'):
            t.append('single layer')

        sz = 1.5
        b.add(kc_gr.Text(text='\\n'.join(t),
                         at=pcb.AtPos(tile_x0, tile_y0),
                         layer=kc_gr.TextLayer('B.SilkS'),
                         effects=pcb.TextEffect(
                             font=pcb.FontSpec(size=xy(sz, sz),
                                               thickness=sz/5),
                             justify=pcb.Justify(h=None, v=None, mirror=True))))

        b.add(kc_gr.Text(text=f'{version_string} {timestamp}\\nTile {index}',
                         at=pcb.AtPos(tile_x0, tile_y0 - tile_height/2 + sz),
                         layer=kc_gr.TextLayer('B.SilkS'),
                         effects=pcb.TextEffect(
                             font=pcb.FontSpec(size=xy(sz, sz),
                                               thickness=sz/5),
                             justify=pcb.Justify(h=None, v=pcb.Atom.top, mirror=True))))

        b.add(kc_gr.Text(text=f'{index}',
                         at=pcb.AtPos(tile_x0, tile_y0 - tile_height/2 + sz),
                         layer=kc_gr.TextLayer('F.SilkS'),
                         effects=pcb.TextEffect(
                             font=pcb.FontSpec(size=xy(sz, sz),
                                               thickness=sz/5),
                             justify=pcb.Justify(h=None, v=pcb.Atom.top, mirror=False))))

        pads_x0 = tile_x0 + tile_width/2 - pad_offset
        pads = make_pads(pads_x0, tile_y0, 270, 2, pad_dia, pad_length, pad_drill, pad_pitch)
        b.add(pads)

        w = min(spec.get('trace_width', pad_dia), pad_dia)
        x, y, _r, _f = pads.pad(2).abs_pos
        w2 = (x - pad_length/2, y)
        x, y, _r, _f = pads.pad(1).abs_pos
        w1 = (x - pad_length/2, y)
        b.add(cad_pr.Trace(w, coil.pad(1), pads.pad(1), waypoints=[w1], orientation=['ccw'], side='top'))
        b.add(cad_pr.Trace(w, coil.pad(2), pads.pad(2), waypoints=[w2], orientation=['cw'], side='bottom'))

        k = 3
        for layer in ['F.SilkS', 'B.SilkS']:
            b.add(kc_gr.Rectangle(start=xy(x-k/2, y-pad_pitch-k/2), end=xy(x+k/2, y-pad_pitch), layer=layer, stroke=pcb.Stroke(width=0),
                                  fill=kc_gr.FillMode(pcb.Atom.solid)))

b.write('coil_test_board.kicad_pcb')