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-sim.org/
14 // Copyright (C) 2001-2013 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 "MSTLLogicControl.h"
42 #include "MSTrafficLightLogic.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // member method definitions
52 // ===========================================================================
54  const std::string& id, const std::string& subid, const Phases& phases,
55  unsigned int step, SUMOTime delay,
56  const std::map<std::string, std::string>& parameters) :
57  MSTrafficLightLogic(tlcontrol, id, subid, delay, parameters),
58  myPhases(phases),
59  myStep(step) {
60  for (size_t i = 0; i < myPhases.size(); i++) {
61  myDefaultCycleTime += myPhases[i]->duration;
62  }
63 }
64 
65 
67  deletePhases();
68 }
69 
70 
71 // ------------ Switching and setting current rows
74  // check whether the current duration shall be increased
78  return delay;
79  }
80 
81  // increment the index
82  myStep++;
83  // if the last phase was reached ...
84  if (myStep >= myPhases.size()) {
85  // ... set the index to the first phase
86  myStep = 0;
87  }
88  assert(myPhases.size() > myStep);
89  //stores the time the phase started
91  // check whether the next duration was overridden
92  if (myOverridingTimes.size() > 0) {
93  SUMOTime nextDuration = myOverridingTimes[0];
94  myOverridingTimes.erase(myOverridingTimes.begin());
95  return nextDuration;
96  }
97  // return offset to the next switch
98  return myPhases[myStep]->duration;
99 }
100 
101 
102 // ------------ Static Information Retrieval
103 unsigned int
105  return (unsigned int) myPhases.size();
106 }
107 
108 
111  return myPhases;
112 }
113 
114 
117  return myPhases;
118 }
119 
120 
121 const MSPhaseDefinition&
122 MSSimpleTrafficLightLogic::getPhase(unsigned int givenStep) const {
123  assert(myPhases.size() > givenStep);
124  return *myPhases[givenStep];
125 }
126 
127 
128 // ------------ Dynamic Information Retrieval
129 unsigned int
131  return myStep;
132 }
133 
134 
135 const MSPhaseDefinition&
137  return *myPhases[myStep];
138 }
139 
140 
141 // ------------ Conversion between time and phase
142 SUMOTime
144  SUMOTime position = 0;
145  if (myStep > 0) {
146  for (unsigned int i = 0; i < myStep; i++) {
147  position = position + getPhase(i).duration;
148  }
149  }
150  position = position + simStep - getPhase(myStep).myLastSwitch;
151  position = position % myDefaultCycleTime;
152  assert(position <= myDefaultCycleTime);
153  return position;
154 }
155 
156 
157 SUMOTime
159  assert(index < myPhases.size());
160  if (index == 0) {
161  return 0;
162  }
163  unsigned int pos = 0;
164  for (unsigned int i = 0; i < index; i++) {
165  pos += getPhase(i).duration;
166  }
167  return pos;
168 }
169 
170 
171 unsigned int
173  assert(offset <= myDefaultCycleTime);
174  if (offset == myDefaultCycleTime) {
175  return 0;
176  }
177  SUMOTime testPos = 0;
178  for (unsigned int i = 0; i < myPhases.size(); i++) {
179  testPos = testPos + getPhase(i).duration;
180  if (testPos > offset) {
181  return i;
182  }
183  if (testPos == offset) {
184  assert(myPhases.size() > (i + 1));
185  return (i + 1);
186  }
187  }
188  return 0;
189 }
190 
191 
192 // ------------ Changing phases and phase durations
193 void
195  SUMOTime simStep, unsigned int step, SUMOTime stepDuration) {
197  mySwitchCommand = new SwitchCommand(tlcontrol, this, stepDuration + simStep);
198  if (step != myStep) {
199  myStep = step;
200  setTrafficLightSignals(simStep);
201  tlcontrol.get(getID()).executeOnSwitchActions();
202  }
204  mySwitchCommand, stepDuration + simStep,
206 }
207 
208 
209 void
210 MSSimpleTrafficLightLogic::setPhases(const Phases& phases, unsigned int step) {
211  assert(step < phases.size());
212  deletePhases();
213  myPhases = phases;
214  myStep = step;
215 }
216 
217 
218 void
220  for (size_t i = 0; i < myPhases.size(); i++) {
221  delete myPhases[i];
222  }
223 }
224 
225 
226 /****************************************************************************/
227