diff --git a/Project.toml b/Project.toml index 68b5f0d..45761e0 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "DiscretePIDs" uuid = "c1363496-6848-4723-8758-079b737f6baf" authors = ["Fredrik Bagge Carlson"] -version = "0.1.4" +version = "0.1.5" [deps] Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7" diff --git a/README.md b/README.md index 340edbc..bb7b38b 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ The parameters $K, T_i, T_d$ may be updated using the functions, `set_K!, set_Ti The numeric type used by the controller (the `T` in `DiscretePID{T}`) is determined by the types of the parameters. To use a custom number type, e.g., a fixed-point number type, simply pass the parameters as that type, see example below. The controller will automatically convert measurements and references to this type before performing the control calculations. +The **internal state** of the controller can be reset to zero using the function `reset_state!(pid)`. If repeated simulations using the same controller object are performed, the state should likely be reset between simulations. + ## Example using ControlSystems: The following example simulates the PID controller using ControlSystems.jl. We will simulate a load disturbance $d(t) = 1$ entering on the process input, while the reference is $r(t) = 0$. diff --git a/src/DiscretePIDs.jl b/src/DiscretePIDs.jl index f660281..06f9ff9 100644 --- a/src/DiscretePIDs.jl +++ b/src/DiscretePIDs.jl @@ -1,6 +1,6 @@ module DiscretePIDs -export DiscretePID, calculate_control!, set_K!, set_Td!, set_Ti! +export DiscretePID, calculate_control!, set_K!, set_Td!, set_Ti!, reset_state! using Printf @@ -184,5 +184,15 @@ function Base.show(io::IO, ::MIME"text/plain", pid::DiscretePID) println(io, ")") end +""" + reset_state!(pid::DiscretePID) + +Set all internal state variables to zero (`I`, `D` and `yold`). +""" +function reset_state!(pid::DiscretePID) + pid.I = zero(pid.I) + pid.D = zero(pid.D) + pid.yold = zero(pid.yold) +end end diff --git a/test/runtests.jl b/test/runtests.jl index b79db35..904bb45 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -91,6 +91,10 @@ res2 = lsim(P, ctrl, Tf) @test res.y ≈ res2.y rtol=0.02 # plot([res, res2]) +reset_state!(pid) +res3 = lsim(P, ctrl, Tf) +@test res3.y == res2.y + ## Test with FixedPointNumbers using FixedPointNumbers T = Fixed{Int16, 10} # 16-bit signed fixed-point with 10 bits for the fractional part