1
2 package nl.tudelft.simulation.traffic.station;
3
4 import java.rmi.RemoteException;
5 import java.util.List;
6 import javax.media.j3d.Bounds;
7 import javax.vecmath.Point3d;
8 import nl.tudelft.simulation.dsol.animation.LocatableInterface;
9 import nl.tudelft.simulation.dsol.simulators.AnimatorInterface;
10 import nl.tudelft.simulation.dsol.simulators.DEVSSimulatorInterface;
11 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
12 import nl.tudelft.simulation.event.Event;
13 import nl.tudelft.simulation.language.d3.BoundingBox;
14 import nl.tudelft.simulation.language.d3.DirectedPoint;
15 import nl.tudelft.simulation.logger.Logger;
16 import nl.tudelft.simulation.traffic.animation.StationLightAnimation;
17 import nl.tudelft.simulation.traffic.controlpoint.real.AbstractVisibleControlPoint;
18 import nl.tudelft.simulation.traffic.controlpoint.real.Changeable;
19 import nl.tudelft.simulation.traffic.controlpoint.real.StopSignInterface;
20 import nl.tudelft.simulation.traffic.track.TrackInterface;
21 import nl.tudelft.simulation.traffic.track.util.TrackProgression;
22 import nl.tudelft.simulation.traffic.vehicle.VehiclePhysicalInterface;
23
24 /***
25 * This class implements the traffic light for the stations. The traffic light
26 * has the objective of controlling the behavior of the vehicles at the
27 * stations. They have only two states: - red -> to make the vehicles stop at
28 * the station - green -> to allow them to depart (after the halting time has
29 * passed <br>
30 * (c) copyright 2003-2004 <a href="http://www.simulation.tudelft.nl">Delft
31 * University of Technology </a>, the Netherlands. <br>
32 * See for project information <a href="http://www.simulation.tudelft.nl">
33 * www.simulation.tudelft.nl </a> <br>
34 * License of use: <a href="http://www.gnu.org/copyleft/gpl.html">General Public
35 * License (GPL) </a>, no warranty <br>
36 *
37 * @version May 31, 2004 <br>
38 * @author <a
39 * href="http://www.tbm.tudelft.nl/webstaf/alexandv/index.htm">Alexander
40 * Verbraeck </a>
41 */
42 public class StationHaltingTrafficLight extends AbstractVisibleControlPoint
43 implements
44 LocatableInterface,
45 StopSignInterface
46 {
47 /*** GREEN state */
48 public static final String STATE_GREEN = "GREEN";
49
50 /*** RED state */
51 public static final String STATE_RED = "RED";
52
53 /*** GREEN sensor */
54 public static final int SENSOR_RED = 0;
55
56 /*** RED sensor */
57 public static final int SENSOR_TIMER = 1;
58
59 /*** sensor names */
60 public static final String[] sensorNames = new String[]{"RED", "TIMER"};
61
62 /*** the distance before the stopping place to request */
63 public double requestDistance;
64
65 /*** the STATE of the current block signal */
66 private String currentState = STATE_GREEN;
67
68 /*** the name */
69 private String name;
70
71 /*** dx for animation (bounds) */
72 private double dx;
73
74 /*** dy for animation (bounds) */
75 private double dy;
76
77 /*** the station */
78 public Station station;
79
80 /*** the simulator */
81 public DEVSSimulatorInterface simulator;
82
83 /***
84 * @param station
85 * @param name
86 * @param track
87 * @param progression
88 * @param visibleDistance
89 * @param requestDistance
90 * @param simulator
91 * @param dx
92 * @param dy
93 */
94 public StationHaltingTrafficLight(final Station station, final String name,
95 final TrackInterface track, final double progression,
96 final double visibleDistance, final double requestDistance,
97 final DEVSSimulatorInterface simulator, final double dx,
98 final double dy)
99 {
100 super(track, progression, visibleDistance, simulator);
101 this.name = name;
102 this.station = station;
103 System.out.println(this + ", placed on " + super.track
104 + " progression " + super.progression + " (length="
105 + super.track.getLength() + ")");
106 this.requestDistance = requestDistance;
107 double vcpProg = progression - requestDistance;
108 addStationSensors(super.track, vcpProg, SENSOR_RED, simulator);
109 vcpProg = progression - 1;
110 addStationSensors(super.track, vcpProg, SENSOR_TIMER, simulator);
111 this.simulator = simulator;
112 this.dx = dx;
113 this.dy = dy;
114 if (simulator instanceof AnimatorInterface)
115 {
116 new StationLightAnimation(this, simulator, dx, dy);
117 }
118 }
119
120 /***
121 * This method creates a sensor for each path along which this ControlPoint
122 * is reachable, also when there are branches in the path
123 *
124 * @param track
125 * @param vcpProg
126 * @param sensor
127 * @param simulator
128 */
129 private void addStationSensors(final TrackInterface track,
130 final double vcpProg, final int sensor,
131 final SimulatorInterface simulator)
132 {
133 List tpList = track.calculateTrackProgressionListAll(vcpProg);
134 for (int i = 0; i < tpList.size(); i++)
135 {
136 TrackProgression tp = (TrackProgression) tpList.get(i);
137 new StationControlPoint(tp.getTrack(), tp.getProgression(), this,
138 sensor, simulator);
139 System.out.println(this + ", sensor "
140 + StationHaltingTrafficLight.sensorNames[sensor]
141 + " added on track " + tp.getTrack() + ", progression "
142 + tp.getProgression());
143 }
144 }
145
146 /***
147 * @param sensor
148 * @param vehicle
149 */
150 public void triggerSensor(final int sensor,
151 final VehiclePhysicalInterface vehicle)
152 {
153 Logger.fine(this, "triggerSensor", this + ", Vehicle "
154 + vehicle.toString() + " triggered sensor "
155 + StationHaltingTrafficLight.sensorNames[sensor]
156 + ", old state was " + this.currentState);
157 System.out.println(this + ", Vehicle " + vehicle.toString()
158 + " triggered sensor "
159 + StationHaltingTrafficLight.sensorNames[sensor]
160 + ", old state was " + this.currentState);
161 switch (sensor)
162 {
163 case SENSOR_RED :
164 this.currentState = STATE_RED;
165 break;
166 case SENSOR_TIMER :
167 System.out.println("station.getHaltingTime: "
168 + (this.station.getHaltingTime()));
169 try
170 {
171 this.simulator.scheduleEvent(this.station.getHaltingTime(),
172 this, this, "stateGreen", null);
173 } catch (Exception e)
174 {
175 Logger.severe(this, "triggerSensor", e);
176 }
177 break;
178 default :
179 Logger.severe(this, "triggerSensor",
180 "reported state not RED or TIMER");
181 break;
182 }
183 Logger.fine(this, "triggerSensor", this + ", Vehicle "
184 + vehicle.toString() + " new state is " + this.currentState);
185 System.out.println(this + ", Vehicle " + vehicle.toString()
186 + " new state is " + this.currentState);
187 Event event = new Event(Changeable.CHANGE_STATUS_EVENT, this, null);
188 this.fireEvent(event);
189 }
190
191 /***
192 *
193 */
194 public void stateGreen()
195 {
196 System.out.println("TIMER state turned to GREEN");
197 this.currentState = STATE_GREEN;
198 Event event = new Event(Changeable.CHANGE_STATUS_EVENT, this, null);
199 this.fireEvent(event);
200 }
201
202 /***
203 * @see nl.tudelft.simulation.traffic.controlpoint.ControlPointInterface#pass(nl.tudelft.simulation.traffic.vehicle.VehiclePhysicalInterface)
204 */
205 public void pass(final VehiclePhysicalInterface vehicle)
206 {
207
208 this.removeListener(vehicle, Changeable.CHANGE_STATUS_EVENT);
209 }
210
211 /***
212 * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getLocation()
213 */
214 public DirectedPoint getLocation() throws RemoteException
215 {
216 DirectedPoint p = super.getTrack().getLocationOfProgression(
217 super.getProgression());
218 return new DirectedPoint(p.x, p.y, p.z);
219 }
220
221 /***
222 * @see nl.tudelft.simulation.dsol.animation.LocatableInterface#getBounds()
223 */
224 public Bounds getBounds() throws RemoteException
225 {
226 return new BoundingBox(new Point3d(this.dx - 2.0, this.dy, 0),
227 new Point3d(this.dx + 2.0, this.dy + 12.0, 0));
228 }
229
230 /***
231 * @return Returns the currentState.
232 */
233 public String getCurrentState()
234 {
235 return this.currentState;
236 }
237
238 /***
239 * @see nl.tudelft.simulation.traffic.controlpoint.real.Changeable#getStatus()
240 */
241 public String getStatus()
242 {
243 if (this.currentState == STATE_RED)
244 return StopSignInterface.STOP;
245 else
246 return StopSignInterface.CONTINUE;
247 }
248
249 /***
250 * @see java.lang.Object#toString()
251 */
252 public String toString()
253 {
254 return "StationHaltingTrafficLight " + this.name;
255 }
256
257 /***
258 * @return
259 */
260 public Station getStation()
261 {
262 return this.station;
263 }
264 }