Skip to content

pid_controlled_plant

PID-controlled plant with actuator saturation.

pid_controlled_plant(kp=6.0, ki=2.0, kd=1.0, stiffness=3.0, damping=0.6, setpoint_amp=2.0, setpoint_freq=1.0, u_min=-1.0, u_max=1.0, initial_state=None)

Create a PID-controlled second-order plant with saturation.

The state is [position, velocity, integral_error]. The controller tracks a sinusoidal setpoint and saturates the control input, yielding hybrid locations.

Parameters:

Name Type Description Default
kp float

Proportional gain.

6.0
ki float

Integral gain.

2.0
kd float

Derivative gain.

1.0
stiffness float

Plant stiffness.

3.0
damping float

Plant damping.

0.6
setpoint_amp float

Setpoint amplitude.

2.0
setpoint_freq float

Setpoint frequency.

1.0
u_min float

Minimum control input.

-1.0
u_max float

Maximum control input.

1.0
initial_state ndarray | None

Optional initial state.

None

Returns:

Type Description
HybridSystem

HybridSystem configured for PID control with saturation.

Source code in src/flowcean/hybrid/benchmarks/pid_controlled_plant.py
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
def pid_controlled_plant(
    kp: float = 6.0,
    ki: float = 2.0,
    kd: float = 1.0,
    stiffness: float = 3.0,
    damping: float = 0.6,
    setpoint_amp: float = 2.0,
    setpoint_freq: float = 1.0,
    u_min: float = -1.0,
    u_max: float = 1.0,
    initial_state: np.ndarray | None = None,
) -> HybridSystem:
    """Create a PID-controlled second-order plant with saturation.

    The state is [position, velocity, integral_error]. The controller tracks a
    sinusoidal setpoint and saturates the control input, yielding hybrid
    locations.

    Args:
        kp: Proportional gain.
        ki: Integral gain.
        kd: Derivative gain.
        stiffness: Plant stiffness.
        damping: Plant damping.
        setpoint_amp: Setpoint amplitude.
        setpoint_freq: Setpoint frequency.
        u_min: Minimum control input.
        u_max: Maximum control input.
        initial_state: Optional initial state.

    Returns:
        HybridSystem configured for PID control with saturation.
    """
    linear_dynamics = ContinuousDynamics(_flow_linear, label="linear")
    sat_high_dynamics = ContinuousDynamics(
        _flow_sat_high,
        label="sat_high",
    )
    sat_low_dynamics = ContinuousDynamics(
        _flow_sat_low,
        label="sat_low",
    )
    linear = Location(linear_dynamics, label="linear")
    sat_high = Location(sat_high_dynamics, label="sat_high")
    sat_low = Location(sat_low_dynamics, label="sat_low")

    transitions = [
        Transition(
            source=linear,
            target=sat_high,
            event=EventSurface(
                _event_surface_high,
                direction=CrossingDirection.RISING,
                label="hit_high",
            ),
        ),
        Transition(
            source=linear,
            target=sat_low,
            event=EventSurface(
                _event_surface_low,
                direction=CrossingDirection.FALLING,
                label="hit_low",
            ),
        ),
        Transition(
            source=sat_high,
            target=linear,
            event=EventSurface(
                _event_surface_high,
                direction=CrossingDirection.FALLING,
                label="leave_high",
            ),
        ),
        Transition(
            source=sat_low,
            target=linear,
            event=EventSurface(
                _event_surface_low,
                direction=CrossingDirection.RISING,
                label="leave_low",
            ),
        ),
    ]

    if initial_state is None:
        initial_state = np.array([0.0, 0.0, 0.0], dtype=float)

    return HybridSystem(
        locations=[linear, sat_high, sat_low],
        transitions=transitions,
        initial_location=linear,
        initial_state=initial_state,
        parameters={
            "kp": kp,
            "ki": ki,
            "kd": kd,
            "stiffness": stiffness,
            "damping": damping,
            "setpoint_amp": setpoint_amp,
            "setpoint_freq": setpoint_freq,
            "u_min": u_min,
            "u_max": u_max,
        },
    )