---
url: /dyad/dev/stdlib/DiscreteComponents/components/TrajectoryLimiter.md
description: >-
  Dyad TrajectoryLimiter docs.  Trajectory limiter: a nonlinear filter that
  converts an arbitrary reference signal into a trajectory whose first and
  second time derivatives...
---

# `TrajectoryLimiter` {#TrajectoryLimiter}

Trajectory limiter: a nonlinear filter that converts an arbitrary reference signal into a trajectory whose first and second time derivatives are bounded.

The block implements the discrete-time nonlinear filter of Zanasi, Guarino Lo Bianco and Tonielli, "Nonlinear filters for the generation of smooth trajectories", Automatica 36 (2000), as implemented in `TrajectoryLimiter` of the Julia package [TrajectoryLimiters.jl](https://github.com/baggepinnen/TrajectoryLimiters.jl). When the reference is a position, the outputs `yd` and `ydd` are the velocity and the acceleration of the limited trajectory; when the reference is a velocity, they are the acceleration and the jerk.

The output $x = y$ is the output of a double integrator $\ddot x = a$ whose input $a$ is computed by a discrete-time sliding-mode control law that drives $x$ toward the reference $r = u$, subject to $|\dot x| \le \dot x_M$ and $|a| \le \ddot x_M$, where $\dot x_M$ is `yd_max` and $\ddot x_M$ is `ydd_max`. The derivative of the reference is estimated by the backward difference $\dot r_k = (r_k - r\_{k-1}) / T_s$. With $e = x - r$, $\dot e = \dot x - \dot r$ and $U = \ddot x_M$, the control law is

$$\begin{aligned}
z &= \frac{1}{T_s U}\left(\frac{e}{T_s} + \frac{\dot e}{2}\right), \qquad \dot z = \frac{\dot e}{T_s U}, \qquad m = \left\lfloor \frac{1 + \sqrt{1 + 8|z|}}{2} \right\rfloor, \\
\sigma &= \dot z + \frac{z}{m} + \frac{m - 1}{2}\operatorname{sign}(z), \\
a &= -U \operatorname{sat}(\sigma) , \frac{1 + \operatorname{sign}\left(\dot x \operatorname{sign}(\sigma) + \dot x_M - T_s U\right)}{2},
\end{aligned}$$

where $\operatorname{sat}$ denotes saturation to $\[-1, 1]$. The state is propagated by

$$\dot x\_{k+1} = \dot x_k + T_s a_k, \qquad x\_{k+1} = x_k + \frac{T_s}{2}\left(\dot x_k + \dot x\_{k+1}\right).$$

When the reference changes faster than the bounds permit, the output deviates from it and converges back to it once the bounds permit; for a step in the reference, the transient is approximately time-optimal under the bounds. After the transient, a reference with constant first time derivative is tracked without error. A reference with nonzero second time derivative $\ddot r$ is tracked with an error proportional to $T_s^2 \ddot r$, also when it satisfies both bounds.

The parameters must satisfy $T_s \ddot x_M < \dot x_M$. Otherwise the factor in the control law that enforces the bound on $\dot x$ is zero at $\dot x = 0$, and an output that starts at rest does not move.

With `bounds_as_inputs = true`, the bounds $\dot x_M$ and $\ddot x_M$ are the values of the inputs `yd_max_in` and `ydd_max_in` at the current sample, and the parameters `yd_max` and `ydd_max` are not used. The state of the limiter does not depend on the bounds, so a change of the bounds leaves `y` and `yd` continuous, and `ydd` satisfies the bound of the current sample. The inputs must satisfy the same conditions as the parameters at every sample. When $\dot x_M$ decreases below the current $|\dot x|$, the control law only prevents a further increase of $|\dot x|$; $|\dot x|$ is not reduced to the new bound until the deceleration toward the reference begins.

The outputs `y` and `yd` at sample $k$ are the state $x_k, \dot x_k$, which depends only on the reference samples before $k$, while `ydd` is the value $a_k$ applied from sample $k$ to sample $k+1$, which depends on the current reference sample. The outputs `y` and `yd` therefore have no direct feedthrough from the input, and the three outputs are mutually consistent, which makes them suitable as references and feedforward signals for a tracking controller.

The state of the limiter at the first sample consists of $x$ and $\dot x$, which equal the first samples of the outputs `y` and `yd`. The estimate of the reference derivative at the first sample is zero under every `initialization` variant. The scalar variants specify $x$ only and set $\dot x = 0$:

* `InitialOutput(y0=...)`: the first sample of `y` equals `y0`.

* `SteadyState`: the first sample of `y` equals the first reference sample, so a constant reference produces no transient.

* `InitialState(x0=...)`: the first sample of `y` equals `x0`.

The array variants specify both $x$ and $\dot x$:

* `InitialStateArray(x0=[x, xd])` and `InitialOutputArray(y0=[x, xd])`: the first samples of `y` and `yd` equal `x` and `xd`. The two variants are equivalent, since the outputs `y` and `yd` equal the state.

## Usage {#Usage}

`DiscreteComponents.TrajectoryLimiter(yd_max=1.0, ydd_max=1.0)`

## Parameters: {#Parameters:}

|               Name |                                                                                                                                                                    Description | Units |             Default value |
| ------------------:| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| -----:| -------------------------:|
|               `Ts` |                                                                                                                                                            Sample interval \[s] |     – |              SampleTime() |
| `bounds_as_inputs` |                                                       Whether the bounds are given by the inputs `yd_max_in` and `ydd_max_in` instead of the parameters `yd_max` and `ydd_max` |     – |                     false |
|   `initialization` |                                                                                                                                                Initial-condition specification |     – | DiscreteCom...t(; y0=0.0) |
|           `yd_max` |                            Upper bound on the magnitude of the first time derivative \[units of u per second]. Must exceed Ts_ydd_max (only used if `bounds*as_inputs = false`) |     – |                       1.0 |
|          `ydd_max` | Upper bound on the magnitude of the second time derivative \[units of u per second squared]. Must be positive and less than yd_max/Ts (only used if `bounds_as_inputs = false`) |     – |                       1.0 |

## Connectors {#Connectors}

* `u` - This connector represents a real signal as an input to a component ([`RealInput`](/stdlib/Dyad/connectors/RealInput#RealInput))

* `y` - This connector represents a real signal as an output from a component ([`RealOutput`](/stdlib/Dyad/connectors/RealOutput#RealOutput))

* `yd` - This connector represents a real signal as an output from a component ([`RealOutput`](/stdlib/Dyad/connectors/RealOutput#RealOutput))

* `ydd` - This connector represents a real signal as an output from a component ([`RealOutput`](/stdlib/Dyad/connectors/RealOutput#RealOutput))

* `yd_max_in` - This connector represents a real signal as an input to a component ([`RealInput`](/stdlib/Dyad/connectors/RealInput#RealInput))

* `ydd_max_in` - This connector represents a real signal as an input to a component ([`RealInput`](/stdlib/Dyad/connectors/RealInput#RealInput))

## Variables {#Variables}

|     Name |                                                          Description | Units |
| --------:| --------------------------------------------------------------------:| -----:|
| `r_rate` | Backward-difference estimate of the time derivative of the reference |     – |
|      `e` |                                  Error with respect to the reference |     – |
| `e_rate` |                                         Time derivative of the error |     – |
|      `z` |                                          Normalized error variable z |     – |
| `z_rate` |                              Normalized time derivative of the error |     – |
|      `m` |                                     Integer-valued switching index m |     – |
|  `sigma` |                                                     Sliding variable |     – |
|    `ydM` |           Active bound on the magnitude of the first time derivative |     – |
|   `yddM` |          Active bound on the magnitude of the second time derivative |     – |

## Behavior {#Behavior}

$$ \begin{align}
\mathtt{r_rate}\left( t \right) &= \frac{ - Shift(t, -1)\left( u\left( t \right) \right) + u\left( t \right)}{SampleTime(ShiftIndex(InferredClock.Inferred(UUID("3498babc-a2a2-4811-9dc7-dbf9998917ce")), 0), nothing)} \\
e\left( t \right) &=  - u\left( t \right) + y\left( t \right) \\
\mathtt{e_rate}\left( t \right) &=  - \mathtt{r_rate}\left( t \right) + \mathtt{yd}\left( t \right) \\
z\left( t \right) &= \frac{\frac{\mathtt{e_rate}\left( t \right)}{2} + \frac{e\left( t \right)}{SampleTime(ShiftIndex(InferredClock.Inferred(UUID("3498babc-a2a2-4811-9dc7-dbf9998917ce")), 0), nothing)}}{SampleTime(ShiftIndex(InferredClock.Inferred(UUID("3498babc-a2a2-4811-9dc7-dbf9998917ce")), 0), nothing) ~ \mathtt{yddM}\left( t \right)} \\
\mathtt{z_rate}\left( t \right) &= \frac{\mathtt{e_rate}\left( t \right)}{SampleTime(ShiftIndex(InferredClock.Inferred(UUID("3498babc-a2a2-4811-9dc7-dbf9998917ce")), 0), nothing) ~ \mathtt{yddM}\left( t \right)} \\
m\left( t \right) &= \left\lfloor \frac{1 + \sqrt{1 + 8 ~ \left|z\left( t \right)\right|}}{2}\right\rfloor  \\
\mathtt{sigma}\left( t \right) &= \mathtt{z_rate}\left( t \right) + \frac{z\left( t \right)}{m\left( t \right)} + \frac{1}{2} ~ \left( -1 + m\left( t \right) \right) ~ sign\left( z\left( t \right) \right) \\
\mathtt{ydd}\left( t \right) &=  - \frac{1}{2} ~ clamp\left( \mathtt{sigma}\left( t \right), -1, 1 \right) ~ \left( 1 + sign\left( \mathtt{ydM}\left( t \right) - SampleTime(ShiftIndex(InferredClock.Inferred(UUID("3498babc-a2a2-4811-9dc7-dbf9998917ce")), 0), nothing) ~ \mathtt{yddM}\left( t \right) + sign\left( \mathtt{sigma}\left( t \right) \right) ~ \mathtt{yd}\left( t \right) \right) \right) ~ \mathtt{yddM}\left( t \right) \\
\mathtt{yd}\left( t \right) &= Shift(t, -1)\left( \mathtt{yd}\left( t \right) \right) + SampleTime(ShiftIndex(InferredClock.Inferred(UUID("3498babc-a2a2-4811-9dc7-dbf9998917ce")), 0), nothing) ~ Shift(t, -1)\left( \mathtt{ydd}\left( t \right) \right) \\
y\left( t \right) &= Shift(t, -1)\left( y\left( t \right) \right) + \frac{1}{2} ~ SampleTime(ShiftIndex(InferredClock.Inferred(UUID("3498babc-a2a2-4811-9dc7-dbf9998917ce")), 0), nothing) ~ \left( Shift(t, -1)\left( \mathtt{yd}\left( t \right) \right) + \mathtt{yd}\left( t \right) \right) \\
\mathtt{ydM}\left( t \right) &= \mathtt{yd_max} \\
\mathtt{yddM}\left( t \right) &= \mathtt{ydd_max}
\end{align}
$$

## Source {#Source}

````dyad
"""
Trajectory limiter: a nonlinear filter that converts an arbitrary reference signal into a trajectory whose first and second time derivatives are bounded.

The block implements the discrete-time nonlinear filter of Zanasi, Guarino Lo Bianco and Tonielli, "Nonlinear filters for the generation of smooth trajectories", Automatica 36 (2000), as implemented in `TrajectoryLimiter` of the Julia package [TrajectoryLimiters.jl](https://github.com/baggepinnen/TrajectoryLimiters.jl). When the reference is a position, the outputs `yd` and `ydd` are the velocity and the acceleration of the limited trajectory; when the reference is a velocity, they are the acceleration and the jerk.

The output ``x = y`` is the output of a double integrator ``\ddot x = a`` whose input ``a`` is computed by a discrete-time sliding-mode control law that drives ``x`` toward the reference ``r = u``, subject to ``|\dot x| \le \dot x_M`` and ``|a| \le \ddot x_M``, where ``\dot x_M`` is `yd_max` and ``\ddot x_M`` is `ydd_max`. The derivative of the reference is estimated by the backward difference ``\dot r_k = (r_k - r_{k-1}) / T_s``. With ``e = x - r``, ``\dot e = \dot x - \dot r`` and ``U = \ddot x_M``, the control law is
```math
\begin{aligned}
z &= \frac{1}{T_s U}\left(\frac{e}{T_s} + \frac{\dot e}{2}\right), \qquad \dot z = \frac{\dot e}{T_s U}, \qquad m = \left\lfloor \frac{1 + \sqrt{1 + 8|z|}}{2} \right\rfloor, \\
\sigma &= \dot z + \frac{z}{m} + \frac{m - 1}{2}\operatorname{sign}(z), \\
a &= -U \operatorname{sat}(\sigma) \, \frac{1 + \operatorname{sign}\left(\dot x \operatorname{sign}(\sigma) + \dot x_M - T_s U\right)}{2},
\end{aligned}
```
where ``\operatorname{sat}`` denotes saturation to ``[-1, 1]``. The state is propagated by
```math
\dot x_{k+1} = \dot x_k + T_s a_k, \qquad x_{k+1} = x_k + \frac{T_s}{2}\left(\dot x_k + \dot x_{k+1}\right).
```
When the reference changes faster than the bounds permit, the output deviates from it and converges back to it once the bounds permit; for a step in the reference, the transient is approximately time-optimal under the bounds. After the transient, a reference with constant first time derivative is tracked without error. A reference with nonzero second time derivative ``\ddot r`` is tracked with an error proportional to ``T_s^2 \ddot r``, also when it satisfies both bounds.

The parameters must satisfy ``T_s \ddot x_M < \dot x_M``. Otherwise the factor in the control law that enforces the bound on ``\dot x`` is zero at ``\dot x = 0``, and an output that starts at rest does not move.

With `bounds_as_inputs = true`, the bounds ``\dot x_M`` and ``\ddot x_M`` are the values of the inputs `yd_max_in` and `ydd_max_in` at the current sample, and the parameters `yd_max` and `ydd_max` are not used. The state of the limiter does not depend on the bounds, so a change of the bounds leaves `y` and `yd` continuous, and `ydd` satisfies the bound of the current sample. The inputs must satisfy the same conditions as the parameters at every sample. When ``\dot x_M`` decreases below the current ``|\dot x|``, the control law only prevents a further increase of ``|\dot x|``; ``|\dot x|`` is not reduced to the new bound until the deceleration toward the reference begins.

The outputs `y` and `yd` at sample ``k`` are the state ``x_k, \dot x_k``, which depends only on the reference samples before ``k``, while `ydd` is the value ``a_k`` applied from sample ``k`` to sample ``k+1``, which depends on the current reference sample. The outputs `y` and `yd` therefore have no direct feedthrough from the input, and the three outputs are mutually consistent, which makes them suitable as references and feedforward signals for a tracking controller.

The state of the limiter at the first sample consists of ``x`` and ``\dot x``, which equal the first samples of the outputs `y` and `yd`. The estimate of the reference derivative at the first sample is zero under every `initialization` variant. The scalar variants specify ``x`` only and set ``\dot x = 0``:
- `InitialOutput(y0=...)`: the first sample of `y` equals `y0`.
- `SteadyState`: the first sample of `y` equals the first reference sample, so a constant reference produces no transient.
- `InitialState(x0=...)`: the first sample of `y` equals `x0`.

The array variants specify both ``x`` and ``\dot x``:
- `InitialStateArray(x0=[x, xd])` and `InitialOutputArray(y0=[x, xd])`: the first samples of `y` and `yd` equal `x` and `xd`. The two variants are equivalent, since the outputs `y` and `yd` equal the state.
"""
component TrajectoryLimiter@[input clk extends Discrete]
  "Reference"
  u = RealInput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 150, "x2": 0, "y2": 250, "rot": 0}
      },
      "tags": []
    }
  }
  "Limited trajectory"
  y = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 150, "x2": 1100, "y2": 250, "rot": 0}
      },
      "tags": []
    }
  }
  "First time derivative of the limited trajectory [units of u per second]"
  yd = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Second time derivative of the limited trajectory [units of u per second squared]"
  ydd = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 750, "x2": 1100, "y2": 850, "rot": 0}
      },
      "tags": []
    }
  }
  "Upper bound on the magnitude of the first time derivative [units of u per second] (only active if `bounds_as_inputs = true`)"
  yd_max_in = RealInput@[clk]() if bounds_as_inputs {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 450, "x2": 0, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Upper bound on the magnitude of the second time derivative [units of u per second squared] (only active if `bounds_as_inputs = true`)"
  ydd_max_in = RealInput@[clk]() if bounds_as_inputs {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 750, "x2": 0, "y2": 850, "rot": 0}
      },
      "tags": []
    }
  }
  "Sample interval [s]"
  structural parameter Ts::Real = SampleTime()
  "Whether the bounds are given by the inputs `yd_max_in` and `ydd_max_in` instead of the parameters `yd_max` and `ydd_max`"
  structural parameter bounds_as_inputs::Boolean = false
  "Upper bound on the magnitude of the first time derivative [units of u per second]. Must exceed Ts*ydd_max (only used if `bounds_as_inputs = false`)"
  parameter yd_max::Real = 1.0
  "Upper bound on the magnitude of the second time derivative [units of u per second squared]. Must be positive and less than yd_max/Ts (only used if `bounds_as_inputs = false`)"
  parameter ydd_max::Real = 1.0
  "Initial-condition specification"
  structural parameter initialization::InitialCondition = DiscreteComponents.InitialCondition.InitialOutput(y0 = 0.0)
  "Backward-difference estimate of the time derivative of the reference"
  variable r_rate::Real
  "Error with respect to the reference"
  variable e::Real
  "Time derivative of the error"
  variable e_rate::Real
  "Normalized error variable z"
  variable z::Real
  "Normalized time derivative of the error"
  variable z_rate::Real
  "Integer-valued switching index m"
  variable m::Real
  "Sliding variable"
  variable sigma::Real
  "Active bound on the magnitude of the first time derivative"
  variable ydM::Real
  "Active bound on the magnitude of the second time derivative"
  variable yddM::Real
