Tuning Discrete Values using the PID tuner application

4 次查看(过去 30 天)
I need help with my Senior Project I am trying to implement a PID Controller that is able to detect when a building power demand is exceeding beyond its power rating it is using a Battery energy storage system to help supply the power towards the building. I am trying to Tune a PID controller using the PID tuner app to get discrete values that are above 5% of the given setpoint 93.75 (KVA). I am trying to get the kp, ki, and kd values and I don't have much experience using the PID tuner application. Attached is the excel file that I was working on to be able to get the values:
Code I was trying to use:
% === STEP 1: Load Power Data from Excel ===
data = readtable('battery_capacity_calculation_with_totals.xlsx');
% Display the column names to verify structure
disp("Column Names:")
disp(data.Properties.VariableNames)
% Extract the power column safely (fix column name if needed)
power_kw = data{:, "Power_kW"}; % Replace "Power_kW" if name differs
% Clean data: ensure numeric, finite, and valid
power_kw = fillmissing(power_kw, 'linear'); % Fill gaps
power_kw = power_kw(~isnan(power_kw)); % Remove remaining NaNs
power_kw = double(power_kw); % Ensure numeric
% Time vector in minutes (15-minute intervals)
time_minutes = (0:length(power_kw)-1) * 15;
% === STEP 2: Define Setpoint ===
setpoint = 93.75; % kW
dt = 15 * 60; % 15 minutes in seconds
% === STEP 3: Define First-Order Plant (or later ID from data) ===
G = tf(1, [300 1]); % First-order plant (adjust as needed)
% === STEP 4: Launch PID Tuner ===
pidTuner(G, 'PID'); % GUI will open
% === STEP 5: Export Tuned PID Controller (replace with your tuned values) ===
% After tuning, you’ll export something like:
% >> C = pid(Kp, Ki, Kd);
C = pid(0.6, 0.05, 0.01); % Example gains; replace after tuning
% === STEP 6: Closed-Loop Simulation ===
T = feedback(C * G, 1); % Closed-loop transfer function
% Simulate step response
figure;
step(T)
title('Step Response of Tuned PID Control System')
xlabel('Time (seconds)')
ylabel('Building Power (kW)')
grid on;
% === STEP 7: Optional – Apply PID to real power data ===
error = setpoint - power_kw;
integral = 0;
prev_error = 0;
pid_output = zeros(size(error));
simulated_power = zeros(size(error));
for i = 1:length(error)
e = error(i);
integral = integral + e * dt;
derivative = (i > 1) * (e - prev_error) / dt;
u = C.Kp * e + C.Ki * integral + C.Kd * derivative;
pid_output(i) = u;
simulated_power(i) = power_kw(i) + u;
prev_error = e;
end
% Plot actual vs. PID-controlled power
figure;
plot(time_minutes, power_kw, 'b-', 'LineWidth', 2); hold on;
plot(time_minutes, simulated_power, 'r--', 'LineWidth', 2);
yline(setpoint, 'k:', 'Setpoint');
xlabel('Time (minutes)');
ylabel('Power (kW)');
title('PID-Controlled Power vs Original Power');
legend('Original Power', 'PID-Controlled Power');
grid on;

回答(1 个)

Ritika Thusoo
Ritika Thusoo 2025-8-6
Hi,
The column name “power_kw” in the MATLAB code is different from the column name in the provided Excel sheet. To avoid any mismatch with the column name, you can use:
power_kw = data{:, 2};
The plant model used in the MATLAB code is a simple first-order system. Alternatively, a first-order system can be derived from the data in the Excel sheet using MATLAB. The ‘lsqcurvefit’ function can be used to estimate a first-order system based on the data given in the .xlsx file.
Reference:
The initial values of the PID gains are assumed in the given code. However, the new tuned values should be properly exported after tuning using the ‘Export’ option in the ‘PID Tuner’. Here is a reference for how to tune the PID Gains using the ‘PID Tuner’.
Reference:
In order to find the Intervals that exceed the threshold, find’ function in MATLAB can be used.
A sample code is given:
exceed_indices = find(power_kva > threshold);
The threshold would be a value above 5% of the setpoint value.
Reference:

类别

Help CenterFile Exchange 中查找有关 PID Controller Tuning 的更多信息

产品


版本

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by