Skip to content

Extending SoRoMoX

This guide explains how to extend SoRoMoX by implementing custom robot systems and renderers.

Adding Custom Systems

Understanding the Base Classes

SoRoMoX uses a two-level inheritance hierarchy for robot systems:

DynamicalSystem (abstract)
└── SoftRobot (abstract)
    ├── PCS, PlanarPCS, ...
    ├── GVS, ...
    ├── Pendulum, ...
    └── YourCustomSystem

DynamicalSystem

The base class for all dynamical systems. Key interface:

from soromox.systems import DynamicalSystem

class DynamicalSystem:
    # Core methods
    def forward_dynamics(self, t, y, tau_ext, actuation_args):
        """Compute state derivatives yd = f(t, y, tau_ext, u)."""
        ...

    def rollout_to(self, ts, y0, actuation_fn, ...):
        """Simulate open-loop trajectory."""
        ...

    def rollout_closed_loop_to(self, ts, y0, controller, ...):
        """Simulate closed-loop trajectory."""
        ...

SoftRobot

Extends DynamicalSystem with soft robot-specific methods:

from soromox.systems import SoftRobot

class SoftRobot(DynamicalSystem):
    # Explicit dimensions
    num_internal_dofs: int
    num_coordinates: int
    num_velocities: int
    num_actuators: int

    # Properties
    @property
    def length(self) -> float:
        """Total backbone length."""
        ...

    @property
    def is_planar(self) -> bool:
        """True for SE(2) robots, False for SE(3)."""
        ...

    # Kinematics (arc-length parameterized)
    def forward_kinematics(self, q, s):
        """Pose at arc length s along the backbone."""
        ...

    def jacobian(self, q, s):
        """Jacobian at arc length s."""
        ...

    # Dynamics
    def inertia_matrix(self, q):
        """Mass/inertia matrix M(q)."""
        ...

    def coriolis_matrix(self, q, qd):
        """Coriolis/centrifugal matrix C(q, qd)."""
        ...

    def elastic_force(self, q):
        """Elastic restoring force."""
        ...

    def gravitational_force(self, q):
        """Gravitational force."""
        ...

Soft-Robot Parameter Classes

Parameter classes derived from BaseSoftRobotParams declare their world-frame dimensionality explicitly:

from typing import ClassVar
from soromox.systems import BaseSoftRobotParams

class MyPlanarParams(BaseSoftRobotParams):
    is_planar: ClassVar[bool] = True
    # Additional numeric fields...

Use False for SE(3) systems. This mirrors the SoftRobot.is_planar property and lets the base class materialize negative-vertical Earth gravity when gravity is omitted. Mounting belongs to the robot rather than the physical parameter tree; use the shared planar or spatial pose utilities when resolving a constructor's base_pose. Do not infer the convention from an input array shape.

Step-by-Step: Implementing a New System

1. Choose Your Base Class

  • Extend SoftRobot for continuum or articulated soft robots
  • Extend an existing system (e.g., PCS, PlanarPCS) for variants

2. Define Your Class

import equinox as eqx
import jax.numpy as jnp
from soromox.systems import SoftRobot

class MyCustomRobot(SoftRobot):
    # Static fields (compile-time constants)
    num_segments: int = eqx.field(static=True)

    # Array fields (dynamic parameters)
    L: jax.Array  # Segment lengths

    def __init__(self, num_segments: int, L: jax.Array, floating_base=False, ...):
        super().__init__(floating_base=floating_base)
        self.num_segments = num_segments
        self.num_internal_dofs = num_segments * 3
        self.L = L
        # Initialize other parameters, then install actuator models.
        self._configure_actuation(None)

3. Implement Required Properties

@property
def length(self) -> float:
    """Total backbone length."""
    return float(jnp.sum(self.L))

@property
def is_planar(self) -> bool:
    """True for 2D robots."""
    return True  # or False for 3D

Set the static num_internal_dofs field during construction, then install actuators with _configure_actuation(...); the base class derives num_coordinates, num_velocities, and num_actuators. Do not assume that configuration and velocity dimensions are equal: spatial floating systems use seven base coordinates and six base velocities.

