-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy path2-hexapod.py
276 lines (202 loc) · 7.85 KB
/
2-hexapod.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
import numpy as np
import cadquery as cq
from cadquery_massembly import MAssembly, relocate
from jupyter_cadquery import web_color
from jupyter_cadquery.viewer.client import show
from jupyter_cadquery.animation import Animation
# Parts
thickness = 2
height = 40
width = 65
length = 100
diam = 4
tol = 0.05
def create_base(rotate=False):
x1, x2 = 0.63, 0.87
base_holes = {
"right_back": (-x1 * length, -x1 * width),
"right_middle": (0, -x2 * width),
"right_front": (x1 * length, -x1 * width),
"left_back": (-x1 * length, x1 * width),
"left_middle": (0, x2 * width),
"left_front": (x1 * length, x1 * width),
}
stand_holes = {"front_stand": (0.75 * length, 0), "back_stand": (-0.8 * length, 0)}
workplane = cq.Workplane()
if rotate:
workplane = workplane.transformed(rotate=(30, 45, 60))
base = (
workplane.ellipseArc(length, width, 25, -25, startAtCurrent=False)
.close()
.pushPoints(list(base_holes.values()))
.circle(diam / 2 + tol)
.moveTo(*stand_holes["back_stand"])
.rect(thickness + 2 * tol, width / 2 + 2 * tol)
.moveTo(*stand_holes["front_stand"])
.rect(thickness + 2 * tol, width / 2 + 2 * tol)
.extrude(thickness)
)
base
# tag mating points
if rotate:
l_coord = lambda vec2d: workplane.plane.toWorldCoords(vec2d).toTuple()
l_nps = lambda vec2d: cq.NearestToPointSelector(l_coord(vec2d))
base.faces(f"<{l_coord((0,0,1))}").tag("bottom")
base.faces(f">{l_coord((0,0,1))}").tag("top")
for name, hole in base_holes.items():
base.faces(f"<{l_coord((0,0,1))}").edges(l_nps(hole)).tag(name)
for name, hole in stand_holes.items():
base.faces(f"<{l_coord((0,0,1))}").wires(l_nps(hole)).tag(name)
else:
base.faces("<Z").tag("bottom")
base.faces(">Z").tag("top")
for name, hole in base_holes.items():
base.faces("<Z").wires(cq.NearestToPointSelector(hole)).tag(name)
for name, hole in stand_holes.items():
base.faces("<Z").wires(cq.NearestToPointSelector(hole)).tag(name)
return base
base_holes_names = {
"right_back",
"right_middle",
"right_front",
"left_back",
"left_middle",
"left_front",
}
def create_stand():
stand = cq.Workplane().box(height, width / 2 + 10, thickness)
inset = cq.Workplane().box(thickness, width / 2, thickness)
backing = cq.Workplane("ZX").polyline([(10, 0), (0, 0), (0, 10)]).close().extrude(thickness)
stand = (
stand.union(inset.translate(((height + thickness) / 2, 0, 0)))
.union(inset.translate((-(height + thickness) / 2, 0, 0)))
.union(backing.translate((-height / 2, -thickness / 2, thickness / 2)))
.union(backing.rotate((0, 0, 0), (0, 1, 0), -90).translate((height / 2, -thickness / 2, thickness / 2)))
)
return stand
stand_names = ("front_stand", "back_stand")
def create_upper_leg():
l1, l2 = 50, 80
pts = [(0, 0), (0, height / 2), (l1, height / 2 - 5), (l2, 0)]
upper_leg_hole = (l2 - 10, 0)
upper_leg = (
cq.Workplane()
.polyline(pts)
.mirrorX()
.pushPoints([upper_leg_hole])
.circle(diam / 2 + tol)
.extrude(thickness)
.edges("|Z and (not <X)")
.fillet(4)
)
axle = (
cq.Workplane("XZ", origin=(0, height / 2 + thickness + tol, thickness / 2))
.circle(diam / 2)
.extrude(2 * (height / 2 + thickness + tol))
)
upper_leg = upper_leg.union(axle)
# tag mating points
upper_leg.faces(">Z").edges(cq.NearestToPointSelector(upper_leg_hole)).tag("top")
upper_leg.faces("<Z").edges(cq.NearestToPointSelector(upper_leg_hole)).tag("bottom")
return upper_leg
def create_lower_leg():
w, l1, l2 = 15, 20, 120
pts = [(0, 0), (l1, w), (l2, 0)]
lower_leg_hole = (l1 - 10, 0)
lower_leg = (
cq.Workplane()
.polyline(pts)
.mirrorX()
.pushPoints([lower_leg_hole])
.circle(diam / 2 + tol)
.extrude(thickness)
.edges("|Z")
.fillet(5)
)
# tag mating points
lower_leg.faces(">Z").edges(cq.NearestToPointSelector(lower_leg_hole)).tag("top"),
lower_leg.faces("<Z").edges(cq.NearestToPointSelector(lower_leg_hole)).tag("bottom")
return lower_leg
leg_angles = {
"right_back": -105,
"right_middle": -90,
"right_front": -75,
"left_back": 105,
"left_middle": 90,
"left_front": 75,
}
leg_names = list(leg_angles.keys())
base = create_base(rotate=False)
stand = create_stand()
upper_leg = create_upper_leg()
lower_leg = create_lower_leg()
# Assembly
def create_hexapod():
# Some shortcuts
L = lambda *args: cq.Location(cq.Vector(*args))
C = lambda name: web_color(name)
# Leg assembly
leg = MAssembly(upper_leg, name="upper", color=C("orange")).add(
lower_leg, name="lower", color=C("orange"), loc=L(80, 0, 0)
)
# Hexapod assembly
hexapod = (
MAssembly(base, name="bottom", color=C("silver"), loc=L(0, 1.1 * width, 0))
.add(base, name="top", color=C("gainsboro"), loc=L(0, -2.2 * width, 0))
.add(stand, name="front_stand", color=C("SkyBlue"), loc=L(40, 100, 0))
.add(stand, name="back_stand", color=C("SkyBlue"), loc=L(-40, 100, 0))
)
for i, name in enumerate(leg_names):
hexapod.add(leg, name=name, loc=L(100, -55 * (i - 1.7), 0))
return hexapod
# Mates
from collections import OrderedDict as odict
hexapod = create_hexapod()
# show(hexapod)
hexapod.mate("bottom?top", name="bottom", origin=True)
hexapod.mate("top?bottom", name="top", origin=True, transforms=odict(rx=180, tz=-(height + 2 * tol)))
for name in stand_names:
hexapod.mate(f"bottom?{name}", name=f"{name}_bottom", transforms=odict(rz=-90 if "f" in name else 90))
hexapod.mate(f"{name}@faces@<X", name=name, origin=True, transforms=odict(rx=180))
for name in base_holes_names:
hexapod.mate(f"bottom?{name}", name=f"{name}_hole", transforms=odict(rz=leg_angles[name]))
for name in leg_names:
lower, upper, angle = ("top", "bottom", -75) if "left" in name else ("bottom", "top", -75)
hexapod.mate(f"{name}?{upper}", name=f"leg_{name}_hole", transforms=odict(rz=angle))
hexapod.mate(f"{name}@faces@<Y", name=f"leg_{name}_hinge", origin=True, transforms=odict(rx=180, rz=-90))
hexapod.mate(f"{name}/lower?{lower}", name=f"leg_{name}_lower_hole", origin=True)
# show(hexapod, reset_camera=False)
relocate(hexapod)
# Assemble the parts
for leg in leg_names:
hexapod.assemble(f"leg_{leg}_lower_hole", f"leg_{leg}_hole")
hexapod.assemble(f"leg_{leg}_hinge", f"{leg}_hole")
hexapod.assemble("top", "bottom")
for stand_name in stand_names:
hexapod.assemble(f"{stand_name}", f"{stand_name}_bottom")
show(hexapod, render_mates=True, mate_scale=5)
# Animation
horizontal_angle = 25
def intervals(count):
r = [min(180, (90 + i * (360 // count)) % 360) for i in range(count)]
return r
def times(end, count):
return np.linspace(0, end, count + 1)
def vertical(count, end, offset, reverse):
ints = intervals(count)
heights = [round(35 * np.sin(np.deg2rad(x)) - 15, 1) for x in ints]
heights.append(heights[0])
return times(end, count), heights[offset:] + heights[1 : offset + 1]
def horizontal(end, reverse):
factor = 1 if reverse else -1
return times(end, 4), [0, factor * horizontal_angle, 0, -factor * horizontal_angle, 0]
leg_group = ("left_front", "right_middle", "left_back")
animation = Animation()
for name in leg_names:
# move upper leg
animation.add_track(f"/bottom/{name}", "rz", *horizontal(4, "middle" in name))
# move lower leg
animation.add_track(f"/bottom/{name}/lower", "rz", *vertical(8, 4, 0 if name in leg_group else 4, "left" in name))
# lift hexapod to run on grid
# animation.add_track(f"bottom", "tz", [0, 4], [61.25] * 2)
animation.animate(speed=3)