sylvie-2024

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

esp32_motor-pcb-x3_final-pcbflow.py (11250B)


      1 import os
      2 import sys
      3 import math
      4 import glob
      5 import shapely.geometry as sg
      6 
      7 from pcbflow import *
      8 from skidl import *
      9 
     10 # Set the default tool to KiCad 7 for footprint management.
     11 set_default_tool(KICAD)
     12 
     13 if __name__ == "__main__":
     14     ###
     15     ### SKiDL Circuit Declarations
     16     ###
     17 
     18 	# Create ESP32 and TMC5160 headers with updated part names
     19 	esp32_header1 = Part("Connector_Generic", "Conn_01x15", footprint="Connector_PinSocket_2.54mm.pretty/PinSocket_1x15_P2.54mm_Vertical")
     20 	esp32_header2 = Part("Connector_Generic", "Conn_01x15", footprint="Connector_PinSocket_2.54mm.pretty/PinSocket_1x15_P2.54mm_Vertical")
     21 	
     22 	tmc5160_header1 = Part("Connector_Generic", "Conn_01x08", footprint="Connector_PinSocket_2.54mm.pretty/PinSocket_1x08_P2.54mm_Vertical")
     23 	tmc5160_header2 = Part("Connector_Generic", "Conn_01x08", footprint="Connector_PinSocket_2.54mm.pretty/PinSocket_1x08_P2.54mm_Vertical")
     24 	
     25 	tmc5160_header1b = Part("Connector_Generic", "Conn_01x08", footprint="Connector_PinSocket_2.54mm.pretty/PinSocket_1x08_P2.54mm_Vertical")
     26 	tmc5160_header2b = Part("Connector_Generic", "Conn_01x08", footprint="Connector_PinSocket_2.54mm.pretty/PinSocket_1x08_P2.54mm_Vertical")
     27 	
     28 	tmc5160_header1c = Part("Connector_Generic", "Conn_01x08", footprint="Connector_PinSocket_2.54mm.pretty/PinSocket_1x08_P2.54mm_Vertical")
     29 	tmc5160_header2c = Part("Connector_Generic", "Conn_01x08", footprint="Connector_PinSocket_2.54mm.pretty/PinSocket_1x08_P2.54mm_Vertical")
     30 	
     31 	# Create 1x4 connector for stepper motor connections
     32 	stepper_motor_header = Part("Connector_Generic", "Conn_01x04", footprint="Connector_PinHeader_2.54mm.pretty/PinHeader_1x04_P2.54mm_Vertical")
     33 	stepper_motor_header_b = Part("Connector_Generic", "Conn_01x04", footprint="Connector_PinHeader_2.54mm.pretty/PinHeader_1x04_P2.54mm_Vertical")
     34 	stepper_motor_header_c = Part("Connector_Generic", "Conn_01x04", footprint="Connector_PinHeader_2.54mm.pretty/PinHeader_1x04_P2.54mm_Vertical")
     35 	
     36 	# Create 1x4 connector for CAN Bus connections
     37 	can_bus_header = Part("Connector_Generic", "Conn_01x04", footprint="Connector_PinHeader_2.54mm.pretty/PinHeader_1x04_P2.54mm_Vertical")
     38 	
     39 	# Create 100uF 63V capacitor with correct footprint
     40 	capacitor = Part("Device", "C", value="100uF", voltage="63V", footprint="Capacitor_THT.pretty/CP_Radial_D10.0mm_P5.00mm")
     41 	capacitor_b = Part("Device", "C", value="100uF", voltage="63V", footprint="Capacitor_THT.pretty/CP_Radial_D10.0mm_P5.00mm")
     42 	capacitor_c = Part("Device", "C", value="100uF", voltage="63V", footprint="Capacitor_THT.pretty/CP_Radial_D10.0mm_P5.00mm")
     43 	
     44 	# Create a 3-pin terminal block for 36V connections
     45 	terminal_block = Part("Connector_Generic", "Conn_01x03", footprint="TerminalBlock_Phoenix.pretty/TerminalBlock_Phoenix_PT-1,5-2-5.0-H_1x02_P5.00mm_Horizontal")
     46 	
     47 	# Create through-hole solder pads for 36V+ and 36V_GND connections with 2.54mm spacing
     48 	terminal_block_2 = Part("Connector_Generic", "Conn_01x02", footprint="TerminalBlock_Phoenix.pretty/TerminalBlock_Phoenix_MPT-0,5-2-2.54_1x02_P2.54mm_Horizontal")
     49 	
     50 	# Create 36V_V+ and 36V_GND nets.
     51 	V_plus_36V = Net("36V_V+")
     52 	GND_36V = Net("36V_GND")
     53 	
     54 	# Define the GPIO pins according to ESP32 30pin version.
     55 	esp32_vin = esp32_header1[1]
     56 	esp32_gnd1 = esp32_header1[2]
     57 	esp32_3v3 = esp32_header2[1]
     58 	esp32_gnd2 = esp32_header2[2]
     59 	esp32_gpio2 = esp32_header2[4]
     60 	esp32_gpio4 = esp32_header2[5]
     61 	esp32_gpio5 = esp32_header2[8]
     62 	esp32_gpio12 = esp32_header1[4]
     63 	esp32_gpio13 = esp32_header1[3]
     64 	esp32_gpio14 = esp32_header1[5]
     65 	esp32_gpio15 = esp32_header2[3]
     66 	esp32_gpio16 = esp32_header2[6]
     67 	esp32_gpio17 = esp32_header2[7]
     68 	esp32_gpio18 = esp32_header2[9]
     69 	esp32_gpio19 = esp32_header2[10]
     70 	esp32_gpio21 = esp32_header2[11]
     71 	esp32_gpio22 = esp32_header2[14]
     72 	esp32_gpio23 = esp32_header2[15]
     73 	esp32_gpio25 = esp32_header1[8]
     74 	esp32_gpio26 = esp32_header1[7]
     75 	esp32_gpio27 = esp32_header1[6]
     76 	esp32_gpio32 = esp32_header1[10]
     77 	esp32_gpio33 = esp32_header1[9]
     78 	
     79 	# Define the pins for TMC5160
     80 	tmc5160_pins = {
     81 	    'gnd2': tmc5160_header1[8], 
     82 	    '3v3': tmc5160_header1[7],
     83 	    'm2b': tmc5160_header1[6],
     84 	    'm1b': tmc5160_header1[5],
     85 	    'm1a': tmc5160_header1[4],
     86 	    'm2a': tmc5160_header1[3],
     87 	    'gnd1': tmc5160_header1[2],
     88 	    'vmot': tmc5160_header1[1],
     89 	    'en': tmc5160_header2[1],
     90 	    'sdi': tmc5160_header2[2],
     91 	    'sck': tmc5160_header2[3],
     92 	    'csn': tmc5160_header2[4],
     93 	    'sdo': tmc5160_header2[5],
     94 	    'step': tmc5160_header2[7],
     95 	    'dir': tmc5160_header2[8]
     96 	}
     97 	
     98 	# Define the pins for TMC5160b
     99 	tmc5160b_pins = {
    100 	    'gnd2': tmc5160_header1b[8], 
    101 	    '3v3': tmc5160_header1b[7],
    102 	    'm2b': tmc5160_header1b[6],
    103 	    'm1b': tmc5160_header1b[5],
    104 	    'm1a': tmc5160_header1b[4],
    105 	    'm2a': tmc5160_header1b[3],
    106 	    'gnd1': tmc5160_header1b[2],
    107 	    'vmot': tmc5160_header1b[1],
    108 	    'en': tmc5160_header2b[1],
    109 	    'sdi': tmc5160_header2b[2],
    110 	    'sck': tmc5160_header2b[3],
    111 	    'csn': tmc5160_header2b[4],
    112 	    'sdo': tmc5160_header2b[5],
    113 	    'step': tmc5160_header2b[7],
    114 	    'dir': tmc5160_header2b[8]
    115 	}
    116 	
    117 	# Define the pins for TMC5160c
    118 	tmc5160c_pins = {
    119 	    'gnd2': tmc5160_header1c[8], 
    120 	    '3v3': tmc5160_header1c[7],
    121 	    'm2b': tmc5160_header1c[6],
    122 	    'm1b': tmc5160_header1c[5],
    123 	    'm1a': tmc5160_header1c[4],
    124 	    'm2a': tmc5160_header1c[3],
    125 	    'gnd1': tmc5160_header1c[2],
    126 	    'vmot': tmc5160_header1c[1],
    127 	    'en': tmc5160_header2c[1],
    128 	    'sdi': tmc5160_header2c[2],
    129 	    'sck': tmc5160_header2c[3],
    130 	    'csn': tmc5160_header2c[4],
    131 	    'sdo': tmc5160_header2c[5],
    132 	    'step': tmc5160_header2c[7],
    133 	    'dir': tmc5160_header2c[8]
    134 	}
    135 	
    136 	# Create VIN and GND nets.
    137 	vin = Net("VIN")
    138 	gnd = Net("GND")
    139 	
    140 	# Create 3.3v gnd net.
    141 	vcc_gnd = Net("VCC_GND")
    142 	
    143 	# Connect 36V_V+ and 36V_GND to the terminal block
    144 	V_plus_36V += terminal_block[1]  # Connect 36V_V+ to the first pin of the PT terminal block slot 1
    145 	GND_36V += terminal_block[2]     # Connect 36V_GND to the third pin of the PT terminal block slot 2
    146 	
    147 	# Connect VIN and GND to ESP32 header 2.
    148 	vin += terminal_block_2[1] # Connect VIN to the MPT TerminalBlock slot 1
    149 	gnd += terminal_block_2[2] # Connect GND to the MPT TerminalBlock slot 2
    150 	
    151 	# Connect capacitor between 36V_V+ and 36V_GND nets
    152 	capacitor[1, 2] += V_plus_36V, GND_36V
    153 	capacitor_b[1, 2] += V_plus_36V, GND_36V
    154 	capacitor_c[1, 2] += V_plus_36V, GND_36V
    155 	
    156 	# Connect one pin of the limit switch header to the resistor and the other pin to ESP32 3.3v (Pull-down Resistor!)
    157 	# limit_switch_header[1] += resistor1[2], esp32_header1[5]  # Connect pin 1 of limit switch header to one pin of the resistor, and pin 5 on esp32 header 1
    158 	# limit_switch_header[2] += esp32_header2[1]           # Connect pin 2 of limit switch header to ESP32 3.3v pin
    159 	
    160 	# Connect VIN and GND to ESP32 and TMC5160 pins.
    161 	vin += esp32_vin
    162 	gnd += esp32_gnd1
    163 	
    164 	vcc_gnd += esp32_gnd2
    165 	
    166 	# vcc_gnd += resistor1[1] # GND connected to Resistor pin 1
    167 	
    168 	# Connect all the CAN Bus headers to the ESP32 Header 2
    169 	can_bus_header[1] += esp32_3v3 # 3.3v
    170 	can_bus_header[2] += esp32_gnd2 # GND
    171 	can_bus_header[3] += esp32_gpio4 # CAN TX
    172 	can_bus_header[4] += esp32_gpio5 # CAN RX
    173 	
    174 	# First TMC5160 Instance
    175 	esp32_3v3 += tmc5160_pins['3v3']
    176 	esp32_gnd2 += tmc5160_pins['gnd2']
    177 	V_plus_36V += tmc5160_pins['vmot']
    178 	GND_36V += tmc5160_pins['gnd1']
    179 	vcc_gnd += tmc5160_pins['gnd2']
    180 	esp32_gpio13 += tmc5160_pins['dir']
    181 	esp32_gpio12 += tmc5160_pins['step']
    182 	esp32_gpio15 += tmc5160_pins['en']
    183 	esp32_gpio23 += tmc5160_pins['sdi']
    184 	esp32_gpio19 += tmc5160_pins['sdo']
    185 	esp32_gpio18 += tmc5160_pins['sck']
    186 	esp32_gpio14 += tmc5160_pins['csn']
    187 	stepper_motor_header[1] += tmc5160_pins['m2a']
    188 	stepper_motor_header[2] += tmc5160_pins['m1a']
    189 	stepper_motor_header[3] += tmc5160_pins['m1b']
    190 	stepper_motor_header[4] += tmc5160_pins['m2b']
    191 	
    192 	# Second TMC5160 Instance
    193 	esp32_3v3 += tmc5160b_pins['3v3']
    194 	esp32_gnd2 += tmc5160b_pins['gnd2']
    195 	V_plus_36V += tmc5160b_pins['vmot']
    196 	GND_36V += tmc5160b_pins['gnd1']
    197 	vcc_gnd += tmc5160b_pins['gnd2']
    198 	esp32_gpio27 += tmc5160b_pins['dir']
    199 	esp32_gpio26 += tmc5160b_pins['step']
    200 	esp32_gpio17 += tmc5160b_pins['en']
    201 	esp32_gpio23 += tmc5160b_pins['sdi']
    202 	esp32_gpio19 += tmc5160b_pins['sdo']
    203 	esp32_gpio18 += tmc5160b_pins['sck']
    204 	esp32_gpio25 += tmc5160b_pins['csn']
    205 	stepper_motor_header_b[1] += tmc5160b_pins['m2a']
    206 	stepper_motor_header_b[2] += tmc5160b_pins['m1a']
    207 	stepper_motor_header_b[3] += tmc5160b_pins['m1b']
    208 	stepper_motor_header_b[4] += tmc5160b_pins['m2b']
    209 	
    210 	# Third TMC5160 Instance
    211 	esp32_3v3 += tmc5160c_pins['3v3']
    212 	esp32_gnd2 += tmc5160c_pins['gnd2']
    213 	V_plus_36V += tmc5160c_pins['vmot']
    214 	GND_36V += tmc5160c_pins['gnd1']
    215 	vcc_gnd += tmc5160c_pins['gnd2']
    216 	esp32_gpio32 += tmc5160c_pins['dir']
    217 	esp32_gpio2 += tmc5160c_pins['step']
    218 	esp32_gpio22 += tmc5160c_pins['en']
    219 	esp32_gpio23 += tmc5160c_pins['sdi']
    220 	esp32_gpio19 += tmc5160c_pins['sdo']
    221 	esp32_gpio18 += tmc5160c_pins['sck']
    222 	esp32_gpio33 += tmc5160c_pins['csn']
    223 	stepper_motor_header_c[1] += tmc5160c_pins['m2a']
    224 	stepper_motor_header_c[2] += tmc5160c_pins['m1a']
    225 	stepper_motor_header_c[3] += tmc5160c_pins['m1b']
    226 	stepper_motor_header_c[4] += tmc5160c_pins['m2b']
    227 
    228     ###
    229     ### pcbflow PCB Declarations
    230     ###
    231 
    232 	# Create a pcbflow Board instance
    233 	brd = Board((80, 60))  # Adjust the board size (width, height) as per your requirement
    234 
    235 	brd.add_inner_copper_layer(2)
    236 
    237 	# Place 2 mm mounting holes in the corners
    238 	holes = ((15, 5), (15, 25), (60, 5), (60, 25))
    239 	for hole in holes:
    240 		brd.add_hole(hole, 2.0)
    241 
    242 	# Assign a convenient reference to the default SKiDL circuit
    243 	ckt = default_circuit
    244 
    245 	print("Circuit:  Parts: %d  Nets: %d" % (len(ckt.parts), len(ckt.nets)))
    246 
    247 	# Assign part locations
    248 	esp32_header1.loc = (5, 55)
    249 	esp32_header2.loc = (10, 55)
    250 	tmc5160_header1.loc = (25, 55)
    251 	tmc5160_header2.loc = (55, 55)
    252 	tmc5160_header1b.loc = (20, 55)
    253 	tmc5160_header2b.loc = (60, 55)
    254 	tmc5160_header1c.loc = (15, 55)
    255 	tmc5160_header2c.loc = (65, 55)
    256 	stepper_motor_header.loc = (30, 55)
    257 	stepper_motor_header_b.loc = (50, 55)
    258 	stepper_motor_header_c.loc = (70, 55)
    259 	can_bus_header.loc = (65, 55)
    260 	capacitor.loc = (25, 25)
    261 	capacitor_b.loc = (45, 25)
    262 	capacitor_c.loc = (65, 25)
    263 	terminal_block.loc = (60, 15)
    264 	terminal_block_2.loc = (75, 15)
    265 
    266 	# Instantiate SkiPart(PCBPart) instances
    267 	sides = ["top"] * 17  # Assuming all parts are on the top side
    268 
    269 	for part, side in zip(ckt.parts, sides):
    270 		sp = SkiPart(brd.DC(part.loc), part, side=side)
    271 		# "fanout" GND and VDD vias from parts with GND and VDD net connections
    272 		sp.fanout(["VDD"])
    273 		sp.fanout(["GND"], relative_to="inside")
    274 
    275 
    276 	# Finish the PCB with an outline and poured copper layers
    277 	brd.add_outline()
    278 	brd.fill_layer("GTL", "GND")
    279 	brd.fill_layer("GBL", "GND")
    280 
    281 	# Save the rendered PCB to asset files
    282 	#brd.save("%s" % (os.path.basename(__file__)[:-3]))
    283 	#brd.save_gerbers("%s" % (os.path.basename(__file__)[:-3]))
    284 	#brd.save_pdf("%s" % (os.path.basename(__file__)[:-3]))
    285 	brd.save_png("%s" % (os.path.basename(__file__)[:-3]))
    286 	brd.save_svg("%s" % (os.path.basename(__file__)[:-3]))
    287 	#brd.save_centroids("%s" % (os.path.basename(__file__)[:-3]))
    288 	#brd.save_bom("%s" % (os.path.basename(__file__)[:-3]))
    289 
    290 	# Generate the netlist (if required)
    291 	generate_netlist()  # If you need to generate a netlist, add this line
    292 
    293 	print("PCB design completed.")