Class: StatefulController

Inherits:
Controller show all
Defined in:
lib/device_control.rb

Overview

A StatefulController tracks its error over time: current, last, accumulated

Direct Known Subclasses

PIDController

Constant Summary collapse

HZ =
1000
TICK =
Rational(1) / HZ

Instance Attribute Summary collapse

Attributes inherited from Controller

#measure, #setpoint

Instance Method Summary collapse

Methods inherited from Controller

#output

Methods included from Updateable

#update

Constructor Details

#initialize(setpoint, dt: TICK) ⇒ StatefulController

Returns a new instance of StatefulController.



162
163
164
165
166
# File 'lib/device_control.rb', line 162

def initialize(setpoint, dt: TICK)
  super(setpoint)
  @dt = dt
  @error, @last_error, @sum_error = 0.0, 0.0, 0.0
end

Instance Attribute Details

#dtObject

Returns the value of attribute dt.



159
160
161
# File 'lib/device_control.rb', line 159

def dt
  @dt
end

#errorObject (readonly)

Returns the value of attribute error.



160
161
162
# File 'lib/device_control.rb', line 160

def error
  @error
end

#last_errorObject (readonly)

Returns the value of attribute last_error.



160
161
162
# File 'lib/device_control.rb', line 160

def last_error
  @last_error
end

#sum_errorObject (readonly)

Returns the value of attribute sum_error.



160
161
162
# File 'lib/device_control.rb', line 160

def sum_error
  @sum_error
end

Instance Method Details

#input=(val) ⇒ Object

update @error, @last_error, and @sum_error



169
170
171
172
173
174
175
176
177
178
# File 'lib/device_control.rb', line 169

def input=(val)
  @measure = val
  @last_error = @error
  @error = @setpoint - @measure
  if @error * @last_error <= 0  # zero crossing; reset the accumulated error
    @sum_error = @error * @dt
  else
    @sum_error += @error * @dt
  end
end

#to_sObject



180
181
182
183
184
185
# File 'lib/device_control.rb', line 180

def to_s
  [super,
   format("Error: %+.3f\tLast: %+.3f\tSum: %+.3f",
          @error, @last_error, @sum_error),
  ].join("\n")
end