SUMO - Simulation of Urban MObility
MSSimpleTrafficLightLogic.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-2018 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 /****************************************************************************/
19 // A fixed traffic light logic
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #include <config.h>
27 
28 #include <cassert>
29 #include <utility>
30 #include <vector>
31 #include <bitset>
32 #include <sstream>
34 #include <microsim/MSNet.h>
35 #include "MSTLLogicControl.h"
36 #include "MSTrafficLightLogic.h"
38 
39 
40 // ===========================================================================
41 // member method definitions
42 // ===========================================================================
44  const std::string& id, const std::string& programID, const TrafficLightType logicType, const Phases& phases,
45  int step, SUMOTime delay,
46  const std::map<std::string, std::string>& parameters) :
47  MSTrafficLightLogic(tlcontrol, id, programID, logicType, delay, parameters),
48  myPhases(phases),
49  myStep(step) {
50  for (int i = 0; i < (int)myPhases.size(); i++) {
51  myDefaultCycleTime += myPhases[i]->duration;
52  }
53 }
54 
55 
57  deletePhases();
58 }
59 
60 
61 // ------------ Switching and setting current rows
64  // check whether the current duration shall be increased
68  return delay;
69  }
70 
71  // increment the index
72  if (myPhases[myStep]->nextPhase >= 0) {
73  myStep = myPhases[myStep]->nextPhase;
74  } else {
75  myStep++;
76  }
77  // if the last phase was reached ...
78  if (myStep >= (int)myPhases.size()) {
79  // ... set the index to the first phase
80  myStep = 0;
81  }
82  assert((int)myPhases.size() > myStep);
83  //stores the time the phase started
85  // check whether the next duration was overridden
86  if (myOverridingTimes.size() > 0) {
87  SUMOTime nextDuration = myOverridingTimes[0];
88  myOverridingTimes.erase(myOverridingTimes.begin());
89  return nextDuration;
90  }
91  // return offset to the next switch
92  return myPhases[myStep]->duration;
93 }
94 
95 
96 // ------------ Static Information Retrieval
97 int
99  return (int) myPhases.size();
100 }
101 
102 
105  return myPhases;
106 }
107 
108 
111  return myPhases;
112 }
113 
114 
115 const MSPhaseDefinition&
117  assert((int)myPhases.size() > givenStep);
118  return *myPhases[givenStep];
119 }
120 
121 
122 // ------------ Dynamic Information Retrieval
123 int
125  return myStep;
126 }
127 
128 
129 const MSPhaseDefinition&
131  return *myPhases[myStep];
132 }
133 
134 
135 // ------------ Conversion between time and phase
136 SUMOTime
138  SUMOTime position = 0;
139  if (myStep > 0) {
140  for (int i = 0; i < myStep; i++) {
141  position = position + getPhase(i).duration;
142  }
143  }
144  position = position + simStep - getPhase(myStep).myLastSwitch;
145  position = position % myDefaultCycleTime;
146  assert(position <= myDefaultCycleTime);
147  return position;
148 }
149 
150 
151 SUMOTime
153  assert(index < (int)myPhases.size());
154  if (index == 0) {
155  return 0;
156  }
157  SUMOTime pos = 0;
158  for (int i = 0; i < index; i++) {
159  pos += getPhase(i).duration;
160  }
161  return pos;
162 }
163 
164 
165 int
167  offset = offset % myDefaultCycleTime;
168  if (offset == myDefaultCycleTime) {
169  return 0;
170  }
171  SUMOTime testPos = 0;
172  for (int i = 0; i < (int)myPhases.size(); i++) {
173  testPos = testPos + getPhase(i).duration;
174  if (testPos > offset) {
175  return i;
176  }
177  if (testPos == offset) {
178  assert((int)myPhases.size() > (i + 1));
179  return (i + 1);
180  }
181  }
182  return 0;
183 }
184 
185 
186 // ------------ Changing phases and phase durations
187 void
189  SUMOTime simStep, int step, SUMOTime stepDuration) {
191  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
192  if (step != myStep) {
193  myStep = step;
194  setTrafficLightSignals(simStep);
195  tlcontrol.get(getID()).executeOnSwitchActions();
196  }
198  mySwitchCommand, stepDuration + simStep);
199 }
200 
201 
202 void
204  assert(step < (int)phases.size());
205  deletePhases();
206  myPhases = phases;
207  myStep = step;
208 }
209 
210 
211 void
213  for (int i = 0; i < (int)myPhases.size(); i++) {
214  delete myPhases[i];
215  }
216 }
217 
218 
219 /****************************************************************************/
220 
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
long long int SUMOTime
Definition: SUMOTime.h:36
std::vector< SUMOTime > myOverridingTimes
A list of duration overrides.
void setPhases(const Phases &phases, int index)
Replaces the phases and set the phase index.
void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)
Changes the current phase and her duration.
Phases myPhases
The list of phases this logic uses.
int getIndexFromOffset(SUMOTime offset) const
Returns the step (the phasenumber) of a given position of the cycle.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
SUMOTime getOffsetFromIndex(int index) const
Returns the position (start of a phase during a cycle) from of a given step.
SUMOTime getPhaseIndexAtTime(SUMOTime simStep) const
Returns the index of the logic at the given simulation step.
const std::string & getID() const
Returns the id.
Definition: Named.h:78
bool setTrafficLightSignals(SUMOTime t) const
Applies the current signal states to controlled links.
const MSPhaseDefinition & getCurrentPhaseDef() const
Returns the definition of the current phase.
A class that stores and controls tls and switching of their programs.
SUMOTime duration
The duration of the phase.
void deschedule(MSTrafficLightLogic *tlLogic)
Marks this swicth as invalid (if the phase duration has changed, f.e.)
Class realising the switch between the traffic light phases.
SUMOTime myDefaultCycleTime
The cycle time (without changes)
SUMOTime myLastSwitch
Stores the timestep of the last on-switched of the phase.
int getCurrentPhaseIndex() const
Returns the current index within the program.
MSEventControl * getBeginOfTimestepEvents()
Returns the event control for events executed at the begin of a time step.
Definition: MSNet.h:409
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
virtual SUMOTime trySwitch()
Switches to the next phase.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:263
void deletePhases()
frees memory responsibilities
std::vector< MSPhaseDefinition * > Phases
Definition of a list of phases, being the junction logic.
int getPhaseNumber() const
Returns the number of phases.
SwitchCommand * mySwitchCommand
The current switch command.
The parent class for traffic light logics.
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
const Phases & getPhases() const
Returns the phases of this tls program.
The definition of a single phase of a tls logic.
MSSimpleTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &programID, const TrafficLightType logicType, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.
TrafficLightType