Skip to content

Commit

Permalink
support changing cycle time with M0018, cycle time is now configured …
Browse files Browse the repository at this point in the history
…for each signal plan
  • Loading branch information
emiltin committed Dec 11, 2024
1 parent bb05a7e commit a10ba58
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
9 changes: 5 additions & 4 deletions config/tlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ sxl_version: '1.2.1'
components:
main:
TC:
cycle_time: 6
ntsOId: KK+AG9998=001TC000
signal_group:
A1:
Expand All @@ -19,15 +18,17 @@ components:
DL2:
signal_plans:
1:
dynamic_bands:
1: 0
2: 5
cycle_time: 6
states:
A1: '111NBB'
A2: '11NBBB'
B1: 'BBB11N'
B2: 'BBB1NB'
dynamic_bands:
1: 0
2: 5
2:
cycle_time: 6
states:
A1: 'NNNNBB'
A2: 'NNNNBN'
Expand Down
3 changes: 2 additions & 1 deletion lib/rsmp/tlc/signal_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def compute_state
states = plan.states[c_id]
return default unless states

state = states[cycle_counter]
counter = [cycle_counter, states.length-1].min
state = states[counter]
return default unless state =~ /[a-hA-G0-9N-P]/ # valid signal group states
state
end
Expand Down
10 changes: 8 additions & 2 deletions lib/rsmp/tlc/signal_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ module TLC
# A signal plan is a description of how all signal groups should change
# state over time.
class SignalPlan
attr_reader :nr, :states, :dynamic_bands
def initialize nr:, states:, dynamic_bands:
attr_reader :nr, :states, :dynamic_bands, :cycle_time
def initialize nr:, cycle_time:, states:, dynamic_bands:
@nr = nr
@states = states
@dynamic_bands = dynamic_bands || {}
@cycle_time = cycle_time
end

def dynamic_bands_string
Expand All @@ -24,6 +25,11 @@ def set_band band, value
def get_band band
@dynamic_bands[ band.to_i ]
end

def set_cycle_time cycle_time
raise ArgumentError if cycle_time < 0
@cycle_time = cycle_time
end
end
end
end
17 changes: 13 additions & 4 deletions lib/rsmp/tlc/traffic_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ class TrafficController < Component
:functional_position,
:startup_sequence_active, :startup_sequence, :startup_sequence_pos

def initialize node:, id:, ntsOId: nil, xNId: nil, cycle_time: 10, signal_plans:,
def initialize node:, id:, ntsOId: nil, xNId: nil, signal_plans:,
startup_sequence:, live_output:nil, inputs:{}
super node: node, id: id, ntsOId: ntsOId, xNId: xNId, grouped: true
@signal_groups = []
@detector_logics = []
@plans = signal_plans
@cycle_time = cycle_time || 10
@num_traffic_situations = 1

if inputs
Expand Down Expand Up @@ -142,7 +141,7 @@ def get_priority_list
end

def move_cycle_counter
counter = Time.now.to_i % @cycle_time
counter = Time.now.to_i % current_plan.cycle_time
changed = counter != @cycle_counter
@cycle_counter = counter
changed
Expand Down Expand Up @@ -450,6 +449,13 @@ def handle_m0017 arg, options={}

def handle_m0018 arg, options={}
@node.verify_security_code 2, arg['securityCode']
nr = arg['plan'].to_i
cycle_time = arg['status'].to_i
plan = @plans[nr]
raise RSMP::MessageRejected.new "Plan '#{nr}' not found" unless plan
raise RSMP::MessageRejected.new "Cycle time must be greater or equal to zero" if cycle_time < 0
log "Set plan #{nr} cycle time to #{cycle_time}", level: :info
plan.set_cycle_time cycle_time
end

def string_to_bool bool_str
Expand Down Expand Up @@ -889,8 +895,11 @@ def handle_s0027 status_code, status_name=nil, options={}
def handle_s0028 status_code, status_name=nil, options={}
case status_name
when 'status'
TrafficControllerSite.make_status '00-00'
times = @plans.map {|nr,plan| "#{"%02d" % plan.nr}-#{"%02d" % plan.cycle_time}"}.join(",")
TrafficControllerSite.make_status times
end
rescue StandardError => e
puts e
end

def handle_s0029 status_code, status_name=nil, options={}
Expand Down
4 changes: 2 additions & 2 deletions lib/rsmp/tlc/traffic_controller_site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ def build_plans signal_plans
signal_plans.each_pair do |id,settings|
states = nil
bands = nil
cycle_time = settings['cycle_time']
states = settings['states'] if settings
dynamic_bands = settings['dynamic_bands'] if settings

@signal_plans[id.to_i] = SignalPlan.new(nr: id.to_i, states:states,dynamic_bands:dynamic_bands)
@signal_plans[id.to_i] = SignalPlan.new(nr: id.to_i, cycle_time: cycle_time, states: states, dynamic_bands: dynamic_bands)
end
end

Expand All @@ -59,7 +60,6 @@ def build_component id:, type:, settings:{}
id: id,
ntsOId: settings['ntsOId'],
xNId: settings['xNId'],
cycle_time: settings['cycle_time'],
startup_sequence: @startup_sequence,
signal_plans: @signal_plans,
live_output: @site_settings['live_output'],
Expand Down

0 comments on commit a10ba58

Please sign in to comment.