Skip to content

allow FirstOrder to operate as highpass instead of lowpass #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Blocks/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,22 @@ function Derivative(; name, k=1, T, x_start=0.0)
end

"""
FirstOrder(; name, k=1, T, x_start=0.0)
FirstOrder(; name, k=1, T, x_start=0.0, lowpass=true)

A first-order filter with a single real pole in `s = -T` and gain `k`. The transfer function
A first-order filter with a single real pole in `s = -T` and gain `k`. If `lowpass=true` (default), the transfer function
is given by `Y(s)/U(s) = `
```
k
───────
sT + 1
```
and if `lowpass=false`, by
```
sT + 1 - k
──────────
sT + 1
```


# Parameters:
- `k`: Gain
Expand All @@ -82,15 +89,15 @@ sT + 1

See also [`SecondOrder`](@ref)
"""
function FirstOrder(; name, k=1, T, x_start=0.0)
function FirstOrder(; name, k=1, T, x_start=0.0, lowpass=true)
T > 0 || throw(ArgumentError("Time constant `T` has to be strictly positive"))
@named siso = SISO()
@unpack u, y = siso
sts = @variables x(t)=x_start
pars = @parameters T=T k=k
eqs = [
D(x) ~ (k*u - x) / T
y ~ x
lowpass ? y ~ x : y ~ k*u - x
]
extend(ODESystem(eqs, t, sts, pars; name=name), siso)
end
Expand Down
11 changes: 10 additions & 1 deletion test/Blocks/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ end
@testset "PT1" begin
pt1_func(t, k, T) = k * (1 - exp(-t / T)) # Known solution to first-order system

k, T = 1.0, 0.1
k, T = 1.2, 0.1
@named c = Constant(; k=1)
@named pt1 = FirstOrder(; k=k, T=T)
@named iosys = ODESystem(connect(c.output, pt1.input), t, systems=[pt1, c])
Expand All @@ -52,6 +52,15 @@ end
sol = solve(prob, Rodas4())
@test sol.retcode == :Success
@test sol[pt1.output.u] ≈ pt1_func.(sol.t, k, T) atol=1e-3

# Test highpass feature
@named pt1 = FirstOrder(; k=k, T=T, lowpass=false)
@named iosys = ODESystem(connect(c.output, pt1.input), t, systems=[pt1, c])
sys = structural_simplify(iosys)
prob = ODEProblem(sys, Pair[], (0.0, 100.0))
sol = solve(prob, Rodas4())
@test sol.retcode == :Success
@test sol[pt1.output.u] ≈ k .- pt1_func.(sol.t, k, T) atol=1e-3
end

@testset "PT2" begin
Expand Down