4. Implement Kinematics

Soft robots use arc-length parametrization for kinematics:

def forward_kinematics(self, q: jax.Array, s: float) -> jax.Array:
    """
    Compute pose at arc length s along the backbone.

    Args:
        q: Configuration vector (num_coordinates,) - strains or joint angles
        s: Arc length along backbone [0, length]

    Returns:
        Pose: [theta, x, y] for 2D or 4x4 SE(3) matrix for 3D
    """
    # For PCS-style robots: integrate constant strains
    # For other models: use appropriate kinematic model
    ...

def jacobian(self, q: jax.Array, s: float) -> jax.Array:
    """
    Compute Jacobian at arc length s.

    Args:
        q: Configuration vector
        s: Arc length

    Returns:
        Jacobian matrix J(q,s) mapping qd to spatial velocity
        Shape: (3, num_velocities) for 2D or (6, num_velocities) for 3D
    """
    # Typically computed via automatic differentiation
    # or analytical derivation
    ...

def jacobian_and_time_derivative(
    self, q: jax.Array, qd: jax.Array, s: float
) -> tuple[jax.Array, jax.Array]:
    """
    Compute Jacobian and its time derivative at arc length s.

    This is useful for operational-space control and analysis.

    Args:
        q: Configuration vector
        qd: Configuration velocity
        s: Arc length

    Returns:
        J: Jacobian matrix
        Jd: Time derivative of Jacobian
    """
    # Can use JAX automatic differentiation:
    # J = self.jacobian(q, s)
    # Jd = jax.jacfwd(lambda q: self.jacobian(q, s))(q) @ qd
    ...

5. Implement Dynamics

Soft robot dynamics follow the equation: M(q)q̈ + C(q,q̇)q̇ + τ_elastic(q) + τ_gravity(q) + D(q)q̇ = τ_actuation(q,u) + τ_external

def inertia_matrix(self, q: jax.Array) -> jax.Array:
    """
    Mass/inertia matrix M(q) for the soft robot.

    For continuum robots, typically computed by integrating
    along the backbone using the mass density distribution.

    Returns:
        M: Inertia matrix (num_velocities, num_velocities)
    """
    # Example: Integrate using quadrature
    # M = sum over integration points of (m_i * J_i^T @ J_i)
    ...

def coriolis_matrix(self, q: jax.Array, qd: jax.Array) -> jax.Array:
    """
    Coriolis/centrifugal matrix C(q, qd).

    Should use the Christoffel/energy-consistent convention associated
    with inertia_matrix(q): C(q, qd) @ qd is the convective force and
    M_dot(q, qd) - 2 * C(q, qd) is skew-symmetric.

    Returns:
        C: Coriolis matrix (num_velocities, num_velocities)
    """
    ...

def elastic_force(self, q: jax.Array) -> jax.Array:
    """
    Elastic restoring force for soft robot.

    Typically linear in strains: tau_el = K @ (q - q_ref)
    where K is the stiffness matrix.

    Returns:
        tau_el: Elastic force vector (num_velocities,)
    """
    ...

def gravitational_force(self, q: jax.Array) -> jax.Array:
    """
    Gravitational force on soft robot.

    Computed by integrating gravity effect along backbone:
    tau_g = integral of (J^T @ [0, 0, m*g])

    Returns:
        tau_g: Gravitational force vector (num_velocities,)
    """
    ...

def damping_matrix(self, q: jax.Array) -> jax.Array:
    """
    Damping matrix D(q) for soft robot (optional).

    Can be configuration-dependent for complex materials.
    Often constant for simple models.

    Returns:
        D: Damping matrix (num_velocities, num_velocities)
    """
    ...

def actuation_matrix(self, q: jax.Array) -> jax.Array:
    """
    Actuation matrix A(q) mapping control inputs to forces.

    For tendon-actuated: based on tendon routing geometry
    For pressure chambers: based on chamber geometry and pressure

    Returns:
        A: Actuation matrix (num_velocities, num_actuators)
    """
    ...

