Skip to content

Commit

Permalink
Merge pull request #12 from JuliaControl/reset
Browse files Browse the repository at this point in the history
add function that resets the internal state
  • Loading branch information
baggepinnen authored Sep 13, 2024
2 parents 9cf52b6 + 36a9350 commit f8af128
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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$.

Expand Down
12 changes: 11 additions & 1 deletion src/DiscretePIDs.jl
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f8af128

Please sign in to comment.