-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Initialization of the PID control simulation | ||
|
||
# controller parameters | ||
Kp = 10 # proportional gain | ||
Ti = 1 # integral time | ||
Td = 0.01 # derivative time | ||
|
||
# simulation parameters | ||
dt = .1 # time step | ||
tt = seq(0, 100, by=dt) # time vector | ||
|
||
# initialize the following to a vector of zeros | ||
# as long as the time variable tt | ||
# - PV, process variable | ||
# - U, control output | ||
# - E, error | ||
# - EI, error integral | ||
# - ED, error derivative | ||
PV = U = E = EI = ED = rep(0, length(tt)) | ||
PV[1] = 5 # initial state of the process variable |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Implementation of a PID control algorithm in R. A simple process for testing the controller. | ||
|
||
pv = function(pv.prev, u, tt) { | ||
out = pv.prev*1.1 + .5 # exponential growth + linear growth | ||
out = out - 0.1*u # the control response | ||
out = out + .5*runif(length(tt)) # a little noise, just for fun | ||
|
||
if (out < 0) out = 0 # keep values positive | ||
return(out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Sinusoidal setpoint profile to test PID control in R | ||
SP = sin(0.5*tt) + cos(.8*tt) + 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# Step change setpoint profile for PID control simulation | ||
SP = rep(10, length(tt)) | ||
SP[which(tt >= 30)] = 5 | ||
SP[which(tt >= 60)] = 20 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Simulation loop for a PID controller in R f | ||
|
||
or (k in 2:length(tt)) { | ||
PV[k] = pv(PV[k-1], U[k-1], tt[k]) | ||
E[k] = PV[k] - SP[k] | ||
|
||
EI[k] = EI[k-1] + E[k]*dt # integral | ||
ED[k] = (E[k] - E[k-1])/dt # derivative | ||
|
||
U[k] = Kp*(E[k] + (1/Ti)*sum(E*dt) + Td*ED[k]) | ||
|
||
if (U[k] < 0) U[k] = 0 | ||
} |