def _assemble_dynamics_terms(self, q, v):
    """Return the shared JAX equations for forward and inverse dynamics."""
    return (
        self.inertia_matrix(q),
        self.coriolis_matrix(q, v) @ v,
        self.gravitational_force(q),
    )

Energy and control consistency: coriolis_matrix(q, qd) must be consistent with the same inertia_matrix(q) used by the model dynamics. In the standard Christoffel convention, C itself is not generally skew-symmetric; instead M_dot(q, qd) - 2 * C(q, qd) is skew-symmetric. This structure is important for the mechanical energy balance, passivity arguments, and model-based control. It also keeps derivative-based APIs consistent. If you override an optimized dynamics_terms(q, qd) method, its returned Cqd vector must equal the convective force C(q, qd) @ qd for this same convention.

passive_force(q, qd) supplies the total elastic and damping effort to both forward and inverse dynamics. Override it when adding a combined passive law. Implement optimized JAX dynamics terms in _assemble_dynamics_terms(q, qd) so forward dynamics, inverse dynamics, and their derivatives share the same equations.

Energy methods are useful for analysis and energy-based control:

def kinetic_energy(self, q: jax.Array, qd: jax.Array) -> jax.Array:
    """
    Compute kinetic energy: T = 0.5 * qd^T @ M(q) @ qd

    Returns:
        T: Kinetic energy (scalar)
    """
    M = self.inertia_matrix(q)
    return 0.5 * qd.T @ M @ qd

def elastic_energy(self, q: jax.Array) -> jax.Array:
    """
    Compute elastic potential energy stored in deformation.

    For linear elasticity: U_elastic = 0.5 * q^T @ K @ q

    Returns:
        U_el Elastic potential energy (scalar)
    """
    K = self.stiffness_matrix()
    return 0.5 * q.T @ K @ q

def gravitational_energy(self, q: jax.Array) -> jax.Array:
    """
    Compute gravitational potential energy.

    Computed by integrating along backbone:
    U_g = integral of (m * g * h(s, q))

    Returns:
        U_g: Gravitational potential energy (scalar)
    """
    ...

def potential_energy(self, q: jax.Array) -> jax.Array:
    """
    Total potential energy: U = U_elastic + U_gravity

    Useful for energy-based control and analysis.

    Returns:
        U: Total potential energy (scalar)
    """
    U_el = self.elastic_energy(q)
    U_g = self.gravitational_energy(q)
    U = U_el + U_g
    return U

def total_energy(self, q: jax.Array, qd: jax.Array) -> jax.Array:
    """
    Total mechanical energy: E = T + U_elastic + U_gravity

    Useful for energy-based control and passivity analysis.

    Returns:
        E: Total energy (scalar)
    """
    T = self.kinetic_energy(q, qd)
    U = self.potential_energy(q)
    E = T + U
    return E

7. Autodiff and analytical custom JVPs

Autodiff is the package default. Analytical derivative hooks are optional optimizations selected only after an explicit opt-in before tracing.

Public methods use shared transformation adapters in execution/transforms.py. These adapters select analytical model hooks or JAX AD of protected primal methods. Users keep calling ordinary JAX transformations; see the Differentiation guide for examples, mode selection, and model support.

Keep three responsibilities separate:

  • Public system methods forward to the shared execution adapters.
  • Protected model hooks provide primal equations and analytical recurrences.
  • Shared mathematical composition lives in systems/_kinematics.py, systems/_dynamics.py, and systems/_energy.py.

Every optional model action uses the same (primals, tangents) interface and returns either (value, direction) or None for autodiff. SoftRobot declares _analytical_forward_kinematics_jvp, _analytical_jacobian_jvp, _analytical_dynamics_terms_jvp, _analytical_forward_dynamics_jvp, _analytical_inverse_dynamics_jvp, and _analytical_energy_jvp. Models supply physical recurrences; shared modules compose active directions.

Energy methods share one adapter, evaluate_energy, with a static energy kind. Its optional _analytical_energy_jvp hook composes force and inertia actions. Returning None selects one AD pass over all active inputs. This is also the fallback convention used by the dynamics adapters. The base energy hook uses this fallback for floating bases and requests involving model parameters.

