Skip to content

Commit a2c1770

Browse files
authored
Merge pull request #7 from JuliaControl/fp2
convert signals to correct type before computations
2 parents 6450135 + 6585eaf commit a2c1770

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "DiscretePIDs"
22
uuid = "c1363496-6848-4723-8758-079b737f6baf"
33
authors = ["Fredrik Bagge Carlson"]
4-
version = "0.1.2"
4+
version = "0.1.3"
55

66
[compat]
77
julia = "1.7"

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ u = calculate_control!(pid, r, y, uff)
3535

3636
The parameters $K, T_i, T_d$ may be updated using the functions, `set_K!, set_Ti!, set_Td!`.
3737

38+
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.
39+
3840
## Example using ControlSystems:
3941
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$.
4042

@@ -125,7 +127,7 @@ pid = DiscretePID(; K = T(K), Ts = T(Ts), Ti = T(Ti), Td = T(Td))
125127
res_fp = lsim(P, ctrl, Tf)
126128
plot([res, res_fp], plotu=true, lab=["Float64" "" string(T) ""]); ylabel!("u + d", sp=2)
127129
```
128-
![Fixed-point simulation result](https://user-images.githubusercontent.com/3797491/249722782-2157d625-7eb0-4f77-b630-69199237f164.png)
130+
![Fixed-point simulation result](https://user-images.githubusercontent.com/3797491/249732319-0a3890d5-cb9c-45c2-93c7-20d3c7db0cf2.png)
129131

130132
The fixed-point controller behaves roughly the same in this case, but artifacts are clearly visible. If the number of bits used for the fractional part is decreased, the controller will start to misbehave.
131133

src/DiscretePIDs.jl

+5-1
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,12 @@ end
136136
(pid)(r, y, uff=0) # Alternative syntax
137137
138138
Calculate the control output from the PID controller when `r` is the reference (set point), `y` is the latest measurement and `uff` is the feed-forward contribution.
139+
If the type of the input arguments differ from the numeric type used by the PID controller, they will be converted before computations are performed.
139140
"""
140-
function calculate_control!(pid::DiscretePID, r, y, uff=0)
141+
function calculate_control!(pid::DiscretePID{T}, r0, y0, uff0=0) where T
142+
r = T(r0)
143+
y = T(y0)
144+
uff = T(uff0)
141145
P = pid.K * (pid.b * r - y)
142146
pid.D = pid.ad * pid.D - pid.bd * (y - pid.yold)
143147
v = P + pid.I + pid.D + uff

test/runtests.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ res2 = lsim(P, ctrl, Tf)
6969

7070
## Test with FixedPointNumbers
7171
using FixedPointNumbers
72-
T = Fixed{Int16, 10} # 16-bit signed fixed-point with 11 bits for the fractional part
72+
T = Fixed{Int16, 10} # 16-bit signed fixed-point with 10 bits for the fractional part
7373
pid = DiscretePID(; K = T(K), Ts = T(Ts), Ti = T(Ti), Td = T(Td))
7474
@test pid isa DiscretePID{T}
7575

0 commit comments

Comments
 (0)