Skip to content

Commit 2aa4924

Browse files
committed
Merge branch 'master' of github.com:JuliaControl/SymbolicControlSystems.jl
2 parents d38b616 + ee20c08 commit 2aa4924

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
@@ -110,3 +110,35 @@ plot([u; y; y_]', lab=["u" "y c-code" "y julia"]) |> display
110110
```
111111

112112
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.
113+
114+
115+
### C-code for gain scheduled systems
116+
The following example writes C-code that interpolates between two linear systems.
117+
The interpolation vector `t` defines the interpolation points.
118+
119+
The system in the example is a double-mass-spring damper, where the inertia of the load is allowed to vary:
120+
121+
```julia
122+
function double_mass_model(Jl) # Inertia load
123+
Jm = 1 # Intertia motor
124+
k = 100 # Spring constant
125+
c0 = 1 # Dampings
126+
c1 = 1
127+
c2 = 1
128+
A = [
129+
0.0 1 0 0
130+
-k/Jm -(c1 + c0)/Jm k/Jm c1/Jm
131+
0 0 0 1
132+
k/Jl c1/Jl -k/Jl -(c1 + c2)/Jl
133+
]
134+
B = [0, 1/Jm, 0, 0]
135+
C = [1 0 0 0]
136+
ss(A,B,C,0)
137+
end
138+
139+
t = [1, 5] # The different inertias in the interpolation
140+
sys = [c2d(double_mass_model(inertia), 0.01) for inertia in t]
141+
SymbolicControlSystems.print_c_array(stdout, sys, t, "mass_spring_damper")
142+
```
143+
144+
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
@@ -558,6 +558,12 @@ end
558558

559559

560560
# Interpolation
561+
562+
"""
563+
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")
564+
565+
Write C-code for interpolating between arrays `a`. The array `t` contains the interpolation points.
566+
"""
561567
function print_c_array(
562568
io,
563569
a::Vector{<:AbstractArray},
@@ -646,6 +652,15 @@ end
646652

647653

648654
# Interpolated StateSpace
655+
"""
656+
print_c_array(io, sys::Vector{<:AbstractStateSpace}, t::AbstractVector, name = "sys"; cse = false, s = "", en = "", struct_name::Union{Nothing, String} = nothing, struct_type = nothing)
657+
658+
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`.
659+
660+
- `s, en`: are strings that are appended at the start and end of variables names in the C-code.
661+
- `struct_name`: If provided, the interpolation matrices will be placed inside a struct with this name.
662+
- `struct_type`: If the struct name is used, provide also the C type of the struct.
663+
"""
649664
function print_c_array(
650665
io,
651666
sys::Vector{<:AbstractStateSpace},

0 commit comments

Comments
 (0)