Skip to content

Instantly share code, notes, and snippets.

@jordanluyke
Last active April 17, 2023 20:18
Show Gist options
  • Save jordanluyke/01231712ca9251dd12a1d506f8e88f86 to your computer and use it in GitHub Desktop.
Save jordanluyke/01231712ca9251dd12a1d506f8e88f86 to your computer and use it in GitHub Desktop.
KiCad coil maker
const { writeFile } = require("fs/promises")
const net = 1
const width = 36
const spacing = 2
const turns = 3
const centerX = 159.5
const centerY = 150
const layer = "B.Cu"
const innerDiameter = 50
const degreeDelta = 1
const startOffsetDegrees = 180
const decimals = 3
const tau = 2 * Math.PI
const hypotenuseIncrement = (spacing + width) * Math.abs(degreeDelta) / 360
main()
async function main() {
let segments = []
let xPrev = null
let yPrev = null
let hypotenuse = innerDiameter / 2
let distance = 0
for(let turn = 0; turn < turns; turn++) {
for(let degree = 0; turn == turns - 1 ? Math.abs(degree) <= 360 : Math.abs(degree) < 360; degree += degreeDelta) {
const radiansX = Math.cos(degreesToRadians(degree + startOffsetDegrees))
const radiansY = Math.sin(degreesToRadians(degree + startOffsetDegrees))
const x = radiansX * hypotenuse
const y = radiansY * hypotenuse
if(xPrev != null) {
const startX = round(centerX + xPrev, decimals)
const startY = round(centerY - yPrev, decimals)
const endX = round(centerX + x, decimals)
const endY = round(centerY - y, decimals)
distance += Math.sqrt((endX - startX) ** 2 + (endY - startY) ** 2)
segments.push(`(segment (start ${startX} ${startY}) (end ${endX} ${endY}) (width ${width}) (layer ${layer}) (net ${net}))`)
}
xPrev = x
yPrev = y
hypotenuse += hypotenuseIncrement
}
}
console.log("Distance:", round(distance, 1))
await writeFile("out.txt", segments.join("\n"))
}
function degreesToRadians(degrees) {
return degrees / 360 * tau
}
function round(v, d) {
return parseFloat(Math.round(Number(v).toFixed(d + 1) + "e" + d) + "e-" + d)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment