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/check-fabrication-with-encoder.mjs

/**
 * Verifies dist/fabrication-with-encoder/circuit.json: the released controller with the encoder
 * joined on as a snap-off piece (scripts/build-fabrication-with-encoder.mjs).
 *
 * Establishes that the controller is unchanged, the encoder is its own build moved into place,
 * the joined board passes every tscircuit check exactly as the two boards do separately, and the
 * break-off geometry is what circuit/breakoff-geometry.ts describes.
 */
import assert from "node:assert/strict"
import { readFileSync, writeFileSync } from "node:fs"
import { resolve } from "node:path"
import * as checks from "@tscircuit/checks"
import { bounds, bridge, bridges, controller as controllerEdges, encoderOrigin, outline, perforations, slotBetweenBridges } 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 joined = read("dist/fabrication-with-encoder/circuit.json")
const near = (a, b) => Math.abs(a - b) < 1e-6

// 1. One board, no panel, no recorded errors.
const boards = joined.filter(row => row.type === "pcb_board")
assert.equal(boards.length, 1, "The joined file must describe one board")
assert.equal(joined.filter(row => row.type === "pcb_panel").length, 0, "The joined file must not be a panel")
assert.equal(joined.filter(row => String(row.type).endsWith("_error")).length, 0, "The joined file records errors")
const [board] = boards
assert.deepEqual(board.outline, outline, "Board outline differs from circuit/breakoff-geometry.ts")
assert.ok(board.num_layers === 4 && board.thickness === 1.6, "Expected the controller's 4-layer, 1.6 mm stack")
assert.ok(near(board.width, bounds.right - bounds.left) && near(board.height, bounds.top - bounds.bottom), "Board size does not match its outline")

// 2. Every controller record is carried over unchanged, in order; only its board record gains the outline.
controller.forEach((row, i) => {
  if (row.type === "pcb_board") {
    const { outline: _o, center: _c, width: _w, height: _h, ...rest } = joined[i]
    const { center: _c2, width: _w2, height: _h2, ...original } = row
    assert.deepEqual(rest, original, "Controller board settings changed")
  } else assert.deepEqual(joined[i], row, `Controller record ${i} (${row.type}) changed`)
})

// 3. The encoder's copper is its own build, moved by encoderOrigin and nothing else.
const round = value => Math.round(value * 1e6) / 1e6
const shifted = (value, key = "") => {
  if (Array.isArray(value)) return value.map(item => shifted(item, key))
  if (!value || typeof value !== "object") return typeof value === "number" ? round(value) : value
  const out = Object.fromEntries(Object.entries(value).filter(([k]) => !k.endsWith("_id") && !k.endsWith("_ids") && !k.endsWith("_key"))
    .map(([k, v]) => [k, shifted(v, k)]))
  if (typeof value.x === "number" && typeof value.y === "number") { out.x = round(value.x + encoderOrigin.x); out.y = round(value.y + encoderOrigin.y) }
  return out
}
const plain = value => {
  if (Array.isArray(value)) return value.map(plain)
  if (!value || typeof value !== "object") return typeof value === "number" ? round(value) : value
  return Object.fromEntries(Object.entries(value).filter(([k]) => !k.endsWith("_id") && !k.endsWith("_ids") && !k.endsWith("_key")).map(([k, v]) => [k, plain(v)]))
}
const copperTypes = ["pcb_smtpad", "pcb_via", "pcb_trace", "pcb_copper_pour", "pcb_hole", "pcb_solder_paste"]
const encoderRows = joined.filter(row => Object.entries(row).some(([k, v]) => k === `${row.type}_id` && String(v).startsWith("enc_")))
const encoderCopper = {}
for (const type of copperTypes) {
  const expected = encoder.filter(row => row.type === type).map(row => JSON.stringify(shifted(row))).sort()
  const actual = encoderRows.filter(row => row.type === type).map(row => JSON.stringify(plain(row))).sort()
  assert.deepEqual(actual, expected, `Encoder ${type} differs from its own build moved into place`)
  encoderCopper[type] = actual.length
}

