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.

enclosure/export-uv-labels.ts

import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises"
import { createHash } from "node:crypto"
import { resolve } from "node:path"
import opentype from "opentype.js"
import SVGPathCommander from "svg-path-commander"
import { Resvg } from "@resvg/resvg-js"
import { body, buttons, buzzerVents, design as d, pinholeRefs, roofVents } from "./dimensions"
import { uvLabels, uvGraphics, encoderPortY } from "./uv-artwork"

// DIN Alternate Bold gives the UV artwork consistent instrument-panel lettering.
// No font is embedded or redistributed: the finished SVG contains only paths.
const fontPath = process.env.UV_LABEL_FONT ?? "/System/Library/Fonts/Supplemental/DIN Alternate Bold.ttf"
const fontBytes = await readFile(fontPath)
const font = opentype.parse(fontBytes.buffer.slice(fontBytes.byteOffset, fontBytes.byteOffset + fontBytes.byteLength))
const fontName = font.names.fullName.en
if (fontName !== "DIN Alternate Bold") throw Error(`Expected DIN Alternate Bold, found ${fontName}; review any font substitution explicitly`)
const x0 = body.x - body.width / 2, y1 = body.y + body.depth / 2
const f = (v: number) => Number(v.toFixed(4))
const esc = (s: string) => s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll('"', "&quot;")
const color = "#181b1e"
const labels = uvLabels.map((l, i) => {
  let advance = 0
  const glyphs: { glyph: any; x: number }[] = []
  font.forEachGlyph(l.text, 0, 0, l.height, {}, (glyph: any, x: number) => {
    const trackedX = x + glyphs.length * l.tracking
    advance = trackedX + glyph.advanceWidth * l.height / font.unitsPerEm
    glyphs.push({ glyph, x: trackedX })
  })
  let x = l.x - x0 - (l.center ? advance / 2 : 0), y = y1 - l.y
  const makePath = () => glyphs.map(g => g.glyph.getPath(x + g.x, y, l.height).toPathData(4)).join(" ")
  let path = makePath(), b = SVGPathCommander.getPathBBox(path)
  // Center visible ink, not the font advance box with unequal side bearings.
  if (l.center) x += l.x - x0 - (b.x + b.width / 2)
  if (l.centerY !== undefined) y += y1 - l.centerY - (b.y + b.height / 2)
  path = makePath(); b = SVGPathCommander.getPathBBox(path)
  return { text: l.text, fontSizeMm: l.height, trackingMm: l.tracking, x: f(x), baselineY: f(y),
    bounds: { x: b.x, y: b.y, width: b.width, height: b.height },
    markup: `<path id="label-${i + 1}" d="${path}"><title>${esc(l.text)}</title></path>` }
})
const graphics = uvGraphics(x0, y1).map((g, i) => {
  let path = g.path, b = SVGPathCommander.getPathBBox(path)
  if (g.name === "power icon") {
    const off = labels.find(l => l.text === "OFF")!.bounds
    const on = labels.find(l => l.text === "ON")!.bounds
    const targetX = off.x + off.width / 2
    const targetY = (off.y + off.height + on.y) / 2
    path = new SVGPathCommander(path, { round: 4 }).transform({ translate: [targetX-b.cx, targetY-b.cy] }).toString()
    b = SVGPathCommander.getPathBBox(path)
  }
  return { text: g.name, bounds: { x: b.x, y: b.y, width: b.width, height: b.height },
    markup: `<path id="graphic-${i+1}" d="${path}"><title>${esc(g.name)}</title></path>` }
})
const artwork = [...labels, ...graphics]
const ink = `<g fill="${color}">\n${artwork.map(l => l.markup).join("\n")}\n</g>`
const svg = (content: string, title: string, width = body.width, height = body.depth) =>
  `<svg xmlns="http://www.w3.org/2000/svg" width="${f(width)}mm" height="${f(height)}mm" viewBox="0 0 ${f(width)} ${f(height)}"><title>${esc(title)}</title>\n${content}\n</svg>\n`
const printSvg = svg(ink, "BLDC UV print ink — outlined dark charcoal lettering — 1:1 — top view, not mirrored")

// Nominal top-view checks only. They do not qualify ink adhesion, a printed lid,
// printer registration, or the existing enclosure's mechanical fit.
type Rect = { name: string; x: number; y: number; width: number; height: number }
const rects: Rect[] = [
  { name: "screen opening", x: d.screen.upperX - d.screen.upperWidth / 2 - x0,
    y: y1 - d.screen.y - d.screen.upperDepth / 2, width: d.screen.upperWidth, height: d.screen.upperDepth },
  ...[...roofVents, ...buzzerVents].map((v, i) => ({ name: `slot ${i + 1}`, x: v.x - v.width / 2 - x0,
    y: y1 - v.y - v.depth / 2, width: v.width, height: v.depth })),
]
const circles = [
  ...buttons.map(b => ({ name: b.label, x: b.x - x0, y: y1 - b.y,
    r: pinholeRefs.includes(b.ref) ? d.pinhole.leadInRadius : d.button.boreRadius })),
  { name: "installed speed knob", x: d.knob.x - x0, y: y1 - d.knob.y, r: d.knob.capRadius },
  { name: "LED", x: d.led.x - x0, y: y1 - d.led.y, r: d.led.radius },
]
const gaps: { label: string; feature: string; gapMm: number }[] = []
for (const l of artwork) {
  const b = l.bounds, right = b.x + b.width, bottom = b.y + b.height
  const edgeGap = Math.min(b.x, b.y, body.width - right, body.depth - bottom)
  if (edgeGap < 0) throw Error(`${l.text} extends outside the lid`)
  gaps.push({ label: l.text, feature: "lid bounding rectangle", gapMm: edgeGap })
  for (const c of circles) {
    const gap = Math.hypot(Math.max(b.x - c.x, 0, c.x - right), Math.max(b.y - c.y, 0, c.y - bottom)) - c.r
    if (gap <= 0) throw Error(`${l.text} bounding box overlaps ${c.name}`)
    gaps.push({ label: l.text, feature: c.name, gapMm: gap })
  }
  for (const r of rects) {
    const gap = Math.hypot(Math.max(b.x - r.x - r.width, r.x - right, 0), Math.max(b.y - r.y - r.height, r.y - bottom, 0))
    if (gap <= 0) throw Error(`${l.text} bounding box overlaps ${r.name}`)
    gaps.push({ label: l.text, feature: r.name, gapMm: gap })
  }
}
const inkBounds = {
  x: Math.min(...artwork.map(l => l.bounds.x)), y: Math.min(...artwork.map(l => l.bounds.y)),
  right: Math.max(...artwork.map(l => l.bounds.x + l.bounds.width)), bottom: Math.max(...artwork.map(l => l.bounds.y + l.bounds.height)),
}
for (let i = 0; i < artwork.length; i++) for (let j = i + 1; j < artwork.length; j++) {
  const a = artwork[i].bounds, b = artwork[j].bounds
  if (Math.min(a.x + a.width, b.x + b.width) > Math.max(a.x, b.x) &&
      Math.min(a.y + a.height, b.y + b.height) > Math.max(a.y, b.y)) {
    throw Error(`Artwork bounding boxes overlap: ${artwork[i].text} / ${artwork[j].text}`)
  }
}
const minGap = gaps.reduce((a, b) => a.gapMm < b.gapMm ? a : b)
const off = labels.find(l => l.text === "OFF")!.bounds, on = labels.find(l => l.text === "ON")!.bounds
const power = graphics.find(g => g.text === "power icon")!.bounds
const encoder = labels.find(l => l.text === "ENCODER")!.bounds
const alignment = {
  powerXErrorMm: Math.abs(power.x+power.width/2 - off.x-off.width/2),
  onOffXErrorMm: Math.abs(on.x+on.width/2 - off.x-off.width/2),
  powerUpperGapMm: power.y-off.y-off.height, powerLowerGapMm: on.y-power.y-power.height,
  encoderPortCenterY: encoderPortY,
  encoderCenterYErrorMm: Math.abs(y1-encoderPortY-encoder.y-encoder.height/2),
}
if (Math.max(alignment.powerXErrorMm, alignment.onOffXErrorMm, alignment.encoderCenterYErrorMm,
  Math.abs(alignment.powerUpperGapMm-alignment.powerLowerGapMm)) > .005) throw Error("UV icon/port alignment check failed")
const report = {
  status: "PASS", scope: "Outlined label bounds versus nominal top openings, pinholes and installed knob; no physical qualification",
  panelMm: { width: f(body.width), height: f(body.depth) }, color, labelCount: labels.length, graphicCount: graphics.length,
  font: { name: fontName, sha256: createHash("sha256").update(fontBytes).digest("hex") },
  printSvgSha256: createHash("sha256").update(printSvg).digest("hex"),
  inkBoundsMm: { x: f(inkBounds.x), y: f(inkBounds.y), width: f(inkBounds.right - inkBounds.x), height: f(inkBounds.bottom - inkBounds.y) },
  minimumConservativeGapMm: { ...minGap, gapMm: f(minGap.gapMm) },
  alignment: Object.fromEntries(Object.entries(alignment).map(([k,v])=>[k,f(v)])),
  labels: labels.map(({ markup, ...l }) => ({ ...l, bounds: Object.fromEntries(Object.entries(l.bounds).map(([k, v]) => [k, f(v)])) })),
  graphics: graphics.map(({ markup, ...g }) => ({ ...g, bounds: Object.fromEntries(Object.entries(g.bounds).map(([k, v]) => [k, f(v)])) })),
}

