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-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 /****************************************************************************/
18 // C++ TraCI client API implementation
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <microsim/MSLane.h>
28 #include <microsim/MSNet.h>
32 #include "TrafficLight.h"
33 
34 
35 namespace libsumo {
36 // ===========================================================================
37 // static member initializations
38 // ===========================================================================
41 
42 
43 // ===========================================================================
44 // static member definitions
45 // ===========================================================================
46 std::vector<std::string>
49 }
50 
51 
52 int
54  return (int)getIDList().size();
55 }
56 
57 
58 std::string
59 TrafficLight::getRedYellowGreenState(const std::string& tlsID) {
60  return getTLS(tlsID).getActive()->getCurrentPhaseDef().getState();
61 }
62 
63 
64 std::vector<TraCILogic>
66  std::vector<TraCILogic> result;
67  const std::vector<MSTrafficLightLogic*> logics = getTLS(tlsID).getAllLogics();
68  for (MSTrafficLightLogic* logic : logics) {
69  TraCILogic l(logic->getProgramID(), (int)logic->getLogicType(), logic->getCurrentPhaseIndex());
70  l.subParameter = logic->getParametersMap();
71  for (const MSPhaseDefinition* const phase : logic->getPhases()) {
72  l.phases.emplace_back(TraCIPhase(STEPS2TIME(phase->duration), phase->getState(), STEPS2TIME(phase->minDuration), STEPS2TIME(phase->maxDuration), phase->getNextPhase()));
73  }
74  result.emplace_back(l);
75  }
76  return result;
77 }
78 
79 
80 std::vector<std::string>
81 TrafficLight::getControlledJunctions(const std::string& tlsID) {
82  std::set<std::string> junctionIDs;
84  for (const MSTrafficLightLogic::LinkVector& llinks : links) {
85  for (const MSLink* l : llinks) {
86  junctionIDs.insert(l->getJunction()->getID());
87  }
88  }
89  return std::vector<std::string>(junctionIDs.begin(), junctionIDs.end());
90 }
91 
92 
93 std::vector<std::string>
94 TrafficLight::getControlledLanes(const std::string& tlsID) {
95  std::vector<std::string> laneIDs;
97  for (const MSTrafficLightLogic::LaneVector& llanes : lanes) {
98  for (const MSLane* l : llanes) {
99  laneIDs.push_back(l->getID());
100  }
101  }
102  return laneIDs;
103 }
104 
105 
106 std::vector<std::vector<TraCILink> >
107 TrafficLight::getControlledLinks(const std::string& tlsID) {
108  std::vector<std::vector<TraCILink> > result;
111  for (int i = 0; i < (int)lanes.size(); ++i) {
112  std::vector<TraCILink> subList;
113  const MSTrafficLightLogic::LaneVector& llanes = lanes[i];
114  const MSTrafficLightLogic::LinkVector& llinks = links[i];
115  // number of links controlled by this signal (signal i)
116  for (int j = 0; j < (int)llanes.size(); ++j) {
117  MSLink* link = llinks[j];
118  // approached non-internal lane (if any)
119  const std::string to = link->getLane() != nullptr ? link->getLane()->getID() : "";
120  // approached "via", internal lane (if any)
121  const std::string via = link->getViaLane() != nullptr ? link->getViaLane()->getID() : "";
122  subList.emplace_back(TraCILink(llanes[j]->getID(), via, to));
123  }
124  result.emplace_back(subList);
125  }
126  return result;
127 }
128 
129 
130 std::string
131 TrafficLight::getProgram(const std::string& tlsID) {
132  return getTLS(tlsID).getActive()->getProgramID();
133 }
134 
135 
136 int
137 TrafficLight::getPhase(const std::string& tlsID) {
138  return getTLS(tlsID).getActive()->getCurrentPhaseIndex();
139 }
140 
141 
142 double
143 TrafficLight::getPhaseDuration(const std::string& tlsID) {
144  return STEPS2TIME(getTLS(tlsID).getActive()->getCurrentPhaseDef().duration);
145 }
146 
147 
148 double
149 TrafficLight::getNextSwitch(const std::string& tlsID) {
150  return STEPS2TIME(getTLS(tlsID).getActive()->getNextSwitchTime());
151 }
152 
153 
154 std::string
155 TrafficLight::getParameter(const std::string& tlsID, const std::string& paramName) {
156  return getTLS(tlsID).getActive()->getParameter(paramName, "");
157 }
158 
159 
160 void
161 TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
162  getTLS(tlsID).setStateInstantiatingOnline(MSNet::getInstance()->getTLSControl(), state);
163 }
164 
165 
166 void
167 TrafficLight::setPhase(const std::string& tlsID, const int index) {
168  MSTrafficLightLogic* const active = getTLS(tlsID).getActive();
169  if (index < 0 || active->getPhaseNumber() <= index) {
170  throw TraCIException("The phase index " + toString(index) + " is not in the allowed range [0,"
171  + toString(active->getPhaseNumber() - 1) + "].");
172  }
174  const SUMOTime duration = active->getPhase(index).duration;
175  active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, duration);
176 }
177 
178 
179 void
180 TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
181  try {
182  getTLS(tlsID).switchTo(MSNet::getInstance()->getTLSControl(), programID);
183  } catch (ProcessError& e) {
184  throw TraCIException(e.what());
185  }
186 }
187 
188 
189 void
190 TrafficLight::setPhaseDuration(const std::string& tlsID, const double phaseDuration) {
191  MSTrafficLightLogic* const active = getTLS(tlsID).getActive();
193  const int index = active->getCurrentPhaseIndex();
194  active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, TIME2STEPS(phaseDuration));
195 }
196 
197 
198 void
199 TrafficLight::setCompleteRedYellowGreenDefinition(const std::string& tlsID, const TraCILogic& logic) {
201  // make sure index and phaseNo are consistent
202  if (logic.currentPhaseIndex >= (int)logic.phases.size()) {
203  throw TraCIException("set program: parameter index must be less than parameter phase number.");
204  }
205  std::vector<MSPhaseDefinition*> phases;
206  for (TraCIPhase phase : logic.phases) {
207  phases.push_back(new MSPhaseDefinition(TIME2STEPS(phase.duration), phase.state, TIME2STEPS(phase.minDur), TIME2STEPS(phase.maxDur), phase.next));
208  }
209  if (vars.getLogic(logic.programID) == nullptr) {
210  MSTrafficLightLogic* mslogic = new MSSimpleTrafficLightLogic(MSNet::getInstance()->getTLSControl(), tlsID, logic.programID, TLTYPE_STATIC, phases, logic.currentPhaseIndex, 0, logic.subParameter);
211  vars.addLogic(logic.programID, mslogic, true, true);
212  } else {
213  static_cast<MSSimpleTrafficLightLogic*>(vars.getLogic(logic.programID))->setPhases(phases, logic.currentPhaseIndex);
214  }
215 }
216 
217 
218 void
219 TrafficLight::setParameter(const std::string& tlsID, const std::string& paramName, const std::string& value) {
220  return getTLS(tlsID).getActive()->setParameter(paramName, value);
221 }
222 
223 
225 
226 
228 TrafficLight::getTLS(const std::string& id) {
229  if (!MSNet::getInstance()->getTLSControl().knows(id)) {
230  throw TraCIException("Traffic light '" + id + "' is not known");
231  }
232  return MSNet::getInstance()->getTLSControl().get(id);
233 }
234 
235 
236 std::shared_ptr<VariableWrapper>
238  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
239 }
240 
241 
242 bool
243 TrafficLight::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
244  switch (variable) {
245  case TRACI_ID_LIST:
246  return wrapper->wrapStringList(objID, variable, getIDList());
247  case ID_COUNT:
248  return wrapper->wrapInt(objID, variable, getIDCount());
250  return wrapper->wrapString(objID, variable, getRedYellowGreenState(objID));
251  case TL_CONTROLLED_LANES:
252  return wrapper->wrapStringList(objID, variable, getControlledLanes(objID));
253  case TL_CURRENT_PHASE:
254  return wrapper->wrapInt(objID, variable, getPhase(objID));
255  case TL_CURRENT_PROGRAM:
256  return wrapper->wrapString(objID, variable, getProgram(objID));
257  case TL_PHASE_DURATION:
258  return wrapper->wrapDouble(objID, variable, getPhaseDuration(objID));
259  case TL_NEXT_SWITCH:
260  return wrapper->wrapDouble(objID, variable, getNextSwitch(objID));
262  return wrapper->wrapStringList(objID, variable, getControlledJunctions(objID));
263  default:
264  return false;
265  }
266 }
267 
268 
269 }
270 
271 
272 /****************************************************************************/
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:199
virtual const MSPhaseDefinition & getCurrentPhaseDef() const =0
Returns the definition of the current phase.
#define TL_NEXT_SWITCH
const std::string & getState() const
Returns the state within this phase.
static double getPhaseDuration(const std::string &tlsID)
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
long long int SUMOTime
Definition: SUMOTime.h:36
Storage for all programs of a single tls.
static void setPhaseDuration(const std::string &tlsID, const double phaseDuration)
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:165
std::map< std::string, std::string > subParameter
Definition: TraCIDefs.h:235
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:200
virtual int getCurrentPhaseIndex() const =0
Returns the current index within the program.
const std::string & getID() const
Returns the id.
Definition: Named.h:78
virtual bool wrapString(const std::string &objID, const int variable, const std::string &value)=0
#define TIME2STEPS(x)
Definition: SUMOTime.h:60
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
#define TL_CONTROLLED_JUNCTIONS
static std::vector< std::string > getControlledLanes(const std::string &tlsID)
#define TL_CURRENT_PHASE
std::vector< TraCIPhase > phases
Definition: TraCIDefs.h:234
const LinkVectorVector & getLinks() const
Returns the list of lists of all affected links.
A fixed traffic light logic.
#define TL_PHASE_DURATION
static void setRedYellowGreenState(const std::string &tlsID, const std::string &state)
#define TL_CURRENT_PROGRAM
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.
#define TRACI_ID_LIST
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
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:389
static double getNextSwitch(const std::string &tlsID)
#define STEPS2TIME(x)
Definition: SUMOTime.h:58
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:263
static int getIDCount()
static SubscriptionResults mySubscriptionResults
Definition: TrafficLight.h:91
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:52
static LIBSUMO_SUBSCRIPTION_API std::shared_ptr< VariableWrapper > makeWrapper()
void setStateInstantiatingOnline(MSTLLogicControl &tlc, const std::string &state)
static std::vector< TraCILogic > getCompleteRedYellowGreenDefinition(const std::string &tlsID)
Definition: Edge.cpp:30
virtual bool wrapDouble(const std::string &objID, const int variable, const double value)=0
#define TL_RED_YELLOW_GREEN_STATE
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.
std::string programID
Definition: TraCIDefs.h:231
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.
static void setCompleteRedYellowGreenDefinition(const std::string &tlsID, const TraCILogic &logic)
The parent class for traffic light logics.
static void setProgram(const std::string &tlsID, const std::string &programID)
#define TL_CONTROLLED_LANES
TLSLogicVariants & get(const std::string &id) const
Returns the variants of a named tls.
static std::string getProgram(const std::string &tlsID)
#define ID_COUNT
MSTrafficLightLogic * getActive() const
static int getPhase(const std::string &tlsID)
std::vector< std::string > getAllTLIds() const
virtual bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
static std::string getParameter(const std::string &tlsID, const std::string &paramName)
MSTrafficLightLogic * getLogic(const std::string &programID) const
static ContextSubscriptionResults myContextSubscriptionResults
Definition: TrafficLight.h:92
std::string state
Definition: TraCIDefs.h:211
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
The definition of a single phase of a tls logic.
static std::vector< std::vector< TraCILink > > getControlledLinks(const std::string &tlsID)