mohan-bee/drone

This code defines the physical hardware layout and component placements for a drone's circuit board, including outlines, cutouts, and detailed PCB footprints for various electronic components like microcontrollers, sensors, connectors, and passive devices.

Version
1.0.11
License
unset
Stars
0

board/PcbPlacement.ts

import {
  kicadPlacements,
  type KicadPlacementReference,
} from "./KicadPlacement"

// These parts use a different zero-angle convention from their KiCad
// footprints. The corrections were measured by comparing the direction of
// matching numbered pads in the generated circuit JSON and the KiCad board.
const footprintRotationCorrections: Partial<
  Record<KicadPlacementReference, number>
> = {
  IC1: -90,
  SW1: -90,
  U7: -90,
  J3: 180,
  J5: -180,
  J6: 180,
  J8: -90,
  J9: -90,
  T1: 180,
  T2: 180,
  T3: 180,
  T4: 180,
}

// tscircuit normally positions a footprint by its geometric center, while
// several KiCad footprints use pin 1 or another mechanical datum as (0, 0).
// These offsets align the numbered copper pads, not merely the body centers.
const footprintOriginCorrections: Partial<
  Record<KicadPlacementReference, { x: number; y: number }>
> = {
  J8: { x: 0, y: -6.35 },
  J9: { x: 0, y: -6.35 },
}

// Minimal clearance adjustments for places where the tscircuit/JLC footprint
// courtyard is larger than the footprint used by the source KiCad board.
const clearanceCorrections: Partial<
  Record<KicadPlacementReference, { x: number; y: number }>
> = {
  // The JLCPCB K3 switch body is taller than the custom switch footprint used
  // in KiCad. Move it inward just enough to keep the complete body on copper.
  SW1: { x: 0, y: -1.8 },
  R2: { x: 7.95, y: -3.15 },
  J5: { x: -0.9, y: 0 },
  J3: { x: -0.5, y: 0 },
  C9: { x: -4.493, y: 24.804 },
  C10: { x: -18.664, y: 35.771 },
  C11: { x: -9.665, y: 16.572 },
  C7: { x: 0, y: 0.8 },
  C8: { x: 0.15, y: 0 },
  R10: { x: -0.18, y: 30.104 },
  U8: { x: -1.2, y: 0 },
  J8: { x: -1.2, y: 0 },
  J9: { x: -0.4, y: 0 },
  U4: { x: -1.238, y: -20.652 },
  U5: { x: -1.238, y: -21.132 },
  D2: { x: -1.739, y: 7.778 },
  C15: { x: 1.346, y: 21.933 },
  C16: { x: -2.279, y: -6.856 },
  D5: { x: -12.277, y: 27.502 },
  C18: { x: 1.868, y: -0.163 },
}

export const pcbPlacement = (reference: KicadPlacementReference) => {
  const placement = kicadPlacements[reference]
  const originCorrection = footprintOriginCorrections[reference] ?? {
    x: 0,
    y: 0,
  }
  const clearanceCorrection = clearanceCorrections[reference] ?? {
    x: 0,
    y: 0,
  }
  return {
    ...placement,
    pcbX: placement.pcbX + originCorrection.x + clearanceCorrection.x,
    pcbY: placement.pcbY + originCorrection.y + clearanceCorrection.y,
    pcbRotation:
      placement.pcbRotation + (footprintRotationCorrections[reference] ?? 0),
  }
}