-
Notifications
You must be signed in to change notification settings - Fork 12
/
kimotor_solver.py
206 lines (165 loc) · 7.08 KB
/
kimotor_solver.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
# Copyright 2022-2024 Stefano Cottafavi <[email protected]>.
# SPDX-License-Identifier: GPL-2.0-only
import math
import numpy as np
from . import kimotor_linalg as kla
import wx
def coil_planner(type, r_in, r_out, dr, n_slot, n_loop, dir, start=0):
if type == "radial":
return radial(r_in, r_out, dr, n_slot, n_loop, dir, start)
def parallel(r1, r2, dr, th, turns, dir):
""" Compute layout points for coil with sides always aligned to radius
Args:
r1 (int): coil inner radius
r2 (int): coil outer radius
dr (int): spacing between coil loops (and also adj. coils)
th (float): coil trapezoid angle
turns (int): number of coil loops (windings)
dir (int): coil direction, from larger to smaller loop (0:CW normal, 1:CCW rverse)
Returns:
matrix, matrix, matrix: corners (excl. arc mids), outer arc mids, inner arc mids
"""
pts = []
mds = []
mdsi = []
# points
c = [0,0,0]
l0 = np.array([ c, [r1*math.cos(th/2), r1*math.sin(th/2), 0] ])
l0 = kla.line_offset(l0, -dr)
for turn in range(turns):
# offset line
lr = kla.line_offset(l0, -turn*dr)
# solve corners and order them
pc1 = kla.circle_line_intersect(lr, c, r1+turn*dr)
pc1 = pc1[0:2]
pc2 = kla.circle_line_intersect(lr, c, r2-turn*dr)
pc2 = pc2[0:2]
pc3 = np.array([ pc2[0],-pc2[1] ])
pc4 = np.array([ pc1[0],-pc1[1] ])
if dir == 0:
pts.extend([pc1, pc2, pc3, pc4])
else:
pts.extend([pc4, pc3, pc2, pc1])
# solve outer and inner mid-points
pm = np.array([ r2-turn*dr, 0 ])
pmi = np.array([ r1+turn*dr, 0 ])
mds.extend([pm])
mdsi.extend([pmi])
pm = np.matrix(pts) # points, excl. arc mids
mm = np.matrix(mds) # arc (outer) mids only
mmi = np.matrix(mdsi) # arc (inner) mids only
return pm, mm, mmi
def radial(r_in, r_out, dr, n_slot, n_loop, dir, start=0):
""" Compute layout points for coil with sides aligned to the local radial direction
Args:
r_in (int): coil inner radius
r_out (int): coil outer radius
dr (int): spacing between coil loops (as well as adjacent coils)
n_slot (in): number of slots
n_loop (int): number of coil loops
start (int): where does the coil start (0: wedge start, 1: wedge mid )
dir (string): coil direction, from larger to smaller loop ("cw" or "ccw" from an observer looking down from Z+ axis)
Returns:
numpy.matrix: list of coil waypoints
"""
# coil trapezoid angle
th = 2*math.pi/n_slot
waypts = []
# center
c = [0,0,0]
# angular shift of first arc and last arc mid-mid-points
Rcw = np.array([
[math.cos(th/4), -math.sin(th/4)],
[math.sin(th/4), math.cos(th/4)],
])
# reference radial line
l0 = np.array([ c, [r_in*math.cos(th/2), r_in*math.sin(th/2), 0] ])
for loop in range(n_loop):
# solve corner waypoint
lr = kla.line_offset(l0, -dr)
wp1 = kla.circle_line_intersect(lr, c, r_in + loop*dr)
wp1 = wp1[0:2]
lr = [c, [wp1[0], wp1[1], 0]]
wp2 = kla.circle_line_intersect(lr, c, r_out - loop*dr)
wp2 = wp2[0:2]
wp3 = np.array([ wp2[0], -wp2[1] ])
wp4 = np.array([ wp1[0], -wp1[1] ])
# solve outer and inner arcs mid waypoints
wpo = np.array([ r_out - loop*dr, 0 ])
wpi = np.array([ r_in + loop*dr, 0 ])
# rearrange waypoints
if dir == "cw":
if loop == 0:
if start == 0:
tp = wpi
waypts.extend([wp4, tp[0:2], wp1, wp2, wpo, wp3])
elif start == 1:
tp = np.matmul(Rcw, wpi)
waypts.extend([wpi, tp[0:2], wp1, wp2, wpo, wp3])
elif loop == n_loop-1:
tp = np.matmul(Rcw, wpo)
waypts.extend([wp4, wpi, wp1, wp2, tp[0:2], wpo])
else:
waypts.extend([wp4, wpi, wp1, wp2, wpo, wp3])
else:
if loop == 0:
if start == 0:
tp = wpi
waypts.extend([wp1, tp[0:2], wp4, wp3, wpo, wp2])
elif start == 1:
tp = np.matmul(Rcw, wpi)
waypts.extend([wpi, tp[0:2], wp4, wp3, wpo, wp2])
elif loop == n_loop-1:
tp = np.matmul(Rcw, wpo)
waypts.extend([wp1, wpi, wp4, wp3, tp[0:2], wpo])
else:
waypts.extend([wp1, wpi, wp4, wp3, wpo, wp2])
# move to the next radial
l0 = [ [wp1[0], wp1[1], 0], [wp2[0], wp2[1], 0] ]
waypts_m = np.matrix(waypts)
return waypts_m
def radial_old(r1,r2, dr,th,turns,dir):
""" Compute layout points for coil with sides aligned to the local radial direction
Args:
r1 (int): coil inner radius
r2 (int): coil outer radius
dr (int): spacing between coil loops (and also adj. coils)
th (float): coil trapezoid angle
turns (int): number of coil loops (windings)
dir (int): coil direction, from larger to smaller loop (0:CW normal, 1:CCW rverse)
Returns:
matrix, matrix, matrix: corners (excl. arc mids), outer arc mids, inner arc mids
"""
pts = []
mds = []
mdsi = []
# center
c = [0,0,0]
# first line
l0 = np.array([ c, [r1*math.cos(th/2), r1*math.sin(th/2), 0] ])
for turn in range(turns):
# offset previous line
lr = kla.line_offset(l0, -dr)
# solve corners and order them
pc1 = kla.circle_line_intersect(lr, c, r1+turn*dr)
pc1 = pc1[0:2]
lr = [c, [pc1[0], pc1[1], 0]]
pc2 = kla.circle_line_intersect(lr, c, r2-turn*dr)
pc2 = pc2[0:2]
pc3 = np.array([ pc2[0],-pc2[1] ])
pc4 = np.array([ pc1[0],-pc1[1] ])
if dir == 0:
pts.extend([pc1, pc2, pc3, pc4])
else:
pts.extend([pc4, pc3, pc2, pc1])
# solve outer and inner mid-points
pm = np.array([ r2-turn*dr, 0 ])
pmi = np.array([ r1+turn*dr, 0 ])
mds.extend([pm])
mdsi.extend([pmi])
#
l0 = [ [pc1[0], pc1[1], 0], [pc2[0], pc2[1], 0] ]
pm = np.matrix(pts) # points, excl. arc mids
mm = np.matrix(mds) # arc (outer) mids only
mmi = np.matrix(mdsi) # arc (inner) mids only
return pm, mm, mmi