SUMO - Simulation of Urban MObility
MSRailCrossing.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // A rail signal logic
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <cassert>
32 #include <utility>
33 #include <vector>
34 #include <bitset>
36 #include <microsim/MSNet.h>
37 #include <microsim/MSEdge.h>
38 #include "MSTrafficLightLogic.h"
39 #include "MSRailCrossing.h"
40 #include <microsim/MSLane.h>
41 #include "MSPhaseDefinition.h"
42 #include "MSTLLogicControl.h"
43 
44 
45 // ===========================================================================
46 // method definitions
47 // ===========================================================================
49  const std::string& id, const std::string& subid,
50  const std::map<std::string, std::string>& parameters) :
51  MSSimpleTrafficLightLogic(tlcontrol, id, subid, Phases(), 0, DELTA_T, parameters),
52  // XXX make this configurable
53  mySecurityGap(TIME2STEPS(15)),
54  myMinGreenTime(TIME2STEPS(5)),
56  myYellowTime(TIME2STEPS(5)) {
57  // dummy phase, used to avoid crashing in MSTrafficLightLogic::setTrafficLightSignals()
58  myPhases.push_back(new MSPhaseDefinition(1, 1, 1, std::string(SUMO_MAX_CONNECTIONS, 'X')));
59 }
60 
61 void
63  delete myPhases.front();
64  myPhases.clear();
65  myPhases.push_back(new MSPhaseDefinition(1, 1, 1, std::string(myLinks.size(), 'G')));
66  myPhases.push_back(new MSPhaseDefinition(myYellowTime, myYellowTime, myYellowTime, std::string(myLinks.size(), 'y')));
67  myPhases.push_back(new MSPhaseDefinition(1, 1, 1, std::string(myLinks.size(), 'r')));
68  // init phases
70  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
71 }
72 
73 
75 
76 
77 // ----------- Handling of controlled links
78 void
82 }
83 
84 
85 // ------------ Switching and setting current rows
88  SUMOTime nextTry = updateCurrentPhase();
89  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
90  //if (getID() == "cluster_1088529493_1260626727") std::cout << " myStep=" << myStep << " nextTry=" << nextTry << "\n";
91  return nextTry;
92 }
93 
94 
98  SUMOTime stayRedUntil = now;
99  // check rail links for approaching foes to determine whether and how long
100  // the crossing must remain closed
101  for (std::vector<MSLink*>::const_iterator it_link = myIncomingRailLinks.begin(); it_link != myIncomingRailLinks.end(); ++it_link) {
102  for (std::map<const SUMOVehicle*, MSLink::ApproachingVehicleInformation>::const_iterator
103  it_avi = (*it_link)->getApproaching().begin();
104  it_avi != (*it_link)->getApproaching().end(); ++it_avi) {
105  const MSLink::ApproachingVehicleInformation& avi = it_avi->second;
106  if (avi.arrivalTime - myYellowTime - now < mySecurityGap) {
107  stayRedUntil = MAX2(stayRedUntil, avi.leavingTime);
108  }
109  }
110  if ((*it_link)->getViaLane() != 0 && (*it_link)->getViaLane()->getVehicleNumberWithPartials() > 0) {
111  // do not open if there is still a train on the crossing
112  stayRedUntil = MAX2(stayRedUntil, now + DELTA_T);
113  }
114  }
115  //if (getID() == "cluster_1088529493_1260626727") std::cout << SIMTIME << " stayRedUntil=" << stayRedUntil;
116  const SUMOTime wait = stayRedUntil - now;
117 
118  if (myStep == 0) {
119  // 'G': check whether the crossing can stay open
120  if (wait == 0) {
121  return DELTA_T;
122  } else {
123  myStep++;
124  return myYellowTime;
125  }
126  } else if (myStep == 1) {
127  // 'y': yellow time is over. switch to red
128  myStep++;
129  return MAX2(DELTA_T, wait);
130  } else {
131  // 'r': check whether we may open again
132  if (wait == 0) {
133  myStep = 0;
134  return myMinGreenTime;
135  } else {
136  return wait;
137  }
138  }
139 }
140 
141 
142 // ------------ Conversion between time and phase
143 SUMOTime
145  return 0;
146 }
147 
148 SUMOTime
150  return 0;
151 }
152 
153 int
155  return 0;
156 }
157 
158 
159 void
160 MSRailCrossing::addLink(MSLink* link, MSLane* lane, int pos) {
161  if (pos >= 0) {
162  MSTrafficLightLogic::addLink(link, lane, pos);
163  } else {
164  myIncomingRailLinks.push_back(link);
165  }
166 }
167 
168 
169 /****************************************************************************/
170 
Builds detectors for microsim.
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:158
T MAX2(T a, T b)
Definition: StdDefs.h:70
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:41
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
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:257
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
long long int SUMOTime
Definition: TraCIDefs.h:52
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:79
The definition of a single phase of a tls logic.
SUMOTime myMinGreenTime
minimum green time