""" 8-DoF Redundant Manipulator Dynamics (16D State, 8D Control) Configuration: 7-DoF Franka Emika mounted on a 1-DoF Linear Prismatic Rail State: x = [q_1..q_8, qdot_1..qdot_8]^T in R^16 Control: u = [tau_rail, tau_1..tau_7]^T in R^8 (Torques / Forces) """ import numpy as np from src.dynamics.base import ControlAffineSystem class Manipulator8DoFSystem(ControlAffineSystem): """ 8-Degree-of-Freedom Manipulator system: Dynamics: M(q) * qddot + C(q, qdot) * qdot + g(q) + F_fric(qdot) = u + w(t) State-space form: dot{q} = qdot dot{qdot} = M(q)^{-1} [ u - C(q, qdot)qdot - g(q) - F_fric ] + w(t) """ def __init__( self, dt: float = 0.01, tau_rail_max: float = 100.0, # Linear actuator force (N) tau_joint_max: float = 50.0, # Joint torques (Nm) ): u_min = np.array([-tau_rail_max] + [-tau_joint_max] * 7, dtype=np.float32) u_max = np.array([tau_rail_max] + [tau_joint_max] * 7, dtype=np.float32) super().__init__(state_dim=16, control_dim=8, u_min=u_min, u_max=u_max, dt=dt) # Nominal inertia parameters (link masses kg and link lengths m) self.link_masses = np.array([5.0, 3.0, 3.0, 2.5, 2.5, 2.0, 1.5, 1.0], dtype=np.float32) self.link_lengths = np.array([1.0, 0.33, 0.33, 0.31, 0.31, 0.28, 0.15, 0.10], dtype=np.float32) self.damping = np.full(8, 0.2, dtype=np.float32) self.friction_coulomb = np.full(8, 0.1, dtype=np.float32) # Joint limits: rail +/- 1.5m, Franka 7 joints in radians self.q_min = np.array([-1.5, -2.8, -1.7, -2.8, -3.0, -1.7, -0.1, -2.8], dtype=np.float32) self.q_max = np.array([ 1.5, 2.8, 1.7, 2.8, 0.1, 2.8, 3.7, 2.8], dtype=np.float32) def _inertia_matrix(self, q: np.ndarray) -> np.ndarray: """ Positive-definite coupled inertia matrix M(q) approximation. """ M = np.diag(self.link_masses.copy()) # Add non-linear off-diagonal coupling terms for adjacent joints for i in range(1, 7): coupling = 0.2 * np.cos(q[i]) M[i, i + 1] = coupling M[i + 1, i] = coupling return M def _coriolis_gravity(self, q: np.ndarray, qdot: np.ndarray) -> np.ndarray: """ Coriolis, centrifugal, and gravity vector h_cg(q, qdot) = C(q, qdot)qdot + g(q) """ g_const = 9.81 h_cg = np.zeros(8, dtype=np.float32) # Prismatic rail has no gravity component in flat floor setup h_cg[0] = 0.0 # Revolute joints experience gravitational torques for i in range(1, 8): h_cg[i] = self.link_masses[i] * g_const * self.link_lengths[i] * np.cos(q[i]) # Coriolis and viscous/Coulomb friction fric = self.damping * qdot + self.friction_coulomb * np.sign(qdot) return h_cg + fric def f(self, x: np.ndarray) -> np.ndarray: # x: [q, qdot] in R^16 q = x[:8] qdot = x[8:16] M = self._inertia_matrix(q) h_cg = self._coriolis_gravity(q, qdot) M_inv = np.linalg.inv(M) qddot_drift = -M_inv @ h_cg f_vec = np.zeros(16, dtype=np.float32) f_vec[:8] = qdot f_vec[8:] = qddot_drift return f_vec def g(self, x: np.ndarray) -> np.ndarray: q = x[:8] M = self._inertia_matrix(q) M_inv = np.linalg.inv(M) g_mat = np.zeros((16, 8), dtype=np.float32) g_mat[8:, :] = M_inv return g_mat def joint_limit_barrier(self, x: np.ndarray, margin: float = 0.05) -> float: """ Composite joint limit barrier across all 8 joints: h(q) = min_i ( (q_i - q_min_i - margin) * (q_max_i - margin - q_i) ) """ q = x[:8] lower_gaps = q - (self.q_min + margin) upper_gaps = (self.q_max - margin) - q min_gap = min(np.min(lower_gaps), np.min(upper_gaps)) return float(min_gap)