|
| 1 | +# Clocks and Sampled-Data Systems |
| 2 | + |
| 3 | +A sampled-data system contains both continuous-time and discrete-time components, such as a continuous-time plant model and a discrete-time control system. ModelingToolkit supports the modeling and simulation of sampled-data systems by means of *clocks*. |
| 4 | + |
| 5 | +!!! danger "Experimental" |
| 6 | + |
| 7 | + The sampled-data interface is currently experimental and at any time subject to breaking changes **not** respecting semantic versioning. |
| 8 | + |
| 9 | +!!! note "Negative shifts" |
| 10 | + |
| 11 | + The initial release of the sampled-data interface **only supports negative shifts**. |
| 12 | + |
| 13 | +A clock can be seen as an *event source*, i.e., when the clock ticks, an event is generated. In response to the event the discrete-time logic is executed, for example, a control signal is computed. For basic modeling of sampled-data systems, the user does not have to interact with clocks explicitly, instead, the modeling is performed using the operators |
| 14 | + |
| 15 | + - [`Sample`](@ref) |
| 16 | + - [`Hold`](@ref) |
| 17 | + - [`ShiftIndex`](@ref) |
| 18 | + |
| 19 | +When a continuous-time variable `x` is sampled using `xd = Sample(x, dt)`, the result is a discrete-time variable `xd` that is defined and updated whenever the clock ticks. `xd` is *only defined when the clock ticks*, which it does with an interval of `dt`. If `dt` is unspecified, the tick rate of the clock associated with `xd` is inferred from the context in which `xd` appears. Any variable taking part in the same equation as `xd` is inferred to belong to the same *discrete partition* as `xd`, i.e., belonging to the same clock. A system may contain multiple different discrete-time partitions, each with a unique clock. This allows for modeling of multi-rate systems and discrete-time processes located on different computers etc. |
| 20 | + |
| 21 | +To make a discrete-time variable available to the continuous partition, the [`Hold`](@ref) operator is used. `xc = Hold(xd)` creates a continuous-time variable `xc` that is updated whenever the clock associated with `xd` ticks, and holds its value constant between ticks. |
| 22 | + |
| 23 | +The operators [`Sample`](@ref) and [`Hold`](@ref) are thus providing the interface between continuous and discrete partitions. |
| 24 | + |
| 25 | +The [`ShiftIndex`](@ref) operator is used to refer to past and future values of discrete-time variables. The example below illustrates its use, implementing the discrete-time system |
| 26 | + |
| 27 | +```math |
| 28 | +x(k+1) = 0.5x(k) + u(k) |
| 29 | +y(k) = x(k) |
| 30 | +``` |
| 31 | + |
| 32 | +```@example clocks |
| 33 | +using ModelingToolkit |
| 34 | +using ModelingToolkit: t_nounits as t |
| 35 | +@variables x(t) y(t) u(t) |
| 36 | +dt = 0.1 # Sample interval |
| 37 | +clock = Clock(t, dt) # A periodic clock with tick rate dt |
| 38 | +k = ShiftIndex(clock) |
| 39 | +
|
| 40 | +eqs = [ |
| 41 | + x(k) ~ 0.5x(k - 1) + u(k - 1), |
| 42 | + y(k) ~ x(k - 1) |
| 43 | +] |
| 44 | +``` |
| 45 | + |
| 46 | +A few things to note in this basic example: |
| 47 | + |
| 48 | + - The equation `x(k+1) = 0.5x(k) + u(k)` has been rewritten in terms of negative shifts since positive shifts are not yet supported. |
| 49 | + - `x` and `u` are automatically inferred to be discrete-time variables, since they appear in an equation with a discrete-time [`ShiftIndex`](@ref) `k`. |
| 50 | + - `y` is also automatically inferred to be a discrete-time-time variable, since it appears in an equation with another discrete-time variable `x`. `x,u,y` all belong to the same discrete-time partition, i.e., they are all updated at the same *instantaneous point in time* at which the clock ticks. |
| 51 | + - The equation `y ~ x` does not use any shift index, this is equivalent to `y(k) ~ x(k)`, i.e., discrete-time variables without shift index are assumed to refer to the variable at the current time step. |
| 52 | + - The equation `x(k) ~ 0.5x(k-1) + u(k-1)` indicates how `x` is updated, i.e., what the value of `x` will be at the *current* time step in terms of the *past* value. The output `y`, is given by the value of `x` at the *past* time step, i.e., `y(k) ~ x(k-1)`. If this logic was implemented in an imperative programming style, the logic would thus be |
| 53 | + |
| 54 | +```julia |
| 55 | +function discrete_step(x, u) |
| 56 | + y = x # y is assigned the current value of x, y(k) = x(k-1) |
| 57 | + x = 0.5x + u # x is updated to a new value, i.e., x(k) is computed |
| 58 | + return x, y # The state x now refers to x at the current time step, x(k), while y refers to x at the past time step, y(k) = x(k-1) |
| 59 | +end |
| 60 | +``` |
| 61 | + |
| 62 | +An alternative and *equivalent* way of writing the same system is |
| 63 | + |
| 64 | +```@example clocks |
| 65 | +eqs = [ |
| 66 | + x(k + 1) ~ 0.5x(k) + u(k), |
| 67 | + y(k) ~ x(k) |
| 68 | +] |
| 69 | +``` |
| 70 | + |
| 71 | +but the use of positive time shifts is not yet supported. |
| 72 | +Instead, we have *shifted all indices* by `-1`, resulting in exactly the same difference equations. However, the next system is *not equivalent* to the previous one: |
| 73 | + |
| 74 | +```@example clocks |
| 75 | +eqs = [ |
| 76 | + x(k) ~ 0.5x(k - 1) + u(k - 1), |
| 77 | + y ~ x |
| 78 | +] |
| 79 | +``` |
| 80 | + |
| 81 | +In this last example, `y` refers to the updated `x(k)`, i.e., this system is equivalent to |
| 82 | + |
| 83 | +``` |
| 84 | +eqs = [ |
| 85 | + x(k+1) ~ 0.5x(k) + u(k), |
| 86 | + y(k+1) ~ x(k+1) |
| 87 | +] |
| 88 | +``` |
| 89 | + |
| 90 | +## Higher-order shifts |
| 91 | + |
| 92 | +The expression `x(k-1)` refers to the value of `x` at the *previous* clock tick. Similarly, `x(k-2)` refers to the value of `x` at the clock tick before that. In general, `x(k-n)` refers to the value of `x` at the `n`th clock tick before the current one. As an example, the Z-domain transfer function |
| 93 | + |
| 94 | +```math |
| 95 | +H(z) = \dfrac{b_2 z^2 + b_1 z + b_0}{a_2 z^2 + a_1 z + a_0} |
| 96 | +``` |
| 97 | + |
| 98 | +may thus be modeled as |
| 99 | + |
| 100 | +```julia |
| 101 | +@variables t y(t) [description = "Output"] u(t) [description = "Input"] |
| 102 | +k = ShiftIndex(Clock(t, dt)) |
| 103 | +eqs = [ |
| 104 | + a2 * y(k) + a1 * y(k - 1) + a0 * y(k - 2) ~ b2 * u(k) + b1 * u(k - 1) + b0 * u(k - 2) |
| 105 | +] |
| 106 | +``` |
| 107 | + |
| 108 | +(see also [ModelingToolkitStandardLibrary](https://docs.sciml.ai/ModelingToolkitStandardLibrary/stable/) for a discrete-time transfer-function component.) |
| 109 | + |
| 110 | +## Initial conditions |
| 111 | + |
| 112 | +The initial condition of discrete-time variables is defined using the [`ShiftIndex`](@ref) operator, for example |
| 113 | + |
| 114 | +```julia |
| 115 | +ODEProblem(model, [x(k) => 1.0], (0.0, 10.0)) |
| 116 | +``` |
| 117 | + |
| 118 | +If higher-order shifts are present, the corresponding initial conditions must be specified, e.g., the presence of the equation |
| 119 | + |
| 120 | +```julia |
| 121 | +x(k) = x(k - 1) + x(k - 2) |
| 122 | +``` |
| 123 | + |
| 124 | +requires specification of the initial condition for both `x(k-1)` and `x(k-2)`. |
| 125 | + |
| 126 | +## Multiple clocks |
| 127 | + |
| 128 | +Multi-rate systems are easy to model using multiple different clocks. The following set of equations is valid, and defines *two different discrete-time partitions*, each with its own clock: |
| 129 | + |
| 130 | +```julia |
| 131 | +yd1 ~ Sample(t, dt1)(y) |
| 132 | +ud1 ~ kp * (Sample(t, dt1)(r) - yd1) |
| 133 | +yd2 ~ Sample(t, dt2)(y) |
| 134 | +ud2 ~ kp * (Sample(t, dt2)(r) - yd2) |
| 135 | +``` |
| 136 | + |
| 137 | +`yd1` and `ud1` belong to the same clock which ticks with an interval of `dt1`, while `yd2` and `ud2` belong to a different clock which ticks with an interval of `dt2`. The two clocks are *not synchronized*, i.e., they are not *guaranteed* to tick at the same point in time, even if one tick interval is a rational multiple of the other. Mechanisms for synchronization of clocks are not yet implemented. |
| 138 | + |
| 139 | +## Accessing discrete-time variables in the solution |
| 140 | + |
| 141 | +## A complete example |
| 142 | + |
| 143 | +Below, we model a simple continuous first-order system called `plant` that is controlled using a discrete-time controller `controller`. The reference signal is filtered using a discrete-time filter `filt` before being fed to the controller. |
| 144 | + |
| 145 | +```@example clocks |
| 146 | +using ModelingToolkit, Plots, OrdinaryDiffEq |
| 147 | +using ModelingToolkit: t_nounits as t |
| 148 | +using ModelingToolkit: D_nounits as D |
| 149 | +dt = 0.5 # Sample interval |
| 150 | +@variables r(t) |
| 151 | +clock = Clock(t, dt) |
| 152 | +k = ShiftIndex(clock) |
| 153 | +
|
| 154 | +function plant(; name) |
| 155 | + @variables x(t)=1 u(t)=0 y(t)=0 |
| 156 | + D = Differential(t) |
| 157 | + eqs = [D(x) ~ -x + u |
| 158 | + y ~ x] |
| 159 | + ODESystem(eqs, t; name = name) |
| 160 | +end |
| 161 | +
|
| 162 | +function filt(; name) # Reference filter |
| 163 | + @variables x(t)=0 u(t)=0 y(t)=0 |
| 164 | + a = 1 / exp(dt) |
| 165 | + eqs = [x(k) ~ a * x(k - 1) + (1 - a) * u(k) |
| 166 | + y ~ x] |
| 167 | + ODESystem(eqs, t, name = name) |
| 168 | +end |
| 169 | +
|
| 170 | +function controller(kp; name) |
| 171 | + @variables y(t)=0 r(t)=0 ud(t)=0 yd(t)=0 |
| 172 | + @parameters kp = kp |
| 173 | + eqs = [yd ~ Sample(y) |
| 174 | + ud ~ kp * (r - yd)] |
| 175 | + ODESystem(eqs, t; name = name) |
| 176 | +end |
| 177 | +
|
| 178 | +@named f = filt() |
| 179 | +@named c = controller(1) |
| 180 | +@named p = plant() |
| 181 | +
|
| 182 | +connections = [r ~ sin(t) # reference signal |
| 183 | + f.u ~ r # reference to filter input |
| 184 | + f.y ~ c.r # filtered reference to controller reference |
| 185 | + Hold(c.ud) ~ p.u # controller output to plant input (zero-order-hold) |
| 186 | + p.y ~ c.y] # plant output to controller feedback |
| 187 | +
|
| 188 | +@named cl = ODESystem(connections, t, systems = [f, c, p]) |
| 189 | +``` |
0 commit comments