Skip to content

Instantly share code, notes, and snippets.

@cadwallion
Created April 29, 2014 14:47
Show Gist options
  • Save cadwallion/11402573 to your computer and use it in GitHub Desktop.
Save cadwallion/11402573 to your computer and use it in GitHub Desktop.
Multi-process PID control
#!/usr/bin/env ruby
require 'brewby'
Signal.trap("INT") { exit }
heat_reader, heat_writer = IO.pipe
heat_pid = fork do
heat_writer.close
adapter = Brewby::Outputs::Test.new
output = Brewby::HeatingElement.new adapter, pulse_range: 5000
loop do
duty_cycle = heat_reader.gets.to_i
output.pulse_width = duty_cycle
output.pulse
end
end
heat_reader.close
temp_reader, temp_writer = IO.pipe
temp_pid = fork do
temp_reader.close
input = Brewby::Inputs::Test.new
loop do
reading = input.read
temp_writer.puts reading
end
end
temp_writer.close
pid = Temper::PID.new maximum: 5000
pid.tune 44, 165, 4
pid.setpoint = ARGV[0].chomp.to_f
puts "Beginning temperature control. Setpoint: #{pid.setpoint}"
loop do
reading = temp_reader.gets.chomp.to_f
if reading
width = pid.control(reading).to_i
heat_writer.puts width
power_level = ((width / 5000.0) * 100).round(2)
puts "Target: #{pid.setpoint}\tActual: #{reading}\tPulse: #{width}\tPower: #{power_level}"
else
puts "Faulty or no reading from Sensor"
end
# slowing it down for demonstration purposes
sleep 1
end
Pandora brewby-web (master) bin/kettle_control 154.0
Beginning temperature control. Setpoint: 154.0
Target: 154.0 Actual: 91.0 Pulse: 5000 Power: 100.0
Target: 154.0 Actual: 138.0 Pulse: 5000 Power: 100.0
Target: 154.0 Actual: 122.0 Pulse: 5000 Power: 100.0
Target: 154.0 Actual: 77.0 Pulse: 5000 Power: 100.0
Target: 154.0 Actual: 146.0 Pulse: 5000 Power: 100.0
Target: 154.0 Actual: 84.0 Pulse: 5000 Power: 100.0
Target: 154.0 Actual: 91.0 Pulse: 5000 Power: 100.0
Target: 154.0 Actual: 143.0 Pulse: 5000 Power: 100.0
Target: 154.0 Actual: 97.0 Pulse: 5000 Power: 100.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment