Eclipse SUMO - Simulation of Urban MObility
MSRailSignal.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // A rail signal logic
17 /****************************************************************************/
18 #ifndef MSRailSignal_h
19 #define MSRailSignal_h
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <vector>
28 #include <microsim/MSRoute.h>
31 
32 
33 // ===========================================================================
34 // class declarations
35 // ===========================================================================
36 class MSLink;
37 class MSPhaseDefinition;
38 
39 
40 // ===========================================================================
41 // class definitions
42 // ===========================================================================
48 public:
55  MSRailSignal(MSTLLogicControl& tlcontrol,
56  const std::string& id, const std::string& programID,
57  const std::map<std::string, std::string>& parameters);
58 
59 
64  void init(NLDetectorBuilder& nb);
65 
66 
68  ~MSRailSignal();
69 
75  void addLink(MSLink* link, MSLane* lane, int pos);
76 
79 
86 
87 
90 
91 
100  void updateCurrentPhase();
102 
108 
110 
111 
114 
119  int getPhaseNumber() const;
120 
121 
126  const Phases& getPhases() const;
127 
136  const MSPhaseDefinition& getPhase(int givenstep) const;
138 
139 
142 
147  int getCurrentPhaseIndex() const;
148 
149 
153  const MSPhaseDefinition& getCurrentPhaseDef() const;
155 
156 
159 
164  SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const;
165 
166 
172  SUMOTime getOffsetFromIndex(int index) const;
173 
174 
180  int getIndexFromOffset(SUMOTime offset) const;
182 
183 
186 
194  void changeStepAndDuration(MSTLLogicControl& tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration) {
195  UNUSED_PARAMETER(tlcontrol);
196  UNUSED_PARAMETER(simStep);
197  UNUSED_PARAMETER(step);
198  UNUSED_PARAMETER(stepDuration);
199  }
201 
202 
204  void writeBlocks(OutputDevice& od) const;
205 
206  static bool hasOncomingRailTraffic(MSLink* link);
207 
208 protected:
209 
210  typedef std::pair<const SUMOVehicle* const, const MSLink::ApproachingVehicleInformation> Approaching;
211  typedef std::set<const MSLane*, ComparatorNumericalIdLess> LaneSet;
212 
213  /* The driveways (Fahrstrassen) for each link index
214  * Each link index has at least one driveway
215  * A driveway describes one possible route that passes the signal up
216  * the next secure point
217  * When a signal guards a switch (indirect guard) that signal stores two
218  * or more driveways
219  */
220  struct DriveWay {
221 
223  DriveWay(int index) :
224  myIndex(index),
225  myMaxFlankLength(0),
226  myActive(nullptr)
227  {}
228 
230  int myIndex;
231 
234 
237 
239  std::vector<const MSEdge*> myRoute;
240 
241  /* @brief the actual driveway part up to the next railsignal (halting position)
242  * This must be free of other trains */
243  std::vector<MSLane*> myForward;
244 
245  /* @brief the list of bidirectional edges that can enter the forward
246  * section and which must also be free of traffic
247  * (up to the first element that could give protection) */
248  std::vector<MSLane*> myBidi;
249 
250  /* @brief the list of edges that merge with the forward section
251  * (found via backward search, up to the first element that could give protection) */
252  std::vector<const MSLane*> myFlank;
253 
255  std::vector<const MSLane*> myConflictLanes;
256 
257  /* @brief the list of switches that threaten the driveway and for which protection must be found
258  */
259  std::vector<MSLink*> myFlankSwitches;
260 
261  /* @brief the list of (first) switches that could give protection from oncoming/flanking vehicles
262  * if any of them fails to do so, upstream search must be performed
263  * until protection or conflict is found
264  */
265  std::vector<MSLink*> myProtectingSwitches;
266 
267  /* The conflict links for this block
268  * Conflict resolution must be performed if vehicles are approaching the
269  * current link and any of the conflict links */
270  std::vector<MSLink*> myConflictLinks;
271 
273  bool conflictLaneOccupied() const;
274 
276  bool reserve(const Approaching& closest, MSEdgeVector& occupied);
277 
279  bool hasLinkConflict(const Approaching& closest, MSLink* foeLink) const;
280 
282  bool conflictLinkApproached() const;
283 
285  bool findProtection(const Approaching& veh, MSLink* link) const;
286 
288  bool overlap(const DriveWay& other) const;
289 
291  void writeBlocks(OutputDevice& od) const;
292 
293  /* @brief determine route that identifies this driveway (a subset of the
294  * vehicle route)
295  * collects:
296  * myRoute
297  * myForward
298  * myBidi
299  * myFlankSwitches
300  * myProtectingSwitches (at most 1 at the end of the bidi block)
301  */
302  void buildRoute(MSLink* origin, double length, MSRouteIterator next, MSRouteIterator end,
303  LaneSet& visited);
304 
306  void checkFlanks(const std::vector<MSLane*>& lanes, const LaneSet& visited, bool allFoes);
307 
309  void checkCrossingFlanks(MSLink* dwLink, const LaneSet& visited);
310 
312  void findFlankProtection(MSLink* link, double length, LaneSet& visited, MSLink* origLink);
313  };
314 
315  /* The driveways for each link
316  */
317  struct LinkInfo {
319  LinkInfo(MSLink* link);
320 
322 
325 
327  std::vector<DriveWay> myDriveways;
328 
330  std::string getID() const;
331 
333  DriveWay& getDriveWay(const SUMOVehicle*);
334 
336  DriveWay& buildDriveWay(MSRouteIterator first, MSRouteIterator end);
337 
339  void reroute(SUMOVehicle* veh, const MSEdgeVector& occupied);
340 
343  };
344 
346  std::vector<LinkInfo> myLinkInfos;
347 
349  static Approaching getClosest(MSLink* link);
350 
352  static std::string getTLLinkID(MSLink* link);
353 
355  static std::string getClickableTLLinkID(MSLink* link);
356 
358  static std::string describeLinks(std::vector<MSLink*> links);
359 
360 protected:
361 
367 
370 
373 
374  static int myNumWarnings;
375 
376 };
377 
378 
379 #endif
380 
381 /****************************************************************************/
382 
Builds detectors for microsim.
void writeBlocks(OutputDevice &od) const
write rail signal block output for all links and driveways
long long int SUMOTime
Definition: SUMOTime.h:35
std::vector< const MSLane * > myConflictLanes
the lanes that must be clear of trains before this signal can switch to green
Definition: MSRailSignal.h:255
static bool hasOncomingRailTraffic(MSLink *link)
void updateCurrentPhase()
returns the state of the signal that actually required
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
double myMaxFlankLength
the maximum flank length searched while building this driveway
Definition: MSRailSignal.h:233
static std::string getClickableTLLinkID(MSLink *link)
return logicID_linkIndex in a way that allows clicking in sumo-gui
A signal for rails.
Definition: MSRailSignal.h:47
std::vector< MSLink * > myConflictLinks
Definition: MSRailSignal.h:270
std::vector< LinkInfo > myLinkInfos
data storage for every link at this node (more than one when directly guarding a switch) ...
Definition: MSRailSignal.h:346
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
Definition: MSRailSignal.h:194
void checkCrossingFlanks(MSLink *dwLink, const LaneSet &visited)
find links that cross the driveway without entering it
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
bool conflictLinkApproached() const
Whether any of the conflict linkes have approaching vehicles.
std::vector< MSLane * > myBidi
Definition: MSRailSignal.h:248
std::vector< MSLink * > myProtectingSwitches
Definition: MSRailSignal.h:265
const std::string & getID() const
Returns the id.
Definition: Named.h:77
bool hasLinkConflict(const Approaching &closest, MSLink *foeLink) const
Whether the approaching vehicle is prevent from driving by another vehicle approaching the given link...
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:32
static int myNumWarnings
Definition: MSRailSignal.h:374
A class that stores and controls tls and switching of their programs.
std::vector< const MSEdge * > myRoute
list of lanes for matching against train routes
Definition: MSRailSignal.h:239
void findFlankProtection(MSLink *link, double length, LaneSet &visited, MSLink *origLink)
find upstream protection from the given link
bool findProtection(const Approaching &veh, MSLink *link) const
find protection for the given vehicle starting at a switch
Representation of a vehicle.
Definition: SUMOVehicle.h:61
void buildRoute(MSLink *origin, double length, MSRouteIterator next, MSRouteIterator end, LaneSet &visited)
void init(NLDetectorBuilder &nb)
Initialises the rail signal with information about adjacent rail signals.
Phases myPhases
The list of phases this logic uses.
Definition: MSRailSignal.h:366
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:58
const Phases & getPhases() const
Returns the phases of this tls program.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
static std::string describeLinks(std::vector< MSLink *> links)
print link descriptions
int getPhaseNumber() const
Returns the number of phases.
DriveWay(int index)
Constructor.
Definition: MSRailSignal.h:223
std::vector< const MSLane * > myFlank
Definition: MSRailSignal.h:252
SUMOTime trySwitch()
Switches to the next phase.
int myPhaseIndex
MSTrafficLightLogic requires that the phase index changes whenever signals change their state...
Definition: MSRailSignal.h:372
const SUMOVehicle * myActive
whether the current signal is switched green for a train approaching this block
Definition: MSRailSignal.h:236
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
MSRailSignal(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const std::map< std::string, std::string > &parameters)
Constructor.
void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
std::set< const MSLane *, ComparatorNumericalIdLess > LaneSet
Definition: MSRailSignal.h:211
void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
bool reserve(const Approaching &closest, MSEdgeVector &occupied)
attempt reserve this driveway for the given vehicle
std::vector< MSLink * > myFlankSwitches
Definition: MSRailSignal.h:259
bool conflictLaneOccupied() const
whether any of myConflictLanes is occupied
static Approaching getClosest(MSLink *link)
get the closest vehicle approaching the given link
void checkFlanks(const std::vector< MSLane *> &lanes, const LaneSet &visited, bool allFoes)
find switches that threathen this driveway
std::pair< const SUMOVehicle *const, const MSLink::ApproachingVehicleInformation > Approaching
Definition: MSRailSignal.h:210
void writeBlocks(OutputDevice &od) const
Write block items for this driveway.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
int myIndex
index in the list of driveways
Definition: MSRailSignal.h:230
The parent class for traffic light logics.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
MSPhaseDefinition myCurrentPhase
The current phase.
Definition: MSRailSignal.h:369
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:72
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
The definition of a single phase of a tls logic.
std::vector< MSLane * > myForward
Definition: MSRailSignal.h:243
static std::string getTLLinkID(MSLink *link)
return logicID_linkIndex
bool overlap(const DriveWay &other) const
Wether this driveway overlaps with the given one.
int getCurrentPhaseIndex() const
Returns the current index within the program.
~MSRailSignal()
Destructor.