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-2018 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  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
83  //if (getID() == "cluster_1088529493_1260626727") std::cout << " myStep=" << myStep << " nextTry=" << nextTry << "\n";
84  return nextTry;
85 }
86 
87 
91  SUMOTime stayRedUntil = now;
92  // check rail links for approaching foes to determine whether and how long
93  // the crossing must remain closed
94  for (std::vector<MSLink*>::const_iterator it_link = myIncomingRailLinks.begin(); it_link != myIncomingRailLinks.end(); ++it_link) {
95 
96  for (auto it_avi : (*it_link)->getApproaching()) {
97  const MSLink::ApproachingVehicleInformation& avi = it_avi.second;
98  if (avi.arrivalTime - myYellowTime - now < mySecurityGap) {
99  stayRedUntil = MAX2(stayRedUntil, avi.leavingTime);
100  }
101  }
102  if ((*it_link)->getViaLane() != nullptr && (*it_link)->getViaLane()->getVehicleNumberWithPartials() > 0) {
103  // do not open if there is still a train on the crossing
104  stayRedUntil = MAX2(stayRedUntil, now + DELTA_T);
105  }
106  }
107  //if (getID() == "cluster_1088529493_1260626727") std::cout << SIMTIME << " stayRedUntil=" << stayRedUntil;
108  const SUMOTime wait = stayRedUntil - now;
109 
110  if (myStep == 0) {
111  // 'G': check whether the crossing can stay open
112  if (wait == 0) {
113  return DELTA_T;
114  } else {
115  myStep++;
116  return myYellowTime;
117  }
118  } else if (myStep == 1) {
119  // 'y': yellow time is over. switch to red
120  myStep++;
121  return MAX2(DELTA_T, wait);
122  } else {
123  // 'r': check whether we may open again
124  if (wait == 0) {
125  myStep = 0;
126  return myMinGreenTime;
127  } else {
128  return wait;
129  }
130  }
131 }
132 
133 
134 // ------------ Conversion between time and phase
135 SUMOTime
137  return 0;
138 }
139 
140 SUMOTime
142  return 0;
143 }
144 
145 int
147  return 0;
148 }
149 
150 
151 void
152 MSRailCrossing::addLink(MSLink* link, MSLane* lane, int pos) {
153  if (pos >= 0) {
154  MSTrafficLightLogic::addLink(link, lane, pos);
155  } else {
156  myIncomingRailLinks.push_back(link);
157  }
158 }
159 
160 
161 /****************************************************************************/
162 
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:36
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:165
T MAX2(T a, T b)
Definition: StdDefs.h:76
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:44
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
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:263
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:78
The definition of a single phase of a tls logic.
SUMOTime myMinGreenTime
minimum green time