Skip to content
LIBRARY
Continuous.Tests.LimPIDExternalDerivative.md

Continuous.Tests.LimPIDExternalDerivative

Test bench for the LimPIDExternalDerivative controller connected to a plant model.

Uses the same plant and step setpoint as LimPIDTest. The external derivative input is provided by a Derivative block that differentiates the plant output externally, demonstrating the intended use case where the derivative signal comes from outside the controller. A constant feedforward signal is also connected.

Usage

BlockComponents.Continuous.Tests.LimPIDExternalDerivative()

Behavior

julia
using BlockComponents #hide
using ModelingToolkit #hide
@named sys = BlockComponents.Continuous.Tests.LimPIDExternalDerivative() #hide
full_equations(sys) #hide
<< @example-block not executed in draft mode >>

Source

dyad
"""
Test bench for the LimPIDExternalDerivative controller connected to a plant model.

Uses the same plant and step setpoint as LimPIDTest. The external derivative input
is provided by a Derivative block that differentiates the plant output externally,
demonstrating the intended use case where the derivative signal comes from outside
the controller. A constant feedforward signal is also connected.
"""
test component LimPIDExternalDerivative
  "PID controller with external derivative input"
  pid = BlockComponents.Continuous.LimPIDExternalDerivative(Ti = 0.5, y_max = 1, y_min = -1, wp = 1, Ni = 0.9, k_ff = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 320, "y1": 43, "x2": 420, "y2": 143, "rot": 0}
      },
      "tags": []
    }
  }
  "Plant model to be controlled"
  plant = BlockComponents.Continuous.Plant() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 40, "y1": 120, "x2": 140, "y2": 220, "rot": 0}
      },
      "tags": []
    }
  }
  "Step input signal used as setpoint"
  signal = BlockComponents.Sources.Step(height = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 20, "x2": 280, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant signal for feedforward control"
  signal_ff = BlockComponents.Sources.Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": -130, "x2": 280, "y2": -30, "rot": 0}
      },
      "tags": []
    }
  }
  "External derivative block computing derivative of plant output"
  ext_der = BlockComponents.Continuous.Derivative(k = 1, T = 0.01) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 230, "x2": 280, "y2": 330, "rot": 0}
      },
      "tags": []
    }
  }
relations
  initial plant.x1 = 0
  initial plant.y = 0
  connect(signal.y, pid.u_s) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(plant.y, pid.u_m) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 300, "y": 170}, {"x": 300, "y": 116}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(pid.y, plant.u) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [
            {"x": 440, "y": 94},
            {"x": 440, "y": 530},
            {"x": 20, "y": 530},
            {"x": 20, "y": 170}
          ],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(pid.u_ff, signal_ff.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 370, "y": -80}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(plant.y, ext_der.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 160, "y": 280}], "E": 2}],
      "junctions": [{"x": 160, "y": 170}],
      "renderStyle": "standard"
    }
  }
  connect(ext_der.y, pid.u_d) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 370, "y": 280}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 10,
        "atol": {"plant.y": 0.0001, "pid.y": 0.0001},
        "expect": {"signals": ["plant.y", "pid.y"]}
      }
    }
  }
}
end
Flattened Source
dyad
"""
Test bench for the LimPIDExternalDerivative controller connected to a plant model.

Uses the same plant and step setpoint as LimPIDTest. The external derivative input
is provided by a Derivative block that differentiates the plant output externally,
demonstrating the intended use case where the derivative signal comes from outside
the controller. A constant feedforward signal is also connected.
"""
test component LimPIDExternalDerivative
  "PID controller with external derivative input"
  pid = BlockComponents.Continuous.LimPIDExternalDerivative(Ti = 0.5, y_max = 1, y_min = -1, wp = 1, Ni = 0.9, k_ff = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 320, "y1": 43, "x2": 420, "y2": 143, "rot": 0}
      },
      "tags": []
    }
  }
  "Plant model to be controlled"
  plant = BlockComponents.Continuous.Plant() {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 40, "y1": 120, "x2": 140, "y2": 220, "rot": 0}
      },
      "tags": []
    }
  }
  "Step input signal used as setpoint"
  signal = BlockComponents.Sources.Step(height = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 20, "x2": 280, "y2": 120, "rot": 0}
      },
      "tags": []
    }
  }
  "Constant signal for feedforward control"
  signal_ff = BlockComponents.Sources.Constant(k = 1) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": -130, "x2": 280, "y2": -30, "rot": 0}
      },
      "tags": []
    }
  }
  "External derivative block computing derivative of plant output"
  ext_der = BlockComponents.Continuous.Derivative(k = 1, T = 0.01) {
    "Dyad": {
      "placement": {
        "diagram": {"iconName": "default", "x1": 180, "y1": 230, "x2": 280, "y2": 330, "rot": 0}
      },
      "tags": []
    }
  }
