SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSSimpleTrafficLightLogic.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // A fixed traffic light logic
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
14 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include <utility>
37 #include <vector>
38 #include <bitset>
39 #include <sstream>
41 #include "MSTrafficLightLogic.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // member method definitions
51 // ===========================================================================
53  const std::string& id, const std::string& subid, const Phases& phases,
54  unsigned int step, SUMOTime delay)
55  : MSTrafficLightLogic(tlcontrol, id, subid, delay), myPhases(phases),
56  myStep(step) {
57  for (size_t i = 0; i < myPhases.size(); i++) {
58  myDefaultCycleTime += myPhases[i]->duration;
59  }
60 }
61 
62 
64  deletePhases();
65 }
66 
67 
68 // ------------ Switching and setting current rows
71  // check whether the current duration shall be increased
75  return delay;
76  }
77 
78  // increment the index
79  myStep++;
80  // if the last phase was reached ...
81  if (myStep >= myPhases.size()) {
82  // ... set the index to the first phase
83  myStep = 0;
84  }
85  assert(myPhases.size() > myStep);
86  //stores the time the phase started
88  // check whether the next duration was overridden
89  if (myOverridingTimes.size() > 0) {
90  SUMOTime nextDuration = myOverridingTimes[0];
91  myOverridingTimes.erase(myOverridingTimes.begin());
92  return nextDuration;
93  }
94  // return offset to the next switch
95  return myPhases[myStep]->duration;
96 }
97 
98 
99 // ------------ Static Information Retrieval
100 unsigned int
102  return (unsigned int) myPhases.size();
103 }
104 
105 
108  return myPhases;
109 }
110 
111 
114  return myPhases;
115 }
116 
117 
118 const MSPhaseDefinition&
119 MSSimpleTrafficLightLogic::getPhase(unsigned int givenStep) const {
120  assert(myPhases.size() > givenStep);
121  return *myPhases[givenStep];
122 }
123 
124 
125 // ------------ Dynamic Information Retrieval
126 unsigned int
128  return myStep;
129 }
130 
131 
132 const MSPhaseDefinition&
134  return *myPhases[myStep];
135 }
136 
137 
138 // ------------ Conversion between time and phase
139 SUMOTime
141  SUMOTime position = 0;
142  if (myStep > 0) {
143  for (unsigned int i = 0; i < myStep; i++) {
144  position = position + getPhase(i).duration;
145  }
146  }
147  position = position + simStep - getPhase(myStep).myLastSwitch;
148  position = position % myDefaultCycleTime;
149  assert(position <= myDefaultCycleTime);
150  return position;
151 }
152 
153 
154 SUMOTime
156  assert(index < myPhases.size());
157  if (index == 0) {
158  return 0;
159  }
160  unsigned int pos = 0;
161  for (unsigned int i = 0; i < index; i++) {
162  pos += getPhase(i).duration;
163  }
164  return pos;
165 }
166 
167 
168 unsigned int
170  assert(offset <= myDefaultCycleTime);
171  if (offset == myDefaultCycleTime) {
172  return 0;
173  }
174  SUMOTime testPos = 0;
175  for (unsigned int i = 0; i < myPhases.size(); i++) {
176  testPos = testPos + getPhase(i).duration;
177  if (testPos > offset) {
178  return i;
179  }
180  if (testPos == offset) {
181  assert(myPhases.size() > (i + 1));
182  return (i + 1);
183  }
184  }
185  return 0;
186 }
187 
188 
189 // ------------ Changing phases and phase durations
190 void
192  SUMOTime simStep, unsigned int step, SUMOTime stepDuration) {
194  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
195  myStep = step;
197  mySwitchCommand, stepDuration + simStep,
199 }
200 
201 
202 void
203 MSSimpleTrafficLightLogic::setPhases(const Phases& phases, unsigned int step) {
204  assert(step < phases.size());
205  deletePhases();
206  myPhases = phases;
207  myStep = step;
208 }
209 
210 
211 void
213  for (size_t i = 0; i < myPhases.size(); i++) {
214  delete myPhases[i];
215  }
216 }
217 
218 
219 /****************************************************************************/
220