Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSRailCrossing.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2001-2024 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
104
105
106// ------------ Switching and setting current rows
109 const int oldStep = myStep;
110 SUMOTime nextTry = updateCurrentPhase();
111 //if (getID() == "cluster_1088529493_1260626727") std::cout << " myStep=" << myStep << " nextTry=" << nextTry << "\n";
112 if (myStep != oldStep) {
114 }
115 return nextTry;
116}
117
118
122 SUMOTime stayRedUntil = now;
123 // check rail links for approaching foes to determine whether and how long
124 // the crossing must remain closed
125 for (const MSLink* const link : myIncomingRailLinks) {
126 for (const auto& it_avi : link->getApproaching()) {
127 const MSLink::ApproachingVehicleInformation& avi = it_avi.second;
128 if (avi.arrivalTime - myYellowTime - now < myTimeGap) {
129 stayRedUntil = MAX2(stayRedUntil, avi.leavingTime + myOpeningDelay);
130 }
131 if (mySpaceGap >= 0 && avi.dist < mySpaceGap) {
132 // TODO maybe check the incoming lanes because stopped vehicles do not register at the oncoming junction
133 stayRedUntil = MAX2(stayRedUntil, avi.leavingTime + myOpeningDelay);
134 }
135 }
136 if (link->getViaLane() != nullptr && link->getViaLane()->getVehicleNumberWithPartials() > 0) {
137 // do not open if there is still a train on the crossing
138 stayRedUntil = MAX2(stayRedUntil, now + DELTA_T + myOpeningDelay);
139 }
140 }
141 //if (getID() == "cluster_1088529493_1260626727") std::cout << SIMTIME << " stayRedUntil=" << stayRedUntil;
142 const SUMOTime wait = stayRedUntil - now;
143
144 if (myStep == 0) {
145 // 'G': check whether the crossing can stay open
146 if (wait == 0) {
147 return DELTA_T;
148 } else {
149 myStep++;
150 return myYellowTime;
151 }
152 } else if (myStep == 1) {
153 // 'y': yellow time is over. switch to red
154 myStep++;
155 return MAX2(DELTA_T, wait);
156 } else if (myStep == 2) {
157 // 'r': check whether we may open again
158 if (wait == 0) {
159 myStep++;
160 return myOpeningTime;
161 } else {
162 return wait;
163 }
164 } else { // (myStep == 3)
165 // 'u': opening time is over, switch to green
166 if (wait == 0) {
167 myStep = 0;
168 return myMinGreenTime;
169 } else {
170 // train approached during opening sequence, close again
171 myStep = 2;
172 return wait;
173 }
174 }
175}
176
177
178// ------------ Conversion between time and phase
183
186 return 0;
187}
188
189int
193
194
195void
196MSRailCrossing::addLink(MSLink* link, MSLane* lane, int pos) {
197 if (pos >= 0) {
198 MSTrafficLightLogic::addLink(link, lane, pos);
199 } else {
200 myIncomingRailLinks.push_back(link);
201 }
202}
203
204
205/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
SUMOTime DELTA_T
Definition SUMOTime.cpp:38
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition SUMOTime.cpp:46
@ 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:82
#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:185
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition MSNet.h:320
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
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