rb-pid-controller 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -0
- data/lib/rb-pid-controller.rb +1 -0
- data/lib/rb-pid-controller/pid.rb +89 -0
- data/rb-pid-controller.gemspec +17 -0
- metadata +70 -0
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'rb-pid-controller/pid'
|
@@ -0,0 +1,89 @@
|
|
1
|
+
|
2
|
+
module PIDController
|
3
|
+
|
4
|
+
class PID
|
5
|
+
|
6
|
+
attr_accessor :kp, :ki, :kd, :consign
|
7
|
+
|
8
|
+
def initialize(kp = 1 ,ki = 1,kd = 1)
|
9
|
+
# save pid coefficient
|
10
|
+
@kp = kp.to_f
|
11
|
+
@ki = ki.to_f
|
12
|
+
@kd = kd.to_f
|
13
|
+
@consign = nil
|
14
|
+
|
15
|
+
self.reset
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
#Public methods
|
20
|
+
public
|
21
|
+
|
22
|
+
def set_consign(consign)
|
23
|
+
@consign = consign.to_f
|
24
|
+
end
|
25
|
+
|
26
|
+
def <<(value)
|
27
|
+
e,dt = error(value)
|
28
|
+
|
29
|
+
out = proportional(e) + integrative(e,dt) + derivative(e,dt)
|
30
|
+
@previous_error = e
|
31
|
+
|
32
|
+
return out
|
33
|
+
end
|
34
|
+
|
35
|
+
def reset
|
36
|
+
@previous_error = 0.0
|
37
|
+
@integrative = 0.0
|
38
|
+
@last_time = nil
|
39
|
+
end
|
40
|
+
|
41
|
+
#Private methods
|
42
|
+
private
|
43
|
+
|
44
|
+
def error(value)
|
45
|
+
out = value - @consign
|
46
|
+
|
47
|
+
t = Time.now.to_i
|
48
|
+
if @last_time.nil?
|
49
|
+
dt = 1.0
|
50
|
+
else
|
51
|
+
dt = (t - @last_time).to_f
|
52
|
+
end
|
53
|
+
@last_time = t
|
54
|
+
return out,dt
|
55
|
+
end
|
56
|
+
|
57
|
+
# compute the proportional term
|
58
|
+
def proportional(error)
|
59
|
+
return @kp*error
|
60
|
+
end
|
61
|
+
|
62
|
+
# compute the derivative term
|
63
|
+
def derivative(error,dt)
|
64
|
+
return @kd*(error - @previous_error)/dt
|
65
|
+
end
|
66
|
+
|
67
|
+
# compute the integrative term
|
68
|
+
def integrative(error,dt)
|
69
|
+
@integrative = @integrative + error*dt
|
70
|
+
return @ki*@integrative
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
class PD < PID
|
78
|
+
def initialize(kp,kd)
|
79
|
+
super(kp,0,kd)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
class PI < PID
|
84
|
+
def initialize(kp,ki)
|
85
|
+
super(kp,ki,0)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
require "rubygems"
|
3
|
+
require "rake"
|
4
|
+
|
5
|
+
GEMSPEC = Gem::Specification.new do |s|
|
6
|
+
s.name = "rb-pid-controller"
|
7
|
+
s.summary = "Library for creating PID controllers"
|
8
|
+
s.description = <<-EOF
|
9
|
+
Tools for PID control
|
10
|
+
EOF
|
11
|
+
s.version = File.read("VERSION").strip
|
12
|
+
s.license = 'GPL-3'
|
13
|
+
s.author = "MiaoufKirsh"
|
14
|
+
s.files = FileList["{lib}/**/*", "rb-pid-controller.gemspec", "VERSION"].to_a.sort
|
15
|
+
s.require_path = "lib"
|
16
|
+
s.has_rdoc = false
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rb-pid-controller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- MiaoufKirsh
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-05-28 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: " Tools for PID control\n"
|
23
|
+
email:
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- VERSION
|
32
|
+
- lib/rb-pid-controller.rb
|
33
|
+
- lib/rb-pid-controller/pid.rb
|
34
|
+
- rb-pid-controller.gemspec
|
35
|
+
has_rdoc: true
|
36
|
+
homepage:
|
37
|
+
licenses:
|
38
|
+
- GPL-3
|
39
|
+
post_install_message:
|
40
|
+
rdoc_options: []
|
41
|
+
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
hash: 3
|
50
|
+
segments:
|
51
|
+
- 0
|
52
|
+
version: "0"
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
requirements: []
|
63
|
+
|
64
|
+
rubyforge_project:
|
65
|
+
rubygems_version: 1.5.3
|
66
|
+
signing_key:
|
67
|
+
specification_version: 3
|
68
|
+
summary: Library for creating PID controllers
|
69
|
+
test_files: []
|
70
|
+
|