imrishabh18/nema-23-stepper-controller

A 4-layer NEMA23 stepper motor controller PCB combining an RP2040, DRV8462 dual-phase driver, USB-C/28V power protection, current/voltage/temperature sensing, alarms, and optional magnetic encoder.

Version
1.4.27
License
unset
Stars
1

routing/thermal-via-policy.ts

import approvedThermalVias from './approved-thermal-vias.json'
import { viaSizes } from './via-sizes'

type PadBounds = {
  id: string
  x: number
  y: number
  width: number
  height: number
  cos: number
  sin: number
}

type ViaDrill = {
  id: string
  x: number
  y: number
  outerDiameter: number
  holeDiameter: number
}

function drillOverlapsPad(via: ViaDrill, pad: PadBounds) {
  // Undo the pad rotation, then find the closest point in the rectangle.
  const dx = via.x - pad.x
  const dy = via.y - pad.y
  const x = Math.abs(dx * pad.cos + dy * pad.sin)
  const y = Math.abs(-dx * pad.sin + dy * pad.cos)
  const distance = Math.hypot(Math.max(0, x - pad.width / 2), Math.max(0, y - pad.height / 2))
  return distance < via.holeDiameter / 2 - 1e-9
}

/** Core currently permits via-in-pad only at board scope. The user approved
 * exactly 18 DRIVER and 4 eFuse thermal vias, not signal/power pad drilling.
 * This gate checks drill holes against the router's rectangular pad obstacles
 * in both the CLI build and the browser preview.
 *
 * Router obstacles describe pad bounding boxes, so this gate is conservative
 * for rounded pads. All vias are through holes on this board, even where the
 * router omits a connected copper layer from its obstacle representation.
 */
export function assertThermalViaPolicy(input: any, traces: any[]) {
  const pads: PadBounds[] = []
  const vias: ViaDrill[] = []
  const obstacles = input.obstacles as any[]
  const sizes = Object.values(viaSizes)
  for (const obstacle of obstacles) {
    const metadata = obstacle.circuitJsonMetadata ?? {}
    if (metadata.pcb_smtpad_id || metadata.pcb_plated_hole_id) {
      const angle = (obstacle.ccwRotationDegrees ?? 0) * Math.PI / 180
      // Use each actual copper layer. A rectangle encloses oval/pill lands.
      for (const layer of obstacle.layers) pads.push({
        id: metadata.pcb_smtpad_id ?? `${metadata.pcb_plated_hole_id}_${layer}`,
        x: obstacle.center.x, y: obstacle.center.y,
        width: obstacle.width, height: obstacle.height,
        cos: Math.cos(angle), sin: Math.sin(angle),
      })
    } else if (metadata.pcb_via_id) {
      const size = sizes.find(s => Math.abs(s.outerDiameter - obstacle.width) < 1e-7)
      if (!size) throw new Error(`Unreviewed fixed via size: ${metadata.pcb_via_id}`)
      vias.push({
        id: metadata.pcb_via_id,
        x: obstacle.center.x, y: obstacle.center.y,
        outerDiameter: size.outerDiameter, holeDiameter: size.holeDiameter,
      })
    }
  }
  for (const trace of traces) for (const [index, point] of trace.route.entries()) {
    if (point.route_type !== 'via') continue
    if (!(point.via_hole_diameter >= viaSizes.small.holeDiameter)
      || !(point.via_diameter >= viaSizes.small.outerDiameter)
      || !(point.via_diameter > point.via_hole_diameter))
      throw new Error(`Invalid via dimensions or below board minimum: ${trace.pcb_trace_id}:${index}`)
    vias.push({
      id: `${trace.pcb_trace_id}_${index}`,
      x: point.x, y: point.y,
      outerDiameter: point.via_diameter, holeDiameter: point.via_hole_diameter,
    })
  }
  if (approvedThermalVias.length !== 22) throw new Error('Exactly 22 thermal vias are approved')
  for (const approved of approvedThermalVias) {
    const actual = vias.find(via => via.id === approved.viaId)
    if (!actual || Math.abs(actual.x - approved.x) > 1e-6 || Math.abs(actual.y - approved.y) > 1e-6
      || actual.outerDiameter !== approved.outerDiameter || actual.holeDiameter !== approved.holeDiameter)
      throw new Error(`Approved thermal-via geometry changed: ${approved.viaId}`)
    if (!pads.some(pad => pad.id === approved.padId))
      throw new Error(`Approved thermal pad missing: ${approved.padId}`)
  }
  const remaining = new Set(approvedThermalVias.map(via => `via_in_pad_${via.viaId}_${via.padId}`))
  const unexpected: string[] = []
  for (const via of vias) {
    for (const pad of pads) {
      if (!drillOverlapsPad(via, pad)) continue
      const id = `via_in_pad_${via.id}_${pad.id}`
      if (!remaining.delete(id)) unexpected.push(id)
    }
  }
  if (unexpected.length) throw new Error(`Unapproved via-in-pad intersections (${unexpected.length}):\n${unexpected.join('\n')}`)
  if (remaining.size) throw new Error(`Expected 22 approved thermal intersections; found ${22 - remaining.size}`)
}