-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathkimotor_action.py
1643 lines (1346 loc) · 56.1 KB
/
kimotor_action.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2022-2024 Stefano Cottafavi <[email protected]>
# SPDX-License-Identifier: GPL-2.0-only
import os
import shutil
import numpy as np
import math
import json
from datetime import datetime
import wx
import wx.lib.agw.persist.persistencemanager as PM
import pcbnew
if __name__ == '__main__':
import kimotor_gui
import kimotor_linalg as kla
else:
from . import kimotor_gui
from . import kimotor_linalg as kla
from . import kimotor_solver as ksolve
from . import kimotor_persist as kpers
class KiMotor(pcbnew.ActionPlugin):
def defaults(self):
self.version = ""
self.metadata_file = os.path.join(os.path.dirname(__file__), 'metadata.json')
with open(self.metadata_file, 'r') as f:
data = json.load(f)
self.version = data['versions'][0]['version']
self.name = "KiMotor"
self.category = "Modify Drawing PCB"
self.description = "KiMotor - Parametric PCB motor design"
self.show_toolbar_button = True
self.icon_file_name = os.path.join(os.path.dirname(__file__), 'kimotor_24x24.png')
def Run( self ):
# grab editor frame and board
self.frame = wx.FindWindowByName("PcbFrame")
self.board = pcbnew.GetBoard()
dlg = KiMotorDialog(self.frame, self.board)
dlg.SetIcon( wx.Icon(self.icon_file_name) )
dlg.SetTitle( self.name+" v"+self.version )
dlg.Show()
class KiMotorDialog ( kimotor_gui.KiMotorGUI ):
group = None
# updated during init (0: not valid)
SCALE = 0
KICAD_VERSION = 0
tl = 0
tr = 0
# parameters
outline = None
trmtype = None
fpoint = None
angle = None
tthick = 35e-6 # [m] copper thickness (1oz layer specs)
# fab capabilities (min)
rules = None
rules_preset_jlcpcb12 = {
"track_width": 0.127, # (5mil)
"track_space": 0.127, # (5mil)
"via_hole": 0.3,
"via_diameter": 0.5,
}
rules_preset_jlcpcb4 = {
"track_width": 0.09, # (3.5mil)
"track_space": 0.09, # (3.5mil)
"via_hole": 0.15,
"via_diameter": 0.25,
}
# terminal footprint dict
term_tht_db = {
"0.1" : "SolderWire-0.1sqmm_1x01_D0.4mm_OD1mm",
"0.15" : "SolderWire-0.15sqmm_1x01_D0.5mm_OD1.5mm",
"0.25" : "SolderWire-0.25sqmm_1x01_D0.65mm_OD1.7mm",
"0.5" : "SolderWire-0.5sqmm_1x01_D0.9mm_OD2.1mm",
"0.75" : "SolderWire-0.75sqmm_1x01_D1.25mm_OD2.3mm",
"1.0" : "SolderWire-1sqmm_1x01_D1.4mm_OD2.7mm",
"1.5" : "SolderWire-1.5sqmm_1x01_D1.7mm_OD3.9mm",
"2.0" : "SolderWire-2sqmm_1x01_D2mm_OD3.9mm",
"2.5" : "SolderWire-2.5sqmm_1x01_D2.4mm_OD3.6mm"
}
term_smd_db = {
"1" : "TestPoint_Pad_1.0x1.0mm",
"1.5" : "TestPoint_Pad_1.5x1.5mm",
"2" : "TestPoint_Pad_2.0x2.0mm",
"2.5" : "TestPoint_Pad_2.5x2.5mm",
"3" : "TestPoint_Pad_3.0x3.0mm",
"4" : "TestPoint_Pad_4.0x4.0mm",
}
term_db = {
"THT" : term_tht_db,
"SMD" : term_smd_db
}
mhole_db = {
"M2" : "MountingHole_2.2mm_M2_Pad",
"M2.5" : "MountingHole_2.7mm_M2.5_Pad",
"M3" : "MountingHole_3.2mm_M3_Pad",
"M3.5" : "MountingHole_3.7mm_M3.5_Pad",
"M4" : "MountingHole_4.3mm_M4_Pad",
"M5" : "MountingHole_5.3mm_M5_Pad",
"M6" : "MountingHole_6.4mm_M6_Pad",
"M8" : "MountingHole_8.4mm_M8_Pad",
}
def __init__(self, parent, board):
kimotor_gui.KiMotorGUI.__init__(self, parent)
self.board = board
self.KICAD_VERSION = int(pcbnew.Version().split(".")[0])
if self.KICAD_VERSION < 7:
self.SCALE = pcbnew.IU_PER_MM
self.fpoint = pcbnew.wxPoint
self.fpoint_vector = pcbnew.wxPoint_Vector
self.fsize = pcbnew.wxSize
else:
self.SCALE = pcbnew.FromMM(1)
self.fpoint = pcbnew.VECTOR2I
self.fpoint_vector = pcbnew.VECTOR_VECTOR2I
self.fsize = pcbnew.VECTOR2I
# kick-off persistence
self.pf = os.path.join(
pcbnew.SETTINGS_MANAGER.GetUserSettingsPath(),
"kimotor.cfg"
)
self.init_persist(self.pf)
self.init_path()
self.init_nets()
self.on_cb_outline(None)
self.on_cb_trmtype(None)
def eda_angle(self,angle):
if self.KICAD_VERSION < 7:
return angle *180/math.pi *100
else:
return pcbnew.EDA_ANGLE(angle, pcbnew.RADIANS_T)
# init functions
def init_persist(self, configFile):
self.pm = PM.PersistenceManager.Get()
self.pm.SetPersistenceFile(configFile)
self.pm.RegisterAndRestoreAll(self)
def get_parameters(self, rules=None):
specs = self.m_cbPreset.GetStringSelection()
if specs=="DRC Rules":
self.rules = self.board.GetDesignSettings()
self.min_clear = self.rules.m_MinClearance
self.trk_w = self.rules.m_TrackMinWidth
self.vmaw = self.rules.m_ViasMinAnnularWidth
self.d_drill = self.rules.m_MinThroughDrill
self.d_via = max(self.rules.m_ViasMinSize, self.d_drill + 2*self.vmaw)
elif specs=="Current T/V":
self.rules = self.board.GetDesignSettings()
self.min_clear = self.rules.m_MinClearance
self.vmaw = self.rules.m_ViasMinAnnularWidth
self.trk_w = self.rules.GetCurrentTrackWidth()
self.d_drill = self.rules.GetCurrentViaDrill()
self.d_via = self.rules.GetCurrentViaSize()
aw = (self.d_via-self.d_drill)/2
if aw < self.vmaw:
wx.LogError(f'Via annular width is smaller than min allowed by DRC ({pcbnew.ToMM(aw)} < {pcbnew.ToMM(self.vmaw)})')
return -1
else:
wx.LogError('Invalid PCB preset!')
return -1
# helpers
self.dr = self.trk_w + self.min_clear
self.dt = self.d_via + self.min_clear
# get gui values and fix units (mm converted to nm where needed)
self.outline = self.m_cbOutline.GetStringSelection()
if self.outline=="Circle":
self.n_edges = 0
elif self.outline=="Square":
self.n_edges = 4
elif self.outline=="Hexagon":
self.n_edges = 6
elif self.outline=="Octagon":
self.n_edges = 8
else:
self.n_edges = -1
# terminals
self.trmtype = self.m_cbTP.GetStringSelection()
self.n_term = 3 #2 if (self.m_cbScheme.GetStringSelection() == "1P") else (
#3 if (self.m_cbScheme.GetStringSelection() == "3P") else 4)
self.n_layers = int(self.m_ctrlLayers.GetValue())
self.lset = self.udpate_lset(self.n_layers)
self.n_loops = int(self.m_ctrlLoops.GetValue())
self.n_phases = int(1 if (self.m_cbScheme.GetStringSelection() == "1P") else 3)
self.n_slots = int(self.m_ctrlSlots.GetValue())
self.strategy = self.m_cbStrategy.GetSelection()
# TODO: this to become a parameter (e.g. via grid size or something)
self.via_rows = 2
self.r_fill = int(self.m_ctrlRfill.GetValue() * self.SCALE) # track fillet
self.o_fill = int(self.m_ctrlFilletRadius.GetValue() * self.SCALE) # outline fillet
# various radius
self.r_in = int(self.m_ctrlDbore.GetValue() /2 * self.SCALE)
self.r_out = int(self.m_ctrlDout.GetValue() /2 * self.SCALE) # board size
self.r_coil_in = int(self.m_ctrlDin.GetValue() /2 * self.SCALE )
self.r_coil_out = int(self.m_ctrlDend.GetValue() /2 * self.SCALE )
self.w_mnt = int(self.m_ctrlWmnt.GetValue() * self.SCALE) # width of the outer no-soldermask annular zone
# TODO: place terminals based on their size
#self.rtrm = int(self.m_ctrlDterm.GetValue()/2 * self.SCALE)
self.r_term = (self.r_coil_out + (self.r_coil_out + self.w_mnt)) / 2
self.mhs = self.m_cbMountSize.GetStringSelection()
self.n_mh_out = int(self.m_mhOut.GetValue())
self.r_mh_out = int(self.m_mhOutR.GetValue() /2 * self.SCALE)
self.n_mh_in = int(self.m_mhIn.GetValue())
self.r_mh_in = int(self.m_mhInR.GetValue() /2 * self.SCALE)
# text
self.txt_size = int(0.5 * self.SCALE)
self.txt_loc = int(self.r_out - 3*self.txt_size)
# init buttons state
if self.group:
self.btn_clear.Enable(True)
return 0
def init_path(self):
self.fp_path = None
# check modified paths
settings = pcbnew.SETTINGS_MANAGER.GetUserSettingsPath()
try:
with open(settings+'/kicad_common.json', 'r') as f:
data = json.load(f)
if not (data["environment"]["vars"] is None) and "KICAD6_FOOTPRINT_DIR" in data["environment"]["vars"]:
self.fp_path = data["environment"]["vars"]["KICAD6_FOOTPRINT_DIR"]
except IOError:
# catch missing file on first Kicad run
wx.LogError("Settings file not found. This might happen, the first time you run the program. Please restart Kicad")
return
# check default paths
if self.fp_path is None:
self.fp_path = os.getenv("KICAD"+str(self.KICAD_VERSION)+"_FOOTPRINT_DIR", default=None)
# ensure only one separator
self.fp_path = os.path.normpath(self.fp_path) + os.sep
# no library found
if self.fp_path is None:
wx.LogError("Footprint library not found - Make sure the KiCad paths are properly configured.")
def init_nets(self):
# init paths
gnd = pcbnew.NETINFO_ITEM(self.board, "gnd")
coil = pcbnew.NETINFO_ITEM(self.board, "coil")
self.board.Add(gnd)
self.board.Add(coil)
def udpate_lset(self, n_layers):
lset = [pcbnew.F_Cu]
if n_layers >= 4:
lset.append(pcbnew.In1_Cu)
lset.append(pcbnew.In2_Cu)
if n_layers >= 6:
lset.append(pcbnew.In3_Cu)
lset.append(pcbnew.In4_Cu)
if n_layers >= 8:
lset.append(pcbnew.In5_Cu)
lset.append(pcbnew.In6_Cu)
if n_layers >= 10:
lset.append(pcbnew.In7_Cu)
lset.append(pcbnew.In8_Cu)
if n_layers >= 12:
lset.append(pcbnew.In9_Cu)
lset.append(pcbnew.In10_Cu)
if n_layers >= 14:
lset.append(pcbnew.In11_Cu)
lset.append(pcbnew.In12_Cu)
if n_layers >= 16:
lset.append(pcbnew.In13_Cu)
lset.append(pcbnew.In14_Cu)
if n_layers >= 18:
lset.append(pcbnew.In15_Cu)
lset.append(pcbnew.In16_Cu)
if n_layers >= 20:
lset.append(pcbnew.In17_Cu)
lset.append(pcbnew.In18_Cu)
lset.append(pcbnew.B_Cu)
return lset
# core functions
def generate(self):
# get GUI parameters
ret = self.get_parameters()
if ret<0:
return
# generate and connect coils
coils = self.do_windings(
self.r_coil_in,
self.r_coil_out,
self.dr,
self.n_slots,
self.n_phases,
self.n_loops,
self.lset,
self.strategy)
# generate phase rings
[cnx_seg, cnx_str, cri] = self.do_rings(
self.r_coil_in,
self.d_via,
self.n_slots,
self.n_phases)
self.do_junctions(
self.r_coil_in,
self.dr,
self.n_slots,
self.n_phases,
coils,
cnx_seg,
cnx_str)
# terminals
if self.trmtype != "None":
trm_lib = self.fp_path + ('Connector_Wire.pretty' if self.trmtype=='THT' else 'TestPoint.pretty')
trm_fp = self.term_db.get(self.trmtype).get(self.m_termSize.GetStringSelection())
self.do_terminals(
self.r_term,
self.n_term,
self.r_coil_in,
coils,
trm_lib, trm_fp)
# create outline, mounting holes and thermal zones
if self.outline != "None":
self.do_outline(
self.r_in,
self.r_out,
self.n_edges,
self.o_fill)
self.do_mounting_holes(
self.r_mh_out,
self.n_mh_out if self.outline=="Circle" else self.n_edges if self.n_mh_out>0 else 0,
self.r_mh_in,
self.n_mh_in if self.outline=="Circle" else self.n_edges if self.n_mh_in>0 else 0,
self.n_edges,
hs=self.mhs)
self.do_thermal_zones(
self.r_out,
cri)
# draw silkscreen
self.do_silkscreen(
self.r_coil_in,
self.r_coil_out+self.trk_w,
self.n_slots)
# update board
pcbnew.Refresh()
# TODO: all stats shall be moved here
# stats (single pole only)
#self.board.Groups()[0].RunOnChildren(item: {item.Type()})
#wx.LogWarning(f'{item.Type()}')
#if p==0:
self.tl = 0 #self.stats_length(self.board)
self.tr = 0 #self.stats_rlc(self.board, self.trk_w/self.SCALE/1000, self.tthick)
# update gui stats
self.lbl_phaseLength.SetLabel( '%.2f' % self.tl )
self.lbl_phaseR.SetLabel( '%.2f' % self.tr )
self.btn_clear.Enable(True)
def coil_tracker(self, waypts, layer, n_loops, group):
""" Connnect the coil waypoints with PCB tracks on the assigned layer
Args:
waypts (numpy.matrix): set of the coil waypoints
layer (pcbnew.LAYER): layer on which the tracks are created
n_loops (int): number of coil loops
Returns:
list : list of coil start and end points
"""
net_coil = self.board.FindNet("coil")
# index of current point
ip = 0
t0 = None
# define how many sides
n_side = n_loops*4 - 1
for side in range(n_side):
# side start point
side_s = self.fpoint(
int(waypts[ip][0,0]),
int(waypts[ip][0,1]))
# even sides (0,2,4,etc.)
if not side%2:
ip += 1
t = pcbnew.PCB_ARC(self.board)
t.SetMid(
self.fpoint(
int(waypts[ip][0,0]),
int(waypts[ip][0,1])))
# 1: outer side, -1: inner side
side = -1 if not side%4 else 1
# odd sides
else:
t = pcbnew.PCB_TRACK(self.board)
ip += 1
side_e = self.fpoint(
int(waypts[ip][0,0]),
int(waypts[ip][0,1]))
t.SetNet(net_coil)
t.SetWidth( self.trk_w )
t.SetLayer( layer )
t.SetStart( side_s )
t.SetEnd( side_e )
self.board.Add(t)
# fillet
if side > 0 and self.r_fill > 0:
fa = self.fillet(self.board, t0, t, self.r_fill, side)
#group.AddItem(fa)
# add to group
group.AddItem(t)
t0 = t
coil_se = []
coil_se.append( self.fpoint( int(waypts[0][0,0]), int(waypts[0][0,1]) ) )
coil_se.append( t.GetEnd() )
return coil_se
def do_windings(self, ri, ro, dr, n_slots, n_phases, n_loops=1, lset=None, mode=0):
""" Generate the coil tracks (with fillet) on the given PCB layers
Args:
ri (int): coil inner radius
ro (int): coil outer radius
dr (int): min track spacing
n_slots (int): number of motor slots
n_loops (int): number of coil loops (per layer)
lset (list): list of the PCB layers to use
mode (int): 0: parallel coil sides, 1: radial coil sides
Returns:
list: all windings start and end points, grouped by phase
"""
net_coil = self.board.FindNet("coil")
th0 = 2*math.pi/n_slots
r_clear = self.trk_w/2 + self.min_clear + self.d_via/2
t_clear = self.d_via + self.min_clear
r_via = ri - r_clear
thv = math.atan2(t_clear, r_via)
r_via_o = ro - (n_loops-1)*self.dr - r_clear
thv_o = math.atan2(t_clear, r_via_o)
n_via = len(lset)/2
vias = range(0,1) if n_via==1 else range(int(-n_via/2), math.ceil(n_via/2))
# generate coil waypoints
if mode == 0:
# TODO: update
waypts_cw, pcu0m, pcu0mi = ksolve.parallel( ri, ro, dr, th0, n_loops, 0 )
waypts_ccw, pcu1m, pcu1mi = ksolve.parallel( ri, ro, dr, th0, n_loops, 1 )
else:
waypts_cw = ksolve.coil_planner( "radial", ri, ro, dr, n_slots, n_loops, "cw" )
waypts_ccw = ksolve.coil_planner( "radial", ri, ro, dr, n_slots, n_loops, "ccw" )
waypts_cw1 = ksolve.coil_planner( "radial", ri, ro, dr, n_slots, n_loops, "cw", 1 )
waypts_ccw1 = ksolve.coil_planner( "radial", ri, ro, dr, n_slots, n_loops, "ccw", 1 )
windings = []
for p in range(n_phases):
windings.append([])
for slot in range(n_slots):
pgroup = pcbnew.PCB_GROUP( self.board )
pgroup.SetName("slot_"+str(slot))
self.board.Add(pgroup)
# start/end points of single winding
winding_se = []
# rotation matrix
th = th0 * slot
R = np.array([
[math.cos(th), -math.sin(th)],
[math.sin(th), math.cos(th)]])
# rotate coil waypoints to the slot position (around PCB motor axis)
waypts_cw_R = np.matmul(R, waypts_cw.transpose()).transpose()
waypts_ccw_R = np.matmul(R, waypts_ccw.transpose()).transpose()
waypts_cw1_R = np.matmul(R, waypts_cw1.transpose()).transpose()
waypts_ccw1_R = np.matmul(R, waypts_ccw1.transpose()).transpose()
for i, layer in enumerate(lset):
# pick the right "template" of waypoints
if i==0 or i==len(lset)-1:
# outer layers: ...
wp = waypts_ccw_R if i%2 else waypts_cw_R
else:
# inner layers: ...
wp = waypts_ccw1_R if i%2 else waypts_cw1_R
# generate coil
coil_se = self.coil_tracker(wp, layer, n_loops, pgroup)
# connect coils across layers to create the winding
if i%2 and i<len(lset):
# on odd layers (coil CCW) stitch coils' end points
iv = math.floor(i/2)
# via index
viv = vias[iv]
# via coordinates
xy_v = self.fpoint(
int(r_via_o*math.cos(th + thv_o*viv)),
int(r_via_o*math.sin(th + thv_o*viv)))
# stitch
via = pcbnew.PCB_VIA(self.board)
via.SetViaType(pcbnew.VIATYPE_THROUGH)
via.SetPosition( xy_v )
via.SetDrill( self.d_drill )
via.SetWidth( self.d_via )
self.board.Add(via)
#
a = pcbnew.PCB_ARC(self.board)
a.SetNet(net_coil)
a.SetLayer(lset[i-1])
a.SetWidth(self.trk_w)
a.SetStart( coil_se[1] )
a.SetEnd(
self.fpoint(
int((r_via_o+r_clear)*math.cos(th + thv_o*vias[iv])),
int((r_via_o+r_clear)*math.sin(th + thv_o*vias[iv]))))
a.SetMid(
self.fpoint(
int((r_via_o+r_clear)*math.cos(th + thv_o*vias[iv] /2 )),
int((r_via_o+r_clear)*math.sin(th + thv_o*vias[iv] /2 ))))
self.board.Add(a)
if viv > 0:
a = pcbnew.PCB_ARC(self.board)
a.SetNet(net_coil)
a.SetLayer(lset[i])
a.SetWidth(self.trk_w)
a.SetStart( coil_se[1] )
a.SetEnd(
self.fpoint(
int((r_via_o+r_clear)*math.cos(th + thv_o*viv)),
int((r_via_o+r_clear)*math.sin(th + thv_o*viv))))
a.SetMid(
self.fpoint(
int((r_via_o+r_clear)*math.cos(th + thv_o*viv /2 )),
int((r_via_o+r_clear)*math.sin(th + thv_o*viv /2 ))))
self.board.Add(a)
t = pcbnew.PCB_TRACK(self.board)
t.SetNet(net_coil)
t.SetWidth( self.trk_w )
t.SetLayer( lset[i] )
t.SetStart( a.GetEnd() )
t.SetEnd( xy_v )
self.board.Add(t)
t = pcbnew.PCB_TRACK(self.board)
a.SetNet(net_coil)
t.SetWidth( self.trk_w )
t.SetLayer( lset[i-1] )
t.SetStart( a.GetEnd() )
t.SetEnd( xy_v )
self.board.Add(t)
# on even layers (coil CW), but first, stitch coils' start points
elif not i%2 and i>0:
iv = int(i/2)
# via index
viv = vias[iv]
xy_v = self.fpoint(
int(r_via*math.cos(th + thv*viv)),
int(r_via*math.sin(th + thv*viv)))
# stitch
via = pcbnew.PCB_VIA(self.board)
via.SetViaType(pcbnew.VIATYPE_THROUGH)
via.SetPosition( xy_v)
via.SetDrill( self.d_drill )
via.SetWidth( self.d_via )
self.board.Add(via)
a = pcbnew.PCB_ARC(self.board)
a.SetNet(net_coil)
a.SetLayer(lset[i-1])
a.SetWidth(self.trk_w)
a.SetStart( coil_se[0] )
a.SetEnd(
self.fpoint(
int((r_via+r_clear)*math.cos(th + thv*viv)),
int((r_via+r_clear)*math.sin(th + thv*viv))))
a.SetMid(
self.fpoint(
int((r_via+r_clear)*math.cos(th + thv*viv /2 )),
int((r_via+r_clear)*math.sin(th + thv*viv /2 ))))
self.board.Add(a)
if viv<0:
a = pcbnew.PCB_ARC(self.board)
a.SetNet(net_coil)
a.SetLayer(lset[i])
a.SetWidth(self.trk_w)
a.SetStart( coil_se[0] )
a.SetEnd(
self.fpoint(
int((r_via+r_clear)*math.cos(th + thv*viv)),
int((r_via+r_clear)*math.sin(th + thv*viv))))
a.SetMid(
self.fpoint(
int((r_via+r_clear)*math.cos(th + thv*viv /2 )),
int((r_via+r_clear)*math.sin(th + thv*viv /2 ))))
self.board.Add(a)
t = pcbnew.PCB_TRACK(self.board)
t.SetNet(net_coil)
t.SetWidth( self.trk_w )
t.SetLayer( lset[i] )
t.SetStart( a.GetEnd() )
t.SetEnd( xy_v )
self.board.Add(t)
t = pcbnew.PCB_TRACK(self.board)
t.SetNet(net_coil)
t.SetWidth( self.trk_w )
t.SetLayer( lset[i-1] )
t.SetStart( a.GetEnd() )
t.SetEnd( xy_v )
self.board.Add(t)
# append first and last only
if i==0 or i==len(lset)-1:
winding_se.append(coil_se[0])
# store coils by phase
windings[ slot % n_phases ].append(winding_se)
return windings
def do_rings(self, r_in, dr, n_slot, n_phase):
""" Create the arc segments that connect the coils of the same phase
Args:
r_in (int): radial position of the coil inner end
dr (int): clearance
n_slot (int): number of motor slots
n_phase (int): number of motor phases
"""
# rings spaced from coils
r_in -= 2*dr
# slot angular width
th0 = 2*math.pi/n_slot
# segment start/end points angular shift
th_shift_start = th0/2
th_shift_end = -th0
# number of distinct segments on each phase ring
n_rc = int(n_slot/n_phase)-1
# all segments, of all the phase rings
conns_t = []
# phase-phase connection
cnx_star = None
# create phase tracks
for p in range(n_phase):
# radial location of the ring
cri = r_in - p*dr
thv = math.atan2(2*self.trk_w,cri)
# segments on the same phase ring
segs_t = []
for i_rc in range(n_rc):
# find directions of start, end, mid points of the arc segment
th_s = th0*( p + i_rc*n_phase ) + th_shift_start - thv
th_e = th_s + th0*n_phase + th_shift_end + 2*thv
th_m = (th_s+th_e)/2
# translate (r,th) to (x,y) coords
xy_s = self.fpoint( int(cri*math.cos(th_s)), int(cri*math.sin(th_s)) )
xy_e = self.fpoint( int(cri*math.cos(th_e)), int(cri*math.sin(th_e)) )
xy_m = self.fpoint( int(cri*math.cos(th_m)), int(cri*math.sin(th_m)) )
conn = pcbnew.PCB_ARC(self.board)
conn.SetLayer(pcbnew.F_Cu)
conn.SetWidth(self.trk_w)
conn.SetStart( xy_s )
conn.SetMid( xy_m )
conn.SetEnd( xy_e )
self.board.Add(conn)
# track ends towards the coil (towards motor internals)
segs_t.append([xy_s,xy_e])
conns_t.append(segs_t)
# star-connection on 1st phase ring
if p==0:
# find directions
ths = -3*th0 + th0/2 - thv
the = -1*th0 + th0/2 - thv
thm = (ths+the)/2
# translate (r,th) to (x,y) coords
rs = self.fpoint( int(cri*math.cos(ths)), int(cri*math.sin(ths)) )
rm = self.fpoint( int(cri*math.cos(thm)), int(cri*math.sin(thm)) )
re = self.fpoint( int(cri*math.cos(the)), int(cri*math.sin(the)) )
# do track
conn = pcbnew.PCB_ARC(self.board)
conn.SetLayer(pcbnew.B_Cu)
conn.SetWidth(self.trk_w)
conn.SetStart( rs )
conn.SetMid( rm )
conn.SetEnd( re )
self.board.Add(conn)
# here append also the mid point
cnx_star = [rs,rm,re]
# return:
# - ring segments coordinates
# - star connection coordinates
# - radial location of the innermost ring + some (used for filling keep-out)
return conns_t, cnx_star, cri-2*dr
def do_junctions(self, r_in, dr, n_slot, n_phase, coils, rings, cnx_str):
""" Create the jumper segments that connect the coil terminations with the
race tracks of the phase connecting rings
Args:
r_in (int): radial position of the coil inner end
dr (int): min track spacing
n_slot (int): number of motor slots
n_phase (int): number of motor phases
coils (list): contains start and end points of each coil, by phase
rings (list): contains start and end points of each ring segment, by phase
"""
# slot angular width
th0 = 2*math.pi/n_slot
thv = math.atan2(dr,r_in)
th_shift_start = th0/2 - thv
th_shift_end = -2*th_shift_start
r_in -= dr
# number of segments on each phase ring
n_ring_seg = int(n_slot/n_phase)-1
for phase in range(n_phase):
for seg in range(n_ring_seg):
ring_seg_s = rings[phase][seg][0]
ring_seg_e = rings[phase][seg][1]
#c1s = coil_p[p][i][0]
coil_1_e = coils[phase][seg][1]
coil_2_s = coils[phase][seg+1][0]
coil_2_e = coils[phase][seg+1][1]
if seg <= n_ring_seg:
j = pcbnew.PCB_TRACK(self.board)
j.SetLayer(pcbnew.B_Cu)
j.SetWidth(self.trk_w)
j.SetStart( coil_1_e )
j.SetEnd( ring_seg_s )
self.board.Add(j)
if seg == 0:
via = pcbnew.PCB_VIA(self.board)
via.SetPosition( ring_seg_s )
via.SetDrill( self.d_drill )
via.SetWidth( self.d_via )
self.board.Add(via)
if phase==0 or seg==n_ring_seg-1:
j = pcbnew.PCB_TRACK(self.board)
j.SetLayer(pcbnew.F_Cu)
j.SetWidth(self.trk_w)
j.SetStart( coil_2_s )
j.SetEnd( ring_seg_e )
self.board.Add(j)
else:
# cross under the other phases
th_s = th0*( phase + seg*n_phase ) + th_shift_start
th_e = th_s + th0*n_phase + th_shift_end
xy_a = self.fpoint(
int(r_in*math.cos(th_e)),
int(r_in*math.sin(th_e)))
j = pcbnew.PCB_TRACK(self.board)
j.SetLayer(pcbnew.B_Cu)
j.SetWidth(self.trk_w)
j.SetStart( ring_seg_e )
j.SetEnd( xy_a )
self.board.Add(j)
via = pcbnew.PCB_VIA(self.board)
via.SetPosition( ring_seg_e )
via.SetDrill( self.d_drill )
via.SetWidth( self.d_via )
self.board.Add(via)
j = pcbnew.PCB_TRACK(self.board)
j.SetLayer(pcbnew.F_Cu)
j.SetWidth(self.trk_w)
j.SetStart( xy_a )
j.SetEnd( coil_2_s )
self.board.Add(j)
via = pcbnew.PCB_VIA(self.board)
via.SetPosition( xy_a )
via.SetDrill( self.d_drill )
via.SetWidth( self.d_via )
self.board.Add(via)
if seg > 0:
# jumper at each coil start, but the first coil, for each phase
j = pcbnew.PCB_TRACK(self.board)
j.SetLayer(pcbnew.B_Cu)
j.SetWidth(self.trk_w)
j.SetStart( coil_1_e )
j.SetEnd( ring_seg_s )
self.board.Add(j)
via = pcbnew.PCB_VIA(self.board)
via.SetPosition( ring_seg_s )
via.SetDrill( self.d_drill )
via.SetWidth( self.d_via )
self.board.Add(via)
# star-connect jumper
if seg == n_ring_seg-1:
j = pcbnew.PCB_TRACK(self.board)
j.SetLayer(pcbnew.B_Cu)
j.SetWidth(self.trk_w)
j.SetStart( coil_2_e )
j.SetEnd( cnx_str[phase] )
self.board.Add(j)
def do_terminals(self, r_term, n_term, r_coil_in, coils, lib='',fp=''):
""" Create the motor terminals, and the tracks that connect the terminals to the coils
Args:
r_term (in): radial position of the motor terminals
n_term (in): number of motor terminals
r_coil_in (in): radial position of the coil inner end
coils (list): list of coils tracks
lib (string): footprint library absolute path
fp (string): terminal footprint name
"""
net_coil = self.board.FindNet("coil")
# base angle, half (to position terminals), quarter (to locate arc track mid-point)
th0 = 2*math.pi/self.n_slots
thr = th0/4
Rcw = np.array([
[math.cos(thr), -math.sin(thr)],
[math.sin(thr), math.cos(thr)],
])
for i in range(n_term):
# radial to cartesian coords of terminal, aux corner, coil start
th = th0*i - th0/2
ax = r_coil_in*math.cos(th)
ay = r_coil_in*math.sin(th)
tp = np.matmul(Rcw, np.array([ax,ay]))
xy_t = self.fpoint(
int(r_term*math.cos(th)),
int(r_term*math.sin(th)))
xy_a = self.fpoint( int(ax), int(ay) )
xy_c = coils[i][0][0]
# terminal
if self.trmtype != "None":
m = pcbnew.FootprintLoad(lib, fp)
m.Value().SetVisible(False)
lib_name= lib.split('.')[-2].split('/')[-1]
m.SetFPIDAsString(lib_name + ":" + fp)
m.SetPosition(xy_t)
m.Rotate(xy_t, self.eda_angle(-th))
for p in m.Pads():
p.SetNet(net_coil)
# REF
dth = 0.05 #rad
m.Reference().SetPosition(
self.fpoint(
int(r_term*math.cos(th+dth)),
int(r_term*math.sin(th+dth))))
m.SetReference( "A" if i==0 else ("B" if i==1 else "C") )
self.board.Add(m)
# track, straight part
conn = pcbnew.PCB_TRACK(self.board)
conn.SetLayer(pcbnew.F_Cu)
conn.SetNet(net_coil)
conn.SetWidth(self.trk_w)
conn.SetStart(xy_t)
conn.SetEnd(xy_a)
self.board.Add(conn)
# track, arc part
conn = pcbnew.PCB_ARC(self.board)
conn.SetLayer(pcbnew.F_Cu)
conn.SetNet(net_coil)
conn.SetWidth(self.trk_w)
conn.SetStart(xy_a)
conn.SetMid( self.fpoint( int(tp[0]), int(tp[1])) )
conn.SetEnd(xy_c)
self.board.Add(conn)
def do_outline(self, r_in, r_out, n_edge=0, r_fill=0):