-- Example Control Expert Program for Dynamic Pump Control with PID
-- Assumptions:
-- 1. PID output (PID_Output) is already computed.
-- 2. Minimum Hz Step: 0.25 Hz
-- 3. Initial pump speeds are stored in an array `Pump_Speeds`.
-- 4. Total number of pumps can vary dynamically (1 to 5).
-- Configuration
local MAX_PUMPS = 5 -- Maximum number of pumps
local MIN_HZ_STEP = 0.25 -- Minimum step for frequency adjustment
-- Variables
local Active_Pumps = 4 -- Number of active pumps (1 to MAX_PUMPS)
local Pump_Speeds = {35.0, 35.0, 35.0, 35.0, 0.0} -- Current pump speeds
local Initial_Frequencies = {35.0, 35.0, 35.0, 35.0, 0.0} -- Variable initial frequencies
local Current_Pump_Index = 1 -- Tracks which pump to adjust next
-- Input: PID Output
local PID_Output = 0.25 -- Example PID output
-- Function to round to the nearest step
local function round_to_step(value, step)
return [Link](value / step + 0.5) * step
end
-- Function to apply PID adjustment
local function adjust_pumps()
-- Check if PID output exceeds minimum step threshold
if [Link](PID_Output) >= MIN_HZ_STEP then
-- Determine the direction of adjustment
local adjustment = round_to_step(PID_Output, MIN_HZ_STEP)
-- Apply adjustment continuously to the current pump in the active range
Pump_Speeds[Current_Pump_Index] = Pump_Speeds[Current_Pump_Index] + adjustment
-- Ensure pump speed stays within safe limits (e.g., 0 Hz to 50 Hz)
if Pump_Speeds[Current_Pump_Index] < 0 then
Pump_Speeds[Current_Pump_Index] = 0
elseif Pump_Speeds[Current_Pump_Index] > 50 then
Pump_Speeds[Current_Pump_Index] = 50
end
-- Move to the next pump in a round-robin sequence within the active pumps
Current_Pump_Index = Current_Pump_Index + 1
if Current_Pump_Index > Active_Pumps then
Current_Pump_Index = 1
end
end
end
-- Function to set the number of active pumps
local function set_active_pumps(num_pumps)
if num_pumps >= 1 and num_pumps <= MAX_PUMPS then
Active_Pumps = num_pumps
print("Number of active pumps set to: " .. Active_Pumps)
-- Reset pump speeds to initial frequencies for any newly added pumps
for i = 1, MAX_PUMPS do
if i > Active_Pumps then
Pump_Speeds[i] = 0 -- Inactive pumps are set to 0 Hz
else
Pump_Speeds[i] = Initial_Frequencies[i]
end
end
else
print("Invalid number of pumps. Must be between 1 and " .. MAX_PUMPS)
end
end
-- Function to set initial frequencies for all pumps
local function set_initial_frequencies(frequencies)
for i = 1, [Link](#frequencies, MAX_PUMPS) do
Pump_Speeds[i] = frequencies[i]
Initial_Frequencies[i] = frequencies[i]
end
print("Initial frequencies set.")
end
-- Main Control Loop (called periodically in PLC)
local function main()
adjust_pumps()
-- Print pump speeds for debugging
for i = 1, Active_Pumps do
print("Pump " .. i .. ": " .. Pump_Speeds[i] .. " Hz")
end
end
-- Example Usage
set_initial_frequencies({35.0, 35.0, 35.0, 35.0, 35.0}) -- Set initial frequencies for pumps
set_active_pumps(4) -- Set number of active pumps to 4
-- Progressive Increase Example
print("--- Progressive Speed Increase ---")
PID_Output = 0.25
for t = 1, 10 do
main()
end
-- Progressive Decrease Example
print("--- Progressive Speed Decrease ---")
PID_Output = -0.25
for t = 11, 20 do
main()
end