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
56 @Override
57 public boolean isEnabled()
58 {
59 Frequency flow = totalFlow();
60 CategoryLogger.always().info("Flow is " + flow.getInUnit(FrequencyUnit.PER_HOUR));
61 if (meanSpeed().le(this.speedThreshold)
62 || (this.lastFlow != null && flow.gt(this.lastFlow) && flow.gt(this.flowThreshold)))
63 {
64 if (flow.lt(this.capacity))
65 {
66 this.cycleTime = Duration.instantiateSI(1.0 / this.capacity.minus(flow).si);
67 this.cycleTime = Duration.min(this.cycleTime, MAX_CYCLE_TIME);
68 }
69 else
70 {
71 this.cycleTime = MAX_CYCLE_TIME;
72 }
73 this.lastFlow = flow;
74 return true;
75 }
76 this.lastFlow = flow;
77 return false;
78 }
79
80
81 @Override
82 public Duration getCycleTime()
83 {
84 Throw.whenNull(this.cycleTime, "The method isEnabled() in a RwsSwitch should set a cycle time.");
85 return this.cycleTime;
86 }
87
88 }