imrishabh18/rp2040-motor-controller

An RP2040-based NEMA17 stepper-motor controller with USB-C programming and PD motor-power negotiation, DRV8825 dual H-bridge drive, current/temperature telemetry, protected power filtering, status RGB LED, buzzer alarm, and optional magnetic encoder.

Version
1.0.41
License
unset
Stars
3

scripts/refresh-routing-terminals.tsx

import { Circuit } from 'tscircuit';
import { readFileSync, writeFileSync } from 'node:fs';
import Board from '../index.circuit';

// Keep the routed net graph while moving terminals after a placement edit.
// This produces a draft only: native and independent DRC must be rerun.
const file=process.argv[2]??'dist/revision/route-plan.json';
const plan=JSON.parse(readFileSync(file,'utf8'));
const pointMap=(input:any)=>new Map<string,any>(input.connections.flatMap((c:any)=>c.pointsToConnect.filter((p:any)=>p.pcb_port_id).map((p:any)=>[p.pcb_port_id,p])));
const route=async(input:any)=>{
 const before=pointMap(plan.expectedInput),after=pointMap(input);
 const changed=[...after].filter(([id,p])=>before.has(id)&&Math.hypot(p.x-before.get(id).x,p.y-before.get(id).y)>1e-6);
 let moved=0;
 for(const trace of plan.traces){
  for(const [id,p] of changed){
   if(!trace.connectsTo?.includes(id))continue;
   const old=before.get(id);
   for(const index of [0,trace.route.length-1]){
    const end=trace.route[index];
    if(Math.hypot(end.x-old.x,end.y-old.y)<1e-5){end.x=p.x;end.y=p.y;moved++;}
   }
  }
 }
 console.log('Moved terminal records',moved,'across',changed.length,'changed PCB ports');
 plan.expectedInput=input;writeFileSync(file,JSON.stringify(plan,null,2));
 const handlers=new Map<string,Function>();
 return {on:(name:string,fn:Function)=>handlers.set(name,fn),stop(){},start(){handlers.get('complete')?.({traces:structuredClone(plan.traces)})}};
};
const circuit=new Circuit();circuit.add(<Board finalizeGroundPours={false} routingAlgorithm={route as any}/>);await circuit.renderUntilSettled();
const json=circuit.getCircuitJson();writeFileSync(file.replace(/route-plan\.json$/, 'circuit.json'),JSON.stringify(json,null,2));
const errors=json.filter(e=>e.type.endsWith('_error'));
console.log('Draft error counts',Object.fromEntries([...new Set(errors.map(e=>e.type))].map(type=>[type,errors.filter(e=>e.type===type).length])));