Commit a81516e6 authored by Neil Gershenfeld's avatar Neil Gershenfeld

Merge branch 'patch-1' into 'master'

Patch 1

See merge request !1
parents 27fcf5b0 fccc646f
Pipeline #892 passed with stage
in 3 seconds
<a href='https://www.mattkeeter.com/projects/kokopelli/'>Kokopelli</a> is an old (but great) project from <a href='https://www.mattkeeter.com/'>Matt Keeter</a>. Its a full-blown CAD program, but it can be used for designing PCBs. The programmatic interface makes it easy to set up relationships between components, traces, etc. and you can toolpath for the circuit mill right in kokopelli.
To use it, clone it from github: https://github.com/mkeeter/kokopelli and install with the makefile.
I've added a couple small utilities to Matt's original PCB design library. If you'd like to use them, you can replace the existing koko/lib/pcb.py with <a href='pcb.py'>pcb.py</a> and reinstall.
A fun example using several of the included features is a stepper driver board I designed:
<img src='servo-stepper-v1-layout.png' width=500px>
import operator
from math import cos, sin, atan2, radians, degrees, sqrt
import koko.lib.shapes2d as s2d
from koko.lib.text import text
from numpy import *
class PCB(object):
def __init__(self, x0, y0, width, height, chamfer_distance=0):
self.x0 = x0
self.y0 = y0
self.width = width
self.height = height
self.components = []
self.connections = []
self._cutout = None
self.custom_cutout = None
self.custom_layers = {}
self.chamfer_distance = chamfer_distance
@property
def traces(self):
L = [c.pads for c in self.components if c.side == 0] + [c.traces[0] for c in self.connections]
if L:
t = reduce(operator.add, L)
#L = [c.holes for c in self.components if c.holes is not None]
#L.extend([c.holes for c in self.connections if c.holes is not None])
#if L:
# t = t - reduce(operator.add,L)
return t
else: return None
@property
def traces_other_side(self):
L = [c.pads for c in self.components if c.side == 1] + [c.traces[1] for c in self.connections if c.traces[1] is not None]
if L:
t = reduce(operator.add, L)
#L = [c.holes for c in self.components if c.holes is not None]
#L.extend([c.holes for c in self.connections if c.holes is not None])
#if L:
# t = t - reduce(operator.add,L)
return t
else: return None
@property
def holes(self):
L = [c.holes for c in self.components if c.holes is not None]
L.extend([c.holes for c in self.connections if c.holes is not None])
if L:
t = reduce(operator.add,L)
return t
else:
return None
@property
def part_labels_top(self):
L = [c.label for c in self.components if c.label is not None and c.side==0]
return reduce(operator.add, L) if L else None
@property
def part_labels_bot(self):
L = [c.label for c in self.components if c.label is not None and c.side==1]
return reduce(operator.add, L) if L else None
@property
def part_shadows_top(self):
L = [c.shadow_shape for c in self.components if c.shadow_shape is not None and c.side==0]
return reduce(operator.add, L) if L else None
@property
def part_shadows_bot(self):
L = [c.shadow_shape for c in self.components if c.shadow_shape is not None and c.side==1]
return reduce(operator.add, L) if L else None
@property
def pin_labels_top(self):
L = [c.pin_labels for c in self.components if c.pin_labels is not None and c.side==0]
return reduce(operator.add, L) if L else None
@property
def pin_labels_bot(self):
L = [c.pin_labels for c in self.components if c.pin_labels is not None and c.side==1]
return reduce(operator.add, L) if L else None
@property
def cutout(self):
if self.custom_cutout is not None:
if self.holes:
return self.custom_cutout - self.holes
else:
return self.custom_cutout
outer = s2d.rectangle(self.x0, self.x0 + self.width,
self.y0, self.y0 + self.height)
if self.chamfer_distance:
c = self.chamfer_distance
c1 = s2d.triangle(self.x0,self.y0,self.x0,self.y0+c,self.x0+c,self.y0)
c2 = s2d.triangle(self.x0+self.width,self.y0+self.height, self.x0+self.width, self.y0+self.height-c, self.x0+self.width-c, self.y0+self.height)
c3 = s2d.triangle(self.x0,self.y0+self.height, self.x0+c, self.y0+self.height, self.x0, self.y0+self.height-c)
c4 = s2d.triangle(self.x0+self.width,self.y0, self.x0+self.width-c, self.y0, self.x0+self.width, self.y0+c)
outer -= c1+c2+c3+c4
#L = [c.holes for c in self.components if c.holes is not None]
#L.extend([c.holes for c in self.connections if c.holes is not None])
return outer - self.holes if self.holes else outer
#@property
def layout(self,sides=[0,1]):
T = []
if 0 in sides:
if self.part_labels_top:
T.append(s2d.color(self.part_labels_top, (125, 200, 60)))
if self.pin_labels_top:
T.append(s2d.color(self.pin_labels_top, (255, 90, 60)))
if self.traces:
T.append(s2d.color(self.traces-self.holes, (125, 90, 60)))
if self.part_shadows_top:
T.append(s2d.color(self.part_shadows_top-self.holes,(55,55,60)))
if 1 in sides:
if self.part_labels_bot:
T.append(s2d.color(self.part_labels_bot, (90, 60, 255)))
if self.pin_labels_bot:
T.append(s2d.color(self.pin_labels_bot, (175, 30, 175)))
if self.traces_other_side:
T.append(s2d.color(self.traces_other_side-self.holes, (90, 60, 125)))
if self.part_shadows_bot:
T.append(s2d.color(self.part_shadows_bot-self.holes,(45, 30, 62)))
for v in sorted(self.custom_layers.values(),key=lambda v: -v['position']):
if v['visible']: T.append(s2d.color(v['layer'],v['color']))
T.append(s2d.color(self.cutout, (35,35,40)))
return T
def __iadd__(self, rhs):
if isinstance(rhs, Component):
self.components.append(rhs)
elif isinstance(rhs, Connection):
self.connections.append(rhs)
else:
raise TypeError("Invalid type for PCB addition (%s)" % type(rhs))
return self
def add_custom_layer(self,name,layer,color):
self.custom_layers[name] = {'layer':layer,'color':color,'position':len(self.custom_layers),'visible':1}
def hide_layer(self,name):
self.custom_layers[name]['visible'] = 0
def connectH(self, *args, **kwargs):
''' Connects a set of pins or points, traveling first
horizontally then vertically
'''
width = kwargs['width'] if 'width' in kwargs else 0.016
mode = kwargs['mode'] if 'mode' in kwargs else 'explicit'
sides = kwargs['sides'] if 'sides' in kwargs else [0 for a in args[:-1]]
new_sides = []
points = []
args = list(args)
for i,p in enumerate(args):
if not isinstance(p,BoundPin):
if mode=='diff':
args[i] = Point(args[i-1].x+p[0],args[i-1].y+p[1])
elif mode=='explicit':
args[i] = Point(*p)
else:
raise NotImplementedError("Unknown mode type %s"%mode)
for A, B, s in zip(args[:-1], args[1:], sides):
points.append(A); new_sides.append(s)
if (A.x != B.x):
points.append(Point(B.x, A.y)); new_sides.append(s)
if A.y != B.y: points.append(B)
c = Connection(width, *points, sides=new_sides)
self.connections.append(c)
return c
def connectV(self, *args, **kwargs):
''' Connects a set of pins or points, travelling first
vertically then horizontally.
'''
width = kwargs['width'] if 'width' in kwargs else 0.016
mode = kwargs['mode'] if 'mode' in kwargs else 'explicit'
sides = kwargs['sides'] if 'sides' in kwargs else [0 for a in args[:-1]]
new_sides = []
points = []
args = list(args)
for i,p in enumerate(args):
if not isinstance(p,BoundPin):
if mode=='diff':
args[i] = Point(args[i-1].x+p[0],args[i-1].y+p[1])
elif mode=='explicit':
args[i] = Point(*p)
else:
raise NotImplementedError("Unknown mode type %s"%mode)
for A, B, s in zip(args[:-1], args[1:], sides):
points.append(A); new_sides.append(s)
if (A.y != B.y):
points.append(Point(A.x, B.y)); new_sides.append(s)
if A.x != B.x: points.append(B)
c = Connection(width, *points, sides=new_sides)
self.connections.append(c)
return c
def connectD(self, *args, **kwargs):
''' Connects a set of pins or points, travelling first
diagonally then horizontally or vertically, depending on geometry.
'''
width = kwargs['width'] if 'width' in kwargs else 0.016
sides = kwargs['sides'] if 'sides' in kwargs else [0 for a in args[:-1]]
new_sides = []
points = []
def sgn(x):
if x>=0:
return 1
else:
return -1
args = list(args)
for i,p in enumerate(args):
if not isinstance(p,BoundPin):
args[i] = Point(*p)
for A, B, s in zip(args[:-1], args[1:], sides):
points.append(A); new_sides.append(s)
if (B.y-A.y != B.x-A.x):
if abs(B.y-A.y) > abs(B.x-A.x):
points.append(Point(B.x, A.y+sgn(B.y-A.y)*abs(B.x-A.x))); new_sides.append(s)
else:
points.append(Point(A.x+sgn(B.x-A.x)*abs(B.y-A.y),B.y)); new_sides.append(s)
if (A.x != B.x) or (A.y != B.y): points.append(B)
c = Connection(width, *points, sides=new_sides)
self.connections.append(c)
return c
################################################################################
class Component(object):
''' Generic PCB component.
'''
def __init__(self, x, y, rot=0, name='',label_size=0.05, side=0):
''' Constructs a Component object
x X position
y Y position
rotation angle (degrees)
name String
side which side of board 0 for top, 1 for bottom
'''
self.x = x
self.y = y
self.rot = rot
self.name = name
self.label_size = label_size
self.side = side
if self.side == 1:
self.pins = [p.mirror_x() for p in self.pins]
self.vias = [v.mirror_x() for v in self.vias]
def __getitem__(self, i):
if isinstance(i, str):
try:
pin = [p for p in self.pins if p.name == i][0]
except IndexError:
raise IndexError("No pin with name %s" % i)
elif isinstance(i, int):
try:
pin = self.pins[i-1]
except IndexError:
raise IndexError("Pin %i is not in array" %i)
return BoundPin(pin, self)
@property
def pads(self):
pads = reduce(operator.add, [p.pad for p in self.pins])
return s2d.move(s2d.rotate(pads, self.rot), self.x, self.y)
@property
def holes(self):
if self.vias:
holes = reduce(operator.add,[v.hole for v in self.vias])
return s2d.move(s2d.rotate(holes,self.rot), self.x, self.y)
else: return None
@property
def pin_labels(self):
L = []
for p in self.pins:
p = BoundPin(p, self)
if p.pin.name:
t = s2d.rotate(text(p.pin.name, 0, 0, p.pin.label_size),self.rot+p.pin.label_rot)
L.append(s2d.move(t, p.x, p.y))
return reduce(operator.add, L) if L else None
@property
def label(self):
return text(self.name, self.x, self.y, self.label_size)
@property
def shadow_shape(self):
try:
return s2d.move(s2d.rotate(self.shadow, self.rot),self.x, self.y)
except AttributeError:
return None
################################################################################
class Pin(object):
''' PCB pin, with name, shape, and position
'''
def __init__(self, x, y, shape, name='', label_size=.03, label_rot=0):
self.x = x
self.y = y
self.shape = shape
self.name = name
self.label_size = label_size
self.label_rot = label_rot
@property
def pad(self):
return s2d.move(self.shape, self.x, self.y)
def mirror_x(self):
return Pin( -self.x, self.y, self.shape, self.name, label_size=self.label_size, label_rot=self.label_rot )
################################################################################
class Via(object):
''' PCB via, with shape, and position
'''
def __init__(self, x, y, shape):
self.x = x
self.y = y
self.shape = shape
@property
def hole(self):
return s2d.move(self.shape, self.x, self.y)
def mirror_x(self):
return Via( -self.x, self.y, self.shape )
################################################################################
class BoundPin(object):
''' PCB pin localized to a specific component
(so that it has correct x and y positions)
'''
def __init__(self, pin, component):
self.pin = pin
self.component = component
@property
def x(self):
return (cos(radians(self.component.rot)) * self.pin.x -
sin(radians(self.component.rot)) * self.pin.y +
self.component.x)
@property
def y(self):
return (sin(radians(self.component.rot)) * self.pin.x +
cos(radians(self.component.rot)) * self.pin.y +
self.component.y)
@property
def point(self):
return Point(self.x,self.y)
################################################################################
class Point(object):
''' Object with x and y member variables
'''
def __init__(self, x, y):
self.x = x
self.y = y
def __iter__(self):
return iter([self.x, self.y])
def __add__(self, p):
return Point(self.x+p.x,self.y+p.y)
def __sub__(self, p):
return Point(self.x-p.x,self.y-p.y)
def __rmul__(self,a):
return Point(a*self.x,a*self.y)
def magnitude(self):
return sqrt(self.x*self.x + self.y+self.y)
def normalized(self):
return Point(self.x/self.magnitude(), self.y/self.magnitude())
@property
def point(self):
return self
################################################################################
class Connection(object):
''' Connects two pins via a series of intermediate points
'''
def __init__(self, width, *args, **kwargs):
self.width = width
self.points = [
a if isinstance(a, BoundPin) else Point(*a) for a in args
]
self.sides = kwargs['sides'] if 'sides' in kwargs else [0 for a in args[:-1]] #0 is base side, 1 is other side
self.holes = None
self.jumpers = []
def add_jumper(self,p,rot=0,width=.12, height=.07,thick=.05):
self.jumpers.append((p,rot,width,height,thick))
return self
def cut_corners(self,idx):
for i in idx:
i,v = i #unpack index and distance
assert(i>0) #start corner numbering at 1
assert(i<len(self.points)) #no corner to cut at end
d = lambda p,q: sqrt( (p.x-q.x)**2 + (p.y-q.y)**2 )
dm = d(self.points[i],self.points[i-1])
dp = d(self.points[i],self.points[i+1])
#if dm > dp:
self.points = self.points[:i] + \
[Point(self.points[i].x-v/dm*(self.points[i].x-self.points[i-1].x ), self.points[i].y-v/dm*(self.points[i].y-self.points[i-1].y )),
Point(self.points[i].x+v/dp*(self.points[i+1].x-self.points[i].x ), self.points[i].y+v/dp*(self.points[i+1].y-self.points[i].y ))
] + \
self.points[i+1:]
self.sides.insert(i,self.sides[i])
#else:
# self.points[i] = self.points[i] - dm/dp*(self.points[i]-self.points[i+1])
return self
@property
def traces(self):
#_pad_1206 = s2d.rectangle(-0.025, 0.025, -0.034, 0.034)
_pad_via = s2d.circle(0,0,.025) #s2d.rectangle(-0.025, 0.025, -0.025, 0.025)
_hole_via = s2d.circle(0,0,.016)
jumper_cuts = []
jumper_pads = []
for p,r,w,h,t in self.jumpers:
_jumper_pad = s2d.move(s2d.rectangle(-.5*t, .5*t, -.5*h, .5*h),-.5*w,0)
_jumper_pad += s2d.move(s2d.rectangle(-.5*t, .5*t, -.5*h, .5*h), .5*w,0)
_cut = s2d.rectangle(-.5*w,.5*w,-.5*h,.5*h)
jumper_cuts.append(s2d.move(s2d.rotate(_cut,r),p[0],p[1]))
jumper_pads.append(s2d.move(s2d.rotate(_jumper_pad,r),p[0],p[1]))
t = [[],[]]
for p1, p2, side in zip(self.points[:-1], self.points[1:], self.sides):
d = sqrt((p1.x - p2.x)**2 + (p1.y - p2.y)**2)
if p2 != self.points[-1]:
d += self.width/2
a = atan2(p2.y - p1.y, p2.x - p1.x)
r = s2d.rounded_rectangle(0, d, -self.width/2, self.width/2,1.)
t[side].append(s2d.move(s2d.rotate(r, degrees(a)), p1.x, p1.y))
try:
result0 = reduce(operator.add, t[0])
except TypeError:
result0 = None
try:
result1 = reduce(operator.add, t[1])
except TypeError:
result1 = None
#calculate locations for via holes and pads
for s1,s2,p in zip(self.sides[:-1],self.sides[1:],self.points[1:-1]):
if s1!=s2:
result0 += s2d.move(_pad_via,p.x,p.y)
result1 += s2d.move(_pad_via,p.x,p.y)
self.holes += s2d.move(_hole_via,p.x,p.y)
if len(self.jumpers)!=0:
result0 -= reduce(operator.add,jumper_cuts)
result0 += reduce(operator.add,jumper_pads)
return result0, result1
################################################################################
# Discrete passive components
################################################################################
_pad_1206 = s2d.rectangle(-0.032, 0.032, -0.034, 0.034)
class R_1206(Component):
''' 1206 Resistor
'''
pins = [Pin(-0.06, 0, _pad_1206), Pin(0.06, 0, _pad_1206)]
prefix = 'R'
vias = []
class C_1206(Component):
''' 1206 Capacitor
'''
pins = [Pin(-0.06, 0, _pad_1206), Pin(0.06, 0, _pad_1206)]
prefix = 'C'
vias = []
_pad_0805 = s2d.rectangle(-.023,.023, -.027, .027)
class R_0805(Component):
''' 0805 Resistor
'''
pins = [Pin(-0.04, 0, _pad_0805), Pin(0.04, 0, _pad_0805)]
prefix = 'R'
vias = []
class C_0805(Component):
''' 0805 Capacitor
'''
pins = [Pin(-0.04, 0, _pad_0805), Pin(0.04, 0, _pad_0805)]
prefix = 'C'
vias = []
_pad_SJ = s2d.rectangle(-0.02, 0.02, -0.03, 0.03)
class SJ(Component):
''' Solder jumper
'''
pins = [Pin(-0.029, 0, _pad_SJ), Pin(0.029, 0, _pad_SJ)]
prefix = 'SJ'
vias = []
_pad_SOD_123 = s2d.rectangle(-0.02, 0.02, -0.024, 0.024)
class D_SOD_123(Component):
''' Diode
'''
pins = [Pin(-0.07, 0, _pad_SOD_123, 'A'),
Pin(0.07, 0, _pad_SOD_123, 'C')]
prefix = 'D'
vias = []
################################################################################
# Connectors
################################################################################
_pad_USB_trace = s2d.rectangle(-0.0075, 0.0075, -0.04, 0.04)
_pad_USB_foot = s2d.rectangle(-0.049, 0.049, -0.043, 0.043)
class USB_mini_B(Component):
''' USB mini B connector
Hirose UX60-MB-5ST
'''
pins = [
Pin(0.063, 0.24, _pad_USB_trace, 'G'),
Pin(0.0315, 0.24, _pad_USB_trace),
Pin(0, 0.24, _pad_USB_trace, '+'),
Pin(-0.0315, 0.24, _pad_USB_trace, '-'),
Pin(-0.063, 0.24, _pad_USB_trace, 'V'),
Pin( 0.165, 0.21, _pad_USB_foot),
Pin(-0.165, 0.21, _pad_USB_foot),
Pin( 0.165, 0.0, _pad_USB_foot),
Pin(-0.165, 0.0, _pad_USB_foot)
]
prefix = 'J'
vias = []
_pad_header = s2d.rectangle(-0.06, 0.06, -0.025, 0.025)
_pad_header_skinny = s2d.rectangle(-0.06, 0.06, -0.020, 0.020)
class Header_4(Component):
''' 4-pin header
fci 95278-101a04lf bergstik 2x2x0.1
'''
pins = [
Pin(-0.107, 0.05, _pad_header),
Pin(-0.107, -0.05, _pad_header),
Pin( 0.107, -0.05, _pad_header),
Pin( 0.107, 0.05, _pad_header)
]
prefix = 'J'
vias = []
class Header_4_skinny(Component):
''' 4-pin header
fci 95278-101a04lf bergstik 2x2x0.1
'''
pins = [
Pin(-0.107, 0.05, _pad_header_skinny),
Pin(-0.107, -0.05, _pad_header_skinny),
Pin( 0.107, -0.05, _pad_header_skinny),
Pin( 0.107, 0.05, _pad_header_skinny)
]
prefix = 'J'
vias = []
class Header_Power(Component):
''' 4-pin header
fci 95278-101a04lf bergstik 2x2x0.1
'''
pins = [
Pin(-0.107, 0.05, _pad_header,"V"),
Pin(-0.107, -0.05, _pad_header,"GND"),
Pin( 0.107, -0.05, _pad_header),
Pin( 0.107, 0.05, _pad_header)
]
prefix = 'J'
vias = []
class Header_ISP(Component):
''' ISP programming header
FCI 95278-101A06LF Bergstik 2x3x0.1
'''
pins = [
Pin(-0.107, 0.1, _pad_header, 'GND'),
Pin(-0.107, 0, _pad_header, 'MOSI'),
Pin(-0.107, -0.1, _pad_header, 'V'),
Pin( 0.107, -0.1, _pad_header, 'MISO'),
Pin( 0.107, 0, _pad_header, 'SCK'),
Pin( 0.107, 0.1, _pad_header, 'RST')
]
prefix = 'J'
vias = []
class Header_ISP_skinny(Component):
''' ISP programming header
FCI 95278-101A06LF Bergstik 2x3x0.1
'''
pins = [
Pin(-0.107, 0.1, _pad_header_skinny, 'GND'),
Pin(-0.107, 0, _pad_header_skinny, 'MOSI'),
Pin(-0.107, -0.1, _pad_header_skinny, 'V'),
Pin( 0.107, -0.1, _pad_header_skinny, 'MISO'),
Pin( 0.107, 0, _pad_header_skinny, 'SCK'),
Pin( 0.107, 0.1, _pad_header_skinny, 'RST')
]
prefix = 'J'
vias = []
#shadow = s2d.rectangle(-.06,8/25.4,-.325,.325)
class Header_FTDI(Component):
''' FTDI cable header
'''
pins = [
Pin(0, 0.25, _pad_header, 'GND'),
Pin(0, 0.15, _pad_header, 'CTS'),
Pin(0, 0.05, _pad_header, 'VCC'),
Pin(0, -0.05, _pad_header, 'TX'),
Pin(0, -0.15, _pad_header, 'RX'),
Pin(0, -0.25, _pad_header, 'RTS')
]
prefix = 'J'
vias = []
shadow = s2d.rectangle(-.06,8/25.4,-.325,.325)
class Header_FTDI_skinny(Component):
''' FTDI cable header
'''
pins = [
Pin(0, 0.25, _pad_header_skinny, 'GND'),
Pin(0, 0.15, _pad_header_skinny, 'CTS'),
Pin(0, 0.05, _pad_header_skinny, 'VCC'),
Pin(0, -0.05, _pad_header_skinny, 'TX'),
Pin(0, -0.15, _pad_header_skinny, 'RX'),
Pin(0, -0.25, _pad_header_skinny, 'RTS')
]
prefix = 'J'
vias = []
shadow = s2d.rectangle(-.06,8/25.4,-.325,.325)
class ScrewTerminal(Component):