class PIDController::PID

Attributes

consign[RW]
kd[RW]
ki[RW]
kp[RW]

Public Class Methods

new(kp = 1 ,ki = 1,kd = 1) click to toggle source
# File lib/rb-pid-controller/pid.rb, line 8
def initialize(kp = 1 ,ki = 1,kd = 1)
  # save pid coefficient
  @kp = kp.to_f
  @ki = ki.to_f
  @kd = kd.to_f
  @consign = nil

  self.reset 
  
end

Public Instance Methods

<<(value) click to toggle source
# File lib/rb-pid-controller/pid.rb, line 26
def <<(value)
  e,dt = error(value)
  
  out = proportional(e) + integrative(e,dt) + derivative(e,dt)
  @previous_error = e
  
  return out
end
reset() click to toggle source
# File lib/rb-pid-controller/pid.rb, line 35
def reset
  @previous_error = 0.0
  @integrative = 0.0
  @last_time = nil
end
set_consign(consign) click to toggle source

Public methods

# File lib/rb-pid-controller/pid.rb, line 22
def set_consign(consign)
  @consign = consign.to_f
end

Private Instance Methods

derivative(error,dt) click to toggle source

compute the derivative term

# File lib/rb-pid-controller/pid.rb, line 63
def derivative(error,dt)
  return @kd*(error - @previous_error)/dt
end
error(value) click to toggle source

Private methods

# File lib/rb-pid-controller/pid.rb, line 44
def error(value)
  out = value - @consign
  
  t = Time.now.to_i
  if @last_time.nil?
    dt = 1.0
  else
    dt = (t - @last_time).to_f
  end
  @last_time = t
  return out,dt
end
integrative(error,dt) click to toggle source

compute the integrative term

# File lib/rb-pid-controller/pid.rb, line 68
def integrative(error,dt)
  @integrative = @integrative + error*dt
  return @ki*@integrative
end
proportional(error) click to toggle source

compute the proportional term

# File lib/rb-pid-controller/pid.rb, line 58
def proportional(error)
  return @kp*error
end