Class: Brewby::Steps::TempControl

Inherits:
Object
  • Object
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

#durationObject (readonly)

Returns the value of attribute duration.



9
10
11
# File 'lib/brewby/steps/temp_control.rb', line 9

def duration
  @duration
end

#inputObject (readonly)

Returns the value of attribute input.



9
10
11
# File 'lib/brewby/steps/temp_control.rb', line 9

def input
  @input
end

#last_readingObject (readonly)

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

#modeObject (readonly)

Returns the value of attribute mode.



9
10
11
# File 'lib/brewby/steps/temp_control.rb', line 9

def mode
  @mode
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/brewby/steps/temp_control.rb', line 9

def name
  @name
end

#outputObject (readonly)

Returns the value of attribute output.



9
10
11
# File 'lib/brewby/steps/temp_control.rb', line 9

def output
  @output
end

#pidObject (readonly)

Returns the value of attribute pid.



9
10
11
# File 'lib/brewby/steps/temp_control.rb', line 9

def pid
  @pid
end

#targetObject (readonly)

Returns the value of attribute target.



9
10
11
# File 'lib/brewby/steps/temp_control.rb', line 9

def target
  @target
end

#threshold_reachedObject (readonly)

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

Returns:

  • (Boolean)


44
45
46
# File 'lib/brewby/steps/temp_control.rb', line 44

def automatic_control?
  @mode == :auto
end

#calculate_power_levelObject



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_completionObject



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_thresholdObject



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

#configure_automatic_control(options) ⇒ Object



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_secondsObject



106
107
108
# File 'lib/brewby/steps/temp_control.rb', line 106

def duration_in_seconds
  @duration * 60
end

#manual_control?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/brewby/steps/temp_control.rb', line 40

def manual_control?
  !automatic_control?
end

#power_levelObject



69
70
71
# File 'lib/brewby/steps/temp_control.rb', line 69

def power_level
  output.pulse_width / @pulse_range.to_f
end

#read_inputObject



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_iterationObject



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_remainingObject



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