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(), 0, logic->getCurrentPhaseIndex());
70  l.subParameter = logic->getParametersMap();
71  for (int j = 0; j < logic->getPhaseNumber(); ++j) {
72  MSPhaseDefinition phase = logic->getPhase(j);
73  l.phases.emplace_back(TraCIPhase(STEPS2TIME(phase.duration), STEPS2TIME(phase.minDuration), STEPS2TIME(phase.maxDuration), phase.getState()));
74  }
75  result.emplace_back(l);
76  }
77  return result;
78 }
79 
80 
81 std::vector<std::string>
82 TrafficLight::getControlledJunctions(const std::string& tlsID) {
83  std::set<std::string> junctionIDs;
85  for (const MSTrafficLightLogic::LinkVector& llinks : links) {
86  for (const MSLink* l : llinks) {
87  junctionIDs.insert(l->getJunction()->getID());
88  }
89  }
90  return std::vector<std::string>(junctionIDs.begin(), junctionIDs.end());
91 }
92 
93 
94 std::vector<std::string>
95 TrafficLight::getControlledLanes(const std::string& tlsID) {
96  std::vector<std::string> laneIDs;
98  for (const MSTrafficLightLogic::LaneVector& llanes : lanes) {
99  for (const MSLane* l : llanes) {
100  laneIDs.push_back(l->getID());
101  }
102  }
103  return laneIDs;
104 }
105 
106 
107 std::vector<std::vector<TraCILink> >
108 TrafficLight::getControlledLinks(const std::string& tlsID) {
109  std::vector<std::vector<TraCILink> > result;
112  for (int i = 0; i < (int)lanes.size(); ++i) {
113  std::vector<TraCILink> subList;
114  const MSTrafficLightLogic::LaneVector& llanes = lanes[i];
115  const MSTrafficLightLogic::LinkVector& llinks = links[i];
116  // number of links controlled by this signal (signal i)
117  for (int j = 0; j < (int)llanes.size(); ++j) {
118  MSLink* link = llinks[j];
119  // approached non-internal lane (if any)
120  const std::string to = link->getLane() != 0 ? link->getLane()->getID() : "";
121  // approached "via", internal lane (if any)
122  const std::string via = link->getViaLane() != 0 ? link->getViaLane()->getID() : "";
123  subList.emplace_back(TraCILink(llanes[j]->getID(), via, to));
124  }
125  result.emplace_back(subList);
126  }
127  return result;
128 }
129 
130 
131 std::string
132 TrafficLight::getProgram(const std::string& tlsID) {
133  return getTLS(tlsID).getActive()->getProgramID();
134 }
135 
136 
137 int
138 TrafficLight::getPhase(const std::string& tlsID) {
139  return getTLS(tlsID).getActive()->getCurrentPhaseIndex();
140 }
141 
142 
143 double
144 TrafficLight::getPhaseDuration(const std::string& tlsID) {
145  return STEPS2TIME(getTLS(tlsID).getActive()->getCurrentPhaseDef().duration);
146 }
147 
148 
149 double
150 TrafficLight::getNextSwitch(const std::string& tlsID) {
151  return STEPS2TIME(getTLS(tlsID).getActive()->getNextSwitchTime());
152 }
153 
154 
155 std::string
156 TrafficLight::getParameter(const std::string& tlsID, const std::string& paramName) {
157  return getTLS(tlsID).getActive()->getParameter(paramName, "");
158 }
159 
160 
161 void
162 TrafficLight::setRedYellowGreenState(const std::string& tlsID, const std::string& state) {
163  getTLS(tlsID).setStateInstantiatingOnline(MSNet::getInstance()->getTLSControl(), state);
164 }
165 
166 
167 void
168 TrafficLight::setPhase(const std::string& tlsID, const int index) {
169  MSTrafficLightLogic* const active = getTLS(tlsID).getActive();
170  if (index < 0 || active->getPhaseNumber() <= index) {
171  throw TraCIException("The phase index " + toString(index) + " is not in the allowed range [0,"
172  + toString(active->getPhaseNumber() - 1) + "].");
173  }
175  const SUMOTime duration = active->getPhase(index).duration;
176  active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, duration);
177 }
178 
179 
180 void
181 TrafficLight::setProgram(const std::string& tlsID, const std::string& programID) {
182  try {
183  getTLS(tlsID).switchTo(MSNet::getInstance()->getTLSControl(), programID);
184  } catch (ProcessError& e) {
185  throw TraCIException(e.what());
186  }
187 }
188 
189 
190 void
191 TrafficLight::setPhaseDuration(const std::string& tlsID, const double phaseDuration) {
192  MSTrafficLightLogic* const active = getTLS(tlsID).getActive();
194  const int index = active->getCurrentPhaseIndex();
195  active->changeStepAndDuration(MSNet::getInstance()->getTLSControl(), cTime, index, TIME2STEPS(phaseDuration));
196 }
197 
198 
199 void
200 TrafficLight::setCompleteRedYellowGreenDefinition(const std::string& tlsID, const TraCILogic& logic) {
202  // make sure index and phaseNo are consistent
203  if (logic.currentPhaseIndex >= (int)logic.phases.size()) {
204  throw TraCIException("set program: parameter index must be less than parameter phase number.");
205  }
206  std::vector<MSPhaseDefinition*> phases;
207  for (TraCIPhase phase : logic.phases) {
208  phases.push_back(new MSPhaseDefinition(TIME2STEPS(phase.duration), TIME2STEPS(phase.duration1), TIME2STEPS(phase.duration2), phase.phase));
209  }
210  if (vars.getLogic(logic.subID) == 0) {
211  MSTrafficLightLogic* mslogic = new MSSimpleTrafficLightLogic(MSNet::getInstance()->getTLSControl(), tlsID, logic.subID, phases, logic.currentPhaseIndex, 0, logic.subParameter);
212  vars.addLogic(logic.subID, mslogic, true, true);
213  } else {
214  static_cast<MSSimpleTrafficLightLogic*>(vars.getLogic(logic.subID))->setPhases(phases, logic.currentPhaseIndex);
215  }
216 }
217 
218 
219 void
220 TrafficLight::setParameter(const std::string& tlsID, const std::string& paramName, const std::string& value) {
221  return getTLS(tlsID).getActive()->setParameter(paramName, value);
222 }
223 
224 
226 
227 
229 TrafficLight::getTLS(const std::string& id) {
230  if (!MSNet::getInstance()->getTLSControl().knows(id)) {
231  throw TraCIException("Traffic light '" + id + "' is not known");
232  }
233  return MSNet::getInstance()->getTLSControl().get(id);
234 }
235 
236 
237 std::shared_ptr<VariableWrapper>
239  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
240 }
241 
242 
243 bool
244 TrafficLight::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
245  switch (variable) {
246  case ID_LIST:
247  return wrapper->wrapStringList(objID, variable, getIDList());
248  case ID_COUNT:
249  return wrapper->wrapInt(objID, variable, getIDCount());
251  return wrapper->wrapString(objID, variable, getRedYellowGreenState(objID));
252  case TL_CONTROLLED_LANES:
253  return wrapper->wrapStringList(objID, variable, getControlledLanes(objID));
254  case TL_CURRENT_PHASE:
255  return wrapper->wrapInt(objID, variable, getPhase(objID));
256  case TL_CURRENT_PROGRAM:
257  return wrapper->wrapString(objID, variable, getProgram(objID));
258  case TL_PHASE_DURATION:
259  return wrapper->wrapDouble(objID, variable, getPhaseDuration(objID));
260  case TL_NEXT_SWITCH:
261  return wrapper->wrapDouble(objID, variable, getNextSwitch(objID));
263  return wrapper->wrapStringList(objID, variable, getControlledJunctions(objID));
264  default:
265  return false;
266  }
267 }
268 
269 
270 }
271 
272 
273 /****************************************************************************/
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.
std::string phase
Definition: TraCIDefs.h:211
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:160
std::map< std::string, std::string > subParameter
Definition: TraCIDefs.h:226
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:69
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:225
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.
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:382
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:256
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.
SUMOTime maxDuration
The maximum duration of the phase.
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:222
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)
#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
#define ID_LIST
Representation of a lane in the micro simulation.
Definition: MSLane.h:71
The definition of a single phase of a tls logic.
static std::vector< std::vector< TraCILink > > getControlledLinks(const std::string &tlsID)