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.sensor.Detector;
13
14
15
16
17
18
19
20
21
22
23
24
25 public class RwsSwitch extends SingleCrossSectionSwitch
26 {
27
28
29 private static final Duration MAX_CYCLE_TIME = Duration.instantiateSI(15);
30
31
32 private final Frequency capacity;
33
34
35 private final Frequency flowThreshold;
36
37
38 private final Speed speedThreshold = new Speed(70, SpeedUnit.KM_PER_HOUR);
39
40
41 private Duration cycleTime;
42
43
44 private Frequency lastFlow;
45
46
47
48
49 public RwsSwitch(final List<Detector> detectors)
50 {
51 super(Duration.instantiateSI(60.0), detectors);
52 this.capacity = new Frequency(2000, FrequencyUnit.PER_HOUR).times(detectors.size());
53 this.flowThreshold = new Frequency(1500, FrequencyUnit.PER_HOUR).times(detectors.size());
54 }
55
56
57 @Override
58 public boolean isEnabled()
59 {
60 Frequency flow = totalFlow();
61 CategoryLogger.always().info("Flow is " + flow.getInUnit(FrequencyUnit.PER_HOUR));
62 if (meanSpeed().le(this.speedThreshold)
63 || (this.lastFlow != null && flow.gt(this.lastFlow) && flow.gt(this.flowThreshold)))
64 {
65 if (flow.lt(this.capacity))
66 {
67 this.cycleTime = Duration.instantiateSI(1.0 / this.capacity.minus(flow).si);
68 this.cycleTime = Duration.min(this.cycleTime, MAX_CYCLE_TIME);
69 }
70 else
71 {
72 this.cycleTime = MAX_CYCLE_TIME;
73 }
74 this.lastFlow = flow;
75 return true;
76 }
77 this.lastFlow = flow;
78 return false;
79 }
80
81
82 @Override
83 public Duration getCycleTime()
84 {
85 Throw.whenNull(this.cycleTime, "The method isEnabled() in a RwsSwitch should set a cycle time.");
86 return this.cycleTime;
87 }
88
89 }