Kinematics adapters share pose derivative selection across the ordinary JAX and accelerated execution paths. Pose, geometric Jacobian, energy, and dynamics adapters check custom_jvp_enabled() before invoking optional analytical hooks. Model hooks only decide whether their formulas support the requested physics. Explicit derivative methods retain their specified algorithms.

Treat filtered None tangents as inactive inputs. Skip their contributions, share primal work across active directions, and keep the tangent expression linear. When a request needs AD for several inputs, differentiate them jointly so their common work can be shared. Do not add a custom rule for each user loss, integration method, or combination of differentiation arguments.

As an implementer, define analytical protected hooks rather than writing custom JVP rules in each system class. For example, a spatial PCS-like system can provide closed-form arc-length kinematics and energy gradients:

from soromox.utils.lie_algebra import se3, so3


def _angular_cross_matrix(self, xi_i):
    return so3.skew(xi_i[:3])


def _forward_kinematics_arc_length_derivative(self, q, s):
    i_segment, _ = self.classify_segment(s)
    xi_i = self.segment_strain(q, i_segment)
    g = self._forward_kinematics(q, s)
    return g @ se3.hat(xi_i)


def _jacobian_and_time_derivative(self, q, qd, s):
    J = self._jacobian(q, s)
    Jd = self._jacobian_time_derivative_recurrence(q, qd, s)
    return J, Jd


def _gravitational_force(self, q):
    return self._integrate_gravitational_generalized_force(q)

Planar systems follow the same pattern, but return derivatives in the planar pose representation:

import jax.numpy as jnp


def _forward_kinematics_arc_length_derivative(self, q, s):
    i_segment, _ = self.classify_segment(s)
    kappa_i, sigma_i = self.segment_strain(q, i_segment)
    theta, _, _ = self._forward_kinematics(q, s)
    R = jnp.array(
        [
            [jnp.cos(theta), -jnp.sin(theta)],
            [jnp.sin(theta), jnp.cos(theta)],
        ]
    )
    return jnp.concatenate([jnp.atleast_1d(kappa_i), R @ sigma_i])

When custom derivatives are enabled, the Jacobian dispatch is split by tangent type. A configuration-only tangent uses jacobian_and_time_derivative(q, qd, s). An arc-length-only tangent uses jacobian_and_arc_length_derivative(q, s). A mixed q and s tangent falls back to JAX autodiff through _jacobian; higher-order benchmarks showed that maintaining a fused mixed custom-JVP path was not worth the additional code complexity.

For forward kinematics, configuration and mixed tangents use JAX autodiff through _forward_kinematics, while arc-length-only tangents use forward_kinematics_and_arc_length_derivative(q, s) so systems can still share the primal pose computation with an analytical d pose / ds. The _forward_kinematics_arc_length_jvp hook selects this action; GVS overrides it to use AD of the scalar pose recurrence. Explicit analytical arc-length helpers keep their specified algorithms.

Kinetic energy and its velocity action both call inertia_matrix(q). Continuum models select this matrix from their protected dynamics assembly with zero velocity so unused force outputs can be pruned. The same matrix call in primal and tangent code lets compilation share the assembly. Gravity energy has its own _gravitational_energy_q_jvp_tangent hook: PCS uses AD of the scalar potential. Potential and total energy compose that action with the elastic and kinetic contributions.

When implementing a new system, prefer overriding the protected hooks below and let the shared execution adapters provide the custom-JVP wrappers:

Hook Purpose
_forward_kinematics(q, s) Primal pose computation
_forward_kinematics_arc_length_derivative(q, s) Analytical d pose / ds
_forward_kinematics_and_arc_length_derivative(q, s) Optional fused pose and d pose / ds
_jacobian(q, s) Primal Jacobian computation
_jacobian_and_time_derivative(q, qd, s) Analytical J and dJ/dq @ qd
_jacobian_and_arc_length_derivative(q, s) Optional fused J and dJ/ds
_gravitational_energy(q) Primal gravitational potential energy
_gravitational_force(q) Analytical gradient of gravitational energy
_elastic_energy(q) / elastic_force(q) Primal elastic energy and analytical gradient

If a hook is not available, the base class falls back to JAX autodiff through the protected primal methods. Analytical hooks are therefore an optimization and a numerical robustness tool, not a different public API.

Avoid exact-class checks such as type(self) is PCS in these hooks. Subclasses often share the same kinematics and should inherit the same protected-hook behavior. If a subclass changes the kinematic model, override the affected hook in that subclass.

8. Register as JAX PyTree (if using Equinox)

Equinox modules are automatically registered as JAX PyTrees. Ensure:

  • Static fields use eqx.field(static=True)
  • Dynamic fields are JAX arrays

Required Methods Reference

Method Required Description
Kinematics
forward_kinematics(q, s) Yes Pose at arc length s
jacobian(q, s) Yes Jacobian at arc length s
jacobian_and_time_derivative(q, qd, s) Recommended Jacobian and time derivative
Dynamics
inertia_matrix(q) Yes Mass/inertia matrix M(q)
coriolis_matrix(q, qd) Yes Coriolis matrix C(q,qd)
elastic_force(q) Yes Elastic restoring force
gravitational_force(q) Yes Gravitational force
damping_matrix(q) Recommended Damping matrix D(q)
actuation_matrix(q) Yes Actuation mapping A(q)
forward_dynamics(...) Yes State derivatives
Energy Methods
kinetic_energy(q, qd) Recommended Kinetic energy T
elastic_energy(q) Recommended Elastic potential energy
gravitational_energy(q) Recommended Gravitational potential energy
potential_energy(q) Recommended Total potential energy
total_energy(q, qd) Recommended Total mechanical energy
Optional
stiffness_matrix() Optional Linear stiffness matrix K

Adding Custom Renderers

Understanding BaseSoftRobotRenderer

All renderers inherit from BaseSoftRobotRenderer:

BaseSoftRobotRenderer (abstract)
├── MatplotlibRenderer
├── Open3DRenderer
├── ViserRenderer
├── OpenCVPlanarRenderer
└── YourCustomRenderer

Base Class Interface

from soromox.rendering import BaseSoftRobotRenderer

class BaseSoftRobotRenderer:
    # Attributes
    robot: SoftRobot           # The robot to render
    width: int                 # Image width
    height: int                # Image height
    num_points: int            # Backbone discretization points
    color_config: RendererColorConfig

    # Helper methods (already implemented)
    def compute_backbone_curve(self, q):
        """Compute backbone points from configuration."""
        ...

    def compute_actuator_visual_layers(self, q):
        """Compute actuator visual layers if supported."""
        ...

    # Abstract methods (must implement)
    def render_frame(self, q, **kwargs) -> np.ndarray:
        """Render single frame to image array."""
        ...

    def show(self, q, **kwargs) -> None:
        """Display single frame interactively."""
        ...

    def render_sequence(self, ts, q_ts, **kwargs) -> None:
        """Render animated sequence."""
        ...

Step-by-Step: Implementing a New Renderer

1. Create Your Class

import numpy as np
from soromox.rendering import BaseSoftRobotRenderer, RendererConfig
from soromox.systems import SoftRobot

class MyCustomRenderer(BaseSoftRobotRenderer):
    def __init__(
        self,
        robot: SoftRobot,
        config: RendererConfig | None = None,
        # Your custom parameters
        my_param: float = 1.0,
    ):
        super().__init__(
            robot=robot,
            config=config,
        )
        self.my_param = my_param

2. Implement render_frame

def render_frame(
    self,
    q: np.ndarray,
    color_config: RendererColorConfig = None,
    **kwargs: Any,
) -> np.ndarray:
    """
    Render a single frame.

    Args:
        q: Configuration vector or batch (N, DOF)
        color_config: Optional color override

    Returns:
        RGB image array (height, width, 3) uint8
    """
    # Use helper to get backbone curve
    backbone_points = self.compute_backbone_curve(q)

    # Create your visualization
    image = np.zeros((self.height, self.width, 3), dtype=np.uint8)

    # Draw the robot...
    # Your rendering logic here

    return image

