MustafaMulla29/rp2040-bldc-motor-controller

Hardware assembly metadata and PCB geometry for a controller board with a snap-off rotary-encoder section, plus four parallel 150 µF/50 V bulk electrolytic capacitors filtering the motor supply.

Version
2.0.40
License
unset
Stars
0

Use with the tscircuit CLI

tsci is the tscircuit CLI. Install it with npm install -g tscircuit.

tsci clone MustafaMulla29/rp2040-bldc-motor-controller  # download the package files into ./rp2040-bldc-motor-controller
tsci add MustafaMulla29/rp2040-bldc-motor-controller    # add it to your project as @tsci/MustafaMulla29.rp2040-bldc-motor-controller

In a cloned or new (tsci init) project: tsci dev starts a local preview, tsci build outputs Circuit JSON, and tsci push publishes to tscircuit.com.

scripts/build-fabrication-with-encoder.mjs

/**
 * Joins the released controller and the encoder board into one fabrication file, with the encoder
 * as a snap-off piece (dist/fabrication-with-encoder/circuit.json).
 *
 * The two finished circuit JSON files are combined here instead of in a tscircuit <panel>:
 * @tscircuit/core 0.0.2320 runs a panel-level routing check with a stand-in board whose layer count
 * is fixed at 2, which reports a false error for every inner-layer via of a 4-layer board.
 * Joining finished files also means the controller is, by construction, the released one.
 *
 * The encoder is only moved (no rotation), its ids get a prefix, and the two board records become
 * one board with a single outline. Run `bun run build:encoder` first.
 */
import assert from "node:assert/strict"
import { mkdirSync, readFileSync, writeFileSync } from "node:fs"
import { resolve } from "node:path"
import { bounds, encoderOrigin, outline, perforations, slotBetweenBridges, bridge } from "../circuit/breakoff-geometry"

const root = resolve(import.meta.dirname, "..")
const read = path => JSON.parse(readFileSync(resolve(root, path), "utf8"))
const controller = read("dist/pcb/circuit.json")
const encoder = read("dist/encoder-board/circuit.json")
const errors = rows => rows.filter(row => String(row.type).endsWith("_error"))
assert.equal(errors(controller).length, 0, "Controller build has errors")
assert.equal(errors(encoder).length, 0, "Encoder build has errors")
const board = controller.find(row => row.type === "pcb_board")
const encoderBoard = encoder.find(row => row.type === "pcb_board")
for (const key of ["num_layers", "thickness", "material"]) {
  assert.equal(encoderBoard[key], board[key], `Encoder ${key} must match the controller: they are one physical board`)
}

const PREFIX = "enc_"
const isReference = key => key.endsWith("_id") || key === "subcircuit_connectivity_map_key"
const isReferenceList = key => key.endsWith("_ids")
/** Prefix every id the encoder defines or refers to, so nothing collides with the controller's ids. */
const rename = (value, key = "") => {
  if (Array.isArray(value)) return value.map(item => typeof item === "string" && isReferenceList(key) ? PREFIX + item : rename(item, key))
  if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, rename(v, k)]))
  if (typeof value === "string" && isReference(key)) {
    if (key.endsWith("pcb_board_id")) return board.pcb_board_id
    if (key === "source_board_id") return board.source_board_id
    return PREFIX + value
  }
  return value
}
/** Move every position of a PCB record. Sizes, rotations and model origins are not positions. */
const fixed = new Set(["rotation", "size", "model_origin_position"])
const move = (value, key = "") => {
  if (Array.isArray(value)) return value.map(item => move(item, key))
  if (!value || typeof value !== "object" || fixed.has(key)) return value
  const out = Object.fromEntries(Object.entries(value).map(([k, v]) => [k, move(v, k)]))
  if (typeof value.x === "number" && typeof value.y === "number") { out.x = value.x + encoderOrigin.x; out.y = value.y + encoderOrigin.y }
  return out
}
const dropped = type => type === "pcb_board" || type === "source_board" || type === "source_project_metadata"
  || type.startsWith("schematic_") || type.endsWith("_warning")
const encoderRows = encoder.filter(row => !dropped(row.type))
  .map(row => rename(row.type.startsWith("pcb_") || row.type === "cad_component" ? move(row) : row))

const subcircuit_id = board.subcircuit_id
const joinedBoard = {
  ...board, outline,
  center: { x: (bounds.left + bounds.right) / 2, y: (bounds.bottom + bounds.top) / 2 },
  width: bounds.right - bounds.left, height: bounds.top - bounds.bottom,
}
const breakoff = [
  { type: "pcb_cutout", pcb_cutout_id: "breakoff_slot", shape: "rect", rotation: 0, subcircuit_id,
    center: { x: (slotBetweenBridges.x0 + slotBetweenBridges.x1) / 2, y: (slotBetweenBridges.y0 + slotBetweenBridges.y1) / 2 },
    width: slotBetweenBridges.x1 - slotBetweenBridges.x0, height: slotBetweenBridges.y1 - slotBetweenBridges.y0 },
  ...perforations.map((p, i) => ({ type: "pcb_hole", pcb_hole_id: `breakoff_perforation_${i}`, pcb_component_id: null,
    hole_shape: "circle", hole_diameter: bridge.holeDiameter, x: p.x, y: p.y, is_covered_with_solder_mask: false, subcircuit_id })),
]
const joined = [...controller.map(row => row === board ? joinedBoard : row), ...encoderRows, ...breakoff]

const ids = new Set()
for (const row of joined) {
  const id = row[`${row.type}_id`]
  if (typeof id !== "string") continue
  assert.ok(!ids.has(`${row.type}:${id}`), `Duplicate id ${id}`)
  ids.add(`${row.type}:${id}`)
}
mkdirSync(resolve(root, "dist/fabrication-with-encoder"), { recursive: true })
writeFileSync(resolve(root, "dist/fabrication-with-encoder/circuit.json"), JSON.stringify(joined))
console.log(`Joined ${controller.length} controller records with ${encoderRows.length} encoder records and ${breakoff.length} break-off features`)