#### Soft Constraint Model
In essence, [[- MuJoCo -|MuJoCo]] is never perfectly rigid as it models contacts as constraint-based spring-damping-friction cones. This is done because hard constraints would lead to non-invertible [[Matrices|matrices]].
Therefore, [[MuJoCo Contacts Cheat Sheet|contacts]] always results in small penetrations / residuals between objects. `solimp` defines the resulting forces, while `solref` defines interaction behavior (snappiness).
---
In the following, we consider a 1-dimensional example of a particle. We assume a constraint-free acceleration $a_0$. Under a constraint (either manual or e.g. via contact) $a_1+\textcolor{blue}{d(r)} \cdot (\textcolor{red}{bv+kr})=(1-\textcolor{blue}{d(r)})\cdot a_0,$with constraint residual / violation $\mathbf{r}(\mathbf{q}) \in \mathbb{R}^{n_q}$ (negative value correspond to penetration).
>[!brainwaves] Intuition
>Impedance controls reference vs constraint free acceleration profile, solref how the reference behaves.
>- Without constraint violation $d=0$ and acceleration is simply the unconstrained one.
>- With increasing violation, the spring damper-like reference acceleration has more and more impact, altering the effective acceleration away from the unconstrained one.
---
#### Stiffness and Damping
>[!info] Definition
>Stiffness and damping create the reference acceleration via$\textcolor{red}{-bv-kr}=a_\text{ref}$and are scaled via$\begin{align}b &=\frac{2}{d_\text{width}\cdot \text{timeconst}} \\ k&=\frac{d(r)}{d_\text{width}^2 \cdot \text{timeconst}^2 \cdot \text{dampratio}^2} \end{align}$
>
>The time constant describes how quickly this decay happens
>- After `timeconst`, the amplitude is reduced to roughly $37\%$
>
>Damping controls the decay of an oscillation:
>- `dampratio`= 1 is critically damped
>- `dampratio`< 1 is under-damped, allowing oscillations
>- `dampratio`> 1 is over-damped, avoiding oscillations but slower than critically
>Both are scaled per default based on the impedance.
```xml
<geom solref="0.002 1"/> # solref="timeconst dampratio"
```
>[!brainwaves] The Gist of It
>Stiffness and damping generate a reference acceleration which determines the **motion that constraint is trying to achieve** in order to rectify violation (e.g. normal force based on penetration).
>
>**Lower timeconstant results in faster constraint resolution.**
>[!warning] Unstable Simulation
>The `timeconst` parameter should be at least twice as large as the simulation timestep to avoid unstable behavior. Otherwise, violations are not resolved fast enough.
---
#### Impedance $\mathbf{d}(\mathbf{r})$
>[!info] Definition
>For each constraint, the impedance is parametrized by $5$ values, controlling a function producing a number in $(0,1)$ via$\text{solimp}=(d_0, d_\mathrm{with}, \text{width}, \text{midpoint},\text{power}).$to interpolate between the unforced acceleration $a_0$ and the reference acceleration. The first three values interpolate between $d_0$ and $d_\text{width}$ over the residual $[0, \text{width}]$. The remaining two are used to control the shape of the respone.
```xml
<geom solimp="0.9 0.95 0.001 0.5 2"/>
```
In the [[XML Files|XML]] or code, this is e.g. (defaults) set via `solimp="0.9 0.95 0.001 0.5 2"`
![[Pasted image 20251115214702.png|center|400]]
>[!brainwaves] The Gist of It
>The _impedance_ $\in (0,1)$ scales the magnitude and smoothness of the constraint response created by `solref` as a function of the residual.
>
>For example, the first value controls the stiffness (...)
---
>[!question]
>There are two modes here depending on the sign, somehow changes the scaling ... see https://mujoco.readthedocs.io/en/stable/modeling.html#equation-eq-constraint
In the positive-value default format, the `timeconst` parameter controls constraint **softness**. It is specified in units of time and means “how quickly is the constraint trying to resolve the violation”. Larger values correspond to softer constraints.
The negative-value “direct” format is more flexible, for example allowing for perfectly elastic collisions (damping=0). It is the recommended format for system identification.
A `dampratio` of 1 in the positive-value format is equivalent to $\text{damping}=2\sqrt{\text{stiffness}}$ in the direct format.