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-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 /****************************************************************************/
15 // A rail signal logic
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <cassert>
25 #include <utility>
26 #include <vector>
27 #include <bitset>
29 #include <microsim/MSNet.h>
30 #include <microsim/MSEdge.h>
31 #include "MSTrafficLightLogic.h"
32 #include "MSRailCrossing.h"
33 #include <microsim/MSLane.h>
34 #include "MSPhaseDefinition.h"
35 #include "MSTLLogicControl.h"
36 
37 
38 // ===========================================================================
39 // method definitions
40 // ===========================================================================
42  const std::string& id, const std::string& programID,
43  const std::map<std::string, std::string>& parameters) :
44  MSSimpleTrafficLightLogic(tlcontrol, id, programID, TLTYPE_RAIL_CROSSING, Phases(), 0, DELTA_T, parameters),
45  // XXX make this configurable
46  mySecurityGap(TIME2STEPS(15)),
47  myMinGreenTime(TIME2STEPS(5)),
49  myYellowTime(TIME2STEPS(5)) {
50  // dummy phase, used to avoid crashing in MSTrafficLightLogic::setTrafficLightSignals()
51  myPhases.push_back(new MSPhaseDefinition(1, std::string(SUMO_MAX_CONNECTIONS, 'X')));
52 }
53 
54 void
56  delete myPhases.front();
57  myPhases.clear();
58  myPhases.push_back(new MSPhaseDefinition(1, std::string(myLinks.size(), 'G')));
59  myPhases.push_back(new MSPhaseDefinition(myYellowTime, std::string(myLinks.size(), 'y')));
60  myPhases.push_back(new MSPhaseDefinition(1, std::string(myLinks.size(), 'r')));
61  // init phases
63  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
64 }
65 
66 
68 
69 
70 // ----------- Handling of controlled links
71 void
75 }
76 
77 
78 // ------------ Switching and setting current rows
81  SUMOTime nextTry = updateCurrentPhase();
82  //if (getID() == "cluster_1088529493_1260626727") std::cout << " myStep=" << myStep << " nextTry=" << nextTry << "\n";
83  return nextTry;
84 }
85 
86 
90  SUMOTime stayRedUntil = now;
91  // check rail links for approaching foes to determine whether and how long
92  // the crossing must remain closed
93  for (std::vector<MSLink*>::const_iterator it_link = myIncomingRailLinks.begin(); it_link != myIncomingRailLinks.end(); ++it_link) {
94 
95  for (auto it_avi : (*it_link)->getApproaching()) {
96  const MSLink::ApproachingVehicleInformation& avi = it_avi.second;
97  if (avi.arrivalTime - myYellowTime - now < mySecurityGap) {
98  stayRedUntil = MAX2(stayRedUntil, avi.leavingTime);
99  }
100  }
101  if ((*it_link)->getViaLane() != nullptr && (*it_link)->getViaLane()->getVehicleNumberWithPartials() > 0) {
102  // do not open if there is still a train on the crossing
103  stayRedUntil = MAX2(stayRedUntil, now + DELTA_T);
104  }
105  }
106  //if (getID() == "cluster_1088529493_1260626727") std::cout << SIMTIME << " stayRedUntil=" << stayRedUntil;
107  const SUMOTime wait = stayRedUntil - now;
108 
109  if (myStep == 0) {
110  // 'G': check whether the crossing can stay open
111  if (wait == 0) {
112  return DELTA_T;
113  } else {
114  myStep++;
115  return myYellowTime;
116  }
117  } else if (myStep == 1) {
118  // 'y': yellow time is over. switch to red
119  myStep++;
120  return MAX2(DELTA_T, wait);
121  } else {
122  // 'r': check whether we may open again
123  if (wait == 0) {
124  myStep = 0;
125  return myMinGreenTime;
126  } else {
127  return wait;
128  }
129  }
130 }
131 
132 
133 // ------------ Conversion between time and phase
134 SUMOTime
136  return 0;
137 }
138 
139 SUMOTime
141  return 0;
142 }
143 
144 int
146  return 0;
147 }
148 
149 
150 void
151 MSRailCrossing::addLink(MSLink* link, MSLane* lane, int pos) {
152  if (pos >= 0) {
153  MSTrafficLightLogic::addLink(link, lane, pos);
154  } else {
155  myIncomingRailLinks.push_back(link);
156  }
157 }
158 
159 
160 /****************************************************************************/
161 
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:35
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
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
Phases myPhases
The list of phases this logic uses.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
T MAX2(T a, T b)
Definition: StdDefs.h:80
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:43
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
#define TIME2STEPS(x)
Definition: SUMOTime.h:59
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
A fixed traffic light logic.
~MSRailCrossing()
Destructor.
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.
A class that stores and controls tls and switching of their programs.
MSRailCrossing(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const std::map< std::string, std::string > &parameters)
Constructor.
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
SUMOTime mySecurityGap
minimum time gap between closing the crossing (end of yellow time) and train passing the crossing ...
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
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...
void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
virtual void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
The parent class for traffic light logics.
SUMOTime myYellowTime
minimum green time
SUMOTime trySwitch()
Switches to the next phase.
std::vector< MSLink * > myIncomingRailLinks
The incoming rail links.
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
The definition of a single phase of a tls logic.
SUMOTime myMinGreenTime
minimum green time