-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.go
243 lines (218 loc) · 6.79 KB
/
main.go
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
// dual-bifilar-coil creates Gerber files (and a bundled ZIP) representing
// two bifilar coils (https://en.wikipedia.org/wiki/Bifilar_coil) (one on top
// layer and one on the bottom layer) for manufacture on a printed circuit
// board (PCB).
package main
import (
"flag"
"fmt"
"log"
"math"
"os"
"runtime/pprof"
_ "github.com/gmlewis/go-fonts-f/fonts/freeserif"
. "github.com/gmlewis/go-gerber/gerber"
"github.com/gmlewis/go-gerber/gerber/viewer"
)
var (
step = flag.Float64("step", 0.01, "Resolution (in radians) of the spiral")
n = flag.Int("n", 100, "Number of full winds in each spiral")
gap = flag.Float64("gap", 0.15, "Gap between traces in mm (6mil = 0.15mm)")
trace = flag.Float64("trace", 0.15, "Width of traces in mm")
prefix = flag.String("prefix", "dual-bifilar-coil", "Filename prefix for all Gerber files and zip")
fontName = flag.String("font", "freeserif", "Name of font to use for writing source on PCB (empty to not write)")
pts = flag.Float64("pts", 18.0, "Font point size (72 pts = 1 inch = 25.4 mm)")
view = flag.Bool("view", false, "View the resulting design using Fyne")
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
)
const (
messageFmt = `This is a dual (2-layer)
bifilar coil.
Trace size = %0.2fmm.
Gap size = %0.2fmm.
Each spiral has %v coils.`
)
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
g := New(fmt.Sprintf("%v-n%v", *prefix, *n))
s := newSpiral()
topSpiralR := s.genSpiral(1.0, 0, 0)
topSpiralL := s.genSpiral(1.0, math.Pi, 0)
botSpiralR := s.genSpiral(-1.0, 0, 0)
startR := genPt(1.0, s.startAngle, 0, 0)
endR := genPt(1.0, s.endAngle, 0, 0)
startL := genPt(1.0, s.startAngle, 0, math.Pi)
endL := genPt(1.0, s.endAngle, 0, math.Pi)
viaPadD := 0.5
viaDrillD := 0.25
viaPadOffset := 0.5 * (viaPadD - *trace)
padD := 2.0
drillD := 1.0
padOffset := 0.5 * (padD - *trace)
botSpiralL := s.genSpiral(-1.0, math.Pi, startL[1]+2*padOffset)
// Lower connecting trace between two spirals
hole1 := Point(startR[0], startR[1]+viaPadOffset)
hole2 := Point(endL[0]-padOffset, endL[1])
// Upper connecting trace for left spiral
hole3 := Point(startL[0], startL[1]-viaPadOffset)
halfTW := *trace * 0.5
hole4 := Point(endR[0]+padOffset-halfTW, startL[1]+2*padOffset)
// Lower connecting trace for right spiral
hole5 := Point(endR[0]+padOffset, endR[1])
top := g.TopCopper()
top.Add(
Polygon(Pt{0, 0}, true, topSpiralR, 0.0),
Polygon(Pt{0, 0}, true, topSpiralL, 0.0),
// Lower connecting trace between two spirals
Circle(hole1, viaPadD),
Circle(hole2, padD),
// Upper connecting trace for left spiral
Circle(hole3, viaPadD),
Circle(hole4, padD),
// Lower connecting trace for right spiral
Circle(hole5, padD),
)
topMask := g.TopSolderMask()
topMask.Add(
// Lower connecting trace between two spirals
Circle(hole1, viaPadD),
Circle(hole2, padD),
// Upper connecting trace for left spiral
Circle(hole3, viaPadD),
Circle(hole4, padD),
// Lower connecting trace for right spiral
Circle(hole5, padD),
)
bottom := g.BottomCopper()
bottom.Add(
Polygon(Pt{0, 0}, true, botSpiralR, 0.0),
Polygon(Pt{0, 0}, true, botSpiralL, 0.0),
// Lower connecting trace between two spirals
Circle(hole1, viaPadD),
Circle(hole2, padD),
// Upper connecting trace for left spiral
Circle(hole3, viaPadD),
Circle(hole4, padD),
// Lower connecting trace for right spiral
Circle(hole5, padD),
)
bottomMask := g.BottomSolderMask()
bottomMask.Add(
// Lower connecting trace between two spirals
Circle(hole1, viaPadD),
Circle(hole2, padD),
// Upper connecting trace for left spiral
Circle(hole3, viaPadD),
Circle(hole4, padD),
// Lower connecting trace for right spiral
Circle(hole5, padD),
)
drill := g.Drill()
drill.Add(
// Lower connecting trace between two spirals
Circle(hole1, viaDrillD),
Circle(hole2, drillD),
// Upper connecting trace for left spiral
Circle(hole3, viaDrillD),
Circle(hole4, drillD),
// Lower connecting trace for right spiral
Circle(hole5, drillD),
)
outline := g.Outline()
r := 0.5*s.size + padD + *trace
outline.Add(
Arc(Pt{0, 0}, 0.5*s.size+padD, CircleShape, 1, 1, 0, 360, 0.1),
)
fmt.Printf("n=%v: (%.2f,%.2f)\n", *n, 2*r, 2*r)
if *fontName != "" {
pts := 48.0 * r / 139.18 // determined emperically
labelSize := 2.0
message := fmt.Sprintf(messageFmt, *trace, *gap, *n)
tss := g.TopSilkscreen()
tss.Add(
Text(0, 0.5*r, 1.0, message, *fontName, pts, &Center),
Text(hole1[0], hole1[1]-viaPadD, 1.0, "TR/BR", *fontName, labelSize, &TopCenter),
Text(hole2[0]+padD, hole2[1], 1.0, "BR/TL", *fontName, labelSize, &CenterLeft),
Text(hole3[0], hole3[1]+viaPadD, 1.0, "TL/BL", *fontName, labelSize, &BottomCenter),
Text(hole4[0]-padD, hole4[1], 1.0, "BL", *fontName, labelSize, &CenterRight),
Text(hole5[0]-padD, hole5[1], 1.0, "TR", *fontName, labelSize, &CenterRight),
)
}
if err := g.WriteGerber(); err != nil {
log.Fatal(err)
}
fmt.Println("Done.")
if *view {
viewer.Gerber(g, true)
}
}
func genPt(xScale, angle, halfTW, offset float64) Pt {
r := (angle + *trace + *gap) / (3 * math.Pi)
x := (r + halfTW) * math.Cos(angle+offset)
y := (r + halfTW) * math.Sin(angle+offset)
return Point(x*xScale, y)
}
type spiral struct {
startAngle float64
endAngle float64
size float64
}
func newSpiral() *spiral {
startAngle := 1.5 * math.Pi
endAngle := 2*math.Pi + float64(*n)*2.0*math.Pi
p1 := genPt(1.0, endAngle, *trace*0.5, 0)
size := 2 * math.Abs(p1[0])
p2 := genPt(1.0, endAngle, *trace*0.5, math.Pi)
xl := 2 * math.Abs(p2[0])
if xl > size {
size = xl
}
return &spiral{
startAngle: startAngle,
endAngle: endAngle,
size: size,
}
}
func (s *spiral) genSpiral(xScale, offset, trimY float64) []Pt {
halfTW := *trace * 0.5
var pts []Pt
steps := int(0.5 + (s.endAngle-s.startAngle) / *step)
for i := 0; i < steps; i++ {
angle := s.startAngle + *step*float64(i)
pts = append(pts, genPt(xScale, angle, halfTW, offset))
}
var trimYsteps int
if trimY > 0 {
trimYsteps++
for {
if pts[len(pts)-trimYsteps][1] > trimY {
break
}
trimYsteps++
}
lastStep := len(pts) - trimYsteps
trimYsteps--
pts = pts[0 : lastStep+1]
pts = append(pts, Pt{pts[lastStep][0], trimY})
angle := s.startAngle + *step*float64(steps-1-trimYsteps)
nextP := genPt(xScale, angle, -halfTW, offset)
pts = append(pts, Pt{nextP[0], trimY})
} else {
pts = append(pts, genPt(xScale, s.endAngle, halfTW, offset))
pts = append(pts, genPt(xScale, s.endAngle, -halfTW, offset))
}
for i := steps - 1 - trimYsteps; i >= 0; i-- {
angle := s.startAngle + *step*float64(i)
pts = append(pts, genPt(xScale, angle, -halfTW, offset))
}
pts = append(pts, pts[0])
return pts
}