"""Crawling simulation of the lambda-lever robot: gait timing and friction ratchet. Part 1 Gait timing: for each wire, pulse current for t_on then rest t_off, run the thermal model to a periodic state, and take the strain swing actually used. speed = amplification * wire_length * swing * ratchet_efficiency / cycle time Part 2 Two-foot dynamic model with direction-dependent Coulomb friction (angled scale feet) to get the ratchet efficiency and the allowed mass split. """ import json, os import numpy as np import matplotlib; matplotlib.use("Agg") import matplotlib.pyplot as plt import sma HERE = os.path.dirname(__file__); FIG = os.path.join(HERE, "..", "..", "docs", "figures") W = sma.fitted() AMP, L_WIRE, STRAIN = 1.6, 100.0, 0.04 # from 02_origami_compare (body B) STROKE = AMP * L_WIRE * STRAIN * 0.97 # mm at the feet (3 % lost to compliance) MASS = 0.022 # kg, robot incl. 500 mAh LiPo G = 9.81 def swing(p, I, t_on, t_off, cycles=4): """Periodic strain swing (0..1) for a pulse train; also peak temperature.""" per = t_on + t_off t, T, s = sma.simulate(lambda tt: I if (tt % per) < t_on else 0.0, p["d"], p["Rm"], p["h"], cycles * per, dt=2e-3) last = t >= (cycles - 1) * per return s[last].max() - s[last].min(), T.max(), (t[last] - t[last][0], s[last]) def crawl(span_fn, t_end, mu_lo, mu_hi, front_share=0.5, k=2000.0, c=3.0, dt=5e-5): """Two point feet joined by a stiff actuator of commanded length span_fn(t) [m].""" mr, mf = MASS * (1 - front_share), MASS * front_share Nr, Nf = mr * G, mf * G xr, xf, vr, vf = 0.0, span_fn(0.0), 0.0, 0.0 n = int(t_end / dt); out = np.empty((n // 200 + 1, 3)); v0 = 2e-4 for i in range(n): t = i * dt F = k * (xf - xr - span_fn(t)) + c * (vf - vr) # >0 pulls feet together fr = -(mu_lo if vr > 0 else mu_hi) * Nr * np.tanh(vr / v0) ff = -(mu_lo if vf > 0 else mu_hi) * Nf * np.tanh(vf / v0) vr += (F + fr) / mr * dt; vf += (-F + ff) / mf * dt xr += vr * dt; xf += vf * dt if i % 200 == 0: out[i // 200] = t, xr, xf return out[: n // 200] res = {"stroke_at_feet_mm": round(STROKE, 2), "robot_mass_g": MASS * 1e3} # ---------------------------------------------------------------- Part 2 first: ratchet p = W["0.10 mm"] t_on, t_off = 1.2, 1.9 per = t_on + t_off _, _, (tc, sc) = swing(p, p["I_rec"], t_on, t_off) span = lambda t: (160.0 - STROKE * np.interp(t % per, tc, sc)) * 1e-3 ratios = [1.0, 1.5, 2.0, 3.0, 4.0, 6.0] eta = [] for r in ratios: o = crawl(span, 3 * per, 0.25, 0.25 * r) eta.append((o[-1, 1] - o[0, 1]) * 1e3 / (3 * STROKE * (sc.max() - sc.min()))) shares = np.linspace(0.2, 0.8, 7) eta_share = [] for s_ in shares: o = crawl(span, 3 * per, 0.25, 0.75, front_share=s_) eta_share.append((o[-1, 1] - o[0, 1]) * 1e3 / (3 * STROKE * (sc.max() - sc.min()))) ETA = float(np.interp(3.0, ratios, eta)) res["ratchet_efficiency_vs_friction_ratio"] = dict(zip(map(str, ratios), np.round(eta, 2).tolist())) res["ratchet_efficiency_vs_front_mass_share(ratio 3)"] = dict(zip(np.round(shares, 2).astype(str), np.round(eta_share, 2).tolist())) trace = crawl(span, 5 * per, 0.25, 0.75) # ---------------------------------------------------------------- Part 1: gait timing fig, ax = plt.subplots(1, 3, figsize=(15, 4.3)) cols = {"0.10 mm": "#1f77b4", "0.13 mm": "#2ca02c", "0.15 mm": "#d62728"} best = {} for name, p in W.items(): for boost, ls in ((1.0, "-"), (1.5, "--")): I = p["I_rec"] * boost t_on = sma.heating_time(I, p["d"], p["Rm"], p["h"]) * 1.05 toffs = np.linspace(0.4, 5.0, 12); sp = [] for to in toffs: sw, Tmax, _ = swing(p, I, t_on, to) sp.append(STROKE * sw * ETA / (t_on + to)) k = int(np.argmax(sp)) best[f"{name} @ {I*1e3:.0f} mA"] = dict(t_on_s=round(t_on, 2), t_off_s=round(float(toffs[k]), 2), speed_mm_s=round(float(sp[k]), 2), stride_mm=round(float(sp[k] * (t_on + toffs[k])), 2)) ax[0].plot(toffs, sp, ls, color=cols[name], label=f"{name}, {I*1e3:.0f} mA, on {t_on:.2f} s") ax[0].axhline(1.0, color="k", ls=":"); ax[0].text(3.3, 1.03, "objective: 1 mm/s", fontsize=8) ax[0].set(xlabel="cooling wait t_off (s)", ylabel="predicted crawl speed (mm/s)", title="(a) Gait timing sweep (conservative thermal model)") ax[0].legend(fontsize=7); ax[0].grid(alpha=.3) res["best_gait"] = best ax[1].plot(ratios, eta, "o-", label="vs friction ratio μ_back / μ_fwd (50/50 mass)") ax[1].set(xlabel="friction ratio | front-foot share of weight ×5", ylabel="ratchet efficiency (net stride / stroke)", title="(b) How good must the feet be?", ylim=(-0.1, 1.05)) ax[1].plot(shares * 5, eta_share, "s--", label="vs front mass share ×5 (ratio 3)") ax[1].legend(fontsize=7); ax[1].grid(alpha=.3) ax[2].plot(trace[:, 0], (trace[:, 1] - trace[0, 1]) * 1e3, label="rear foot") ax[2].plot(trace[:, 0], (trace[:, 2] - trace[0, 2]) * 1e3, label="front foot") v = (trace[-1, 1] - trace[0, 1]) * 1e3 / trace[-1, 0] ax[2].set(xlabel="time (s)", ylabel="foot travel (mm)", title=f"(c) 0.10 mm wire, 200 mA, friction ratio 3: {v:.2f} mm/s") ax[2].legend(); ax[2].grid(alpha=.3) res["dynamic_run_speed_mm_s"] = round(float(v), 2) fig.tight_layout(); fig.savefig(os.path.join(FIG, "sim_crawl.png"), dpi=160) # force margin at the feet res["force_check_N"] = dict(wire_pull=1.40, leaf_spring_at_wire=0.72, available_at_feet=round((1.40 - 0.72) / AMP, 2), needed_to_drag_foot=round(0.75 * MASS * G / 2, 3)) print(json.dumps(res, indent=1)) json.dump(res, open(os.path.join(HERE, "results_crawl.json"), "w"), indent=1)