tars-mini / sim /model.py
tuanhqv123's picture
TARS mini: CAD, MuJoCo sim, trained policies and 5-action demo
4b7148d verified
Raw History Blame Contribute Delete
7.73 kB
"""
TARS mini – physical model (MuJoCo), built on the TARS-AI V3 mechanism.
Mechanism (from TARS-AI src/config.ini.template + 3D part list):
* torso = the two middle slabs, one rigid block, holds electronics + battery
* per side: a "height" servo in the torso drives a pusher in a guided channel
-> the leg's shaft slides up/down relative to the torso (servo 0/1)
* per side: a "leg swing" servo rotates the leg about that shaft (servo 2/3)
Geometry = TARS-AI parts scaled x0.6 (measured from the STL files:
torso 110 mm wide, legs 64 mm wide, depth 65 mm, height ~290 mm).
Every number that is an assumption (not read from a datasheet/STL) is marked ASSUMED
and is swept in the sensitivity study.
"""
from __future__ import annotations
import dataclasses, math
import numpy as np
KGCM = 0.0980665 # N·m per kg·cm
@dataclasses.dataclass
class Params:
# ---- geometry, m (TARS-AI x0.6) ----
H: float = 0.175 # slab height (290 mm * 0.6)
D: float = 0.039 # depth front-back (65 * 0.6)
W_torso: float = 0.066 # torso width (110 * 0.6)
W_leg: float = 0.0385 # leg width (64 * 0.6)
gap: float = 0.003
hip_from_top: float = 0.030 # ASSUMED: shaft sits in the leg "hull", ~50 mm from top at full scale
pad: float = 0.0035 # TPU foot pad thickness (6.2 mm * 0.6)
pad_D: float | None = None # foot-pad depth front-back (None = same as body depth D); a deeper TPU sole
# ---- mass budget, kg ----
torso_shell: float = 0.102 # PLA, 1.6 mm walls + internal mounts (computed from geometry)
leg_shell: float = 0.055 # PLA, 1.2 mm walls + ribs
servo: float = 0.0134 # MG90S (TowerPro datasheet 13.4 g)
board: float = 0.015 # ESP32-S3-LCD-1.54
pca: float = 0.010 # Grove PCA9685
ups: float = 0.035 # Waveshare UPS HAT (B) board
cells: float = 0.092 # 2 x 18650 (~46 g each)
misc: float = 0.040 # speaker, wiring, pushers, shafts, screws, TPU pads
batt_z: float = -0.045 # battery centre below torso centre (mounted low)
mass_scale: float = 1.0
# ---- MG90S @ 5 V (TowerPro: 1.8 kg·cm @4.8 V, 2.2 @6 V; 0.10 s/60° @4.8 V) ----
tau_stall_kgcm: float = 1.85
t60: float = 0.10
p_band_deg: float = 4.0 # ASSUMED: error at which an analog servo reaches full torque
i_stall: float = 0.75 # A, datasheet 750 mA ±10 % @4.8 V
i_idle: float = 0.010 # A, holding with no load
i_run: float = 0.15 # A, moving with no load (typical micro servo)
servo_J: float = 3e-4 # ASSUMED: rotor inertia reflected through the gearbox, kg·m²
@property
def servo_b(self):
"""Back-EMF damping = stall torque / no-load speed (straight from the datasheet curve)."""
return self.tau_stall / self.w0
# ---- optional different servo for the 2 HEIGHT joints (None = same as leg servos) ----
tau_h_kgcm: float | None = None
t60_h: float | None = None
servo_h: float | None = None # mass of one height servo (0 = no height servos, fixed slide)
i_stall_h: float | None = None
@property
def tau_stall_h(self): return (self.tau_h_kgcm if self.tau_h_kgcm is not None else self.tau_stall_kgcm) * KGCM
@property
def w0_h(self): return math.radians(60) / (self.t60_h if self.t60_h is not None else self.t60)
@property
def servo_b_h(self): return self.tau_stall_h / self.w0_h
# ---- height pusher (crank) ----
crank_r: float = 0.012 # ASSUMED: horn hole radius; swept 8–16 mm
# ---- contact ----
mu: float = 0.8 # TPU pad on wood/table (swept 0.5–1.0)
# ---- derived ----
@property
def tau_stall(self): return self.tau_stall_kgcm * KGCM
@property
def w0(self): return math.radians(60) / self.t60
@property
def x_leg(self): return self.W_torso / 2 + self.gap + self.W_leg / 2
def masses(self):
s = self.mass_scale
sh = self.servo if self.servo_h is None else self.servo_h
torso = (self.torso_shell + self.board + self.pca + self.ups + self.misc + 2 * sh) * s
cells = self.cells * s
leg = (self.leg_shell + self.servo) * s
return dict(torso=torso, cells=cells, leg=leg, total=torso + cells + 2 * leg)
def build_xml(p: Params) -> str:
m = p.masses()
hz = p.H / 2
hip_z = hz - p.hip_from_top # shaft height in torso frame
leg_cz = -(p.H / 2 - p.hip_from_top) # leg centre below shaft
pad_z = -p.H / 2 + p.pad / 2
xl = p.x_leg
def leg(side, sx):
return f"""
<body name="slide{side}" pos="{sx*xl} 0 {hip_z}">
<joint name="h{side}" type="slide" axis="0 0 1" range="-0.02 0.02" armature="{p.servo_J/p.crank_r**2}" damping="{p.servo_b_h/p.crank_r**2}"/>
<geom type="cylinder" fromto="{-sx*(p.gap+0.004)} 0 0 {sx*0.006} 0 0" size="0.004" mass="0.004" contype="0" conaffinity="0" rgba=".15 .15 .15 1"/>
<body name="leg{side}">
<joint name="s{side}" type="hinge" axis="1 0 0" range="-95 95" armature="{p.servo_J}" damping="{p.servo_b}"/>
<geom name="leg{side}" type="box" size="{p.W_leg/2} {p.D/2} {(p.H-p.pad)/2}" pos="0 0 {leg_cz + p.pad/2}" mass="{m['leg']}" material="leg"/>
<geom name="pad{side}" type="box" size="{p.W_leg/2} {(p.pad_D or p.D)/2} {p.pad/2}" pos="0 0 {leg_cz + pad_z}" mass="0.002" material="tpu"/>
</body>
</body>"""
return f"""
<mujoco model="tars_mini">
<option timestep="0.0005" integrator="implicitfast" cone="elliptic" impratio="10"/>
<default>
<geom condim="4" friction="{p.mu} 0.005 0.0001" solref="0.004 1" solimp="0.95 0.99 0.001"/>
<joint armature="0.0002" damping="0.003"/>
</default>
<visual><global offwidth="1280" offheight="960"/><quality shadowsize="2048"/></visual>
<asset>
<texture name="grid" type="2d" builtin="checker" rgb1=".86 .86 .84" rgb2=".78 .78 .76" width="300" height="300"/>
<material name="grid" texture="grid" texrepeat="12 12"/>
<material name="torso" rgba=".80 .81 .83 1"/>
<material name="leg" rgba=".28 .40 .58 1"/>
<material name="tpu" rgba=".12 .12 .12 1"/>
</asset>
<worldbody>
<light pos="0.3 -0.6 1.2" dir="-0.2 0.5 -1" castshadow="true"/>
<geom name="floor" type="plane" size="4 4 0.1" material="grid"/>
<body name="torso" pos="0 0 {hz}">
<freejoint name="root"/>
<geom name="torso" type="box" size="{p.W_torso/2} {p.D/2} {(p.H-p.pad)/2}" pos="0 0 {p.pad/2}" mass="{m['torso']}" material="torso"/>
<geom name="tpad" type="box" size="{p.W_torso/2} {(p.pad_D or p.D)/2} {p.pad/2}" pos="0 0 {pad_z}" mass="0.004" material="tpu"/>
<geom name="cells" type="box" size="0.018 0.009 0.033" pos="0 0 {p.batt_z}" mass="{m['cells']}" contype="0" conaffinity="0" rgba="0 0 0 0"/>
<geom name="screen" type="box" size="0.0145 0.0006 0.0145" pos="0 {-p.D/2-0.0006} {hz*0.55}" rgba=".05 .2 .35 1" mass="0" contype="0" conaffinity="0"/>
<site name="imu" pos="0 0 0"/>
{leg('L', -1)}
{leg('R', +1)}
</body>
</worldbody>
<contact>
<exclude body1="torso" body2="legL"/><exclude body1="torso" body2="legR"/>
<exclude body1="torso" body2="slideL"/><exclude body1="torso" body2="slideR"/>
<exclude body1="slideL" body2="legL"/><exclude body1="slideR" body2="legR"/>
</contact>
<actuator>
<motor name="hL" joint="hL" ctrllimited="false"/><motor name="hR" joint="hR" ctrllimited="false"/>
<motor name="sL" joint="sL" ctrllimited="false"/><motor name="sR" joint="sR" ctrllimited="false"/>
</actuator>
<sensor><framezaxis name="up" objtype="site" objname="imu"/></sensor>
</mujoco>"""