SUMO - Simulation of Urban MObility
TrafficLight.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2017-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // C++ TraCI client API implementation
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <microsim/MSLane.h>
34 #include <microsim/MSNet.h>
37 #include "TrafficLight.h"
38 
39 
40 // ===========================================================================
41 // member definitions
42 // ===========================================================================
43 namespace libsumo {
44 std::vector<std::string>
47 }
48 
49 
50 int
52  return (int)getIDList().size();
53 }
54 
55 
56 std::string
57 TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
58  return getTLS(tlsID).getActive()->getCurrentPhaseDef().getState();
59 }
60 
61 
62 std::vector<TraCILogic>
64  std::vector<TraCILogic> result;
65  const std::vector<MSTrafficLightLogic*> logics = getTLS(tlsID).getAllLogics();
66  for (MSTrafficLightLogic* logic : logics) {
67  TraCILogic l(logic->getProgramID(), 0, logic->getCurrentPhaseIndex());
68  l.subParameter = logic->getMap();
69  for (int j = 0; j < logic->getPhaseNumber(); ++j) {
70  MSPhaseDefinition phase = logic->getPhase(j);
71  l.phases.emplace_back(TraCIPhase(phase.duration, phase.minDuration, phase.maxDuration, phase.getState()));
72  }
73  result.emplace_back(l);
74  }
75  return result;
76 }
77 
78 
79 std::vector<std::string>
80 TrafficLight::getControlledJunctions(const std::string& tlsID) {
81  std::set<std::string> junctionIDs;
83  for (const MSTrafficLightLogic::LinkVector& llinks : links) {
84  for (const MSLink* l : llinks) {
85  junctionIDs.insert(l->getJunction()->getID());
86  }
87  }
88  return std::vector<std::string>(junctionIDs.begin(), junctionIDs.end());
89 }
90 
91 
92 std::vector<std::string>
93 TrafficLight::getControlledLanes(const std::string& tlsID) {
94  std::vector<std::string> laneIDs;
96  for (const MSTrafficLightLogic::LaneVector& llanes : lanes) {
97  for (const MSLane* l : llanes) {
98  laneIDs.push_back(l->getID());
99  }
100  }
101  return laneIDs;
102 }
103 
104 
105 std::vector<std::vector<TraCILink> >
106 TrafficLight::getControlledLinks(const std::string& tlsID) {
107  std::vector<std::vector<TraCILink> > result;
110  for (int i = 0; i < (int)lanes.size(); ++i) {
111  std::vector<TraCILink> subList;
112  const MSTrafficLightLogic::LaneVector& llanes = lanes[i];
113  const MSTrafficLightLogic::LinkVector& llinks = links[i];
114  // number of links controlled by this signal (signal i)
115  for (int j = 0; j < (int)llanes.size(); ++j) {
116  MSLink* link = llinks[j];
117  // approached non-internal lane (if any)
118  const std::string to = link->getLane() != 0 ? link->getLane()->getID() : "";
119  // approached "via", internal lane (if any)
120  const std::string via = link->getViaLane() != 0 ? link->getViaLane()->getID() : "";
121  subList.emplace_back(TraCILink(llanes[j]->getID(), via, to));
122  }
123  result.emplace_back(subList);
124  }
125  return result;
126 }
127 
128 
129 std::string
130 TrafficLight::getProgram(const std::string& tlsID) {
131  return getTLS(tlsID).getActive()->getProgramID();
132 }
133 
134 
135 int
136 TrafficLight::getPhase(const std::string& tlsID) {
137  return getTLS(tlsID).getActive()->getCurrentPhaseIndex();
138 }
139 
140 
141 SUMOTime
142 TrafficLight::getPhaseDuration(const std::string& tlsID) {
143  return getTLS(tlsID).getActive()->getCurrentPhaseDef().duration;
144 }
145 
146 
147 SUMOTime
148 TrafficLight::getNextSwitch(const std::string& tlsID) {
149  return getTLS(tlsID).getActive()->getNextSwitchTime();
150 }
151 
152 
153 std::string
154 TrafficLight::getParameter(const std::string& tlsID, const std::string& paramName) {
155  return getTLS(tlsID).getActive()->getParameter(paramName, "");
156 }
157 
158 
159 void
160 TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
161  getTLS(tlsID).setStateInstantiatingOnline(MSNet::getInstance()->getTLSControl(), state);
162 }
163 
164 
165 void
166 TrafficLight::setPhase(const std::string& tlsID, const int index) {
167  MSTrafficLightLogic* const active = getTLS(tlsID).getActive();
168  if (index < 0 || active->getPhaseNumber() <= index) {
169  throw TraCIException("The phase index " + toString(index) + " is not in the allowed range [0,"
170  + toString(active->getPhaseNumber() - 1) + "].");
171  }
173  const SUMOTime duration = active->getPhase(index).duration;
174  active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, duration);
175 }
176 
177 
178 void
179 TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
180  try {
181  getTLS(tlsID).switchTo(MSNet::getInstance()->getTLSControl(), programID);
182  } catch (ProcessError& e) {
183  throw TraCIException(e.what());
184  }
185 }
186 
187 
188 void
189 TrafficLight::setPhaseDuration(const std::string& tlsID, const SUMOTime phaseDuration) {
190  MSTrafficLightLogic* const active = getTLS(tlsID).getActive();
192  const int index = active->getCurrentPhaseIndex();
193  active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, phaseDuration);
194 }
195 
196 
197 void
198 TrafficLight::setCompleteRedYellowGreenDefinition(const std::string& tlsID, const TraCILogic& logic) {
200  // make sure index and phaseNo are consistent
201  if (logic.currentPhaseIndex >= (int)logic.phases.size()) {
202  throw TraCIException("set program: parameter index must be less than parameter phase number.");
203  }
204  std::vector<MSPhaseDefinition*> phases;
205  for (TraCIPhase phase : logic.phases) {
206  phases.push_back(new MSPhaseDefinition(phase.duration, phase.duration1, phase.duration2, phase.phase));
207  }
208  if (vars.getLogic(logic.subID) == 0) {
209  MSTrafficLightLogic* mslogic = new MSSimpleTrafficLightLogic(MSNet::getInstance()->getTLSControl(), tlsID, logic.subID, phases, logic.currentPhaseIndex, 0, logic.subParameter);
210  vars.addLogic(logic.subID, mslogic, true, true);
211  } else {
212  static_cast<MSSimpleTrafficLightLogic*>(vars.getLogic(logic.subID))->setPhases(phases, logic.currentPhaseIndex);
213  }
214 }
215 
216 
217 void
218 TrafficLight::setParameter(const std::string& tlsID, const std::string& paramName, const std::string& value) {
219  return getTLS(tlsID).getActive()->setParameter(paramName, value);
220 }
221 
222 
224 TrafficLight::getTLS(const std::string& id) {
225  if (!MSNet::getInstance()->getTLSControl().knows(id)) {
226  throw TraCIException("Traffic light '" + id + "' is not known");
227  }
228  return MSNet::getInstance()->getTLSControl().get(id);
229 }
230 }
231 
232 
233 /****************************************************************************/
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
const std::string & getState() const
Returns the state within this phase.
std::string phase
Definition: TraCIDefs.h:114
Storage for all programs of a single tls.
static std::string getRedYellowGreenState(const std::string &tlsID)
virtual void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)=0
Changes the current phase and her duration.
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
static std::vector< std::string > getControlledJunctions(const std::string &tlsID)
static void setParameter(const std::string &tlsID, const std::string &paramName, const std::string &value)
virtual const MSPhaseDefinition & getPhase(int givenstep) const =0
Returns the definition of the phase from the given position within the plan.
static std::vector< std::string > getIDList()
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
std::map< std::string, std::string > subParameter
Definition: TraCIDefs.h:129
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
static std::vector< std::string > getControlledLanes(const std::string &tlsID)
std::vector< TraCIPhase > phases
Definition: TraCIDefs.h:128
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
A fixed traffic light logic.
static void setRedYellowGreenState(const std::string &tlsID, const std::string &state)
SUMOTime duration
The duration of the phase.
std::vector< MSTrafficLightLogic * > getAllLogics() const
static MSTLLogicControl::TLSLogicVariants & getTLS(const std::string &id)
bool addLogic(const std::string &programID, MSTrafficLightLogic *logic, bool netWasLoaded, bool isNewDefault=true)
Adds a logic (program)
std::vector< LinkVector > LinkVectorVector
Definition of a list that holds lists of links that do have the same attribute.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
virtual int getPhaseNumber() const =0
Returns the number of phases.
const std::string & getProgramID() const
Returns this tl-logic&#39;s id.
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:379
static void setPhaseDuration(const std::string &tlsID, const SUMOTime phaseDuration)
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
static int getIDCount()
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
void setStateInstantiatingOnline(MSTLLogicControl &tlc, const std::string &state)
static std::vector< TraCILogic > getCompleteRedYellowGreenDefinition(const std::string &tlsID)
Definition: Edge.cpp:31
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
void switchTo(MSTLLogicControl &tlc, const std::string &programID)
std::vector< MSLane * > LaneVector
Definition of the list of arrival lanes subjected to this tls.
std::vector< LaneVector > LaneVectorVector
Definition of a list that holds lists of lanes that do have the same attribute.
SUMOTime maxDuration
The maximum duration of the phase.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
static SUMOTime getNextSwitch(const std::string &tlsID)
SUMOTime duration2
Definition: TraCIDefs.h:113
static void setPhase(const std::string &tlsID, const int index)
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
std::string subID
Definition: TraCIDefs.h:125
static SUMOTime getPhaseDuration(const std::string &tlsID)
static void setCompleteRedYellowGreenDefinition(const std::string &tlsID, const TraCILogic &logic)
SUMOTime minDuration
The minimum duration of the phase.
The parent class for traffic light logics.
static void setProgram(const std::string &tlsID, const std::string &programID)
SUMOTime duration1
Definition: TraCIDefs.h:113
long long int SUMOTime
Definition: TraCIDefs.h:51
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
static std::string getProgram(const std::string &tlsID)
MSTrafficLightLogic * getActive() const
static int getPhase(const std::string &tlsID)
std::vector< std::string > getAllTLIds() const
static std::string getParameter(const std::string &tlsID, const std::string &paramName)
MSTrafficLightLogic * getLogic(const std::string &programID) const
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
The definition of a single phase of a tls logic.
static std::vector< std::vector< TraCILink > > getControlledLinks(const std::string &tlsID)