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& subid, const Phases& phases,
45  int step, SUMOTime delay,
46  const std::map<std::string, std::string>& parameters) :
47  MSTrafficLightLogic(tlcontrol, id, subid, 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  myStep++;
73  // if the last phase was reached ...
74  if (myStep >= (int)myPhases.size()) {
75  // ... set the index to the first phase
76  myStep = 0;
77  }
78  assert((int)myPhases.size() > myStep);
79  //stores the time the phase started
81  // check whether the next duration was overridden
82  if (myOverridingTimes.size() > 0) {
83  SUMOTime nextDuration = myOverridingTimes[0];
84  myOverridingTimes.erase(myOverridingTimes.begin());
85  return nextDuration;
86  }
87  // return offset to the next switch
88  return myPhases[myStep]->duration;
89 }
90 
91 
92 // ------------ Static Information Retrieval
93 int
95  return (int) myPhases.size();
96 }
97 
98 
101  return myPhases;
102 }
103 
104 
107  return myPhases;
108 }
109 
110 
111 const MSPhaseDefinition&
113  assert((int)myPhases.size() > givenStep);
114  return *myPhases[givenStep];
115 }
116 
117 
118 // ------------ Dynamic Information Retrieval
119 int
121  return myStep;
122 }
123 
124 
125 const MSPhaseDefinition&
127  return *myPhases[myStep];
128 }
129 
130 
131 // ------------ Conversion between time and phase
132 SUMOTime
134  SUMOTime position = 0;
135  if (myStep > 0) {
136  for (int i = 0; i < myStep; i++) {
137  position = position + getPhase(i).duration;
138  }
139  }
140  position = position + simStep - getPhase(myStep).myLastSwitch;
141  position = position % myDefaultCycleTime;
142  assert(position <= myDefaultCycleTime);
143  return position;
144 }
145 
146 
147 SUMOTime
149  assert(index < (int)myPhases.size());
150  if (index == 0) {
151  return 0;
152  }
153  SUMOTime pos = 0;
154  for (int i = 0; i < index; i++) {
155  pos += getPhase(i).duration;
156  }
157  return pos;
158 }
159 
160 
161 int
163  offset = offset % myDefaultCycleTime;
164  if (offset == myDefaultCycleTime) {
165  return 0;
166  }
167  SUMOTime testPos = 0;
168  for (int i = 0; i < (int)myPhases.size(); i++) {
169  testPos = testPos + getPhase(i).duration;
170  if (testPos > offset) {
171  return i;
172  }
173  if (testPos == offset) {
174  assert((int)myPhases.size() > (i + 1));
175  return (i + 1);
176  }
177  }
178  return 0;
179 }
180 
181 
182 // ------------ Changing phases and phase durations
183 void
185  SUMOTime simStep, int step, SUMOTime stepDuration) {
187  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
188  if (step != myStep) {
189  myStep = step;
190  setTrafficLightSignals(simStep);
191  tlcontrol.get(getID()).executeOnSwitchActions();
192  }
194  mySwitchCommand, stepDuration + simStep);
195 }
196 
197 
198 void
200  assert(step < (int)phases.size());
201  deletePhases();
202  myPhases = phases;
203  myStep = step;
204 }
205 
206 
207 void
209  for (int i = 0; i < (int)myPhases.size(); i++) {
210  delete myPhases[i];
211  }
212 }
213 
214 
215 /****************************************************************************/
216 
SUMOTime myCurrentDurationIncrement
A value for enlarge the current duration.
long long int SUMOTime
Definition: SUMOTime.h:36
MSSimpleTrafficLightLogic(MSTLLogicControl &tlcontrol, const std::string &id, const std::string &subid, const Phases &phases, int step, SUMOTime delay, const std::map< std::string, std::string > &parameters)
Constructor.
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:160
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:69
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:402
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:256
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.
const MSPhaseDefinition & getPhase(int givenstep) const
Returns the definition of the phase from the given position within the plan.