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