Skip to content

Commit

Permalink
Добавлены изыскания про PID
Browse files Browse the repository at this point in the history
  • Loading branch information
iMissile committed Apr 1, 2016
1 parent 743c14f commit 57ffb13
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 0 deletions.
Binary file modified 42 IoT_test/.RData
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions 43 PID control sample/PIDControlR_Initialize.R
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
10 changes: 10 additions & 0 deletions 43 PID control sample/PIDControlR_ProcessVariable.R
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)
}
2 changes: 2 additions & 0 deletions 43 PID control sample/PIDControlR_SetpointSinusoid.R
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
4 changes: 4 additions & 0 deletions 43 PID control sample/PIDControlR_SetpointSteps.R
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
13 changes: 13 additions & 0 deletions 43 PID control sample/PIDControlR_SimulationLoop.R
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
}

0 comments on commit 57ffb13

Please sign in to comment.