Class: StatefulController
- Inherits:
-
Controller
- Object
- Controller
- StatefulController
- Defined in:
- lib/device_control.rb
Overview
A StatefulController tracks its error over time: current, last, accumulated
Direct Known Subclasses
Constant Summary collapse
- HZ =
1000- TICK =
Rational(1) / HZ
Instance Attribute Summary collapse
-
#dt ⇒ Object
Returns the value of attribute dt.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#last_error ⇒ Object
readonly
Returns the value of attribute last_error.
-
#sum_error ⇒ Object
readonly
Returns the value of attribute sum_error.
Attributes inherited from Controller
Instance Method Summary collapse
-
#initialize(setpoint, dt: TICK) ⇒ StatefulController
constructor
A new instance of StatefulController.
-
#input=(val) ⇒ Object
update @error, @last_error, and @sum_error.
- #to_s ⇒ Object
Methods inherited from Controller
Methods included from Updateable
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
#dt ⇒ Object
Returns the value of attribute dt.
159 160 161 |
# File 'lib/device_control.rb', line 159 def dt @dt end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
160 161 162 |
# File 'lib/device_control.rb', line 160 def error @error end |
#last_error ⇒ Object (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_error ⇒ Object (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_s ⇒ Object
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 |