relations
  if bounds_as_inputs
    ydM = yd_max_in
    yddM = ydd_max_in
  else
    ydM = yd_max
    yddM = ydd_max
  end
  r_rate = (u - u@(clk-1)) / Ts
  e = y - u
  e_rate = yd - r_rate
  z = (e / Ts + e_rate / 2) / (Ts * yddM)
  z_rate = e_rate / (Ts * yddM)
  m = floor((1 + sqrt(1 + 8 * abs(z))) / 2)
  sigma = z_rate + z / m + (m - 1) / 2 * sign(z)
  ydd = -yddM * clamp(sigma, -1, 1) * (1 + sign(yd * sign(sigma) + ydM - Ts * yddM)) / 2
  yd = yd@(clk-1) + Ts * ydd@(clk-1)
  y = y@(clk-1) + Ts / 2 * (yd + yd@(clk-1))
  initial ydd@(clk-1) = 0
  initial u@(clk-1) = u
  switch initialization
    case InitialOutput
      initial yd@(clk-1) = 0
      initial y@(clk-1) = initialization.y0
    case SteadyState
      initial yd@(clk-1) = 0
      initial y@(clk-1) = u
    case InitialState
      initial yd@(clk-1) = 0
      initial y@(clk-1) = initialization.x0
    case InitialStateArray
      initial yd@(clk-1) = initialization.x0[2]
      initial y@(clk-1) = initialization.x0[1] - Ts * initialization.x0[2]
    case InitialOutputArray
      initial yd@(clk-1) = initialization.y0[2]
      initial y@(clk-1) = initialization.y0[1] - Ts * initialization.y0[2]
  end
