Eclipse SUMO - Simulation of Urban MObility
MSRailCrossing.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // A rail signal logic
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <cassert>
23 #include <utility>
24 #include <vector>
25 #include <bitset>
27 #include <microsim/MSNet.h>
28 #include <microsim/MSEdge.h>
29 #include "MSTrafficLightLogic.h"
30 #include "MSRailCrossing.h"
31 #include <microsim/MSLane.h>
32 #include "MSPhaseDefinition.h"
33 #include "MSTLLogicControl.h"
34 
35 
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
40  const std::string& id, const std::string& programID, SUMOTime delay,
41  const std::map<std::string, std::string>& parameters) :
42  MSSimpleTrafficLightLogic(tlcontrol, id, programID, TrafficLightType::RAIL_CROSSING, Phases(), 0, delay, parameters),
43  // XXX make this configurable
44  mySecurityGap(TIME2STEPS(15)),
45  myMinGreenTime(TIME2STEPS(5)),
47  myYellowTime(TIME2STEPS(5)) {
48  // dummy phase, used to avoid crashing in MSTrafficLightLogic::setTrafficLightSignals()
49  myPhases.push_back(new MSPhaseDefinition(1, std::string(SUMO_MAX_CONNECTIONS, 'X')));
50 }
51 
52 void
54  delete myPhases.front();
55  myPhases.clear();
56  myPhases.push_back(new MSPhaseDefinition(1, std::string(myLinks.size(), 'G')));
57  myPhases.push_back(new MSPhaseDefinition(myYellowTime, std::string(myLinks.size(), 'y')));
58  myPhases.push_back(new MSPhaseDefinition(1, std::string(myLinks.size(), 'r')));
59  // init phases
61  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
62  myNumLinks = (int)myLinks.size();
63 }
64 
65 
67 
68 
69 // ----------- Handling of controlled links
70 void
74 }
75 
76 
77 // ------------ Switching and setting current rows
80  SUMOTime nextTry = updateCurrentPhase();
81  //if (getID() == "cluster_1088529493_1260626727") std::cout << " myStep=" << myStep << " nextTry=" << nextTry << "\n";
82  return nextTry;
83 }
84 
85 
89  SUMOTime stayRedUntil = now;
90  // check rail links for approaching foes to determine whether and how long
91  // the crossing must remain closed
92  for (const MSLink* const link : myIncomingRailLinks) {
93  for (auto it_avi : link->getApproaching()) {
94  const MSLink::ApproachingVehicleInformation& avi = it_avi.second;
95  if (avi.arrivalTime - myYellowTime - now < mySecurityGap) {
96  stayRedUntil = MAX2(stayRedUntil, avi.leavingTime);
97  }
98  }
99  if (link->getViaLane() != nullptr && link->getViaLane()->getVehicleNumberWithPartials() > 0) {
100  // do not open if there is still a train on the crossing
101  stayRedUntil = MAX2(stayRedUntil, now + DELTA_T);
102  }
103  }
104  //if (getID() == "cluster_1088529493_1260626727") std::cout << SIMTIME << " stayRedUntil=" << stayRedUntil;
105  const SUMOTime wait = stayRedUntil - now;
106 
107  if (myStep == 0) {
108  // 'G': check whether the crossing can stay open
109  if (wait == 0) {
110  return DELTA_T;
111  } else {
112  myStep++;
113  return myYellowTime;
114  }
115  } else if (myStep == 1) {
116  // 'y': yellow time is over. switch to red
117  myStep++;
118  return MAX2(DELTA_T, wait);
119  } else {
120  // 'r': check whether we may open again
121  if (wait == 0) {
122  myStep = 0;
123  return myMinGreenTime;
124  } else {
125  return wait;
126  }
127  }
128 }
129 
130 
131 // ------------ Conversion between time and phase
132 SUMOTime
134  return 0;
135 }
136 
137 SUMOTime
139  return 0;
140 }
141 
142 int
144  return 0;
145 }
146 
147 
148 void
149 MSRailCrossing::addLink(MSLink* link, MSLane* lane, int pos) {
150  if (pos >= 0) {
151  MSTrafficLightLogic::addLink(link, lane, pos);
152  } else {
153  myIncomingRailLinks.push_back(link);
154  }
155 }
156 
157 
158 /****************************************************************************/
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
#define TIME2STEPS(x)
Definition: SUMOTime.h:55
long long int SUMOTime
Definition: SUMOTime.h:31
TrafficLightType
T MAX2(T a, T b)
Definition: StdDefs.h:79
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:40
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:313
The definition of a single phase of a tls logic.
void init(NLDetectorBuilder &nb)
Initialises the rail signal with information about adjacent rail signals.
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
SUMOTime myYellowTime
minimum green time
SUMOTime mySecurityGap
minimum time gap between closing the crossing (end of yellow time) and train passing the crossing
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
~MSRailCrossing()
Destructor.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
SUMOTime trySwitch()
Switches to the next phase.
MSRailCrossing(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
SUMOTime updateCurrentPhase()
updates the current phase of the signal
std::vector< MSLink * > myIncomingRailLinks
The incoming rail links.
void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
SUMOTime myMinGreenTime
minimum green time
A fixed traffic light logic.
Phases myPhases
The list of phases this logic uses.
A class that stores and controls tls and switching of their programs.
The parent class for traffic light logics.
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
int myNumLinks
number of controlled links
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
virtual void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
LinkVectorVector myLinks
The list of LinkVectors; each vector contains the links that belong to the same link index.
Builds detectors for microsim.