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-2022 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
19// A rail signal logic
20/****************************************************************************/
21#include <config.h>
22
23#include <cassert>
24#include <utility>
25#include <vector>
26#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, SUMOTime delay,
43 const Parameterised::Map& parameters) :
44 MSSimpleTrafficLightLogic(tlcontrol, id, programID, 0, TrafficLightType::RAIL_CROSSING, Phases(), 0, delay, parameters) {
45 // dummy phase, used to avoid crashing in MSTrafficLightLogic::setTrafficLightSignals()
46 myPhases.push_back(new MSPhaseDefinition(1, std::string(SUMO_MAX_CONNECTIONS, 'X')));
48}
49
50
52
53
54void
57 myTimeGap = string2time(getParameter("time-gap", "15"));
58 //use time-gap by default
59 mySpaceGap = StringUtils::toDouble(getParameter("space-gap", "-1"));
60 myMinGreenTime = string2time(getParameter("min-green", "5"));
61 myOpeningDelay = string2time(getParameter("opening-delay", "3"));
62 myOpeningTime = string2time(getParameter("opening-time", "3")); // red-yellow while opening
64 myYellowTime = string2time(getParameter("yellow-time", "5"));
65 delete myPhases.front();
66 myPhases.clear();
67 myPhases.push_back(new MSPhaseDefinition(1, std::string(myLinks.size(), LINKSTATE_TL_GREEN_MAJOR)));
68 myPhases.push_back(new MSPhaseDefinition(myYellowTime, std::string(myLinks.size(), LINKSTATE_TL_YELLOW_MINOR)));
69 myPhases.push_back(new MSPhaseDefinition(1, std::string(myLinks.size(), LINKSTATE_TL_RED)));
70 myPhases.push_back(new MSPhaseDefinition(myOpeningTime, std::string(myLinks.size(), LINKSTATE_TL_REDYELLOW)));
71 // init phases
73 setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
74 myNumLinks = (int)myLinks.size();
75}
76
77
78void
79MSRailCrossing::setParameter(const std::string& key, const std::string& value) {
80 // some pre-defined parameters can be updated at runtime
81 if (key == "time-gap") {
82 myTimeGap = string2time(value);
83 } else if (key == "space-gap") {
85 } else if (key == "min-green") {
87 } else if (key == "opening-delay") {
89 } else if (key == "opening-time") {
90 myOpeningTime = string2time(value); // TODO update phases
91 } else if (key == "yellow-time") {
92 myYellowTime = string2time(value); // TODO update phases
93 }
95}
96
97
98// ----------- Handling of controlled links
99void
103}
104
105
106// ------------ Switching and setting current rows
109 SUMOTime nextTry = updateCurrentPhase();
110 //if (getID() == "cluster_1088529493_1260626727") std::cout << " myStep=" << myStep << " nextTry=" << nextTry << "\n";
111 return nextTry;
112}
113
114
118 SUMOTime stayRedUntil = now;
119 // check rail links for approaching foes to determine whether and how long
120 // the crossing must remain closed
121 for (const MSLink* const link : myIncomingRailLinks) {
122 for (const auto& it_avi : link->getApproaching()) {
123 const MSLink::ApproachingVehicleInformation& avi = it_avi.second;
124 if (avi.arrivalTime - myYellowTime - now < myTimeGap) {
125 stayRedUntil = MAX2(stayRedUntil, avi.leavingTime + myOpeningDelay);
126 }
127 if (mySpaceGap >= 0 && avi.dist < mySpaceGap) {
128 // TODO maybe check the incoming lanes because stopped vehicles do not register at the oncoming junction
129 stayRedUntil = MAX2(stayRedUntil, avi.leavingTime + myOpeningDelay);
130 }
131 }
132 if (link->getViaLane() != nullptr && link->getViaLane()->getVehicleNumberWithPartials() > 0) {
133 // do not open if there is still a train on the crossing
134 stayRedUntil = MAX2(stayRedUntil, now + DELTA_T + myOpeningDelay);
135 }
136 }
137 //if (getID() == "cluster_1088529493_1260626727") std::cout << SIMTIME << " stayRedUntil=" << stayRedUntil;
138 const SUMOTime wait = stayRedUntil - now;
139
140 if (myStep == 0) {
141 // 'G': check whether the crossing can stay open
142 if (wait == 0) {
143 return DELTA_T;
144 } else {
145 myStep++;
146 return myYellowTime;
147 }
148 } else if (myStep == 1) {
149 // 'y': yellow time is over. switch to red
150 myStep++;
151 return MAX2(DELTA_T, wait);
152 } else if (myStep == 2) {
153 // 'r': check whether we may open again
154 if (wait == 0) {
155 myStep++;
156 return myOpeningTime;
157 } else {
158 return wait;
159 }
160 } else { // (myStep == 3)
161 // 'u': opening time is over, switch to green
162 if (wait == 0) {
163 myStep = 0;
164 return myMinGreenTime;
165 } else {
166 // train approached during opening sequence, close again
167 myStep = 2;
168 return wait;
169 }
170 }
171}
172
173
174// ------------ Conversion between time and phase
177 return 0;
178}
179
182 return 0;
183}
184
185int
187 return 0;
188}
189
190
191void
192MSRailCrossing::addLink(MSLink* link, MSLane* lane, int pos) {
193 if (pos >= 0) {
194 MSTrafficLightLogic::addLink(link, lane, pos);
195 } else {
196 myIncomingRailLinks.push_back(link);
197 }
198}
199
200
201/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
TrafficLightType
@ LINKSTATE_TL_REDYELLOW
The link has red light (must brake) but indicates upcoming green.
@ LINKSTATE_TL_GREEN_MAJOR
The link has green light, may pass.
@ LINKSTATE_TL_YELLOW_MINOR
The link has yellow light, has to brake anyway.
@ LINKSTATE_TL_RED
The link has red light (must brake)
T MAX2(T a, T b)
Definition: StdDefs.h:77
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:41
Representation of a lane in the micro simulation.
Definition: MSLane.h:84
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:321
The definition of a single phase of a tls logic.
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.
void setParameter(const std::string &key, const std::string &value)
Sets a parameter and updates internal constants.
SUMOTime myOpeningDelay
red time after the train has left
SUMOTime myYellowTime
yellow time
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
~MSRailCrossing()
Destructor.
MSRailCrossing(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, SUMOTime delay, const Parameterised::Map &parameters)
Constructor.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
SUMOTime trySwitch()
Switches to the next phase.
double mySpaceGap
minimum distance between the train and the crossing which triggers closing (-1 means time only)
void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
SUMOTime myTimeGap
minimum time gap between closing the crossing (end of yellow time) and train passing the crossing
SUMOTime updateCurrentPhase()
updates the current phase of the signal
std::vector< MSLink * > myIncomingRailLinks
The incoming rail links.
void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
SUMOTime myOpeningTime
red-yellow time after the delay while opening
SUMOTime myMinGreenTime
minimum green time
A fixed traffic light logic.
Phases myPhases
The list of phases this logic uses.
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const override
gets a parameter
A class that stores and controls tls and switching of their programs.
The parent class for traffic light logics.
virtual void adaptLinkInformationFrom(const MSTrafficLightLogic &logic)
Applies information about controlled links and lanes from the given logic.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
int myNumLinks
number of controlled links
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
virtual void addLink(MSLink *link, MSLane *lane, int pos)
Adds a link on building.
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.
Builds detectors for microsim.
std::map< std::string, std::string > Map
parameters map
Definition: Parameterised.h:45
const Parameterised::Map & getParametersMap() const
Returns the inner key/value map.
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter