SUMO - Simulation of Urban MObility
TraCI_Lane.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 /****************************************************************************/
22 // C++ TraCI client API implementation
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 <microsim/MSNet.h>
36 #include <microsim/MSLane.h>
37 #include <microsim/MSEdge.h>
38 #include <microsim/MSVehicle.h>
39 #include "TraCI_Lane.h"
40 
41 
42 // ===========================================================================
43 // member definitions
44 // ===========================================================================
45 std::vector<std::string>
47  std::vector<std::string> ids;
48  MSLane::insertIDs(ids);
49  return ids;
50 }
51 
52 
53 int
55  return (int)getIDList().size();
56 }
57 
58 
59 std::string
60 TraCI_Lane::getEdgeID(std::string laneID) {
61  return getLane(laneID)->getEdge().getID();
62 }
63 
64 
65 double
66 TraCI_Lane::getLength(std::string laneID) {
67  return getLane(laneID)->getLength();
68 }
69 
70 
71 double
72 TraCI_Lane::getMaxSpeed(std::string laneID) {
73  return getLane(laneID)->getSpeedLimit();
74 }
75 
76 
77 int
78 TraCI_Lane::getLinkNumber(std::string laneID) {
79  return (int) getLane(laneID)->getLinkCont().size();
80 }
81 
82 
83 std::vector<TraCIConnection>
84 TraCI_Lane::getLinks(std::string laneID) {
85  std::vector<TraCIConnection> v;
86  const MSLane* lane = getLane(laneID);
87  const SUMOTime currTime = MSNet::getInstance()->getCurrentTimeStep();
88  const MSLinkCont& links = lane->getLinkCont();
89  for (MSLinkCont::const_iterator i = links.begin(); i != links.end(); ++i) {
90  MSLink* link = (*i);
91  const std::string approachedLane = link->getLane() != 0 ? link->getLane()->getID() : "";
92  const bool hasPrio = link->havePriority() ? 1 : 0;
93  const double speed = MIN2(lane->getSpeedLimit(), link->getLane()->getSpeedLimit());
94  const bool isOpen = link->opened(currTime, speed, speed, SUMOVTypeParameter::getDefault().length,
96  const bool hasFoe = link->hasApproachingFoe(currTime, currTime, 0, SUMOVTypeParameter::getDefaultDecel()) ? 1 : 0;
97  const std::string approachedInternal = link->getViaLane() != 0 ? link->getViaLane()->getID() : "";
98  const std::string state = SUMOXMLDefinitions::LinkStates.getString(link->getState());
99  const std::string direction = SUMOXMLDefinitions::LinkDirections.getString(link->getDirection());
100  const double length = link->getLength();
101  v.push_back(TraCIConnection(approachedLane, hasPrio, isOpen, hasFoe, approachedInternal, state, direction, length));
102  }
103  return v;
104 }
105 
106 
107 std::vector<std::string>
108 TraCI_Lane::getAllowed(std::string laneID) {
109  SVCPermissions permissions = getLane(laneID)->getPermissions();
110  if (permissions == SVCAll) { // special case: write nothing
111  permissions = 0;
112  }
113  return getVehicleClassNamesList(permissions);
114 }
115 
116 
117 std::vector<std::string>
118 TraCI_Lane::getDisallowed(std::string laneID) {
119  return getVehicleClassNamesList(invertPermissions((getLane(laneID)->getPermissions()))); // negation yields disallowed
120 }
121 
122 
124 TraCI_Lane::getShape(std::string laneID) {
126  const PositionVector& shp = getLane(laneID)->getShape();
127  for (PositionVector::const_iterator pi = shp.begin(); pi != shp.end(); ++pi) {
128  TraCIPosition p;
129  p.x = pi->x();
130  p.y = pi->y();
131  p.z = pi->z();
132  pv.push_back(p);
133  }
134  return pv;
135 }
136 
137 
138 double
139 TraCI_Lane::getWidth(std::string laneID) {
140  return getLane(laneID)->getWidth();
141 }
142 
143 
144 double
145 TraCI_Lane::getCO2Emission(std::string laneID) {
146  return getLane(laneID)->getCO2Emissions();
147 }
148 
149 
150 double
151 TraCI_Lane::getCOEmission(std::string laneID) {
152  return getLane(laneID)->getCOEmissions();
153 }
154 
155 
156 double
157 TraCI_Lane::getHCEmission(std::string laneID) {
158  return getLane(laneID)->getHCEmissions();
159 }
160 
161 
162 double
163 TraCI_Lane::getPMxEmission(std::string laneID) {
164  return getLane(laneID)->getPMxEmissions();
165 }
166 
167 
168 double
169 TraCI_Lane::getNOxEmission(std::string laneID) {
170  return getLane(laneID)->getNOxEmissions();
171 }
172 
173 double
174 TraCI_Lane::getFuelConsumption(std::string laneID) {
175  return getLane(laneID)->getFuelConsumption();
176 }
177 
178 
179 double
180 TraCI_Lane::getNoiseEmission(std::string laneID) {
181  return getLane(laneID)->getHarmonoise_NoiseEmissions();
182 }
183 
184 
185 double
187  return getLane(laneID)->getElectricityConsumption();
188 }
189 
190 
191 double
192 TraCI_Lane::getLastStepMeanSpeed(std::string laneID) {
193  return getLane(laneID)->getMeanSpeed();
194 }
195 
196 
197 double
198 TraCI_Lane::getLastStepOccupancy(std::string laneID) {
199  return getLane(laneID)->getNettoOccupancy();
200 }
201 
202 
203 double
204 TraCI_Lane::getLastStepLength(std::string laneID) {
205  const MSLane* lane = getLane(laneID);
206  double length = 0;
207  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
208  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
209  length += (*j)->getVehicleType().getLength();
210  }
211  if (vehs.size() > 0) {
212  length = length / (double) vehs.size();
213  }
214  lane->releaseVehicles();
215  return length;
216 }
217 
218 
219 double
220 TraCI_Lane::getWaitingTime(std::string laneID) {
221  return getLane(laneID)->getWaitingSeconds();
222 }
223 
224 
225 double
226 TraCI_Lane::getTraveltime(std::string laneID) {
227  const MSLane* lane = getLane(laneID);
228  double meanSpeed = lane->getMeanSpeed();
229  if (meanSpeed != 0) {
230  return lane->getLength() / meanSpeed;
231  } else {
232  return 1000000.;
233  }
234 }
235 
236 
237 int
239  return (int) getLane(laneID)->getVehicleNumber();
240 }
241 
242 int
244  const MSLane* lane = getLane(laneID);
245  int halting = 0;
246  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
247  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
248  if ((*j)->getSpeed() < SUMO_const_haltingSpeed) {
249  ++halting;
250  }
251  }
252  lane->releaseVehicles();
253  return halting;
254 }
255 
256 
257 std::vector<std::string>
259  const MSLane* lane = getLane(laneID);
260  std::vector<std::string> vehIDs;
261  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
262  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
263  vehIDs.push_back((*j)->getID());
264  }
265  lane->releaseVehicles();
266  return vehIDs;
267 }
268 
269 std::vector<std::string>
270 TraCI_Lane::getFoes(const std::string& laneID, const std::string& toLaneID) {
271  std::vector<std::string> foeIDs;
272  const MSLane* from = getLane(laneID);
273  const MSLane* to = getLane(toLaneID);
274  const MSLink* link = MSLinkContHelper::getConnectingLink(*from, *to);
275  if (link == 0) {
276  throw TraCIException("No connection from lane '" + laneID + "' to lane '" + toLaneID + "'");
277  }
278  for (MSLink* foe : link->getFoeLinks()) {
279  foeIDs.push_back(foe->getLaneBefore()->getID());
280  }
281  return foeIDs;
282 }
283 
284 
285 std::vector<std::string>
286 TraCI_Lane::getInternalFoes(const std::string& laneID) {
287  const MSLane *lane = getLane(laneID);
288  const std::vector<const MSLane*> *foeLanes;
289  std::vector<const MSLane*>::const_iterator it;
290  std::vector<std::string> foeIDs;
291 
292  if (lane->isInternal()) {
293  MSLink *link = lane->getLinkCont().front();
294  foeLanes = &link->getFoeLanes();
295 
296  for (it = foeLanes->begin(); foeLanes->end() != it; ++it) {
297  foeIDs.push_back((*it)->getID());
298  }
299  }
300  return foeIDs;
301 }
302 
303 
304 void
305 TraCI_Lane::setAllowed(std::string laneID, std::vector<std::string> allowedClasses) {
306  MSLane* l = const_cast<MSLane*>(getLane(laneID));
309 }
310 
311 
312 void
313 TraCI_Lane::setDisallowed(std::string laneID, std::vector<std::string> disallowedClasses) {
314  MSLane* l = const_cast<MSLane*>(getLane(laneID));
315  l->setPermissions(invertPermissions(parseVehicleClasses(disallowedClasses)), MSLane::CHANGE_PERMISSIONS_PERMANENT); // negation yields allowed
317 }
318 
319 
320 void
321 TraCI_Lane::setMaxSpeed(std::string laneID, double speed) {
322  MSLane* l = const_cast<MSLane*>(getLane(laneID));
323  l->setMaxSpeed(speed);
324 }
325 
326 
327 void
328 TraCI_Lane::setLength(std::string laneID, double length) {
329  MSLane* l = const_cast<MSLane*>(getLane(laneID));
330  l->setLength(length);
331 }
332 
333 
334 std::string
335 TraCI_Lane::getParameter(const std::string& laneID, const std::string& param) {
336  return getLane(laneID)->getParameter(param, "");
337 }
338 
339 
340 void
341 TraCI_Lane::setParameter(const std::string& laneID, const std::string& key, const std::string& value) {
342  MSLane* l = const_cast<MSLane*>(getLane(laneID));
343  l->setParameter(key, value);
344 }
345 
346 
347 const MSLane*
348 TraCI_Lane::getLane(const std::string& id) {
349  const MSLane* r = MSLane::dictionary(id);
350  if (r == 0) {
351  throw TraCIException("Lane '" + id + "' is not known");
352  }
353  return r;
354 }
355 
356 
357 /****************************************************************************/
static double getCO2Emission(std::string laneID)
Definition: TraCI_Lane.cpp:145
double x
Definition: TraCIDefs.h:71
static int getLastStepHaltingNumber(std::string laneID)
Definition: TraCI_Lane.cpp:243
double getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:475
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:583
double getHarmonoise_NoiseEmissions() const
Returns the sum of last step noise emissions.
Definition: MSLane.cpp:2439
double getWidth() const
Returns the lane&#39;s width.
Definition: MSLane.h:499
int getVehicleNumber() const
Returns the number of vehicles on this lane (for which this lane is responsible)
Definition: MSLane.h:351
static void insertIDs(std::vector< std::string > &into)
Adds the ids of all stored lanes into the given vector.
Definition: MSLane.cpp:1593
static double getHCEmission(std::string laneID)
Definition: TraCI_Lane.cpp:157
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:405
static double getLength(std::string laneID)
Definition: TraCI_Lane.cpp:66
static std::vector< std::string > getLastStepVehicleIDs(std::string laneID)
Definition: TraCI_Lane.cpp:258
double getCOEmissions() const
Returns the sum of last step CO emissions.
Definition: MSLane.cpp:2367
static double getWaitingTime(std::string laneID)
Definition: TraCI_Lane.cpp:220
void setMaxSpeed(double val)
Sets a new maximum speed for the lane (used by TraCI and MSCalibrator)
Definition: MSLane.cpp:1832
double getPMxEmissions() const
Returns the sum of last step PMx emissions.
Definition: MSLane.cpp:2379
static void setAllowed(std::string laneID, std::vector< std::string > allowedClasses)
Definition: TraCI_Lane.cpp:305
double getNettoOccupancy() const
Returns the netto (excluding minGaps) occupancy of this lane during the last step (including minGaps)...
Definition: MSLane.cpp:2311
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
std::vector< std::string > getVehicleClassNamesList(SVCPermissions permissions)
Returns the ids of the given classes, divided using a &#39; &#39;.
static double getElectricityConsumption(std::string laneID)
Definition: TraCI_Lane.cpp:186
static std::string getEdgeID(std::string laneID)
Definition: TraCI_Lane.cpp:60
double getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:483
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
static std::vector< std::string > getIDList()
Definition: TraCI_Lane.cpp:46
static std::vector< TraCIConnection > getLinks(std::string laneID)
Definition: TraCI_Lane.cpp:84
bool isInternal() const
Definition: MSLane.cpp:1687
static double getNoiseEmission(std::string laneID)
Definition: TraCI_Lane.cpp:180
static std::vector< std::string > getInternalFoes(const std::string &laneID)
Definition: TraCI_Lane.cpp:286
static StringBijection< LinkState > LinkStates
link states
const SVCPermissions SVCAll
all VClasses are allowed
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:253
static void setDisallowed(std::string laneID, std::vector< std::string > disallowedClasses)
Definition: TraCI_Lane.cpp:313
double getNOxEmissions() const
Returns the sum of last step NOx emissions.
Definition: MSLane.cpp:2391
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:378
SVCPermissions invertPermissions(SVCPermissions permissions)
negate the given permissions and ensure that only relevant bits are set
static double getWidth(std::string laneID)
Definition: TraCI_Lane.cpp:139
static double getLastStepMeanSpeed(std::string laneID)
Definition: TraCI_Lane.cpp:192
double z
Definition: TraCIDefs.h:71
static StringBijection< LinkDirection > LinkDirections
link directions
const std::string & getID() const
Returns the id.
Definition: Named.h:65
static double getNOxEmission(std::string laneID)
Definition: TraCI_Lane.cpp:169
void rebuildAllowedLanes()
Definition: MSEdge.cpp:246
double getFuelConsumption() const
Returns the sum of last step fuel consumption.
Definition: MSLane.cpp:2415
void setLength(double val)
Sets a new length for the lane (used by TraCI only)
Definition: MSLane.cpp:1839
A 3D-position.
Definition: TraCIDefs.h:70
static double getLastStepLength(std::string laneID)
Definition: TraCI_Lane.cpp:204
A list of positions.
double getElectricityConsumption() const
Returns the sum of last step electricity consumption.
Definition: MSLane.cpp:2427
const std::string & getString(const T key) const
T MIN2(T a, T b)
Definition: StdDefs.h:67
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
static double getDefaultDecel(const SUMOVehicleClass vc=SVC_IGNORING)
Returns the default deceleration for the given vehicle class This needs to be a function because the ...
static double getTraveltime(std::string laneID)
Definition: TraCI_Lane.cpp:226
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers Deprecated classes g...
SVCPermissions getPermissions() const
Returns the vehicle class permissions for this lane.
Definition: MSLane.h:491
const std::string getParameter(const std::string &key, const std::string &defaultValue="") const
Returns the value for a given key.
static std::vector< std::string > getFoes(const std::string &laneID, const std::string &toLaneID)
Definition: TraCI_Lane.cpp:270
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:90
static int getIDCount()
Definition: TraCI_Lane.cpp:54
double getMeanSpeed() const
Returns the mean speed on this lane.
Definition: MSLane.cpp:2339
static std::string getParameter(const std::string &laneID, const std::string &param)
Definition: TraCI_Lane.cpp:335
void setPermissions(SVCPermissions permissions, long transientID)
Sets the permissions to the given value. If a transientID is given, the permissions are recored as te...
Definition: MSLane.cpp:3032
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1561
static std::vector< std::string > getAllowed(std::string laneID)
Definition: TraCI_Lane.cpp:108
static TraCIPositionVector getShape(std::string laneID)
Definition: TraCI_Lane.cpp:124
static double getLastStepOccupancy(std::string laneID)
Definition: TraCI_Lane.cpp:198
static double getFuelConsumption(std::string laneID)
Definition: TraCI_Lane.cpp:174
static int getLinkNumber(std::string laneID)
Definition: TraCI_Lane.cpp:78
const double SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:57
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:425
static void setParameter(const std::string &routeID, const std::string &key, const std::string &value)
Definition: TraCI_Lane.cpp:341
static void setLength(std::string laneID, double length)
Definition: TraCI_Lane.cpp:328
static const MSLane * getLane(const std::string &id)
Definition: TraCI_Lane.cpp:348
long long int SUMOTime
Definition: TraCIDefs.h:51
static double getCOEmission(std::string laneID)
Definition: TraCI_Lane.cpp:151
double y
Definition: TraCIDefs.h:71
double getHCEmissions() const
Returns the sum of last step HC emissions.
Definition: MSLane.cpp:2403
const MSLinkCont & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.cpp:1787
double getCO2Emissions() const
Returns the sum of last step CO2 emissions.
Definition: MSLane.cpp:2355
static const long CHANGE_PERMISSIONS_PERMANENT
Definition: MSLane.h:1048
static int getLastStepVehicleNumber(std::string laneID)
Definition: TraCI_Lane.cpp:238
static const SUMOVTypeParameter & getDefault()
return the default parameters, this is a function due to the http://www.parashift.com/c++-faq/static-init-order.html
double getWaitingSeconds() const
Returns the overall waiting time on this lane.
Definition: MSLane.cpp:2326
Representation of a lane in the micro simulation.
Definition: MSLane.h:78
A list of positions.
static double getPMxEmission(std::string laneID)
Definition: TraCI_Lane.cpp:163
static std::vector< std::string > getDisallowed(std::string laneID)
Definition: TraCI_Lane.cpp:118
static void setMaxSpeed(std::string laneID, double speed)
Definition: TraCI_Lane.cpp:321
static double getMaxSpeed(std::string laneID)
Definition: TraCI_Lane.cpp:72