SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
53  const std::string& id, const std::string& subid,
54  const std::map<std::string, std::string>& parameters) :
55  MSSimpleTrafficLightLogic(tlcontrol, id, subid, Phases(), 0, DELTA_T, parameters),
56  // XXX make this configurable
57  mySecurityGap(TIME2STEPS(15)),
58  myMinGreenTime(TIME2STEPS(5)),
60  myYellowTime(TIME2STEPS(5)) {
61  // dummy phase, used to avoid crashing in MSTrafficLightLogic::setTrafficLightSignals()
62  myPhases.push_back(new MSPhaseDefinition(1, 1, 1, std::string(SUMO_MAX_CONNECTIONS, 'X')));
63 }
64 
65 void
67  delete myPhases.front();
68  myPhases.clear();
69  myPhases.push_back(new MSPhaseDefinition(1, 1, 1, std::string(myLinks.size(), 'G')));
70  myPhases.push_back(new MSPhaseDefinition(myYellowTime, myYellowTime, myYellowTime, std::string(myLinks.size(), 'y')));
71  myPhases.push_back(new MSPhaseDefinition(1, 1, 1, std::string(myLinks.size(), 'r')));
72  // init phases
74  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
75 }
76 
77 
79 
80 
81 // ----------- Handling of controlled links
82 void
86 }
87 
88 
89 // ------------ Switching and setting current rows
92  SUMOTime nextTry = updateCurrentPhase();
93  setTrafficLightSignals(MSNet::getInstance()->getCurrentTimeStep());
94  //if (getID() == "cluster_1088529493_1260626727") std::cout << " myStep=" << myStep << " nextTry=" << nextTry << "\n";
95  return nextTry;
96 }
97 
98 
102  SUMOTime stayRedUntil = now;
103  // check rail links for approaching foes to determine whether and how long
104  // the crossing must remain closed
105  for (std::vector<MSLink*>::const_iterator it_link = myIncomingRailLinks.begin(); it_link != myIncomingRailLinks.end(); ++it_link) {
106  for (std::map<const SUMOVehicle*, MSLink::ApproachingVehicleInformation>::const_iterator
107  it_avi = (*it_link)->getApproaching().begin();
108  it_avi != (*it_link)->getApproaching().end(); ++it_avi) {
109  const MSLink::ApproachingVehicleInformation& avi = it_avi->second;
110  if (avi.arrivalTime - myYellowTime - now < mySecurityGap) {
111  stayRedUntil = MAX2(stayRedUntil, avi.leavingTime);
112  }
113  }
114 #ifdef HAVE_INTERNAL_LANES
115  if ((*it_link)->getViaLane() != 0 && (*it_link)->getViaLane()->getVehicleNumberWithPartials() > 0) {
116  // do not open if there is still a train on the crossing
117  stayRedUntil = MAX2(stayRedUntil, now + DELTA_T);
118  }
119 #endif
120  }
121  //if (getID() == "cluster_1088529493_1260626727") std::cout << SIMTIME << " stayRedUntil=" << stayRedUntil;
122  const SUMOTime wait = stayRedUntil - now;
123 
124  if (myStep == 0) {
125  // 'G': check whether the crossing can stay open
126  if (wait == 0) {
127  return DELTA_T;
128  } else {
129  myStep++;
130  return myYellowTime;
131  }
132  } else if (myStep == 1) {
133  // 'y': yellow time is over. switch to red
134  myStep++;
135  return MAX2(DELTA_T, wait);
136  } else {
137  // 'r': check whether we may open again
138  if (wait == 0) {
139  myStep = 0;
140  return myMinGreenTime;
141  } else {
142  return wait;
143  }
144  }
145 }
146 
147 
148 // ------------ Conversion between time and phase
149 SUMOTime
151  return 0;
152 }
153 
154 SUMOTime
156  return 0;
157 }
158 
159 int
161  return 0;
162 }
163 
164 
165 void
166 MSRailCrossing::addLink(MSLink* link, MSLane* lane, int pos) {
167  if (pos >= 0) {
168  MSTrafficLightLogic::addLink(link, lane, pos);
169  } else {
170  myIncomingRailLinks.push_back(link);
171  }
172 }
173 
174 
175 /****************************************************************************/
176 
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
Builds detectors for microsim.
long long int SUMOTime
Definition: SUMOTime.h:43
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
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:159
T MAX2(T a, T b)
Definition: StdDefs.h:75
#define SUMO_MAX_CONNECTIONS
the maximum number of connections across an intersection
Definition: StdDefs.h:42
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:254
A fixed traffic light logic.
~MSRailCrossing()
Destructor.
void init(NLDetectorBuilder &nb)
Initialises the rail signal with information about adjacent rail signals.
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 mySecurityGap
minimum time gap between closing the crossing (end of yellow time) and train passing the crossing ...
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.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
The parent class for traffic light logics.
SUMOTime myYellowTime
minimum green time
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