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& subid,
43  const std::map<std::string, std::string>& parameters) :
44  MSSimpleTrafficLightLogic(tlcontrol, id, subid, 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, 1, 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, 1, 1, std::string(myLinks.size(), 'G')));
59  myPhases.push_back(new MSPhaseDefinition(myYellowTime, myYellowTime, myYellowTime, std::string(myLinks.size(), 'y')));
60  myPhases.push_back(new MSPhaseDefinition(1, 1, 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  for (std::map<const SUMOVehicle*, MSLink::ApproachingVehicleInformation>::const_iterator
96  it_avi = (*it_link)->getApproaching().begin();
97  it_avi != (*it_link)->getApproaching().end(); ++it_avi) {
98  const MSLink::ApproachingVehicleInformation& avi = it_avi->second;
99  if (avi.arrivalTime - myYellowTime - now < mySecurityGap) {
100  stayRedUntil = MAX2(stayRedUntil, avi.leavingTime);
101  }
102  }
103  if ((*it_link)->getViaLane() != 0 && (*it_link)->getViaLane()->getVehicleNumberWithPartials() > 0) {
104  // do not open if there is still a train on the crossing
105  stayRedUntil = MAX2(stayRedUntil, now + DELTA_T);
106  }
107  }
108  //if (getID() == "cluster_1088529493_1260626727") std::cout << SIMTIME << " stayRedUntil=" << stayRedUntil;
109  const SUMOTime wait = stayRedUntil - now;
110 
111  if (myStep == 0) {
112  // 'G': check whether the crossing can stay open
113  if (wait == 0) {
114  return DELTA_T;
115  } else {
116  myStep++;
117  return myYellowTime;
118  }
119  } else if (myStep == 1) {
120  // 'y': yellow time is over. switch to red
121  myStep++;
122  return MAX2(DELTA_T, wait);
123  } else {
124  // 'r': check whether we may open again
125  if (wait == 0) {
126  myStep = 0;
127  return myMinGreenTime;
128  } else {
129  return wait;
130  }
131  }
132 }
133 
134 
135 // ------------ Conversion between time and phase
136 SUMOTime
138  return 0;
139 }
140 
141 SUMOTime
143  return 0;
144 }
145 
146 int
148  return 0;
149 }
150 
151 
152 void
153 MSRailCrossing::addLink(MSLink* link, MSLane* lane, int pos) {
154  if (pos >= 0) {
155  MSTrafficLightLogic::addLink(link, lane, pos);
156  } else {
157  myIncomingRailLinks.push_back(link);
158  }
159 }
160 
161 
162 /****************************************************************************/
163 
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:160
T MAX2(T a, T b)
Definition: StdDefs.h:68
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:36
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.
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:256
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
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:71
The definition of a single phase of a tls logic.
SUMOTime myMinGreenTime
minimum green time