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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
17 // A rail signal logic
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #ifdef _MSC_VER
25 #include <windows_config.h>
26 #else
27 #include <config.h>
28 #endif
29 
30 #include <cassert>
31 #include <utility>
32 #include <vector>
33 #include <bitset>
35 #include <microsim/MSNet.h>
36 #include <microsim/MSEdge.h>
37 #include "MSTrafficLightLogic.h"
38 #include "MSRailCrossing.h"
39 #include <microsim/MSLane.h>
40 #include "MSPhaseDefinition.h"
41 #include "MSTLLogicControl.h"
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
48  const std::string& id, const std::string& subid,
49  const std::map<std::string, std::string>& parameters) :
50  MSSimpleTrafficLightLogic(tlcontrol, id, subid, Phases(), 0, DELTA_T, parameters),
51  // XXX make this configurable
52  mySecurityGap(TIME2STEPS(15)),
53  myMinGreenTime(TIME2STEPS(5)),
55  myYellowTime(TIME2STEPS(5)) {
56  // dummy phase, used to avoid crashing in MSTrafficLightLogic::setTrafficLightSignals()
57  myPhases.push_back(new MSPhaseDefinition(1, 1, 1, std::string(SUMO_MAX_CONNECTIONS, 'X')));
58 }
59 
60 void
62  delete myPhases.front();
63  myPhases.clear();
64  myPhases.push_back(new MSPhaseDefinition(1, 1, 1, std::string(myLinks.size(), 'G')));
65  myPhases.push_back(new MSPhaseDefinition(myYellowTime, myYellowTime, myYellowTime, std::string(myLinks.size(), 'y')));
66  myPhases.push_back(new MSPhaseDefinition(1, 1, 1, std::string(myLinks.size(), 'r')));
67  // init phases
69  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
70 }
71 
72 
74 
75 
76 // ----------- Handling of controlled links
77 void
81 }
82 
83 
84 // ------------ Switching and setting current rows
87  SUMOTime nextTry = updateCurrentPhase();
88  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
89  //if (getID() == "cluster_1088529493_1260626727") std::cout << " myStep=" << myStep << " nextTry=" << nextTry << "\n";
90  return nextTry;
91 }
92 
93 
97  SUMOTime stayRedUntil = now;
98  // check rail links for approaching foes to determine whether and how long
99  // the crossing must remain closed
100  for (std::vector<MSLink*>::const_iterator it_link = myIncomingRailLinks.begin(); it_link != myIncomingRailLinks.end(); ++it_link) {
101  for (std::map<const SUMOVehicle*, MSLink::ApproachingVehicleInformation>::const_iterator
102  it_avi = (*it_link)->getApproaching().begin();
103  it_avi != (*it_link)->getApproaching().end(); ++it_avi) {
104  const MSLink::ApproachingVehicleInformation& avi = it_avi->second;
105  if (avi.arrivalTime - myYellowTime - now < mySecurityGap) {
106  stayRedUntil = MAX2(stayRedUntil, avi.leavingTime);
107  }
108  }
109  if ((*it_link)->getViaLane() != 0 && (*it_link)->getViaLane()->getVehicleNumberWithPartials() > 0) {
110  // do not open if there is still a train on the crossing
111  stayRedUntil = MAX2(stayRedUntil, now + DELTA_T);
112  }
113  }
114  //if (getID() == "cluster_1088529493_1260626727") std::cout << SIMTIME << " stayRedUntil=" << stayRedUntil;
115  const SUMOTime wait = stayRedUntil - now;
116 
117  if (myStep == 0) {
118  // 'G': check whether the crossing can stay open
119  if (wait == 0) {
120  return DELTA_T;
121  } else {
122  myStep++;
123  return myYellowTime;
124  }
125  } else if (myStep == 1) {
126  // 'y': yellow time is over. switch to red
127  myStep++;
128  return MAX2(DELTA_T, wait);
129  } else {
130  // 'r': check whether we may open again
131  if (wait == 0) {
132  myStep = 0;
133  return myMinGreenTime;
134  } else {
135  return wait;
136  }
137  }
138 }
139 
140 
141 // ------------ Conversion between time and phase
142 SUMOTime
144  return 0;
145 }
146 
147 SUMOTime
149  return 0;
150 }
151 
152 int
154  return 0;
155 }
156 
157 
158 void
159 MSRailCrossing::addLink(MSLink* link, MSLane* lane, int pos) {
160  if (pos >= 0) {
161  MSTrafficLightLogic::addLink(link, lane, pos);
162  } else {
163  myIncomingRailLinks.push_back(link);
164  }
165 }
166 
167 
168 /****************************************************************************/
169 
Builds detectors for microsim.
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:167
T MAX2(T a, T b)
Definition: StdDefs.h:73
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:42
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
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.
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:253
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...
MSRailCrossing(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &subid, const std::map< std::string, std::string > &parameters)
Constructor.
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
long long int SUMOTime
Definition: TraCIDefs.h:51
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:77
The definition of a single phase of a tls logic.
SUMOTime myMinGreenTime
minimum green time