metadata {
  "Dyad": {"icons": {"default": "dyad://DiscreteComponents/TrajectoryLimiter.svg"}}
}
end
````

````dyad
"""
Trajectory limiter: a nonlinear filter that converts an arbitrary reference signal into a trajectory whose first and second time derivatives are bounded.

The block implements the discrete-time nonlinear filter of Zanasi, Guarino Lo Bianco and Tonielli, "Nonlinear filters for the generation of smooth trajectories", Automatica 36 (2000), as implemented in `TrajectoryLimiter` of the Julia package [TrajectoryLimiters.jl](https://github.com/baggepinnen/TrajectoryLimiters.jl). When the reference is a position, the outputs `yd` and `ydd` are the velocity and the acceleration of the limited trajectory; when the reference is a velocity, they are the acceleration and the jerk.

The output ``x = y`` is the output of a double integrator ``\ddot x = a`` whose input ``a`` is computed by a discrete-time sliding-mode control law that drives ``x`` toward the reference ``r = u``, subject to ``|\dot x| \le \dot x_M`` and ``|a| \le \ddot x_M``, where ``\dot x_M`` is `yd_max` and ``\ddot x_M`` is `ydd_max`. The derivative of the reference is estimated by the backward difference ``\dot r_k = (r_k - r_{k-1}) / T_s``. With ``e = x - r``, ``\dot e = \dot x - \dot r`` and ``U = \ddot x_M``, the control law is
```math
\begin{aligned}
z &= \frac{1}{T_s U}\left(\frac{e}{T_s} + \frac{\dot e}{2}\right), \qquad \dot z = \frac{\dot e}{T_s U}, \qquad m = \left\lfloor \frac{1 + \sqrt{1 + 8|z|}}{2} \right\rfloor, \\
\sigma &= \dot z + \frac{z}{m} + \frac{m - 1}{2}\operatorname{sign}(z), \\
a &= -U \operatorname{sat}(\sigma) \, \frac{1 + \operatorname{sign}\left(\dot x \operatorname{sign}(\sigma) + \dot x_M - T_s U\right)}{2},
\end{aligned}
```
where ``\operatorname{sat}`` denotes saturation to ``[-1, 1]``. The state is propagated by
```math
\dot x_{k+1} = \dot x_k + T_s a_k, \qquad x_{k+1} = x_k + \frac{T_s}{2}\left(\dot x_k + \dot x_{k+1}\right).
```
When the reference changes faster than the bounds permit, the output deviates from it and converges back to it once the bounds permit; for a step in the reference, the transient is approximately time-optimal under the bounds. After the transient, a reference with constant first time derivative is tracked without error. A reference with nonzero second time derivative ``\ddot r`` is tracked with an error proportional to ``T_s^2 \ddot r``, also when it satisfies both bounds.

The parameters must satisfy ``T_s \ddot x_M < \dot x_M``. Otherwise the factor in the control law that enforces the bound on ``\dot x`` is zero at ``\dot x = 0``, and an output that starts at rest does not move.

With `bounds_as_inputs = true`, the bounds ``\dot x_M`` and ``\ddot x_M`` are the values of the inputs `yd_max_in` and `ydd_max_in` at the current sample, and the parameters `yd_max` and `ydd_max` are not used. The state of the limiter does not depend on the bounds, so a change of the bounds leaves `y` and `yd` continuous, and `ydd` satisfies the bound of the current sample. The inputs must satisfy the same conditions as the parameters at every sample. When ``\dot x_M`` decreases below the current ``|\dot x|``, the control law only prevents a further increase of ``|\dot x|``; ``|\dot x|`` is not reduced to the new bound until the deceleration toward the reference begins.

The outputs `y` and `yd` at sample ``k`` are the state ``x_k, \dot x_k``, which depends only on the reference samples before ``k``, while `ydd` is the value ``a_k`` applied from sample ``k`` to sample ``k+1``, which depends on the current reference sample. The outputs `y` and `yd` therefore have no direct feedthrough from the input, and the three outputs are mutually consistent, which makes them suitable as references and feedforward signals for a tracking controller.

The state of the limiter at the first sample consists of ``x`` and ``\dot x``, which equal the first samples of the outputs `y` and `yd`. The estimate of the reference derivative at the first sample is zero under every `initialization` variant. The scalar variants specify ``x`` only and set ``\dot x = 0``:
- `InitialOutput(y0=...)`: the first sample of `y` equals `y0`.
- `SteadyState`: the first sample of `y` equals the first reference sample, so a constant reference produces no transient.
- `InitialState(x0=...)`: the first sample of `y` equals `x0`.

The array variants specify both ``x`` and ``\dot x``:
- `InitialStateArray(x0=[x, xd])` and `InitialOutputArray(y0=[x, xd])`: the first samples of `y` and `yd` equal `x` and `xd`. The two variants are equivalent, since the outputs `y` and `yd` equal the state.
"""
component TrajectoryLimiter
  "Reference"
  u = RealInput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 150, "x2": 0, "y2": 250, "rot": 0}
      },
      "tags": []
    }
  }
  "Limited trajectory"
  y = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 150, "x2": 1100, "y2": 250, "rot": 0}
      },
      "tags": []
    }
  }
  "First time derivative of the limited trajectory [units of u per second]"
  yd = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 450, "x2": 1100, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Second time derivative of the limited trajectory [units of u per second squared]"
  ydd = RealOutput@[clk]() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 1000, "y1": 750, "x2": 1100, "y2": 850, "rot": 0}
      },
      "tags": []
    }
  }
  "Upper bound on the magnitude of the first time derivative [units of u per second] (only active if `bounds_as_inputs = true`)"
  yd_max_in = RealInput@[clk]() if bounds_as_inputs {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 450, "x2": 0, "y2": 550, "rot": 0}
      },
      "tags": []
    }
  }
  "Upper bound on the magnitude of the second time derivative [units of u per second squared] (only active if `bounds_as_inputs = true`)"
  ydd_max_in = RealInput@[clk]() if bounds_as_inputs {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": -100, "y1": 750, "x2": 0, "y2": 850, "rot": 0}
      },
      "tags": []
    }
  }
  "Sample interval [s]"
  structural parameter Ts::Real = SampleTime()
  "Whether the bounds are given by the inputs `yd_max_in` and `ydd_max_in` instead of the parameters `yd_max` and `ydd_max`"
  structural parameter bounds_as_inputs::Boolean = false
  "Upper bound on the magnitude of the first time derivative [units of u per second]. Must exceed Ts*ydd_max (only used if `bounds_as_inputs = false`)"
  parameter yd_max::Real = 1.0
  "Upper bound on the magnitude of the second time derivative [units of u per second squared]. Must be positive and less than yd_max/Ts (only used if `bounds_as_inputs = false`)"
  parameter ydd_max::Real = 1.0
  "Initial-condition specification"
  structural parameter initialization::InitialCondition = DiscreteComponents.InitialCondition.InitialOutput(y0 = 0.0)
  "Backward-difference estimate of the time derivative of the reference"
  variable r_rate::Real
  "Error with respect to the reference"
  variable e::Real
  "Time derivative of the error"
  variable e_rate::Real
  "Normalized error variable z"
  variable z::Real
  "Normalized time derivative of the error"
  variable z_rate::Real
  "Integer-valued switching index m"
  variable m::Real
  "Sliding variable"
  variable sigma::Real
  "Active bound on the magnitude of the first time derivative"
  variable ydM::Real
  "Active bound on the magnitude of the second time derivative"
  variable yddM::Real
