Permalink
Browse files

More work on timing of sequences

  • Loading branch information...
1 parent 822db24 commit 803cd308b686ae126075ed9388e65ad72daf0217 @tallakt committed Feb 22, 2009
@@ -2,14 +2,24 @@ module RubyPlc
module Physical
class Motor
attr_reader :name, :description, :interlocks
+ attr_accessor :start_out, :contactor
def initialize(name = nil, description = nil)
@name, @description = name, description
@interlocks = Interlocks.new
@manual = false
+ @startup_timer = Timer.new 500
yield self if block_given?
end
+ def startup_time
+ @startup_timer.time
+ end
+
+ def startup_time=(t)
+ @startup_timer.time = t
+ end
+
def start(mode = :auto)
update_manual mode
# TODO
@@ -1,35 +1,33 @@
-module RubyPlc
+include 'ruby_plc/sequences/step_listeners'
+
+mmodule RubyPlc
module Sequences
class Branch
+ include StepListeners
+
attr_reader :name
def initialize(name = nil)
+ init_step_listeners
@name = name
@branches = []
@started = false
@chosen_branch = nil
yield self if block_given?
end
- def branch(s, &condition)
+ def branch(s, condition_signal)
if s.respond_to? :to_step
- @branches << [condition, s.to_step]
+ @branches << [condition_signal, s.to_step]
else
- @branches << [condition, s]
+ @branches << [condition_signal, s]
end
- end
-
- def run
- if active?
- if @chosen_branch
- @chosen_branch.run
- if @chosen_branch.finished?
- started = false
- @chosen_branch = nil
- end
- else
- choose_branch
- end
+ step = @branches.last.last
+ condition_signal.at_re do
+ start_branch step
+ end
+ s.at_end do
+ finish_up
end
end
@@ -44,30 +42,35 @@ def active?
def start
unless @started
@started = true
+ notify_enter
choose_branch
end
end
def reset(mode = :all)
- @branches.each {|br| br.last.reset(mode) }
+ @branches.each {|br| br.last.reset(mode) } if mode == :all
+ notify_reset
end
private
- def choose_branch
- unless @chosen_branch
- @branches.each do |br|
- condition, step = br
- if condition.call
- @chosen_branch = br
- br.start
- break
- end
- end
+ def start_branch(branch)
+ if branch && not @chosen_branch
+ @chosen_branch = branch
+ branch.start
end
end
+ def choose_branch
+ start_branch(@branches.find {|br| br.first.call })
+ end
+
+ def finish_up
+ notify_exit
+ @started = false
+ @chosen_branch = nil
+ end
end
end
end
@@ -1,9 +1,13 @@
+include 'ruby_plc/sequences/step_listeners'
+
module RubyPlc
module Sequences
class InParallel
+ include StepListeners
attr_reader :name
def initialize(name = nil)
+ init_step_listeners
@name = name
@steps = []
yield self if block_given?
@@ -15,10 +19,10 @@ def step(s)
else
@steps << s
end
- end
-
- def run
- @steps.each {|s| s.run }
+ step = @steps.last
+ step.on_exit do
+ notify_exit if finished?
+ end
end
def finished?
@@ -31,11 +35,13 @@ def active?
end
def start
+ notify_enter
@steps.each {|s| s.start }
end
def reset(mode = :all)
@steps.each {|s| s.reset(mode) }
+ notify_reset
end
end
end
@@ -1,9 +1,17 @@
-module RubyPlc
+include 'ruby-plc/sequences/step_listeners'
+include 'ruby-plc/timedomain/wait_step'
+include 'ruby-plc/timedomain/timer'
+include 'ruby-plc/timedomain/sequencer'
+
+mmodule RubyPlc
module Sequences
class Sequence
+ include StepListeners
+
attr_reader :name
def initialize(name = nil, options = {})
+ init_step_listeners
@name = name
@steps = []
@current_step_index = nil # index of current step
@@ -17,29 +25,24 @@ def step(s)
else
add_step s
end
+ s
end
def wait(time)
step WaitStep.new time
end
- def run
- if active?
- current_step.run
- if current_step.finished?
- @current_step_index += 1
- @current_step = nil if @current_step >= @steps.size
- if current_step
- current_step.start
- end
- end
- else
- if @options[:auto_start]
+ def circular(delay = 100)
+ Timer t = Timer.new delay
+ on_exit do
+ t.start
+ t.at_end do
start
end
end
end
+
def finished?
current_step
end
@@ -49,17 +52,21 @@ def active?
end
def start
- @current_step_index = 0 unless (current_step or @steps.empty?)
+ if not current_step || @steps.any?
+ notify_enter
+ @current_step_index = 0
+ end
end
def current_step
- @current_step && @steps[@current_step]
+ @current_step_index && @steps[@current_step_index]
end
def reset(mode = :all)
@steps.each {|s| s.reset(mode) } if mode == :all
@steps[0..@current_step_index].each {|s| s.reset(mode) } if (mode == :used && @current_step_index)
@current_step_index = nil
+ notify_reset
end
private
@@ -70,8 +77,24 @@ def add_step(s)
else
@steps << s
end
+
+ step.on_exit do
+ Sequencer::at_once do
+ current_step_finished
+ end
+ end
end
+ def current_step_finished
+ if @current_step_index
+ @current_step_index += 1
+ if @current_step_index >= @steps.size
+ @current_step_index = nil
+ notify_exit
+ end
+ current_step.start if current_step
+ end
+ end
end
end
end
@@ -1,21 +1,33 @@
+include 'ruby_plc/sequences/step_listeners'
+include 'ruby-plc/sequences/sequence'
+
module RubyPlc
module Sequences
class StackSequence
+ include StepListeners
+
attr_reader :name
def initialize(name = nil, options = {})
+ init_step_listeners
@name = name
@up = Sequence.new nil, options
@down = Sequence.new nil, :reverse => true
+ @up.at_end do
+ @down.start
+ end
+ @down.at_end do
+ notify_exit
+ end
yield self if block_given?
end
def step(s)
if s.respond_to? :up_step && s.respond_to? :down_step
- @up.step s.up_step
- @down.step s.down_step
+ up_step s.up_step
+ down_step s.down_step
else
- @up.step s
+ up_step s
end
end
@@ -35,28 +47,19 @@ def down_wait(time)
@down.wait time
end
- def run
- if @up.active?
- @up.run
- if @up.finished?
- @down.start
- end
- else
- @down.run if @down.active?
- end
- end
-
def finished?
@up.finished? && @down.finished?
end
def start
+ notify_enter
@up.start
end
def reset(mode = :all)
@up.reset(mode)
@down.reset(mode)
+ notify_reset
end
end
end
Oops, something went wrong.

0 comments on commit 803cd30

Please sign in to comment.