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 
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:415
MSTLLogicControl.h
MSNet.h
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
DELTA_T
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
MSTrafficLightLogic::Phases
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
Definition: MSTrafficLightLogic.h:62
SUMOTime
long long int SUMOTime
Definition: SUMOTime.h:35
MSRailCrossing::~MSRailCrossing
~MSRailCrossing()
Destructor.
Definition: MSRailCrossing.cpp:67
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:80
MSTrafficLightLogic.h
MSRailCrossing::getIndexFromOffset
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
Definition: MSRailCrossing.cpp:145
TIME2STEPS
#define TIME2STEPS(x)
Definition: SUMOTime.h:59
MSRailCrossing::init
void init(NLDetectorBuilder &nb)
Initialises the rail signal with information about adjacent rail signals.
Definition: MSRailCrossing.cpp:55
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:140
MSNet::getCurrentTimeStep
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:284
MSRailCrossing.h
MSRailCrossing::getPhaseIndexAtTime
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
Definition: MSRailCrossing.cpp:135
MSSimpleTrafficLightLogic
A fixed traffic light logic.
Definition: MSSimpleTrafficLightLogic.h:55
MSRailCrossing::trySwitch
SUMOTime trySwitch()
Switches to the next phase.
Definition: MSRailCrossing.cpp:80
MSRailCrossing::MSRailCrossing
MSRailCrossing(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const std::map< std::string, std::string > &parameters)
Constructor.
Definition: MSRailCrossing.cpp:41
MSTrafficLightLogic
The parent class for traffic light logics.
Definition: MSTrafficLightLogic.h:56
MSRailCrossing::myIncomingRailLinks
std::vector< MSLink * > myIncomingRailLinks
The incoming rail links.
Definition: MSRailCrossing.h:144
MSRailCrossing::myMinGreenTime
SUMOTime myMinGreenTime
minimum green time
Definition: MSRailCrossing.h:153
TLTYPE_RAIL_CROSSING
Definition: SUMOXMLDefinitions.h:1195
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
SUMO_MAX_CONNECTIONS
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:43
MSRailCrossing::adaptLinkInformationFrom
void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
Definition: MSRailCrossing.cpp:72
MSSimpleTrafficLightLogic::myPhases
Phases myPhases
The list of phases this logic uses.
Definition: MSSimpleTrafficLightLogic.h:199
MSPhaseDefinition.h
MSRailCrossing::addLink
void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
Definition: MSRailCrossing.cpp:151
config.h
MSTLLogicControl
A class that stores and controls tls and switching of their programs.
Definition: MSTLLogicControl.h:60
MSEventControl.h
MSLane.h
MSPhaseDefinition
The definition of a single phase of a tls logic.
Definition: MSPhaseDefinition.h:52
MSSimpleTrafficLightLogic::myStep
int myStep
The current step.
Definition: MSSimpleTrafficLightLogic.h:202
MSRailCrossing::mySecurityGap
SUMOTime mySecurityGap
minimum time gap between closing the crossing (end of yellow time) and train passing the crossing
Definition: MSRailCrossing.h:150
MSRailCrossing::myYellowTime
SUMOTime myYellowTime
minimum green time
Definition: MSRailCrossing.h:156
MSRailCrossing::updateCurrentPhase
SUMOTime updateCurrentPhase()
updates the current phase of the signal
Definition: MSRailCrossing.cpp:88
NLDetectorBuilder
Builds detectors for microsim.
Definition: NLDetectorBuilder.h:56