relations
  initial plant.x1 = 0
  initial plant.y = 0
  connect(signal.y, pid.u_s) {"Dyad": {"edges": [{"S": 1, "M": [], "E": 2}], "renderStyle": "standard"}}
  connect(plant.y, pid.u_m) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 300, "y": 170}, {"x": 300, "y": 116}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(pid.y, plant.u) {
    "Dyad": {
      "edges": [
        {
          "S": 1,
          "M": [
            {"x": 440, "y": 94},
            {"x": 440, "y": 530},
            {"x": 20, "y": 530},
            {"x": 20, "y": 170}
          ],
          "E": 2
        }
      ],
      "renderStyle": "standard"
    }
  }
  connect(pid.u_ff, signal_ff.y) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 370, "y": -80}], "E": 2}],
      "renderStyle": "standard"
    }
  }
  connect(plant.y, ext_der.u) {
    "Dyad": {
      "edges": [{"S": 1, "M": [], "E": -1}, {"S": -1, "M": [{"x": 160, "y": 280}], "E": 2}],
      "junctions": [{"x": 160, "y": 170}],
      "renderStyle": "standard"
    }
  }
  connect(ext_der.y, pid.u_d) {
    "Dyad": {
      "edges": [{"S": 1, "M": [{"x": 370, "y": 280}], "E": 2}],
      "renderStyle": "standard"
    }
  }
metadata {
  "Dyad": {
    "icons": {"default": "dyad://BlockComponents/Example.svg"},
    "tests": {
      "case1": {
        "stop": 10,
        "atol": {"plant.y": 0.0001, "pid.y": 0.0001},
        "expect": {"signals": ["plant.y", "pid.y"]}
      }
    }
  }
}
end


Test Cases

julia
using BlockComponents
using DyadInterface: TransientAnalysis, rebuild_sol, ODEAlg
using ModelingToolkit: toggle_namespacing, get_initial_conditions, @named
using CSV, DataFrames, Plots

snapshotsdir = joinpath(dirname(dirname(pathof(BlockComponents))), "test", "snapshots")
<< @setup-block not executed in draft mode >>

Test Case case1

julia
@named model_case1 = BlockComponents.Continuous.Tests.LimPIDExternalDerivative()
model_case1 = toggle_namespacing(model_case1, false)

model_case1 = toggle_namespacing(model_case1, true)
result_case1 = TransientAnalysis(; model = model_case1, alg = ODEAlg.Auto(), start = 0e+0, stop = 1e+1, abstol=1e-6, reltol=1e-6)
sol_case1 = rebuild_sol(result_case1)
<< @setup-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.plant.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.LimPIDExternalDerivative_case1_sig0.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.plant.y], width=2, label="Actual value of plant.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of plant.y")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>
julia
df_case1 = DataFrame(:t => sol_case1[:t], :actual => sol_case1[model_case1.pid.y])
dfr_case1 = try CSV.read(joinpath(snapshotsdir, "BlockComponents.Continuous.Tests.LimPIDExternalDerivative_case1_sig1.ref"), DataFrame); catch e; nothing; end
plt = plot(sol_case1, idxs=[model_case1.pid.y], width=2, label="Actual value of pid.y")
if !isnothing(dfr_case1)
  scatter!(plt, dfr_case1.t, dfr_case1.expected, mc=:red, ms=3, label="Expected value of pid.y")
end
<< @setup-block not executed in draft mode >>
julia
plt
<< @example-block not executed in draft mode >>