You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* update to MTKv10
* bump cache version
* up CI script
* remove unused export
* update other files
* update usage of `get_u0_p`
* more updates
* turn off existing defaults
* handle operating points
* careful with state-space basis in interpolation
* simplify implementation of `named_sensitivity_function`
* add specification of op for original inputs
does not work, since the original inputs have the wrong namespace
* unnamespace
* finally working
* add a test
* return named systems
* add names to batch_ss as well
* getfield -> getproperty
* .sys
* handle non-unique names
To perform batch linearization, we create a vector of operating points, and then linearize the model around each of these points. The function [`batch_ss`](@ref) does this for us, and returns a vector of `StateSpace` models, one for each operating point. An operating point is a `Dict` that maps variables in the MTK model to numerical values. In the example below, we simply sample the variables uniformly within their bounds specified when we created the variables (normally, we might want to linearize on stationary points)
@@ -56,19 +56,24 @@ P = RobustAndOptimalControl.ss2particles(Ps) # convert to a single StateSpace sy
56
56
57
57
notice how some coefficients are plotted like uncertain numbers `-13.8 ± 4.3`. We can plot such models as well:
58
58
```@example BATCHLIN
59
-
bodeplot(P, w, legend=:bottomright) # Should look similar to the one above
59
+
bodeplot(P, w, legend=:bottomright, adaptive=false) # Should look similar to the one above
60
60
```
61
61
62
62
## Controller tuning
63
63
Let's also do some controller tuning for the linearized models above. The function `batch_tune` is not really required here, but it shows how we might go about building more sophisticated tools for batch tuning. In this example, we will tune a PID controller using the function [`loopshapingPID`](@ref). Note, this procedure is not limited to tuning a gain-scheduled PID controller, it should work for gain-scheduling of any LTI controller.
64
+
65
+
!!! "note" Interpolating between controllers
66
+
There are multiple ways in which one could interpolate between different controllers. The two most common approaches are to interpolate their outputs, and interpolating their coefficients. When interpolating the coefficients, it is important to ensure that all controllers have the same structure for the interpolation to be meaningful. One may for example interpolate between PID coefficients, or between the coefficients of a state-space model. When interpolating state-space matrices, the systems must all share the same basis, i.e., the state variables must all have the same meaning among the interpolated systems. When converting a transfer function to state-space form, a numerical balancing is performed, this alters the meaning of the state variables and may introduce artificial dynamics to the interpolated system. We thus pass `balance=false` to the function `ss` to avoid this, or pick a form explicitly, e.g., `modal_form`.
If everything worked as expected, the gain-scheduled controller should perform better than each of the included controllers individually.
146
+
If everything worked as expected, the gain-scheduled controller should perform reasonably well across the entire scheduling range.
142
147
143
148
144
149
## C-Code generation
145
-
We can generate C-code to interpolate our controller using the function [`SymbolicControlSystems.print_c_array`](@ref) from [SymbolicControlSystems.jl](https://github.com/JuliaControl/SymbolicControlSystems.jl). If the controller is a standard [`ControlSystemsBase.StateSpace`](@ref) object, a function that filters the input through the controller can be generated by calling [`SymbolicControlSystems.ccode`](@ref). But if the controller is a vector of controllers representing a gain-scheduled controller, a function that creates the interpolated dynamics is written. In the code below, we shorten the vector of controllers to make the generated C-code easier to read by passing `Cs[1:7:end]` and `xs[1:7:end]`
150
+
We can generate C-code to interpolate our controller using the function [`SymbolicControlSystems.print_c_array`](@ref) from [SymbolicControlSystems.jl](https://github.com/JuliaControl/SymbolicControlSystems.jl). If the controller is a standard [`ControlSystemsBase.StateSpace`](@ref) object, a function that filters the input through the controller can be generated by calling [`SymbolicControlSystems.ccode`](@ref). But if the controller is a vector of controllers representing a gain-scheduled controller, a function that creates the interpolated dynamics is written. In the code below, we shorten the vector of controllers to make the generated C-code easier to read by passing `Cs[1:3:end]` and `xs[1:3:end]`
146
151
```@example BATCHLIN
147
152
using SymbolicControlSystems, ControlSystemsBase
148
153
Cs_disc = c2d.(Cs, 0.05, :tustin) # Discretize the controller before generating code
The generated code starts by defining the interpolation vector `xs`, this variable is called `Cs_interp_vect` in the generated code. The code then defines all the ``A`` matrices as a 3-dimensional array, followed by a function that performs the interpolation `interpolate_Cs_A`. This function takes the output array as the first argument, a pointer to the 3D array with interpolation matrices, the interpolation vector as well as the interpolation variable `t`, in this document called ``v``. The same code is then repeated for the matrices ``B,C,D`` as well if they require interpolation (if they are all the same, no interpolation code is written).
152
157
153
158
## Linearize around a trajectory
154
159
We can linearize around a trajectory obtained from `solve` using the function [`trajectory_ss`](@ref). We provide it with a vector of time points along the trajectory at which to linearize, and in this case we specify the inputs and outputs to linearize between as analysis points `r` and `y`.
Not how the closed-loop system changes very little along the trajectory, this is a good indication that the gain-scheduled controller is able to make the system appear linear.
160
166
161
167
Internally, [`trajectory_ss`](@ref) works very much the same as [`batch_ss`](@ref), but constructs operating points automatically along the trajectory. This requires that the solution contains the states of the simplified system, accessible through the `idxs` argument like `sol(t, idxs=x)`. By linearizing the same system as we simulated, we ensure that this condition holds, doing so requires that we specify the inputs and outputs as analysis points rather than as variables.
162
168
163
169
170
+
We can replicate the figure above by linearizing the plant and the controller individually, by providing the `loop_openings` argument. When linearizing the plant, we disconnect the controller input by passing `loop_openings=[closed_loop.u]`, and when linearizing the controller, we have various options for disconnecting the the plant:
171
+
- Break the connection from plant output to controller input by passing `loop_openings=[closed_loop.y]`
172
+
- Break the connection between the controller and the plant input by passing `loop_openings=[closed_loop.u]`
173
+
- Break the connection `y` as well as the scheduling variable `v` (which is another form of feedback) by passing `loop_openings=[closed_loop.y, closed_loop.v]`
174
+
175
+
We will explore these options below, starting with the first option, breaking the connection `y`:
bodeplot(closed_loopsu, w; title="Loop open at u", kwargs...)
190
+
```
191
+
In this case, all static gains are 1. A similar result is obtained by explicitly breaking the scheduling feedback `v` in addition to an opening of `y`:
bodeplot(closed_loopsv, w; title="Loop open at v and y", kwargs...)
197
+
```
198
+
199
+
We have thus far treated the controller as a SISO system, but we could also view it as a system with two inputs, the measurement feedback and the scheduling feedback. For completeness, we show below how to derive the corresponding MIMO systems
bodeplot(controllersy, w, legend=false, plotphase=false, title="Loop open at y"),
217
+
bodeplot(controllersu, w, legend=false, plotphase=false, title="Loop open at u"),
218
+
bodeplot(controllersv, w, legend=false, plotphase=false, title="Loop open at v and y"),
219
+
)
220
+
```
221
+
if we open at both `y` and `v` or we open at `u`, we get controllers for the different values of the scheduling variable, and the corresponding measurement feedback (which is the same as the scheduling variable in this case).
However, if we only open at `y` we get controller linearizations that _still contain the closed loop through the scheduling connection_`v`. We can verify this by looking at what variables are present in the input-output map
228
+
```@example BATCHLIN
229
+
sminreal(controllersy[end]).x
230
+
```
231
+
notice how the state of the plant is included in the controller, this is an indication that we didn't fully isolate the controller during the linearizaiton. If we do the same thing for the controller with the loop opened at `u`, we see that the state of the plant is not included in the controller:
232
+
```@example BATCHLIN
233
+
sminreal(controllersu[end]).x
234
+
```
235
+
The call to `sminreal` is important here, it removes the states that are not needed to represent the input-output map of the system. The state of the full model, including the plant state, is present in the linearization before this call.
236
+
237
+
238
+
239
+
The easiest way to ensure that the controller is properly disconnected from the plant while taking into account the different scheduling along the trajectory is thus to break at `u`.
240
+
164
241
## Summary
165
242
We have seen how to
166
243
- Perform linearization of a nonlinear ModelingToolkit model in multiple different operating points
@@ -173,6 +250,5 @@ Batch linearization in multiple different operating points is an intuitive way t
0 commit comments