SUMO - Simulation of Urban MObility
TraCI_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 "TraCI_TrafficLight.h"
38 
39 
40 // ===========================================================================
41 // member definitions
42 // ===========================================================================
43 std::vector<std::string>
46 }
47 
48 
49 int
51  return (int)getIDList().size();
52 }
53 
54 
55 std::string
56 TraCI_TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
57  return getTLS(tlsID).getActive()->getCurrentPhaseDef().getState();
58 }
59 
60 
61 std::vector<TraCILogic>
63  std::vector<TraCILogic> result;
64  const std::vector<MSTrafficLightLogic*> logics = getTLS(tlsID).getAllLogics();
65  for (MSTrafficLightLogic* logic : logics) {
66  TraCILogic l(logic->getProgramID(), 0, logic->getCurrentPhaseIndex());
67  l.subParameter = logic->getMap();
68  for (int j = 0; j < logic->getPhaseNumber(); ++j) {
69  MSPhaseDefinition phase = logic->getPhase(j);
70  l.phases.emplace_back(TraCIPhase(phase.duration, phase.minDuration, phase.maxDuration, phase.getState()));
71  }
72  result.emplace_back(l);
73  }
74  return result;
75 }
76 
77 
78 std::vector<std::string>
79 TraCI_TrafficLight::getControlledJunctions(const std::string& tlsID) {
80  std::set<std::string> junctionIDs;
82  for (const MSTrafficLightLogic::LinkVector& llinks : links) {
83  for (const MSLink* l : llinks) {
84  junctionIDs.insert(l->getJunction()->getID());
85  }
86  }
87  return std::vector<std::string>(junctionIDs.begin(), junctionIDs.end());
88 }
89 
90 
91 std::vector<std::string>
92 TraCI_TrafficLight::getControlledLanes(const std::string& tlsID) {
93  std::vector<std::string> laneIDs;
95  for (const MSTrafficLightLogic::LaneVector& llanes : lanes) {
96  for (const MSLane* l : llanes) {
97  laneIDs.push_back(l->getID());
98  }
99  }
100  return laneIDs;
101 }
102 
103 
104 std::vector<std::vector<TraCILink> >
105 TraCI_TrafficLight::getControlledLinks(const std::string& tlsID) {
106  std::vector<std::vector<TraCILink> > result;
109  for (int i = 0; i < (int)lanes.size(); ++i) {
110  std::vector<TraCILink> subList;
111  const MSTrafficLightLogic::LaneVector& llanes = lanes[i];
112  const MSTrafficLightLogic::LinkVector& llinks = links[i];
113  // number of links controlled by this signal (signal i)
114  for (int j = 0; j < (int)llanes.size(); ++j) {
115  MSLink* link = llinks[j];
116  // approached non-internal lane (if any)
117  const std::string to = link->getLane() != 0 ? link->getLane()->getID() : "";
118  // approached "via", internal lane (if any)
119  const std::string via = link->getViaLane() != 0 ? link->getViaLane()->getID() : "";
120  subList.emplace_back(TraCILink(llanes[j]->getID(), via, to));
121  }
122  result.emplace_back(subList);
123  }
124  return result;
125 }
126 
127 
128 std::string
129 TraCI_TrafficLight::getProgram(const std::string& tlsID) {
130  return getTLS(tlsID).getActive()->getProgramID();
131 }
132 
133 
134 int
135 TraCI_TrafficLight::getPhase(const std::string& tlsID) {
136  return getTLS(tlsID).getActive()->getCurrentPhaseIndex();
137 }
138 
139 
140 SUMOTime
141 TraCI_TrafficLight::getPhaseDuration(const std::string& tlsID) {
142  return getTLS(tlsID).getActive()->getCurrentPhaseDef().duration;
143 }
144 
145 
146 SUMOTime
147 TraCI_TrafficLight::getNextSwitch(const std::string& tlsID) {
148  return getTLS(tlsID).getActive()->getNextSwitchTime();
149 }
150 
151 
152 std::string
153 TraCI_TrafficLight::getParameter(const std::string& tlsID, const std::string& paramName) {
154  return getTLS(tlsID).getActive()->getParameter(paramName, "");
155 }
156 
157 
158 void
159 TraCI_TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
160  getTLS(tlsID).setStateInstantiatingOnline(MSNet::getInstance()->getTLSControl(), state);
161 }
162 
163 
164 void
165 TraCI_TrafficLight::setPhase(const std::string& tlsID, const int index) {
166  MSTrafficLightLogic* const active = getTLS(tlsID).getActive();
167  if (index < 0 || active->getPhaseNumber() <= index) {
168  throw TraCIException("The phase index " + toString(index) + " is not in the allowed range [0,"
169  + toString(active->getPhaseNumber() - 1) + "].");
170  }
172  const SUMOTime duration = active->getPhase(index).duration;
173  active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, duration);
174 }
175 
176 
177 void
178 TraCI_TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
179  try {
180  getTLS(tlsID).switchTo(MSNet::getInstance()->getTLSControl(), programID);
181  } catch (ProcessError& e) {
182  throw TraCIException(e.what());
183  }
184 }
185 
186 
187 void
188 TraCI_TrafficLight::setPhaseDuration(const std::string& tlsID, const SUMOTime phaseDuration) {
189  MSTrafficLightLogic* const active = getTLS(tlsID).getActive();
191  const int index = active->getCurrentPhaseIndex();
192  active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, phaseDuration);
193 }
194 
195 
196 void
199  // make sure index and phaseNo are consistent
200  if (logic.currentPhaseIndex >= (int)logic.phases.size()) {
201  throw TraCIException("set program: parameter index must be less than parameter phase number.");
202  }
203  std::vector<MSPhaseDefinition*> phases;
204  for (TraCIPhase phase : logic.phases) {
205  phases.push_back(new MSPhaseDefinition(phase.duration, phase.duration1, phase.duration2, phase.phase));
206  }
207  if (vars.getLogic(logic.subID) == 0) {
208  MSTrafficLightLogic* mslogic = new MSSimpleTrafficLightLogic(MSNet::getInstance()->getTLSControl(), tlsID, logic.subID, phases, logic.currentPhaseIndex, 0, logic.subParameter);
209  vars.addLogic(logic.subID, mslogic, true, true);
210  } else {
211  static_cast<MSSimpleTrafficLightLogic*>(vars.getLogic(logic.subID))->setPhases(phases, logic.currentPhaseIndex);
212  }
213 }
214 
215 
216 void
217 TraCI_TrafficLight::setParameter(const std::string& tlsID, const std::string& paramName, const std::string& value) {
218  return getTLS(tlsID).getActive()->setParameter(paramName, value);
219 }
220 
221 
223 TraCI_TrafficLight::getTLS(const std::string& id) {
224  if (!MSNet::getInstance()->getTLSControl().knows(id)) {
225  throw TraCIException("Traffic light '" + id + "' is not known");
226  }
227  return MSNet::getInstance()->getTLSControl().get(id);
228 }
229 
230 
231 /****************************************************************************/
SUMOTime duration1
Definition: TraCIDefs.h:112
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
const std::string & getState() const
Returns the state within this phase.
Storage for all programs of a single tls.
static int getPhase(const std::string &tlsID)
std::string subID
Definition: TraCIDefs.h:124
static void setCompleteRedYellowGreenDefinition(const std::string &tlsID, const TraCILogic &logic)
static void setPhaseDuration(const std::string &tlsID, const SUMOTime phaseDuration)
static void setPhase(const std::string &tlsID, const int index)
virtual void changeStepAndDuration(MSTLLogicControl &tlcontrol, SUMOTime simStep, int step, SUMOTime stepDuration)=0
Changes the current phase and her duration.
std::map< std::string, std::string > subParameter
Definition: TraCIDefs.h:128
virtual const MSPhaseDefinition & getPhase(int givenstep) const =0
Returns the definition of the phase from the given position within the plan.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
static std::vector< std::string > getControlledJunctions(const std::string &tlsID)
std::vector< std::string > getAllTLIds() const
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
static void setProgram(const std::string &tlsID, const std::string &programID)
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
MSTrafficLightLogic * getLogic(const std::string &programID) const
A fixed traffic light logic.
SUMOTime getNextSwitchTime() const
Returns the assumed next switch time.
static MSTLLogicControl::TLSLogicVariants & getTLS(const std::string &id)
std::vector< MSTrafficLightLogic * > getAllLogics() const
const std::string & getID() const
Returns the id.
Definition: Named.h:65
SUMOTime duration
The duration of the phase.
bool addLogic(const std::string &programID, MSTrafficLightLogic *logic, bool netWasLoaded, bool isNewDefault=true)
Adds a logic (program)
SUMOTime duration2
Definition: TraCIDefs.h:112
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.
MSTrafficLightLogic * getActive() const
static SUMOTime getNextSwitch(const std::string &tlsID)
static std::vector< std::vector< TraCILink > > getControlledLinks(const std::string &tlsID)
MSTLLogicControl & getTLSControl()
Returns the tls logics control.
Definition: MSNet.h:379
SUMOTime duration
Definition: TraCIDefs.h:112
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
static std::string getProgram(const std::string &tlsID)
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
static void setRedYellowGreenState(const std::string &tlsID, const std::string &state)
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
void setStateInstantiatingOnline(MSTLLogicControl &tlc, const std::string &state)
const LaneVectorVector & getLaneVectors() const
Returns the list of lists of all lanes controlled by this tls.
static std::string getRedYellowGreenState(const std::string &tlsID)
std::vector< MSLink * > LinkVector
Definition of the list of links that are subjected to this tls.
static std::vector< TraCILogic > getCompleteRedYellowGreenDefinition(const std::string &tlsID)
std::vector< TraCIPhase > phases
Definition: TraCIDefs.h:127
void switchTo(MSTLLogicControl &tlc, const std::string &programID)
static std::string getParameter(const std::string &tlsID, const std::string &paramName)
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.
static std::vector< std::string > getControlledLanes(const std::string &tlsID)
int currentPhaseIndex
Definition: TraCIDefs.h:126
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
SUMOTime minDuration
The minimum duration of the phase.
The parent class for traffic light logics.
const std::string & getProgramID() const
Returns this tl-logic&#39;s id.
long long int SUMOTime
Definition: TraCIDefs.h:51
static std::vector< std::string > getIDList()
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
The definition of a single phase of a tls logic.
static SUMOTime getPhaseDuration(const std::string &tlsID)
std::string phase
Definition: TraCIDefs.h:113
static void setParameter(const std::string &tlsID, const std::string &paramName, const std::string &value)