temper-control 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +67 -0
- data/Rakefile +1 -0
- data/lib/temper.rb +109 -0
- data/lib/temper/version.rb +3 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/temper_spec.rb +25 -0
- data/temper.gemspec +21 -0
- metadata +73 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Andrew Nordman
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
# Temper
|
2
|
+
|
3
|
+
Temperature controlling is easy with Temper. It uses an improved PID algorithm to
|
4
|
+
decrease overshoots and regulate based on continued inputs.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'temper-control', require: 'temper'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install temper-control
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
To start, create an instance of Temper::PID. The PID algorithm can be configured with
|
23
|
+
custom minimum and maximum values for ease of integration with external control systems
|
24
|
+
(PWM-controlled heating elements, for example). Once created, run `Temper::PID#control`
|
25
|
+
in your control loop, feeding it sensor data.
|
26
|
+
|
27
|
+
### Minimum Interval Calculation
|
28
|
+
|
29
|
+
The algorithm being used is minimum interval and will not recalibrate until the time interval
|
30
|
+
has passed before recalibrating. This helps mitigate excess compensation and inconsistent
|
31
|
+
adjustment. The update interval is also configurable in Temper with the `interval` option.
|
32
|
+
|
33
|
+
### Directional Control
|
34
|
+
|
35
|
+
When handling cooling-based temperature control, negative values are a pain for
|
36
|
+
translation. To assist with this, Temper uses a directional control parameter. The two
|
37
|
+
possible states are `:direct` and `:reverse`. When using `:reverse`, negative values
|
38
|
+
are inverted.
|
39
|
+
|
40
|
+
### Tuning
|
41
|
+
|
42
|
+
Temper's PID is manually tuned with the `tune` method, which takes a Kp, Ki, and Kd value. By
|
43
|
+
default, Temper will set them to 1.0
|
44
|
+
|
45
|
+
## Example
|
46
|
+
|
47
|
+
``` ruby
|
48
|
+
require 'temper'
|
49
|
+
|
50
|
+
temper = Temper::PID.new(interval: 1000, minimum: 0, maximum: 1000, direction: :direct)
|
51
|
+
temper.tune(9.0, 25.0, 6.0) # Set Kp, Ki, and Kd
|
52
|
+
temper.setpoint = 100.0 # Set target temperature
|
53
|
+
|
54
|
+
while input = read_sensor() # Replace read_sensor with your external system
|
55
|
+
output = temper.control(input)
|
56
|
+
# output is a value betwen minimum and maximum. This can be used for thresholds or
|
57
|
+
# PWM-based control
|
58
|
+
end
|
59
|
+
```
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/temper.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require "temper/version"
|
2
|
+
|
3
|
+
module Temper
|
4
|
+
class PID
|
5
|
+
attr_accessor :kp, :ki, :kd, :setpoint, :direction, :output
|
6
|
+
|
7
|
+
def initialize options = {}
|
8
|
+
@interval = options[:interval] || 1000
|
9
|
+
@last_time = 0.0
|
10
|
+
@last_input = 0.0
|
11
|
+
@integral_term = 0.0
|
12
|
+
@output_maximum = options[:maximum] || 1000
|
13
|
+
@output_minimum = options[:minimum] || 0
|
14
|
+
|
15
|
+
set_mode options[:mode] || :auto
|
16
|
+
set_direction options[:direction] || :direct
|
17
|
+
end
|
18
|
+
|
19
|
+
def control input
|
20
|
+
return if !@auto # manual mode
|
21
|
+
|
22
|
+
now = Time.now.to_f
|
23
|
+
time_change = (now - @last_time) * 1000
|
24
|
+
|
25
|
+
if time_change >= @interval
|
26
|
+
error = @setpoint - input
|
27
|
+
|
28
|
+
calculate_proportional error
|
29
|
+
calculate_integral error
|
30
|
+
calculate_derivative input
|
31
|
+
|
32
|
+
calculate_output
|
33
|
+
|
34
|
+
@last_time = now
|
35
|
+
@last_input = input
|
36
|
+
|
37
|
+
@output
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def calculate_proportional error
|
42
|
+
@proportional_term = @kp * error
|
43
|
+
end
|
44
|
+
|
45
|
+
def calculate_integral error
|
46
|
+
@integral_term += @ki * error
|
47
|
+
|
48
|
+
if @integral_term > @output_maximum
|
49
|
+
@integral_term = @output_maximum
|
50
|
+
elsif @integral_term < @output_minimum
|
51
|
+
@integral_term = @output_minimum
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def calculate_derivative input
|
56
|
+
@derivative_term = @kd * (input - @last_input)
|
57
|
+
end
|
58
|
+
|
59
|
+
def calculate_output
|
60
|
+
@output = @proportional_term + @integral_term - @derivative_term
|
61
|
+
|
62
|
+
if @output > @output_maximum
|
63
|
+
@output = @output_maximum
|
64
|
+
elsif @output < @output_minimum
|
65
|
+
@output = @output_minimum
|
66
|
+
end
|
67
|
+
|
68
|
+
@output
|
69
|
+
end
|
70
|
+
|
71
|
+
def tune kp, ki, kd
|
72
|
+
return if kp < 0 || ki < 0 || kd < 0
|
73
|
+
|
74
|
+
interval_seconds = (@interval / 1000.0)
|
75
|
+
|
76
|
+
@kp = kp
|
77
|
+
@ki = ki * interval_seconds
|
78
|
+
@kd = kd / interval_seconds
|
79
|
+
|
80
|
+
if @direction != :direct
|
81
|
+
@kp = 0 - @kp
|
82
|
+
@ki = 0 - @ki
|
83
|
+
@kd = 0 - @kd
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def update_interval new_interval
|
88
|
+
if new_interval > 0
|
89
|
+
ratio = new_interval / @interval
|
90
|
+
|
91
|
+
@ki *= ratio
|
92
|
+
@kd /= ratio
|
93
|
+
@interval = new_interval
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def set_mode mode
|
98
|
+
@auto = mode == :auto
|
99
|
+
end
|
100
|
+
|
101
|
+
def set_direction direction
|
102
|
+
@direction = direction
|
103
|
+
end
|
104
|
+
|
105
|
+
def mode
|
106
|
+
@auto ? :auto : :manual
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/temper_spec.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Temper::PID do
|
4
|
+
before do
|
5
|
+
controller.setpoint = 100.0
|
6
|
+
controller.tune 1.0, 1.0, 1.0
|
7
|
+
end
|
8
|
+
|
9
|
+
let(:controller) { Temper::PID.new }
|
10
|
+
subject { controller }
|
11
|
+
|
12
|
+
its(:kp) { should == 1.0 }
|
13
|
+
its(:ki) { should == 1.0 }
|
14
|
+
its(:kd) { should == 1.0 }
|
15
|
+
its(:mode) { should == :auto }
|
16
|
+
its(:direction) { should == :direct }
|
17
|
+
|
18
|
+
context 'computing data' do
|
19
|
+
before do
|
20
|
+
controller.control 50.0
|
21
|
+
end
|
22
|
+
|
23
|
+
its(:output) { should == 50.0 }
|
24
|
+
end
|
25
|
+
end
|
data/temper.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'temper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "temper-control"
|
8
|
+
gem.version = Temper::VERSION
|
9
|
+
gem.authors = ["Andrew Nordman"]
|
10
|
+
gem.email = ["cadwallion@gmail.com"]
|
11
|
+
gem.description = %q{Temperature Controller Library}
|
12
|
+
gem.summary = %q{Temperature controller based on the PID algorithm}
|
13
|
+
gem.homepage = ""
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec'
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: temper-control
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrew Nordman
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
description: Temperature Controller Library
|
31
|
+
email:
|
32
|
+
- cadwallion@gmail.com
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- .gitignore
|
38
|
+
- Gemfile
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- lib/temper.rb
|
43
|
+
- lib/temper/version.rb
|
44
|
+
- spec/spec_helper.rb
|
45
|
+
- spec/temper_spec.rb
|
46
|
+
- temper.gemspec
|
47
|
+
homepage: ''
|
48
|
+
licenses: []
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
requirements: []
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.8.24
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Temperature controller based on the PID algorithm
|
71
|
+
test_files:
|
72
|
+
- spec/spec_helper.rb
|
73
|
+
- spec/temper_spec.rb
|