3. Implement show

def show(self, q: np.ndarray, **kwargs) -> None:
    """Display single frame interactively."""
    image = self.render_frame(q, **kwargs)

    # Display using your preferred method
    # e.g., matplotlib, OpenCV, PIL, etc.
    import matplotlib.pyplot as plt
    plt.imshow(image)
    plt.show()

4. Implement render_sequence

def render_sequence(
    self,
    ts: np.ndarray,
    q_ts: np.ndarray,
    record_path: str = None,
    **kwargs: Any,
) -> None:
    """
    Render animated sequence.

    Args:
        ts: Time points (T,)
        q_ts: Configurations (T, DOF) or (N, T, DOF)
        record_path: Optional path to save video
    """
    for i, (t, q) in enumerate(zip(ts, q_ts)):
        frame = self.render_frame(q, **kwargs)

        # Display or record frame
        # Your animation logic here

    if record_path:
        # Save video
        ...

Using Helper Methods

The base class provides useful helpers:

# Get backbone curve points
backbone = self.compute_backbone_curve(q)
# Returns: (num_points, 2) for 2D or (num_points, 3) for 3D

# Get actuator visual layers from the renderer-owned generic adapter or a
# robot-specific override
actuator_layers = self.compute_actuator_visual_layers(q)
# Returns: tuple of ActuatorVisualLayer objects

# Batched and trajectory actuator visual helpers use optional robot fast paths
# named actuator_visual_layers_batched(...) and
# actuator_visual_layers_trajectory(...) when available. Otherwise they use the
# generic SoftRobot adapter or fall back to repeated custom-hook calls.

# Resolve colors with hierarchy
colors = self.resolve_backbone_colors(num_robots=1, color_config=color_config)

# Check robot dimension
if self.is_planar:
    # 2D rendering
else:
    # 3D rendering

Best Practices

  1. Support batched input: Handle both single configs (DOF,) and batches (N, DOF)

  2. Use color configuration: Respect the color_config parameter and hierarchy

  3. Support recording: Implement record_path parameter for video export

  4. Handle both 2D and 3D: Check self.is_planar for dimension-appropriate rendering

  5. Cache expensive computations: Use the base class caching mechanisms


Adding Custom Controllers

Understanding the Controller Hierarchy

SoRoMoX provides two base classes for implementing controllers:

BaseController (abstract)
└── ClosedFormModelBasedController (abstract)
    ├── Configuration-space controllers
    ├── Operational-space controllers
    └── Actuation-space controllers

BaseController Interface

The BaseController class provides the fundamental interface for all controllers:

from soromox.control import BaseController
from soromox.systems import SystemState

class BaseController(eqx.Module):
    """
    Abstract base class for controllers.

    Controllers compute actuation inputs based on the current system state
    and a reference trajectory.
    """

    robot: SoftRobot
    reference_trajectory: ReferenceTrajectory

    def __call__(
        self, system_state: SystemState
    ) -> tuple[Array, Any | None]:
        """
        Compute control action given the current system state.

        Args:
            system_state: Current state of the system containing:
                - t (Array): Current simulation time
                - y (Array): Robot state [q, qd] concatenated
                - u (Optional[Array]): Previous actuation input
                - control_state (Optional[Any]): Internal controller state

        Returns:
            u_control (Array): Control input to apply, shape (num_actuators,)
            control_state_dot (Optional[Any]): Time derivative of internal
                controller state (e.g., integrator error), or None if stateless
        """
        raise NotImplementedError

ClosedFormModelBasedController Interface

For model-based controllers that decompose control into feedforward and feedback terms:

from soromox.control import ClosedFormModelBasedController
from soromox.systems import SystemState

class ClosedFormModelBasedController(BaseController):
    """
    Base class for closed-form model-based controllers.

    Combines a model-based feedforward term with an error-based feedback term.
    The control law is: u_control = model_based_term + error_based_feedback_term
    """

    # Override these methods
    def model_based_term(
        self, system_state: SystemState
    ) -> tuple[Array, Any | None]:
        """
        Compute the model-based feedforward control term.

        This term typically computes the control input required to achieve
        the desired trajectory based on the system dynamics model (e.g.,
        inverse dynamics, gravity compensation).

        Args:
            system_state: Current system state containing:
                - t (Array): Current simulation time
                - y (Array): Robot state [q, qd] concatenated
                - u (Optional[Array]): Previous actuation input
                - control_state (Optional[Any]): Internal controller state

        Returns:
            u_model (Array): Model-based control input, shape (num_actuators,)
            control_state_dot (Optional[Any]): Time derivative of internal
                controller state contributed by this term, or None
        """
        # Default: returns zero (override in subclass)
        return jnp.zeros((self.robot.num_actuators,)), None

    def error_based_feedback_term(
        self, system_state: SystemState
    ) -> tuple[Array, Any | None]:
        """
        Compute the error-based feedback control term.

        This term typically computes a corrective control input based on
        the tracking error (e.g., PD control, PID control).

        Args:
            system_state: Current system state

        Returns:
            u_feedback (Array): Feedback control input, shape (num_actuators,)
            control_state_dot (Optional[Any]): Time derivative of internal
                controller state contributed by this term, or None
        """
        # Default: returns zero (override in subclass)
        return jnp.zeros((self.robot.num_actuators,)), None

Step-by-Step: Implementing a Custom Controller

1. Choose Your Base Class

  • Extend BaseController for general controllers (e.g., learning-based, hybrid)
  • Extend ClosedFormModelBasedController for model-based controllers with feedforward/feedback structure

2. Define Your Controller Class

import equinox as eqx
import jax.numpy as jnp
from jax import Array
from soromox.control import ClosedFormModelBasedController, ReferenceTrajectory
from soromox.systems import SoftRobot, SystemState

class MyCustomController(ClosedFormModelBasedController):
    """Custom model-based controller implementation."""

    # Required attributes (from BaseController)
    robot: SoftRobot
    reference_trajectory: ReferenceTrajectory

    # Controller gains
    K_p: Array
    K_d: Array

    def __init__(
        self,
        robot: SoftRobot,
        reference_trajectory: ReferenceTrajectory,
        K_p: Array,
        K_d: Array,
    ):
        self.robot = robot
        self.reference_trajectory = reference_trajectory
        self.K_p = K_p
        self.K_d = K_d

3. Implement Required Methods

@eqx.filter_jit
def model_based_term(
    self,
    system_state: SystemState,
) -> tuple[Array, None]:
    """
    Compute model-based feedforward term.

    Implements inverse dynamics feedforward control.
    """
    # Extract state
    t = system_state.t
    y = system_state.y
    num_dofs = self.robot.num_internal_dofs
    q, qd = y[:num_dofs], y[num_dofs:]

    # Get desired trajectory from reference
    q_des, qd_des, qdd_des = self.reference_trajectory.get_state(t)

    # Compute inverse dynamics
    M, Cqd, G = self.robot.dynamics_terms(q_des, qd_des)
    tau_el = self.robot.elastic_force(q_des)

    # Feedforward torque
    tau_ff = M @ qdd_des + Cqd + G + tau_el

    # Map to actuation space
    A = self.robot.actuation_matrix(q_des)
    u_model = jnp.linalg.lstsq(A, tau_ff)[0]

    return u_model, None  # None = no internal state

@eqx.filter_jit
def error_based_feedback_term(
    self,
    system_state: SystemState,
) -> tuple[Array, None]:
    """
    Compute PD feedback term based on tracking error.
    """
    # Extract state
    t = system_state.t
    y = system_state.y
    num_dofs = self.robot.num_internal_dofs
    q, qd = y[:num_dofs], y[num_dofs:]

    # Get desired trajectory
    q_des, qd_des, _ = self.reference_trajectory.get_state(t)

    # Compute errors
    e = q_des - q
    ed = qd_des - qd

    # PD feedback torque
    tau_fb = self._apply_gain(self.K_p, e) + self._apply_gain(self.K_d, ed)

    # Map to actuation space
    A = self.robot.actuation_matrix(q)
    u_feedback = jnp.linalg.lstsq(A, tau_fb)[0]

    return u_feedback, None  # None = no internal state

