Skip to content

Commit 2652dce

Browse files
authored
add on-off example (#20)
* add on-off example * import packages
1 parent 77992a6 commit 2652dce

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

docs/make.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ makedocs(;
3232
],
3333
"Examples" => [
3434
"Controlled DC motor" => "examples/dc_motor_pi.md",
35+
"On-off controller" => "examples/onoffcontroller.md",
3536
],
3637
"Library API" => "blocks.md",
3738
],
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# On-off controller
2+
A common form of controller is one which can output two distinct values only, such as on/off, or 1/-1. Below, we demonstrate the usage of such a controller, the [`DiscreteOnOffController`](@ref), and let it control an unstable first-order system. The system is given by the equation
3+
```math
4+
τ \dot x = x + 10u
5+
```
6+
where ``τ = 100`` is a time constant. The controller ``u = f(x)`` is an on/off controller with a hysteresis band of 0.3 and possible output values -1 and 1. The controller runs with an update frequency of 1 Hz.
7+
8+
9+
```@example ONOFF
10+
using ModelingToolkit, ModelingToolkitSampledData, OrdinaryDiffEq, Plots
11+
using ModelingToolkit: t_nounits as t, D_nounits as D
12+
using ModelingToolkitStandardLibrary.Blocks
13+
using JuliaSimCompiler
14+
cl = Clock(1)
15+
z = ShiftIndex(cl)
16+
@mtkmodel OnOffModel begin
17+
@components begin
18+
onoff = DiscreteOnOffController(; b = 0.3, z, bool=false)
19+
c = Constant(k=1)
20+
end
21+
@variables begin
22+
x(t) = 0
23+
end
24+
@equations begin
25+
onoff.u ~ Sample(cl)(x)
26+
connect(c.output, onoff.reference)
27+
100D(x) ~ x + 10Hold(onoff.y)
28+
end
29+
end
30+
@named m = OnOffModel()
31+
m = complete(m)
32+
ssys = structural_simplify(IRSystem(m))
33+
prob = ODEProblem(ssys, [m.onoff.y(z-1) => 0], (0.0, 40.0))
34+
sol = solve(prob, Tsit5(), dtmax=0.1)
35+
plot(sol, idxs=[m.x, m.onoff.y], title="On-off control of an unstable first-order system")
36+
```
37+
38+
The parameter `b` controls the width of the hysteresis band, and the structural parameter `bool = false` indicates that this is a 1/-1 controller and not a 0/1 controller. The controller additionally takes a parameter `k` which is the gain of the controller, here we used the default `k = 1`.

0 commit comments

Comments
 (0)