1 package org.opentrafficsim.road.network.control.rampmetering;
2
3 import java.util.List;
4
5 import org.djunits.unit.FrequencyUnit;
6 import org.djunits.unit.SpeedUnit;
7 import org.djunits.value.vdouble.scalar.Duration;
8 import org.djunits.value.vdouble.scalar.Frequency;
9 import org.djunits.value.vdouble.scalar.Speed;
10 import org.djutils.exceptions.Throw;
11 import org.djutils.logger.CategoryLogger;
12 import org.opentrafficsim.road.network.lane.object.detector.LoopDetector;
13
14
15
16
17
18
19
20
21
22
23
24 public class RwsSwitch extends SingleCrossSectionSwitch
25 {
26
27
28 private static final Duration MAX_CYCLE_TIME = Duration.instantiateSI(15);
29
30
31 private final Frequency capacity;
32
33
34 private final Frequency flowThreshold;
35
36
37 private final Speed speedThreshold = new Speed(70, SpeedUnit.KM_PER_HOUR);
38
39
40 private Duration cycleTime;
41
42
43 private Frequency lastFlow;
44
45
46
47
48 public RwsSwitch(final List<LoopDetector> detectors)
49 {
50 super(Duration.instantiateSI(60.0), detectors);
51 this.capacity = new Frequency(2000, FrequencyUnit.PER_HOUR).times(detectors.size());
52 this.flowThreshold = new Frequency(1500, FrequencyUnit.PER_HOUR).times(detectors.size());
53 }
54
55 @Override
56 public boolean isEnabled()
57 {
58 Frequency flow = totalFlow();
59 CategoryLogger.always().info("Flow is " + flow.getInUnit(FrequencyUnit.PER_HOUR));
60 if (meanSpeed().le(this.speedThreshold)
61 || (this.lastFlow != null && flow.gt(this.lastFlow) && flow.gt(this.flowThreshold)))
62 {
63 if (flow.lt(this.capacity))
64 {
65 this.cycleTime = Duration.instantiateSI(1.0 / this.capacity.minus(flow).si);
66 this.cycleTime = Duration.min(this.cycleTime, MAX_CYCLE_TIME);
67 }
68 else
69 {
70 this.cycleTime = MAX_CYCLE_TIME;
71 }
72 this.lastFlow = flow;
73 return true;
74 }
75 this.lastFlow = flow;
76 return false;
77 }
78
79 @Override
80 public Duration getCycleTime()
81 {
82 Throw.whenNull(this.cycleTime, "The method isEnabled() in a RwsSwitch should set a cycle time.");
83 return this.cycleTime;
84 }
85
86 }