Design a PID Room Temperature Controller
Design a PID Room Temperature Controller
Design a PID controller that maintains room temperature near 22 °C even with fluctuations of the outside temperature, modeled as a disturbance.
{valve, burner, thermres, room} = {TransferFunctionModel[Unevaluated[{{(1/(1 + 4s))}}], s, SamplingPeriod -> None, SystemsModelLabels -> {{None}, {None}}], TransferFunctionModel[Unevaluated[{{5}}], s, SamplingPeriod -> None, SystemsModelLabels -> {{None}, {None}}], TransferFunctionModel[Unevaluated[{{0.1}}], s, SamplingPeriod -> None, SystemsModelLabels -> {{None}, {None}}], TransferFunctionModel[Unevaluated[{{(1/(1 + 8s))}}], s, SamplingPeriod -> None, SystemsModelLabels -> {{None}, {None}}]};hsys = SystemsModelSeriesConnect[valve, SystemsModelSeriesConnect[burner, thermres]];lsys = SystemsModelSeriesConnect[room, hsys];pid = PIDTune[lsys, {Automatic, "AMIGO"}, "PIDData"];doTFM = SystemsModelFeedbackConnect[room, SystemsModelSeriesConnect[pid["Feedback"], hsys]]{times, values} = WeatherData["Chicago", "Temperature", {DatePlus[Date[], -1], Date[]}]["Path"]//Transpose;values = QuantityMagnitude /@ values;timeSec = QuantityMagnitude[DateDifference[First[times], #, "Second"]]& /@ times;d = Interpolation[Transpose[{timeSec, values}]];temp = OutputResponse[pid["ReferenceOutput"], 22, {t, 0, Last[timeSec]}] + OutputResponse[doTFM, d[t], {t, 0, Last[timeSec]}];temps = Table[temp, {t, 0, Last[timeSec], 60}]//Flatten;roomTplot = DateListPlot[temps, {First[times], Last[times]}, PlotStyle -> Red, Joined -> True, PlotLegends -> LineLegend[{"Room temperature"}]];outTplot = DateListPlot[Transpose[{times, values}], Joined -> True, PlotLegends -> LineLegend[{"Outside temperature"}]];Show[roomTplot, outTplot, ImageSize -> Medium, FrameLabel -> {"Hours", "°C"}, PlotRange -> {-5, 25}]