Skip to content

Commit b1871ae

Browse files
committed
add docs for interpolated systems
1 parent 1787cf7 commit b1871ae

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,35 @@ plot([u; y; y_]', lab=["u" "y c-code" "y julia"]) |> display
108108
```
109109

110110
NOTE: The usual caveats for transfer-function filtering applies. High-order transfer functions might cause numerical problems. Consider either filtering through many smaller transfer function in series, or convert the system into a well-balanced statespace system and generate code for this instead. See [lecture notes](http://www.control.lth.se/fileadmin/control/Education/EngineeringProgram/FRTN01/lectures/L11_slides6.pdf) slide 45 and onwards as well as the [ControlSystems docs on numerical accuracy.](https://juliacontrol.github.io/ControlSystems.jl/latest/man/numerical/#Performance-considerations). The function `ControlSystems.ss` converts a transfer function to a statespace system and performs automatic balancing.
111+
112+
113+
### C-code for gain scheduled systems
114+
The following example writes C-code that interpolates between two linear systems.
115+
The interpolation vector `t` defines the interpolation point.
116+
117+
The system in the example is a double-mass-spring damper, where the inertia of the load is allowed to vary:
118+
119+
```
120+
function double_mass_model(Jl) # Inertia load
121+
Jm = 1 # Intertia motor
122+
k = 100 # Spring constant
123+
c0 = 1 # Dampings
124+
c1 = 1
125+
c2 = 1
126+
A = [
127+
0.0 1 0 0
128+
-k/Jm -(c1 + c0)/Jm k/Jm c1/Jm
129+
0 0 0 1
130+
k/Jl c1/Jl -k/Jl -(c1 + c2)/Jl
131+
]
132+
B = [0, 1/Jm, 0, 0]
133+
C = [1 0 0 0]
134+
ss(A,B,C,0)
135+
end
136+
137+
t = [1, 5] # The different inertias in the interpolation
138+
sys = [c2d(double_mass_model(inertia), 0.01) for inertia in t]
139+
SymbolicControlSystems.print_c_array(stdout, sys, t, "mass_spring_damper")
140+
```
141+
142+
This will print C-code functions for the interpolation of each of the system matrices. See the docstring for `print_c_array` for more customization options.

src/SymbolicControlSystems.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,12 @@ end
556556

557557

558558
# Interpolation
559+
560+
"""
561+
print_c_array(io, a::Vector{<:AbstractArray}, t::AbstractVector, name = "mat"; cse = false, s = "", print_vector = true, print_logic = true, struct_name::Union{Nothing, String} = nothing, struct_type = nothing, ivecname = name * "_interp_vect")
562+
563+
Write C-code for interpolating between arrays `a`. The array `t` contains the interpolation points.
564+
"""
559565
function print_c_array(
560566
io,
561567
a::Vector{<:AbstractArray},
@@ -644,6 +650,15 @@ end
644650

645651

646652
# Interpolated StateSpace
653+
"""
654+
print_c_array(io, sys::Vector{<:AbstractStateSpace}, t::AbstractVector, name = "sys"; cse = false, s = "", en = "", struct_name::Union{Nothing, String} = nothing, struct_type = nothing)
655+
656+
Write C-code for an interpolated linear system. The interpolation vector `t` defines the interpolation points, this vector is expected to be of the same length as the vector of linear systems `sys`.
657+
658+
- `s, en`: are strings that are appended at the start and end of variables names in the C-code.
659+
- `struct_name`: If provided, the interpolation matrices will be placed inside a struct with this name.
660+
- `struct_type`: If the struct name is used, provide also the C type of the struct.
661+
"""
647662
function print_c_array(
648663
io,
649664
sys::Vector{<:AbstractStateSpace},

0 commit comments

Comments
 (0)