# The __call__ method is already implemented in the base class
# It combines: u_control = model_based_term + error_based_feedback_term

4. Usage Example

from soromox.systems import LinkSpec, PlanarPCS
from soromox.control import ReferenceTrajectory

# Create robot
robot = PlanarPCS.from_links([
    LinkSpec.circular(
        length=0.1, radius=0.01, density=1000.0,
        young_modulus=1e6, shear_modulus=1e5,
        material_damping_coefficient=318.0,
        reference_strain=[0.0, 1.0, 0.0],
    )
    for _ in range(3)
])

# Create reference trajectory (defines desired motion)
ref_traj = ReferenceTrajectory(...)  # Your trajectory

# Create controller
controller = MyCustomController(
    robot=robot,
    reference_trajectory=ref_traj,
    K_p=jnp.diag(jnp.array([100.0, 100.0, 100.0])),
    K_d=jnp.diag(jnp.array([10.0, 10.0, 10.0])),
)

# Use in closed-loop simulation
ts, ys = robot.rollout_closed_loop_to(
    ts=jnp.linspace(0, 5, 100),
    y0=jnp.zeros(robot.state_size),
    controller=controller,
)

Controller Design Patterns

Configuration-Space Controllers

Operate directly in joint/strain coordinates:

class ConfigurationSpaceController(ClosedFormModelBasedController):
    def model_based_term(self, system_state: SystemState):
        # Extract state
        num_dofs = self.robot.num_internal_dofs
        q, qd = system_state.y[:num_dofs], system_state.y[num_dofs:]

        # Compute in configuration space
        tau = ...  # Your control law

        # Map to actuation space
        A = self.robot.actuation_matrix(q)
        u = jnp.linalg.lstsq(A, tau)[0]
        return u, None

Operational-Space Controllers

Operate in Cartesian/task space:

class OperationalSpaceController(ClosedFormModelBasedController):
    s: float  # Arc length for end-effector control

    def model_based_term(self, system_state: SystemState):
        # Extract state
        num_dofs = self.robot.num_internal_dofs
        q, qd = system_state.y[:num_dofs], system_state.y[num_dofs:]

        # Get task-space state
        chi = self.robot.forward_kinematics(q, s=self.s)
        J = self.robot.jacobian(q, s=self.s)

        # Compute task-space force
        F = ...  # Your operational-space control law

        # Map to joint torques then actuation
        tau = J.T @ F
        A = self.robot.actuation_matrix(q)
        u = jnp.linalg.lstsq(A, tau)[0]
        return u, None

Actuation-Space Controllers

New actuator modalities should implement the composable Transmission and EffortModel contracts described in the actuation model, rather than introducing a new continuum host subclass. An effort law should set its static requires_coordinate and requires_velocity flags to False for transmission state that it does not consume. The defaults are both True, which is safe for state-dependent laws.

Operate directly in the transmission's work coordinates:

class ActuationSpaceController(ClosedFormModelBasedController):
    def model_based_term(self, system_state: SystemState):
        # Extract state
        num_dofs = self.robot.num_internal_dofs
        q = system_state.y[:num_dofs]

        # Coordinates follow the installed transmissions. For a tendon preset,
        # these are negative path lengths; raw lengths remain available from
        # the threadlike actuator when needed.
        y_a = self.robot.actuator_coordinates(q)
        u = ...  # Your actuation-space control law using y_a

        return u, None

Best Practices

  1. Use JAX transformations: Decorate methods with @eqx.filter_jit for performance
  2. Handle singularities: Use jnp.linalg.lstsq instead of matrix inversion
  3. Test thoroughly: Verify stability with different robot configurations
  4. Document control law: Clearly explain the mathematical formulation in docstrings

API Reference

For detailed API documentation of the base classes, see: