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