ShiboSoftwareDev/nema8-20mm-dual-usbc-stepper-controller

A compact 4-layer USB‑C PD/data controller board using a CH32X035 MCU and TMC2209 driver to power and control a NEMA‑8 stepper motor, with 3.3‑V regulation, USB ESD/reverse-polarity/TVS protection, current sensing, and boot control.

Version
1.0.12
License
unset
Stars
0

scripts/search-current-via.mjs

import fs from "node:fs"
import { runAllRoutingChecks } from "@tscircuit/checks"

const viaId = process.argv[2]
const radius = Number(process.argv[3] ?? 1)
const step = Number(process.argv[4] ?? 0.1)
if (!viaId) throw new Error("usage: node search-current-via.mjs <pcb-via-id> [radius] [step]")

const circuit = JSON.parse(fs.readFileSync("dist/index/circuit.json", "utf8"))
  .filter((element) => !String(element.type).endsWith("error"))
const via = circuit.find((element) => element.type === "pcb_via" && element.pcb_via_id === viaId)
if (!via) throw new Error(`missing ${viaId}`)
const same = (point) => Math.abs(point.x - via.x) < 1e-6 && Math.abs(point.y - via.y) < 1e-6
const moved = (x, y) => circuit.map((element) => {
  if (element.type === "pcb_via" && element.pcb_via_id === viaId) return { ...element, x, y }
  if (element.type !== "pcb_trace" || element.pcb_trace_id !== via.pcb_trace_id) return element
  return { ...element, route: element.route.map((point) => same(point) ? { ...point, x, y } : point) }
})
const targetCount = (errors) => errors.filter((error) =>
  error.pcb_via_id === viaId || error.pcb_via_ids?.includes(viaId) || error.pcb_pad_ids?.includes(viaId) ||
  error.pcb_trace_id === via.pcb_trace_id).length

const startErrors = await runAllRoutingChecks(circuit)
if (process.argv.length >= 7) {
  const x = Number(process.argv[5]); const y = Number(process.argv[6])
  const errors = await runAllRoutingChecks(moved(x, y))
  console.log(errors.length)
  console.log(errors.filter((error) => error.type !== "pcb_trace_warning").map((error) => error.message).join("\n"))
  process.exit(0)
}
let best = { x: via.x, y: via.y, errors: startErrors }
console.log(`start\t${startErrors.length}\t${targetCount(startErrors)}\t${via.pcb_trace_id}\t${via.x},${via.y}`)
for (let dx = -radius; dx <= radius + 1e-9; dx += step) {
  for (let dy = -radius; dy <= radius + 1e-9; dy += step) {
    const x = Number((via.x + dx).toFixed(4))
    const y = Number((via.y + dy).toFixed(4))
    if (Math.abs(x) > 9.4 || Math.abs(y) > 9.4) continue
    let errors
    try { errors = await runAllRoutingChecks(moved(x, y)) } catch { continue }
    if (targetCount(errors) < targetCount(best.errors) ||
        (targetCount(errors) === targetCount(best.errors) && errors.length < best.errors.length)) {
      best = { x, y, errors }
      console.log(`best\t${errors.length}\t${targetCount(errors)}\t${x},${y}`)
    }
  }
}
console.log(`winner\t${best.errors.length}\t${targetCount(best.errors)}\t${best.x},${best.y}`)
console.log(best.errors.map((error) => error.message).join("\n"))