const holes = [
  ...rects.map(r => `<rect x="${f(r.x)}" y="${f(r.y)}" width="${r.width}" height="${r.height}" rx=".4"/>`),
  ...buttons.map(b => `<circle cx="${f(b.x - x0)}" cy="${f(y1 - b.y)}" r="${pinholeRefs.includes(b.ref) ? d.pinhole.leadInRadius : d.button.boreRadius}"/>`),
  `<circle cx="${f(d.knob.x - x0)}" cy="${f(y1 - d.knob.y)}" r="${d.knob.openingRadius}"/>`,
  `<circle cx="${f(d.led.x - x0)}" cy="${f(y1 - d.led.y)}" r="${d.led.radius}"/>`,
].join("\n")
const referenceSvg = svg(`
<rect width="100%" height="100%" fill="#f6f7f8"/>
<g font-family="Arial,sans-serif" fill="#343b43">
<text x="8" y="7" font-size="3" font-weight="bold">UV LABEL ALIGNMENT — REFERENCE ONLY</text>
<text x="8" y="12" font-size="2.1">Top view · ${f(body.width)} × ${f(body.depth)} mm lid · do not mirror</text>
</g>
<g transform="translate(8 18)">
<rect width="${f(body.width)}" height="${f(body.depth)}" rx="3" fill="#e4e8e2" stroke="#909b94" stroke-width=".2"/>
<g fill="#87949a">${holes}</g>
<circle cx="${f(d.knob.x - x0)}" cy="${f(y1 - d.knob.y)}" r="${d.knob.capRadius}" fill="none" stroke="#a89561" stroke-width=".2" stroke-dasharray="1 .6"/>
${ink}
</g>
<g font-family="Arial,sans-serif" fill="#343b43" font-size="2">
<text x="8" y="${f(body.depth + 24)}">Gray: openings and pinholes. Dashed circle: installed knob clearance.</text>
<text x="8" y="${f(body.depth + 28)}">Print only panel-uv-print.svg. This sheet includes guides and background.</text>
</g>`, "UV label alignment reference — DO NOT PRINT ON LID", body.width + 16, body.depth + 33)
const preview = new Resvg(referenceSvg, { fitTo: { mode: "width", value: 1200 }, font: { fontFiles: [fontPath] } }).render().asPng()
// Top-view presentation of the actual artwork with illustrative installed
// controls. These hardware shapes and sample screen content are never print ink.
const screen = rects[0], knobX = d.knob.x - x0, knobY = y1 - d.knob.y
const controlPreview = buttons.map(b => {
  const x = f(b.x-x0), y = f(y1-b.y)
  // BOOT and RESET: a 2 mm pinhole with its lead-in, no cap.
  if (pinholeRefs.includes(b.ref)) return `<circle cx="${x}" cy="${y}" r="${d.pinhole.leadInRadius}" fill="#cfd4cf"/>
<circle cx="${x}" cy="${y}" r="${d.pinhole.radius}" fill="#1c2124"/>`
  return `<circle cx="${x}" cy="${y+.25}" r="3.2" fill="#b5bcb8"/>
<circle cx="${x}" cy="${y}" r="3.05" fill="${b.ref==="SW3"?"#b97e24":"#242b2e"}"/>
<circle cx="${x}" cy="${y-.16}" r="2.75" fill="${b.ref==="SW3"?"url(#amber)":"url(#button)"}"/>`
}).join("\n")
const appearanceSvg = svg(`
<defs>
<linearGradient id="lid" x2="0" y2="1"><stop stop-color="#f0f0e9"/><stop offset="1" stop-color="#e4e7e0"/></linearGradient>
<linearGradient id="amber" x2=".5" y2="1"><stop stop-color="#f3c773"/><stop offset="1" stop-color="#e5a440"/></linearGradient>
<linearGradient id="button" x2="0" y2="1"><stop stop-color="#535b60"/><stop offset="1" stop-color="#3a4145"/></linearGradient>
</defs>
<rect width="100%" height="100%" fill="#f6f7f8"/>
<g font-family="Arial,sans-serif" fill="#343b43">
<text x="8" y="7" font-size="3" font-weight="bold">BLDC · REVISED UV LABEL LAYOUT</text>
<text x="8" y="12" font-size="2.1">Dark charcoal on off-white · actual label and control positions</text>
</g>
<g transform="translate(8 18)">
<rect y="1" width="${f(body.width)}" height="${f(body.depth)}" rx="3.2" fill="#c8ceca"/>
<rect width="${f(body.width)}" height="${f(body.depth)}" rx="3" fill="url(#lid)" stroke="#a3aba5" stroke-width=".18"/>
${ink}
<rect x="${f(screen.x)}" y="${f(screen.y)}" width="${screen.width}" height="${screen.height}" rx=".6" fill="#4d5654"/>
<rect x="${f(screen.x+.65)}" y="${f(screen.y+.65)}" width="${screen.width-1.3}" height="${screen.height-1.3}" rx=".2" fill="#0d1516"/>
<g font-family="Arial,sans-serif" text-anchor="middle" fill="#e2ece9">
<text x="${f(screen.x+screen.width/2)}" y="${f(screen.y+8)}" font-size="3.7" font-weight="bold">READY</text>
<text x="${f(screen.x+screen.width/2)}" y="${f(screen.y+12)}" font-size="2">SPEED 0%</text>
</g>
<rect x="${f(screen.x+5)}" y="${f(screen.y+15)}" width="${screen.width-10}" height="2.6" fill="none" stroke="#c8d8d2" stroke-width=".25"/>
<circle cx="${f(knobX)}" cy="${f(knobY+.4)}" r="6.65" fill="#bdc2b7"/>
<circle cx="${f(knobX)}" cy="${f(knobY)}" r="6.5" fill="#b47f29"/>
<circle cx="${f(knobX)}" cy="${f(knobY-.15)}" r="6.15" fill="url(#amber)"/>
<path d="M${f(knobX)} ${f(knobY+3)} V${f(knobY+5.5)}" stroke="#a67730" stroke-width=".3" stroke-linecap="round"/>
${controlPreview}
<circle cx="${f(d.led.x-x0)}" cy="${f(y1-d.led.y)}" r="${d.led.radius}" fill="#61746c" stroke="#a9b4a8" stroke-width=".12"/>
<g fill="#59675f">${rects.slice(1).map(r=>`<rect x="${f(r.x)}" y="${f(r.y)}" width="${r.width}" height="${r.height}" rx=".4"/>`).join("")}</g>
<rect x="${f(body.width-.1)}" y="${f(y1-encoderPortY-2.3)}" width="1.5" height="4.6" rx=".4" fill="#50585a"/>
${["#a8595d", "#d3b95c", "#58a589", "#5899b7", "#737a7b"].map((c,i)=>`<path d="M${f(body.width+1.3)} ${f(y1-encoderPortY+(i-2)*.7)} H${f(body.width+4)}" stroke="${c}" stroke-width=".45"/>`).join("")}
</g>
<g font-family="Arial,sans-serif" fill="#535c62" font-size="2">
<text x="8" y="${f(body.depth+25)}">Layout preview only. Screen content and control shading are illustrative.</text>
<text x="8" y="${f(body.depth+29)}">Print only the dark artwork. ENCODER arrow aligns with the side connector.</text>
</g>`, "Revised UV labels on off-white enclosure — illustrative top-view preview", body.width+16, body.depth+34)
const appearancePng = new Resvg(appearanceSvg, { fitTo: { mode: "width", value: 1400 } }).render().asPng()
const outputs: Record<string, string | Uint8Array> = {
  "panel-uv-print.svg": printSvg,
  "panel-uv-reference.svg": referenceSvg,
  "panel-uv-reference.png": preview,
  "panel-uv-report.json": JSON.stringify(report, null, 2) + "\n",
  "panel-uv-preview.svg": appearanceSvg,
  "panel-uv-preview.png": appearancePng,
}
// These 2D files do not regenerate or certify the mechanical/PCB outputs.
for (const dir of ["../assets/enclosure", "../dist/assets/enclosure"]) {
  const out = resolve(import.meta.dir, dir)
  await mkdir(out, { recursive: true })
  for (const [name, bytes] of Object.entries(outputs)) await writeFile(resolve(out, name), bytes)
}
for (const name of ["UV-PRINTING.md", "README.md"]) {
  await copyFile(resolve(import.meta.dir, "../assets/enclosure", name), resolve(import.meta.dir, "../dist/assets/enclosure", name))
}
console.log(JSON.stringify({ labelCount: report.labelCount, panelMm: report.panelMm, inkBoundsMm: report.inkBoundsMm,
  minimumConservativeGapMm: report.minimumConservativeGapMm, output: "assets/enclosure/panel-uv-print.svg" }, null, 2))