Saturday, 15 February 2025

Procedural Generation of Castellated Footprints

Each of the modules I am creating need a unique module footprint, so that they can't be installed in the wrong bay, or with incorrect orientation.

I've already spent quite a bit of time in the footprint editor making variants, and I've only designed 4 of the perhaps 30 modules I will need -- and then each needs its matching bay.

To save on work going forward, I want to explore writing a program that can generate a unique module footprint that has the required component area and number of pins.

This should be possible, because KiCad uses a text-based format for footprints.

After an hour or so of hacking up some C, I have it well under way, producing footprints like this:

From running a command like this:

./footprint-gen 10 10 10 1 > ../MegaCastle.pretty/test.kicad_mod

The numbers mean the width and height of the cut-out area in the middle, and the minimum number of  pins required. The final number, 1 in this case, is to select the variant (i.e., which pins are missing to make the result unambiguous in terms of rotation or flipping). It will make the module bigger if the number of pins or the cut-out size require it.

I still need to add the no-component zones, and then the logic for working out which pins to remove, when less than the full pin-count is required.

That latter will be fairly simple, if a little fiddly, as we need to consider all possible variants (or at least several) that meet the criteria of:

1. Has the correct number of pins.

2. Has no rotational or translational symmetry to itself, nor to any of the previously identified variants.

It might be that I try to come up with up to unique variants, until we run out of possibilities, or the requested variant number is reached.

This felt like the kind of problem to set ChatGPT against to work out. And it got it mostly right, but forgot to bother flipping, when testing. It probably saved some time overall. And now I can generate variants with any number of pins and any given cut-out size for components. It even works out the name for the variant, and adds the labels, like this:

So now I just need to make it write it out into the correct file name, and we are most of the way there. I will also need to make the module bay version that accepts the corresponding module, and then my life when defining new modules will be super easy.

This will not only save time, but likely help me work faster, as coming up with the footprints for these was a significant psychological drag, especially with the low-level concern that I might have to re-do it a different size or something, if I wasn't 100% sure I had the rest of the circuit done.  It also meant that I was using cut-out areas that weren't optimal, because of the effort of making a new one, instead of just re-using an existing one.

So, onward with making the module bay versions. It was a bit of fiddling again, to get this right, but it now generates all four variants that I could need: Normal, No cutout for components in the middle for modules that don't need it (so that we keep ground planes, routing layers etc intact), Edge-of-board and both edge and no cutout, like this:





I am very happy with this -- as I can now just generate them as I need. I haven't done the same for symbol generation, but the symbols are much quicker and easier to generate by hand. It doesn't mean that I won't tackle it, but I want to get back to making modules using these footprints again first!


No comments:

Post a Comment