Skip to content

Commit

Permalink
add fast path for kd=0 in pid_2dof
Browse files Browse the repository at this point in the history
  • Loading branch information
baggepinnen committed Jan 16, 2025
1 parent 82a5014 commit 7068fbd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/ControlSystemsBase/src/pid_design.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,23 @@ function pid_2dof(args...; state_space = true, Ts = nothing, disc = :tustin, kwa
end

function pid_ss_2dof(param_p, param_i, param_d=zero(typeof(param_p)); form=:standard, b = 1, c = 0, Tf=nothing, N=nothing, balance=true)
kp, ki, kd = convert_pidparams_to_parallel(param_p, param_i, param_d, form)
if kd == 0
if ki == 0
return ss([kp -kp])

Check warning on line 181 in lib/ControlSystemsBase/src/pid_design.jl

View check run for this annotation

Codecov / codecov/patch

lib/ControlSystemsBase/src/pid_design.jl#L181

Added line #L181 was not covered by tests
else
A = [0.0;;]
B = [ki -ki]
C = [1.0;;] # Ti == 0 would result in division by zero, but typically indicates that the user wants no integral action
D = [b*kp -kp]
return ss(A, B, C, D)
end
end
# On standard form we use N
Tf !== nothing && N !== nothing && throw(ArgumentError("Cannot supply both Tf and N"))
if Tf === nothing && N === nothing
N = 10 # Default value
end
kp, ki, kd = convert_pidparams_to_parallel(param_p, param_i, param_d, form)
Tf = @something(Tf, kd / N)
Tf <= 0 && throw(ArgumentError("Tf must be strictly positive"))
if ki == 0
Expand Down
6 changes: 6 additions & 0 deletions lib/ControlSystemsBase/test/test_pid_design.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ Ktf = [(kp*b + kd*s*c/(Tf*s + 1)) -(kp + kd*s/(Tf*s + 1))]
Kss = ControlSystemsBase.pid_ss_2dof(kp, ki, kd; Tf, b, c, form=:parallel)
@test freqresptest(Kss, Ktf) < 1e-10

kp, ki, kd, b, c, Tf = rand(6)
kd = 0
Ktf = [(kp*b + ki/s + kd*s*c/(Tf*s + 1)) -(kp + ki/s + kd*s/(Tf*s + 1))]
Kss = ControlSystemsBase.pid_ss_2dof(kp, ki, kd; Tf, b, c, form=:parallel)
@test freqresptest(Kss, Ktf) < 1e-10

kp, ki, kd, b, c, Tf = rand(6)
Ktf = [(kp*b + ki/s + kd*s*c/(Tf*s + 1)) -(kp + ki/s + kd*s/(Tf*s + 1))]
Kss = ControlSystemsBase.pid_ss_2dof(kp, ki, kd; Tf, b, c, form=:parallel)
Expand Down

0 comments on commit 7068fbd

Please sign in to comment.