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 
334 
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 
UNUSED_PARAMETER
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:32
MSRailSignal::DriveWay::myConflictLanes
std::vector< const MSLane * > myConflictLanes
the lanes that must be clear of trains before this signal can switch to green
Definition: MSRailSignal.h:255
MSRailSignal::changeStepAndDuration
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
Definition: MSRailSignal.h:194
MSRailSignal::getOffsetFromIndex
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
Definition: MSRailSignal.cpp:209
MSRailSignal::getIndexFromOffset
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
Definition: MSRailSignal.cpp:214
MSRailSignal::DriveWay::myIndex
int myIndex
index in the list of driveways
Definition: MSRailSignal.h:230
MSTLLogicControl.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
MSRailSignal::getPhaseIndexAtTime
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
Definition: MSRailSignal.cpp:204
MSRailSignal::trySwitch
SUMOTime trySwitch()
Switches to the next phase.
Definition: MSRailSignal.cpp:114
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
MSRailSignal::DriveWay::myProtectingSwitches
std::vector< MSLink * > myProtectingSwitches
Definition: MSRailSignal.h:265
MSRouteIterator
ConstMSEdgeVector::const_iterator MSRouteIterator
Definition: MSRoute.h:58
MSRailSignal::DriveWay::buildRoute
void buildRoute(MSLink *origin, double length, MSRouteIterator next, MSRouteIterator end, LaneSet &visited)
Definition: MSRailSignal.cpp:730
MSTrafficLightLogic::Phases
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Definition: MSTrafficLightLogic.h:62
MSRailSignal::DriveWay::hasLinkConflict
bool hasLinkConflict(const Approaching &closest, MSLink *foeLink) const
Whether the approaching vehicle is prevent from driving by another vehicle approaching the given link...
Definition: MSRailSignal.cpp:549
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
MSRailSignal::myPhaseIndex
int myPhaseIndex
MSTrafficLightLogic requires that the phase index changes whenever signals change their state.
Definition: MSRailSignal.h:372
MSRailSignal::DriveWay::myBidi
std::vector< MSLane * > myBidi
Definition: MSRailSignal.h:248
SUMOVehicle
Representation of a vehicle.
Definition: SUMOVehicle.h:61
MSRailSignal::init
void init(NLDetectorBuilder &nb)
Initialises the rail signal with information about adjacent rail signals.
Definition: MSRailSignal.cpp:86
MSRailSignal::getPhaseNumber
int getPhaseNumber() const
Returns the number of phases.
Definition: MSRailSignal.cpp:177
MSRailSignal::getPhases
const Phases & getPhases() const
Returns the phases of this tls program.
Definition: MSRailSignal.cpp:182
MSRailSignal::DriveWay::overlap
bool overlap(const DriveWay &other) const
Wether this driveway overlaps with the given one.
Definition: MSRailSignal.cpp:692
MSRailSignal::getTLLinkID
static std::string getTLLinkID(MSLink *link)
return logicID_linkIndex
Definition: MSRailSignal.cpp:228
MSRailSignal::DriveWay::findFlankProtection
void findFlankProtection(MSLink *link, double length, LaneSet &visited, MSLink *origLink)
find upstream protection from the given link
Definition: MSRailSignal.cpp:875
MSRailSignal::DriveWay::myFlank
std::vector< const MSLane * > myFlank
Definition: MSRailSignal.h:252
MSRailSignal::LaneSet
std::set< const MSLane *, ComparatorNumericalIdLess > LaneSet
Definition: MSRailSignal.h:211
MSRailSignal::DriveWay::checkCrossingFlanks
void checkCrossingFlanks(MSLink *dwLink, const LaneSet &visited)
find links that cross the driveway without entering it
Definition: MSRailSignal.cpp:843
MSTrafficLightLogic.h
MSRailSignal::adaptLinkInformationFrom
void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
Definition: MSRailSignal.cpp:106
MSRailSignal::DriveWay::conflictLinkApproached
bool conflictLinkApproached() const
Whether any of the conflict linkes have approaching vehicles.
Definition: MSRailSignal.cpp:538
MSRailSignal::getClosest
static Approaching getClosest(MSLink *link)
get the closest vehicle approaching the given link
Definition: MSRailSignal.cpp:247
MSRailSignal::getClickableTLLinkID
static std::string getClickableTLLinkID(MSLink *link)
return logicID_linkIndex in a way that allows clicking in sumo-gui
Definition: MSRailSignal.cpp:233
MSRailSignal::DriveWay::DriveWay
DriveWay(int index)
Constructor.
Definition: MSRailSignal.h:223
MSRailSignal::getCurrentPhaseIndex
int getCurrentPhaseIndex() const
Returns the current index within the program.
Definition: MSRailSignal.cpp:193
MSRailSignal::MSRailSignal
MSRailSignal(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const std::map< std::string, std::string > &parameters)
Constructor.
Definition: MSRailSignal.cpp:76
MSRailSignal::DriveWay::myMaxFlankLength
double myMaxFlankLength
the maximum flank length searched while building this driveway
Definition: MSRailSignal.h:233
MSRailSignal::describeLinks
static std::string describeLinks(std::vector< MSLink * > links)
print link descriptions
Definition: MSRailSignal.cpp:238
MSRailSignal::Approaching
std::pair< const SUMOVehicle *const, const MSLink::ApproachingVehicleInformation > Approaching
Definition: MSRailSignal.h:210
MSRailSignal
A signal for rails.
Definition: MSRailSignal.h:47
MSRailSignal::DriveWay::checkFlanks
void checkFlanks(const std::vector< MSLane * > &lanes, const LaneSet &visited, bool allFoes)
find switches that threathen this driveway
Definition: MSRailSignal.cpp:819
MSRailSignal::DriveWay::findProtection
bool findProtection(const Approaching &veh, MSLink *link) const
find protection for the given vehicle starting at a switch
Definition: MSRailSignal.cpp:631
MSRailSignal::writeBlocks
void writeBlocks(OutputDevice &od) const
write rail signal block output for all links and driveways
Definition: MSRailSignal.cpp:274
MSRailSignal::myCurrentPhase
MSPhaseDefinition myCurrentPhase
The current phase.
Definition: MSRailSignal.h:369
MSRailSignal::addLink
void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
Definition: MSRailSignal.cpp:220
MSRailSignal::DriveWay::myActive
const SUMOVehicle * myActive
whether the current signal is switched green for a train approaching this block
Definition: MSRailSignal.h:236
MSRailSignal::DriveWay::myRoute
std::vector< const MSEdge * > myRoute
list of lanes for matching against train routes
Definition: MSRailSignal.h:239
MSRailSignal::~MSRailSignal
~MSRailSignal()
Destructor.
Definition: MSRailSignal.cpp:100
MSTrafficLightLogic
The parent class for traffic light logics.
Definition: MSTrafficLightLogic.h:56
MSRailSignal::myPhases
Phases myPhases
The list of phases this logic uses.
Definition: MSRailSignal.h:366
MSEdgeVector
std::vector< MSEdge * > MSEdgeVector
Definition: MSEdge.h:72
MSRoute.h
MSRailSignal::DriveWay::conflictLaneOccupied
bool conflictLaneOccupied() const
whether any of myConflictLanes is occupied
Definition: MSRailSignal.cpp:615
MSRailSignal::DriveWay::myFlankSwitches
std::vector< MSLink * > myFlankSwitches
Definition: MSRailSignal.h:259
MSRailSignal::hasOncomingRailTraffic
static bool hasOncomingRailTraffic(MSLink *link)
Definition: MSRailSignal.cpp:293
config.h
MSRailSignal::updateCurrentPhase
void updateCurrentPhase()
returns the state of the signal that actually required
Definition: MSRailSignal.cpp:122
MSTLLogicControl
A class that stores and controls tls and switching of their programs.
Definition: MSTLLogicControl.h:60
MSRailSignal::DriveWay::reserve
bool reserve(const Approaching &closest, MSEdgeVector &occupied)
attempt reserve this driveway for the given vehicle
Definition: MSRailSignal.cpp:498
MSRailSignal::myLinkInfos
std::vector< LinkInfo > myLinkInfos
data storage for every link at this node (more than one when directly guarding a switch)
Definition: MSRailSignal.h:346
MSPhaseDefinition
The definition of a single phase of a tls logic.
Definition: MSPhaseDefinition.h:52
MSRailSignal::getCurrentPhaseDef
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
Definition: MSRailSignal.cpp:198
MSRailSignal::DriveWay::myForward
std::vector< MSLane * > myForward
Definition: MSRailSignal.h:243
MSRailSignal::DriveWay
Definition: MSRailSignal.h:220
MSRailSignal::myNumWarnings
static int myNumWarnings
Definition: MSRailSignal.h:374
MSRailSignal::DriveWay::writeBlocks
void writeBlocks(OutputDevice &od) const
Write block items for this driveway.
Definition: MSRailSignal.cpp:706
MSRailSignal::getPhase
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
Definition: MSRailSignal.cpp:187
NLDetectorBuilder
Builds detectors for microsim.
Definition: NLDetectorBuilder.h:56
MSRailSignal::DriveWay::myConflictLinks
std::vector< MSLink * > myConflictLinks
Definition: MSRailSignal.h:270