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

A compact 4-layer USB‑C PD stepper-motor controller PCB using a CH32X035 MCU, TMC2209 driver, dual USB‑C ports, power protection/regulation, ESD/TVS diodes, current-sense resistors, and NEMA‑8 motor connector.

Version
1.0.13
License
unset
Stars
0

scripts/search-r-usb-dp.mjs

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

const base = JSON.parse(fs.readFileSync("dist/index/circuit.json", "utf8"))
  .filter((element) => !String(element.type).endsWith("error"))
const component = base.find((element) => element.type === "pcb_component" && element.pcb_component_id === "pcb_component_6")
const pads = base.filter((element) => element.type === "pcb_smtpad" && element.pcb_component_id === component.pcb_component_id)
const ports = new Set(pads.map((pad) => pad.pcb_port_id))

const moved = (x, y) => {
  const dx = x - component.center.x; const dy = y - component.center.y
  return base.map((element) => {
    if (element.pcb_component_id === component.pcb_component_id) {
      if (element.type === "pcb_component") return { ...element, center: { x, y } }
      if (typeof element.x === "number" && typeof element.y === "number") return { ...element, x: element.x + dx, y: element.y + dy }
    }
    if (element.type === "pcb_trace") {
      return { ...element, route: element.route.map((point, index) => {
        const port = index === 0 ? point.start_pcb_port_id : index === element.route.length - 1 ? point.end_pcb_port_id : undefined
        return port && ports.has(port) ? { ...point, x: point.x + dx, y: point.y + dy } : point
      }) }
    }
    return element
  })
}

if (process.argv.length >= 4) {
  const errors = await runAllRoutingChecks(moved(Number(process.argv[2]), Number(process.argv[3])))
  console.log(errors.length); console.log(errors.map((e) => e.message).join("\n")); process.exit(0)
}

let best = { count: Infinity }
for (let x = -1; x <= 2 + 1e-9; x += 0.2) {
  for (let y = 1; y <= 4 + 1e-9; y += 0.2) {
    const cx = Number(x.toFixed(3)); const cy = Number(y.toFixed(3))
    const errors = await runAllRoutingChecks(moved(cx, cy))
    if (errors.length < best.count) {
      best = { count: errors.length, x: cx, y: cy, errors }
      console.log("best", best.count, best.x, best.y)
    }
    if (errors.length <= 6) {
      console.log(JSON.stringify({ count: errors.length, x: cx, y: cy }))
      console.log(errors.map((e) => e.message).join("\n")); process.exit(0)
    }
  }
}
console.log(JSON.stringify(best, null, 2))