2018.12.8-hex-speaker-spiral.py 4.94 KB
Newer Older
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
#!/usr/bin/env python
from __future__ import division,absolute_import
import rhinoscriptsyntax as rs
from math import *	
import sys

#simple class for vec2
class V2(object):
	def __init__(self,*args):
		if len(args)>1:
			self.x = args[0]
			self.y = args[1]
		else:
			self.x = args[0][0]
			self.y = args[0][1]
		self.p3l = [self.x,self.y,0]
	def __add__(self,other):
		return V2(self.x+other.x,self.y+other.y)
	def __sub__(self,other):
		return V2(self.x-other.x,self.y-other.y)
	def __mul__(self,other):
		try:
			return V2(self.x*other.x,self.y*other.y)
		except(AttributeError):
			return V2(self.x*other,self.y*other)
	def __rmul__(self,other):
		try:
			return V2(self.x*other.x, self.y*other.y)
		except(AttributeError):
			return V2(self.x*other,self.y*other)
	def __getitem__(self,index):
		return [self.x,self.y][index]
	def __repr__(self):
		return "V2(%.6f,%.6f)"%(self.x,self.y)
	def rotate(self,th):
		return V2(self.x*cos(th)-self.y*sin(th), self.x*sin(th)+self.y*cos(th))
	def rotate90(self):
		return V2(-self.y,self.x)
	def rotate_p(self,b,th):
		return b + (self-b).rotate(th)
	def magnitude(self):
		return sqrt(self.x*self.x + self.y*self.y)
	def normalized(self):
		return self*(1./self.magnitude())
	def dot(self,other):
		return self.x*other.x + self.y*other.y
	def cross(self,other):
		return self.x*other.y - self.y*other.x
	def angle_between(self,other):
		#unsigned angle between two vectors
		c = self.cross(other)
		return atan2(c,self.dot(other))
	def projected_onto(self,other):
		return ((self.dot(other))/(other.dot(other)))*other
	def projected_orthogonal_to(self,other):
		return self - self.projected_onto(other)

	def close(self,other,tol=1e-6):
		return (abs(self.x-other.x)<tol) and (abs(self.y-other.y)<tol)
	def p3lz(self,z):
		return [self.x,self.y,z]

# a few helper functions
def line(p1,p2,layer,bridge_w=0,cut_w=0):
	d = p2-p1; dl = d.magnitude()
	if dl==0:
		return None
	dn = d.normalized()
	if bridge_w==0 or cut_w==0: 
		rs.CurrentLayer(layer)
		return rs.AddLine(p1.p3l, p2.p3l)
	else:
		rs.CurrentLayer(layer)
		output = []; dist = bridge_w
		ds = []
		while dist < dl-2*bridge_w:#-cut_w:
			ds.append((dist, dist+cut_w))
			#print bridge_w, (p1+dist*dn).p3l , (p1+(dist+bridge_w)*dn).p3l
			dist += cut_w+bridge_w
		#leftover = dl-bridge_w-cut_w - dist + cut_w+bridge_w
		leftover = dl-bridge_w - dist + cut_w+bridge_w
		for pair in ds:
			output.append(rs.AddLine( (p1+(pair[0] + leftover/2)*dn).p3l , (p1+(pair[1]+ leftover/2)*dn).p3l) )

		return output
def circle(c,d,layer):
	rs.CurrentLayer(layer)
	return rs.AddCircle(c.p3l, .5*d)
def arc(c,d,th1,th2,layer):
	rs.CurrentLayer(layer)
	p1 = c + d/2*V2(cos(pi/180.*th1),sin(pi/180.*th1))
	p2 = c + d/2*V2(cos(pi/180.*th2),sin(pi/180.*th2))
	pm = c + d/2*V2(cos(pi/180.*(th1+th2)/2),sin(pi/180.*(th1+th2)/2))
	return rs.AddArc3Pt(p1.p3l,p2.p3l,pm.p3l)
def filleted_hex(c,R,r,layer):
	crvs = []
	x = r/sqrt(3)
	for i in range(6):
		v0 = R*V2(cos(i*2*pi/6),sin(i*2*pi/6))
		v1 = R*V2(cos((i+1)*2*pi/6),sin((i+1)*2*pi/6))
		d = (v1 - v0).normalized()
		crvs.append( line( c+v0 + x*d, c+v1 - x*d, layer) )
		crvs.append( arc( c+v0-2*x*v0.normalized(),2*r,-30+i*60,30+i*60, layer) )
	return crvs






#main
def main():
	rs.AddLayer('magnets_a',(255,0,0))
	rs.AddLayer('magnets_b',(0,255,255))
	rs.AddLayer('holes',(0,255,0))
	rs.AddLayer('coils',(0,0,255))
	rs.AddLayer('frame',(255,0,255))

	mag_d = 3.12 #mm, diameter of magnets, as cut by laser
	hole_d = 6 #mm, diameter of air hole
	s = 6 #mm, hex lattice side length (2xmag_d?)
	s32 = s*sqrt(3)/2.
	frame_inner = 60 #mm, radius / side length of inner hex of frame 
	frame_inner_fillet = 10 #mm, fillet radius
	frame_outer = 80 #mm, radius / side length of outer hex of frame 
	frame_outer_fillet = 20 #mm, fillet radius
	frame_bolt_d = 4.1 #mm, diameter of bolt holes

	wire_pitch = 2*.088 #mm, pitch, .088 = measured diameter (.080) + .008 mm slop (10% applied)
	N = 11 #number of turns
	Nr = 4 #number of radial layers in the hex lattice

	#make frame
	frame = []
	frame += filleted_hex(V2(0,0), frame_inner, frame_inner_fillet, 'frame')
	frame += filleted_hex(V2(0,0), frame_outer, frame_outer_fillet, 'frame')
	for i in range(6):
		v0 = .5*(frame_inner+frame_outer)*V2(cos(i*2*pi/6),sin(i*2*pi/6))
		v1 = .5*(frame_inner+frame_outer)*V2(cos((i+1)*2*pi/6),sin((i+1)*2*pi/6))
		frame += [
			circle(v0, frame_bolt_d, 'frame'),
			circle(.5*(v0+v1), frame_bolt_d, 'frame'),
			]

	#make magnet grid and air hole grid
	magnets = [];
	magnets += [circle(V2(0,0),mag_d,'magnets_a')]
	for i in range(3): #3-fold angular symmetry
		vr = V2(cos(i*2*pi/3),sin(i*2*pi/3))
		vth = V2(cos(i*2*pi/3+pi/2),sin(i*2*pi/3+pi/2))
		vk = V2(cos((i+1)*2*pi/3),sin((i+1)*2*pi/3))
		for j in range(Nr):
			for k in range(Nr+1):
				magnets += [circle(2*s32*vr*(j+1) + 2*s32*vk*k, mag_d, 'magnets_a')]
				if k<Nr:
					magnets += [
						circle(2*s32*vr*(j+.5) + 2*s32*vk*k + .5*s*vth, mag_d, 'magnets_b'),
						circle(2*s32*vr*j + 2*s32*vk*k + s*vth, hole_d, 'holes'),
					]			

if __name__ == '__main__':
	main()