Class: Brewby::Steps::TempControl
- Inherits:
-
Object
- Object
- Brewby::Steps::TempControl
show all
- Includes:
- Timed
- Defined in:
- lib/brewby/steps/temp_control.rb
Instance Attribute Summary collapse
Attributes included from Timed
#end_time, #start_time
Instance Method Summary
collapse
Methods included from Timed
#countdown_for, #elapsed, #ended?, #in_progress?, #start_timer, #started?, #stop_timer, #time_from_seconds, #timer_for
Constructor Details
#initialize(options = {}) ⇒ TempControl
Returns a new instance of TempControl.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/brewby/steps/temp_control.rb', line 13
def initialize options = {}
@mode = options[:mode] || :manual
@output = 0
@pulse_range = options[:pulse_range] || 5000
@input = options[:input]
@output = options[:output]
@threshold_reached = false
@name = options[:name]
@last_reading = 0.0
if automatic_control?
configure_automatic_control options
else
set_power_level options[:power_level] || 1.0
end
end
|
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
9
10
11
|
# File 'lib/brewby/steps/temp_control.rb', line 9
def duration
@duration
end
|
Returns the value of attribute input.
9
10
11
|
# File 'lib/brewby/steps/temp_control.rb', line 9
def input
@input
end
|
#last_reading ⇒ Object
Returns the value of attribute last_reading.
9
10
11
|
# File 'lib/brewby/steps/temp_control.rb', line 9
def last_reading
@last_reading
end
|
#mode ⇒ Object
Returns the value of attribute mode.
9
10
11
|
# File 'lib/brewby/steps/temp_control.rb', line 9
def mode
@mode
end
|
#name ⇒ Object
Returns the value of attribute name.
9
10
11
|
# File 'lib/brewby/steps/temp_control.rb', line 9
def name
@name
end
|
#output ⇒ Object
Returns the value of attribute output.
9
10
11
|
# File 'lib/brewby/steps/temp_control.rb', line 9
def output
@output
end
|
#pid ⇒ Object
Returns the value of attribute pid.
9
10
11
|
# File 'lib/brewby/steps/temp_control.rb', line 9
def pid
@pid
end
|
#target ⇒ Object
Returns the value of attribute target.
9
10
11
|
# File 'lib/brewby/steps/temp_control.rb', line 9
def target
@target
end
|
#threshold_reached ⇒ Object
Returns the value of attribute threshold_reached.
9
10
11
|
# File 'lib/brewby/steps/temp_control.rb', line 9
def threshold_reached
@threshold_reached
end
|
Instance Method Details
#automatic_control? ⇒ Boolean
44
45
46
|
# File 'lib/brewby/steps/temp_control.rb', line 44
def automatic_control?
@mode == :auto
end
|
#calculate_power_level ⇒ Object
57
58
59
60
61
|
# File 'lib/brewby/steps/temp_control.rb', line 57
def calculate_power_level
if read_input
set_pulse_width pid.control @last_reading
end
end
|
#check_step_completion ⇒ Object
85
86
87
88
89
|
# File 'lib/brewby/steps/temp_control.rb', line 85
def check_step_completion
if threshold_reached && Time.now.to_i > @step_finishes_at
stop_timer
end
end
|
#check_temp_threshold ⇒ Object
99
100
101
102
103
104
|
# File 'lib/brewby/steps/temp_control.rb', line 99
def check_temp_threshold
if last_reading >= target
@threshold_reached = true
@step_finishes_at = Time.now.to_i + duration_in_seconds
end
end
|
31
32
33
34
35
36
37
38
|
# File 'lib/brewby/steps/temp_control.rb', line 31
def configure_automatic_control options
@target = options[:target]
@duration = options[:duration] || 1
@pid = Temper::PID.new maximum: @pulse_range
@pid.tune 44, 165, 4
@pid.setpoint = @target
end
|
#duration_in_seconds ⇒ Object
106
107
108
|
# File 'lib/brewby/steps/temp_control.rb', line 106
def duration_in_seconds
@duration * 60
end
|
#manual_control? ⇒ Boolean
40
41
42
|
# File 'lib/brewby/steps/temp_control.rb', line 40
def manual_control?
!automatic_control?
end
|
#power_level ⇒ Object
69
70
71
|
# File 'lib/brewby/steps/temp_control.rb', line 69
def power_level
output.pulse_width / @pulse_range.to_f
end
|
48
49
50
51
|
# File 'lib/brewby/steps/temp_control.rb', line 48
def read_input
reading = input.read
@last_reading = reading if reading
end
|
#set_power_level(level) ⇒ Object
53
54
55
|
# File 'lib/brewby/steps/temp_control.rb', line 53
def set_power_level level
set_pulse_width (level * @pulse_range).to_i
end
|
#set_pulse_width(width) ⇒ Object
63
64
65
66
67
|
# File 'lib/brewby/steps/temp_control.rb', line 63
def set_pulse_width width
if width
output.pulse_width = width
end
end
|
#step_iteration ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/brewby/steps/temp_control.rb', line 73
def step_iteration
if automatic_control?
calculate_power_level
output.pulse
check_temp_threshold unless threshold_reached
check_step_completion
else
read_input if input
output.pulse
end
end
|
#time_remaining ⇒ Object
91
92
93
94
95
96
97
|
# File 'lib/brewby/steps/temp_control.rb', line 91
def time_remaining
if @step_finishes_at
@step_finishes_at - Time.now.to_i
else
duration_in_seconds
end
end
|