// 4. Every exported tscircuit check gives, on the joined board, the sum of the two boards on their own.
const clean = rows => rows.filter(row => !String(row.type).endsWith("_error"))
const names = Object.keys(checks).filter(name => /^check/.test(name) && typeof checks[name] === "function").sort()
const run = async rows => Object.fromEntries(await Promise.all(names.map(async name => {
  const result = await checks[name](rows)
  return [name, Array.isArray(result) ? result : []]
})))
const [alone, encoderAlone, together] = await Promise.all([run(clean(controller)), run(clean(encoder)), run(clean(joined))])
const differences = []
const table = {}
for (const name of names) {
  const expected = alone[name].length + encoderAlone[name].length
  if (together[name].length !== expected) differences.push({ check: name, expected, found: together[name].length, sample: together[name].slice(0, 3).map(e => e.message) })
  if (together[name].length) table[name] = { controller: alone[name].length, encoder: encoderAlone[name].length, joined: together[name].length }
}
assert.deepEqual(differences, [], "Joined board check results differ from the two boards on their own")

// 5. Break-off geometry: two 3 mm bridges, one slot between them, two rows of four perforations each.
const slot = joined.filter(row => row.type === "pcb_cutout" && row.pcb_cutout_id === "breakoff_slot")
assert.equal(slot.length, 1, "Expected one slot between the bridges")
assert.ok(near(slot[0].center.x - slot[0].width / 2, bridges[0].x1) && near(slot[0].center.x + slot[0].width / 2, bridges[1].x0), "Slot does not span the gap between the bridges")
assert.ok(near(slot[0].center.y + slot[0].height / 2, controllerEdges.bottom) && near(slot[0].height, slotBetweenBridges.y1 - slotBetweenBridges.y0), "Slot does not reach both boards")
for (const { x0, x1 } of bridges) assert.ok(near(x1 - x0, bridge.width), "Bridge width changed")
const holes = joined.filter(row => row.type === "pcb_hole" && String(row.pcb_hole_id).startsWith("breakoff_perforation_"))
assert.equal(holes.length, perforations.length, "Perforation count changed")
assert.equal(holes.length, bridges.length * 2 * bridge.holesPerRow, "Expected two rows of perforations per bridge")
for (const hole of holes) {
  assert.ok(bridges.some(({ x0, x1 }) => hole.x - bridge.holeDiameter / 2 >= x0 - 1e-6 && hole.x + bridge.holeDiameter / 2 <= x1 + 1e-6), "Perforation lies outside a bridge")
  assert.ok(hole.y + bridge.holeDiameter / 2 <= controllerEdges.bottom + 1e-6, "Perforation cuts into the controller")
}

// 6. Nothing shared by accident: ids, net names, via type.
const seen = new Set()
for (const row of joined) {
  const id = row[`${row.type}_id`]
  if (typeof id !== "string") continue
  assert.ok(!seen.has(`${row.type}:${id}`), `Duplicate id ${id}`)
  seen.add(`${row.type}:${id}`)
}
assert.equal(together.checkSameNameNetsAreConnected.length, 0, "The two boards share a net name")
assert.ok(joined.filter(row => row.type === "pcb_via").every(via => via.layers.length === board.num_layers), "Only through vias are allowed")

const result = {
  status: "PASS", singleBoard: true, recordedErrors: 0,
  controllerRecordsUnchanged: controller.length - 1, encoderCopperMovedOnly: encoderCopper,
  checksEqualSumOfBothBoards: names.length, nonZeroChecks: table,
  bridges: bridges.length, perforations: holes.length, boardSizeMm: [board.width, board.height],
}
writeFileSync(resolve(root, "dist/fabrication-with-encoder/fabrication-check.json"), JSON.stringify(result, null, 2) + "\n")
console.log(`PASS: one board, no errors; ${controller.length - 1} controller records unchanged; encoder copper only moved; ${names.length} tscircuit checks equal the two boards on their own; ${bridges.length} bridges, ${holes.length} perforations`)