Skip to content
235 changes: 88 additions & 147 deletions configs/sim/axis/vismach/delta/lineardelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from vismach import *
import hal
import lineardeltakins
Expand All @@ -35,172 +36,112 @@
c['R'], c['L'] = lineardeltakins.get_geometry()
c.ready()

class DeltaTranslate(Collection):
class DeltaTranslate(Translate):
"""Computes forward linear delta kinematics and feeds Translate."""
def __init__(self, parts, comp):
self.comp = comp
self.parts = parts
self.x = self.y = self.z = 0
self.x = self.y = self.z = 0.0

def apply(self):
glPushMatrix()
lineardeltakins.set_geometry(self.comp['R'], self.comp['L'])
f = lineardeltakins.forward(self.comp['joint0'], self.comp['joint1'], self.comp['joint2'])
def coords(self):
lineardeltakins.set_geometry(self.comp["R"], self.comp["L"])
f = lineardeltakins.forward(
self.comp["joint0"], self.comp["joint1"], self.comp["joint2"]
)
if f is not None:
self.x = x = f[0]
self.y = y = f[1]
self.z = z = f[2]
else:
x = self.x
y = self.y
z = self.z
glTranslatef(x, y, z)

def unapply(self):
glPopMatrix()

class TriangularPrismZ:
def __init__(self, z0, z1, h):
self.z0 = z0
self.z1 = z1
self.h = h

def draw(self):
x0 = 0
x1 = -self.h*sin(pi/3)
x2 = self.h*sin(pi/3)
y0 = self.h
y1 = y2 = -self.h*cos(pi/3)
z0 = self.z0
z1 = self.z1

glBegin(GL_TRIANGLES)
glNormal3f(0, 0, 1)
glVertex3f(x0, y0, z1)
glVertex3f(x1, y1, z1)
glVertex3f(x2, y2, z1)

glNormal3f(0, 0, -1)
glVertex3f(x2, y2, z0)
glVertex3f(x1, y1, z0)
glVertex3f(x0, y0, z0)

glNormal3f(-cos(pi/3), sin(pi/3), 0)
glVertex3f(x0, y0, z1)
glVertex3f(x1, y1, z0)
glVertex3f(x1, y1, z1)
glVertex3f(x0, y0, z1)
glVertex3f(x0, y0, z0)
glVertex3f(x1, y1, z0)

glNormal3f(0, -1, 0)
glVertex3f(x1, y1, z1)
glVertex3f(x2, y2, z0)
glVertex3f(x2, y2, z1)
glVertex3f(x1, y1, z1)
glVertex3f(x1, y1, z0)
glVertex3f(x2, y2, z0)

glNormal3f(cos(pi/3), sin(pi/3), 0)
glVertex3f(x2, y2, z1)
glVertex3f(x0, y0, z0)
glVertex3f(x0, y0, z1)
glVertex3f(x2, y2, z1)
glVertex3f(x2, y2, z0)
glVertex3f(x0, y0, z0)

glEnd()
def build_joint(angle, joint):
self.x, self.y, self.z = f[0], f[1], f[2]
return [1, self.x, self.y, self.z]


def build_triangular_prism_z(z0, z1, h):
"""Builds an equilateral triangular prism along Z using TriangleXY."""
x0 = 0.0
x1 = -h * sin(pi / 3.0)
x2 = h * sin(pi / 3.0)
y0 = h
y1 = y2 = -h * cos(pi / 3.0)
return TriangleXY(x0, y0, x1, y1, x2, y2, z0, z1)


def build_joint(angle, joint, carr_left, carr_right):
"""Builds a vertical rail, sliding carriage, and two strut Capture anchors."""
oo = 0.4 * CARRIAGE_OFFSET
return Rotate([
Translate([
HalTranslate([
Translate([
CylinderZ(MIN_JOINT, 5, MAX_JOINT, 5),
HalTranslate([
Box(-CARRIAGE_OFFSET, -20, -20, 0, 20, 20)
], c, joint, 0, 0, 1)
], c, "R", 1, 0, 0)
], EFFECTOR_OFFSET + CARRIAGE_OFFSET, 0, 0)
Translate([
Box(-CARRIAGE_OFFSET, -20, -20, 0, 20, 20),
Translate([carr_left], -CARRIAGE_OFFSET, oo, 0),
Translate([carr_right], -CARRIAGE_OFFSET, -oo, 0),
], c, joint, 0, 0, 1),
], c, "R", 1, 0, 0),
], EFFECTOR_OFFSET + CARRIAGE_OFFSET, 0, 0),
], angle, 0, 0, 1)

class Strut:
def __init__(self, platform, angle, component, joint):
self.platform = platform
self.angle = radians(angle)
self.component = component
self.joint = joint
self.q = gluNewQuadric()

def draw(self):
c = cos(self.angle)
s = sin(self.angle)
o = CARRIAGE_OFFSET
oo = .4 * o
x0 = self.platform.x + c*o
sx = oo * -s
y0 = self.platform.y + s*o
sy = oo * c
z0 = self.platform.z
r2 = self.component['R'] + CARRIAGE_OFFSET
x1 = r2 * cos(self.angle)
y1 = r2 * sin(self.angle)
z1 = self.component[self.joint]

d = x1-x0, y1-y0, z1-z0
mag = sqrt(sum(di*di for di in d))
dx, dy, dz = (di/mag for di in d)
L = self.component['L']
theta = atan2(dz, hypot(dx,dy))
phi = atan2(dy, dx)

glPushMatrix()
glTranslatef(x0+sx, y0+sy, z0)
glRotatef(degrees(phi), 0, 0, 1)
glRotatef(90-degrees(theta), 0, 1, 0)
self.cylinder(L)
glPopMatrix()

glPushMatrix()
glTranslatef(x0-sx, y0-sy, z0)
glRotatef(degrees(phi), 0, 0, 1)
glRotatef(90-degrees(theta), 0, 1, 0)
self.cylinder(L)
glPopMatrix()

def cylinder(self, L):
gluCylinder(self.q, 5, 5, L, 32, 1)
# bottom cap
glRotatef(180,1,0,0)
gluDisk(self.q, 0, 5, 32, 1)
glRotatef(180,1,0,0)
# the top cap needs flipped and translated
glTranslatef(0,0, L)
gluDisk(self.q, 0, 5, 32, 1)


def build_strut_pair(plat_left, plat_right, carr_left, carr_right, world_cap):
"""Connects two platform anchors to two carriage anchors with capped CylinderZ rods."""
return Collection([
Track([CylinderZ(c, 0, 5, "L", 5)], plat_left, carr_left, world_cap),
Track([CylinderZ(c, 0, 5, "L", 5)], plat_right, carr_right, world_cap),
])



world = Capture()
tooltip = Capture()

oo = 0.4 * CARRIAGE_OFFSET
plat0_l, plat0_r = Capture(), Capture()
plat1_l, plat1_r = Capture(), Capture()
plat2_l, plat2_r = Capture(), Capture()

effector_anchors = Collection([
Rotate([
Translate([plat0_l], CARRIAGE_OFFSET, oo, 0),
Translate([plat0_r], CARRIAGE_OFFSET, -oo, 0),
], 90, 0, 0, 1),
Rotate([
Translate([plat1_l], CARRIAGE_OFFSET, oo, 0),
Translate([plat1_r], CARRIAGE_OFFSET, -oo, 0),
], 210, 0, 0, 1),
Rotate([
Translate([plat2_l], CARRIAGE_OFFSET, oo, 0),
Translate([plat2_r], CARRIAGE_OFFSET, -oo, 0),
], 330, 0, 0, 1),
])

tool = DeltaTranslate([
effector_anchors,
Translate([
Color((.5,.5,.5,0), [
Translate([tooltip], 0,0,-40),
Rotate([TriangularPrismZ(0, 5, EFFECTOR_OFFSET*2)], 180, 0, 0, 1),
Color((0.5, 0.5, 0.5, 0), [
Translate([tooltip], 0, 0, -40),
Rotate([build_triangular_prism_z(0, 5, EFFECTOR_OFFSET * 2)], 180, 0, 0, 1),
CylinderZ(-40, 0, -30, 10),
CylinderZ(-30, 10, 10, 10)
CylinderZ(-30, 10, 10, 10),
])
], 0, 0, -5)], c)
], 0, 0, -5),
], c)

red = (1,.5,.5,0)
green = (.5,1,.5,0)
blue = (.5,.5,1,0)
red = (1, 0.5, 0.5, 0)
green = (0.5, 1, 0.5, 0)
blue = (0.5, 0.5, 1, 0)

joint0 = Color(red, [build_joint(90, "joint0")])
joint1 = Color(green, [build_joint(210, "joint1")])
joint2 = Color(blue, [build_joint(330, "joint2")])
carr0_l, carr0_r = Capture(), Capture()
carr1_l, carr1_r = Capture(), Capture()
carr2_l, carr2_r = Capture(), Capture()

work = Capture()
joint0 = Color(red, [build_joint(90, "joint0", carr0_l, carr0_r)])
joint1 = Color(green, [build_joint(210, "joint1", carr1_l, carr1_r)])
joint2 = Color(blue, [build_joint(330, "joint2", carr2_l, carr2_r)])

strut0 = Color(red, [Strut(tool, 90, c, "joint0")])
strut1 = Color(green, [Strut(tool, 210, c, "joint1")])
strut2 = Color(blue, [Strut(tool, 330, c, "joint2")])
strut0 = Color(red, [build_strut_pair(plat0_l, plat0_r, carr0_l, carr0_r, world)])
strut1 = Color(green, [build_strut_pair(plat1_l, plat1_r, carr1_l, carr1_r, world)])
strut2 = Color(blue, [build_strut_pair(plat2_l, plat2_r, carr2_l, carr2_r, world)])

table = CylinderZ(MIN_JOINT-5, 300, MIN_JOINT, 300)
work = Capture()
table = CylinderZ(MIN_JOINT - 5, 300, MIN_JOINT, 300)

model = Collection([table, joint0, joint1, joint2, tool, strut0, strut1, strut2, work])
model = Collection([table, joint0, joint1, joint2, tool, strut0, strut1, strut2, work, world])
main(model, tooltip, work, 1500)
Loading