"""Origami mechanics toolkit used to compare candidate robot bodies. 1. Flat-foldability check of a crease pattern (Maekawa and Kawasaki theorems). 2. Bar-and-hinge structural model (Schenk & Guest 2011; Liu & Paulino 2017): every panel is triangulated, triangle edges are stiff bars, every edge shared by two triangles is a torsional spring (a crease, or a stiff "facet" hinge that represents panel bending). An SMA wire is a tension-only cable whose rest length is shortened by the actuation strain. Equilibrium is found by nonlinear least squares on the spring residuals. Units: mm, N, N.mm, rad. """ import numpy as np from scipy.optimize import least_squares E_CARD, T_CARD = 3000.0, 0.25 # MPa, mm (200 gsm card) D_PANEL = E_CARD * T_CARD**3 / 12.0 # N.mm per mm width, panel bending rigidity K_BAR = 300.0 # N/mm, in-plane (panels ~inextensible) # ---------------------------------------------------------------- foldability def flat_foldability(verts2d, creases): """creases: list of (i, j, 'M'|'V'). Returns per-interior-vertex report.""" verts2d = np.asarray(verts2d, float) inc = {} for i, j, a in creases: inc.setdefault(i, []).append((j, a)); inc.setdefault(j, []).append((i, a)) lo, hi = verts2d.min(0), verts2d.max(0) rep = [] for v, lst in inc.items(): p = verts2d[v] if np.any(np.isclose(p, lo)) or np.any(np.isclose(p, hi)): continue # boundary vertex ang = sorted((np.arctan2(*(verts2d[j] - p)[::-1]), a) for j, a in lst) th = np.array([a for a, _ in ang]) sect = np.diff(np.append(th, th[0] + 2 * np.pi)) M = sum(a == "M" for _, a in ang); V = len(ang) - M maekawa = abs(M - V) == 2 kawasaki = len(sect) % 2 == 0 and np.isclose(sect[::2].sum(), np.pi) rep.append(dict(vertex=v, degree=len(ang), M=M, V=V, maekawa=bool(maekawa), kawasaki=bool(kawasaki))) return rep # ------------------------------------------------------------- bar and hinge def fold_angle(X, I, J, K, L): """Signed fold angle (0 = flat) of hinges with edge I-J and wing nodes K, L.""" e = X[J] - X[I] n1 = np.cross(e, X[K] - X[I]); n2 = np.cross(X[L] - X[I], e) en = e / np.linalg.norm(e, axis=1, keepdims=True) return np.arctan2(np.einsum("ij,ij->i", np.cross(n1, n2), en), np.einsum("ij,ij->i", n1, n2)) class Model: def __init__(self, X, tris, hinge_k, fixed, wires=()): """X (n,3); tris list of (a,b,c); hinge_k(edge i,j, length) -> N.mm/rad; fixed: dict node -> iterable of fixed axes; wires: list of (i, j, k_wire).""" self.X0 = np.asarray(X, float).copy() self.tris = [tuple(t) for t in tris] edges = {} for t in self.tris: for a, b, c in ((t[0], t[1], t[2]), (t[1], t[2], t[0]), (t[2], t[0], t[1])): edges.setdefault((min(a, b), max(a, b)), []).append(c) self.bars = np.array(list(edges)) self.L0 = np.linalg.norm(self.X0[self.bars[:, 1]] - self.X0[self.bars[:, 0]], axis=1) H = [(i, j, w[0], w[1]) for (i, j), w in edges.items() if len(w) == 2] self.H = np.array(H) hl = np.linalg.norm(self.X0[self.H[:, 1]] - self.X0[self.H[:, 0]], axis=1) self.kh = np.array([hinge_k(i, j, l) for (i, j, _, _), l in zip(H, hl)]) self.phi0 = fold_angle(self.X0, *self.H.T) self.free = np.ones(self.X0.size, bool) for n, axes in fixed.items(): for a in axes: self.free[3 * n + a] = False self.wires = list(wires) self.wL0 = [np.linalg.norm(self.X0[j] - self.X0[i]) for i, j, _ in self.wires] def _res(self, q, Xb, wrest): X = Xb.copy().ravel(); X[self.free] = q; X = X.reshape(-1, 3) l = np.linalg.norm(X[self.bars[:, 1]] - X[self.bars[:, 0]], axis=1) d = fold_angle(X, *self.H.T) - self.phi0 d = (d + np.pi) % (2 * np.pi) - np.pi r = [np.sqrt(K_BAR) * (l - self.L0), np.sqrt(self.kh) * d] for (i, j, k), l0 in zip(self.wires, wrest): r.append([np.sqrt(k) * max(0.0, np.linalg.norm(X[j] - X[i]) - l0)]) return np.concatenate([np.atleast_1d(x) for x in r]) def solve(self, strains, Xstart=None): """strains: one actuation strain per wire. Returns X, wire tensions.""" Xb = self.X0 if Xstart is None else Xstart wrest = [l * (1 - s) for l, s in zip(self.wL0, strains)] sol = least_squares(self._res, Xb.ravel()[self.free], args=(Xb, wrest), method="trf", xtol=1e-10, ftol=1e-12, max_nfev=400) X = Xb.copy().ravel(); X[self.free] = sol.x; X = X.reshape(-1, 3) T = [k * max(0.0, np.linalg.norm(X[j] - X[i]) - l0) for (i, j, k), l0 in zip(self.wires, wrest)] return X, T def reset_rest(self, X): """Make configuration X the new stress-free state (creases plastically set).""" self.X0 = X.copy(); self.phi0 = fold_angle(X, *self.H.T) self.wL0 = [np.linalg.norm(X[j] - X[i]) for i, j, _ in self.wires] def strip_mesh(profile_xz, ys): """Prismatic folded strip: polyline profile in x-z extruded over the y values.""" X, idx = [], {} for r, (x, z) in enumerate(profile_xz): for c, y in enumerate(ys): idx[r, c] = len(X); X.append((x, y, z)) tris = [] for r in range(len(profile_xz) - 1): for c in range(len(ys) - 1): a, b, cc, d = idx[r, c], idx[r + 1, c], idx[r + 1, c + 1], idx[r, c + 1] tris += [(a, b, cc), (a, cc, d)] return np.array(X), tris, idx