relations
  if bounds_as_inputs
    ydM = yd_max_in
    yddM = ydd_max_in
  else
    ydM = yd_max
    yddM = ydd_max
  end
  r_rate = (u - u@(clk-1)) / Ts
  e = y - u
  e_rate = yd - r_rate
  z = (e / Ts + e_rate / 2) / (Ts * yddM)
  z_rate = e_rate / (Ts * yddM)
  m = floor((1 + sqrt(1 + 8 * abs(z))) / 2)
  sigma = z_rate + z / m + (m - 1) / 2 * sign(z)
  ydd = -yddM * clamp(sigma, -1, 1) * (1 + sign(yd * sign(sigma) + ydM - Ts * yddM)) / 2
  yd = yd@(clk-1) + Ts * ydd@(clk-1)
  y = y@(clk-1) + Ts / 2 * (yd + yd@(clk-1))
  initial ydd@(clk-1) = 0
  initial u@(clk-1) = u
  switch initialization
    case InitialOutput
      initial yd@(clk-1) = 0
      initial y@(clk-1) = initialization.y0
    case SteadyState
      initial yd@(clk-1) = 0
      initial y@(clk-1) = u
    case InitialState
      initial yd@(clk-1) = 0
      initial y@(clk-1) = initialization.x0
    case InitialStateArray
      initial yd@(clk-1) = initialization.x0[2]
      initial y@(clk-1) = initialization.x0[1] - Ts * initialization.x0[2]
    case InitialOutputArray
      initial yd@(clk-1) = initialization.y0[2]
      initial y@(clk-1) = initialization.y0[1] - Ts * initialization.y0[2]
  end
metadata {
  "Dyad": {"icons": {"default": "dyad://DiscreteComponents/TrajectoryLimiter.svg"}}
}
end
````

## Test Cases {#Test-Cases}

No test cases defined.

## Related {#Related}

* Examples

* Experiments

* Analyses
