SUMO - Simulation of Urban MObility
TraCIServerAPI_Lane.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // APIs for getting/setting lane values via TraCI
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2009-2015 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
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 #ifndef NO_TRACI
36 
37 #include <microsim/MSEdge.h>
38 #include <microsim/MSEdgeControl.h>
39 #include <microsim/MSLane.h>
40 #include <microsim/MSNet.h>
41 #include "TraCIConstants.h"
42 #include "TraCIServer.h"
43 #include "TraCIServerAPI_Lane.h"
44 
45 #ifdef CHECK_MEMORY_LEAKS
46 #include <foreign/nvwa/debug_new.h>
47 #endif // CHECK_MEMORY_LEAKS
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
53 bool
55  tcpip::Storage& outputStorage) {
56  // variable
57  int variable = inputStorage.readUnsignedByte();
58  std::string id = inputStorage.readString();
59  // check variable
60  if (variable != ID_LIST && variable != LANE_LINK_NUMBER && variable != LANE_EDGE_ID && variable != VAR_LENGTH
61  && variable != VAR_MAXSPEED && variable != LANE_LINKS && variable != VAR_SHAPE
62  && variable != VAR_CO2EMISSION && variable != VAR_COEMISSION && variable != VAR_HCEMISSION && variable != VAR_PMXEMISSION
63  && variable != VAR_NOXEMISSION && variable != VAR_FUELCONSUMPTION && variable != VAR_NOISEEMISSION && variable != VAR_WAITING_TIME
64  && variable != LAST_STEP_MEAN_SPEED && variable != LAST_STEP_VEHICLE_NUMBER
65  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY && variable != LAST_STEP_VEHICLE_HALTING_NUMBER
66  && variable != LAST_STEP_LENGTH && variable != VAR_CURRENT_TRAVELTIME
67  && variable != LANE_ALLOWED && variable != LANE_DISALLOWED && variable != VAR_WIDTH && variable != ID_COUNT
68  && variable != VAR_PARAMETER
69  ) {
70  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable specified", outputStorage);
71  }
72  // begin response building
73  tcpip::Storage tempMsg;
74  // response-code, variableID, objectID
76  tempMsg.writeUnsignedByte(variable);
77  tempMsg.writeString(id);
78  if (variable == ID_LIST) {
79  std::vector<std::string> ids;
80  MSLane::insertIDs(ids);
82  tempMsg.writeStringList(ids);
83  } else if (variable == ID_COUNT) {
84  std::vector<std::string> ids;
85  MSLane::insertIDs(ids);
87  tempMsg.writeInt((int) ids.size());
88  } else {
89  MSLane* lane = MSLane::dictionary(id);
90  if (lane == 0) {
91  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
92  }
93  switch (variable) {
94  case LANE_LINK_NUMBER:
96  tempMsg.writeUnsignedByte((int) lane->getLinkCont().size());
97  break;
98  case LANE_EDGE_ID:
100  tempMsg.writeString(lane->getEdge().getID());
101  break;
102  case VAR_LENGTH:
104  tempMsg.writeDouble(lane->getLength());
105  break;
106  case VAR_MAXSPEED:
108  tempMsg.writeDouble(lane->getSpeedLimit());
109  break;
110  case LANE_LINKS: {
112  tcpip::Storage tempContent;
113  unsigned int cnt = 0;
114  tempContent.writeUnsignedByte(TYPE_INTEGER);
115  const MSLinkCont& links = lane->getLinkCont();
116  tempContent.writeInt((int) links.size());
117  ++cnt;
118  const SUMOTime currTime = MSNet::getInstance()->getCurrentTimeStep();
119  for (MSLinkCont::const_iterator i = links.begin(); i != links.end(); ++i) {
120  MSLink* link = (*i);
121  // approached non-internal lane (if any)
122  tempContent.writeUnsignedByte(TYPE_STRING);
123  tempContent.writeString(link->getLane() != 0 ? link->getLane()->getID() : "");
124  ++cnt;
125  // approached "via", internal lane (if any)
126  tempContent.writeUnsignedByte(TYPE_STRING);
127 #ifdef HAVE_INTERNAL_LANES
128  tempContent.writeString(link->getViaLane() != 0 ? link->getViaLane()->getID() : "");
129 #else
130  tempContent.writeString("");
131 #endif
132  ++cnt;
133  // priority
134  tempContent.writeUnsignedByte(TYPE_UBYTE);
135  tempContent.writeUnsignedByte(link->havePriority() ? 1 : 0);
136  ++cnt;
137  // opened
138  tempContent.writeUnsignedByte(TYPE_UBYTE);
139  const SUMOReal speed = MIN2(lane->getSpeedLimit(), link->getLane()->getSpeedLimit());
140  tempContent.writeUnsignedByte(link->opened(currTime, speed, speed, SUMOVTypeParameter::getDefault().length,
142  ++cnt;
143  // approaching foe
144  tempContent.writeUnsignedByte(TYPE_UBYTE);
145  tempContent.writeUnsignedByte(link->hasApproachingFoe(currTime, currTime, 0, SUMOVTypeParameter::getDefaultDecel()) ? 1 : 0);
146  ++cnt;
147  // state (not implemented, yet)
148  tempContent.writeUnsignedByte(TYPE_STRING);
149  tempContent.writeString(SUMOXMLDefinitions::LinkStates.getString(link->getState()));
150  ++cnt;
151  // direction
152  tempContent.writeUnsignedByte(TYPE_STRING);
153  tempContent.writeString(SUMOXMLDefinitions::LinkDirections.getString(link->getDirection()));
154  ++cnt;
155  // length
156  tempContent.writeUnsignedByte(TYPE_DOUBLE);
157  tempContent.writeDouble(link->getLength());
158  ++cnt;
159  }
160  tempMsg.writeInt((int) cnt);
161  tempMsg.writeStorage(tempContent);
162  }
163  break;
164  case LANE_ALLOWED: {
166  SVCPermissions permissions = lane->getPermissions();
167  if (permissions == SVCAll) { // special case: write nothing
168  permissions = 0;
169  }
170  tempMsg.writeStringList(getVehicleClassNamesList(permissions));
171  }
172  case LANE_DISALLOWED: {
174  tempMsg.writeStringList(getVehicleClassNamesList(~(lane->getPermissions()))); // negation yields disallowed
175  }
176  break;
177  case VAR_SHAPE:
179  tempMsg.writeUnsignedByte((int)MIN2(static_cast<size_t>(255), lane->getShape().size()));
180  for (unsigned int iPoint = 0; iPoint < MIN2(static_cast<size_t>(255), lane->getShape().size()); ++iPoint) {
181  tempMsg.writeDouble(lane->getShape()[iPoint].x());
182  tempMsg.writeDouble(lane->getShape()[iPoint].y());
183  }
184  break;
185  case VAR_CO2EMISSION:
187  tempMsg.writeDouble(lane->getCO2Emissions());
188  break;
189  case VAR_COEMISSION:
191  tempMsg.writeDouble(lane->getCOEmissions());
192  break;
193  case VAR_HCEMISSION:
195  tempMsg.writeDouble(lane->getHCEmissions());
196  break;
197  case VAR_PMXEMISSION:
199  tempMsg.writeDouble(lane->getPMxEmissions());
200  break;
201  case VAR_NOXEMISSION:
203  tempMsg.writeDouble(lane->getNOxEmissions());
204  break;
205  case VAR_FUELCONSUMPTION:
207  tempMsg.writeDouble(lane->getFuelConsumption());
208  break;
209  case VAR_NOISEEMISSION:
211  tempMsg.writeDouble(lane->getHarmonoise_NoiseEmissions());
212  break;
215  tempMsg.writeInt((int) lane->getVehicleNumber());
216  break;
219  tempMsg.writeDouble(lane->getMeanSpeed());
220  break;
222  std::vector<std::string> vehIDs;
223  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
224  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
225  vehIDs.push_back((*j)->getID());
226  }
227  lane->releaseVehicles();
229  tempMsg.writeStringList(vehIDs);
230  }
231  break;
232  case LAST_STEP_OCCUPANCY:
234  tempMsg.writeDouble(lane->getNettoOccupancy());
235  break;
237  int halting = 0;
238  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
239  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
240  if ((*j)->getSpeed() < SUMO_const_haltingSpeed) {
241  ++halting;
242  }
243  }
244  lane->releaseVehicles();
246  tempMsg.writeInt(halting);
247  }
248  break;
249  case LAST_STEP_LENGTH: {
250  SUMOReal lengthSum = 0;
251  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
252  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
253  lengthSum += (*j)->getVehicleType().getLength();
254  }
256  if (vehs.size() == 0) {
257  tempMsg.writeDouble(0);
258  } else {
259  tempMsg.writeDouble(lengthSum / (SUMOReal) vehs.size());
260  }
261  lane->releaseVehicles();
262  }
263  break;
264  case VAR_WAITING_TIME: {
266  tempMsg.writeDouble(lane->getWaitingSeconds());
267  }
268  break;
269  case VAR_CURRENT_TRAVELTIME: {
270  SUMOReal meanSpeed = lane->getMeanSpeed();
272  if (meanSpeed != 0) {
273  tempMsg.writeDouble(lane->getLength() / meanSpeed);
274  } else {
275  tempMsg.writeDouble(1000000.);
276  }
277  }
278  break;
279  case VAR_WIDTH:
281  tempMsg.writeDouble(lane->getWidth());
282  break;
283  case VAR_PARAMETER: {
284  std::string paramName = "";
285  if (!server.readTypeCheckingString(inputStorage, paramName)) {
286  return server.writeErrorStatusCmd(RESPONSE_GET_LANE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
287  }
289  tempMsg.writeString(lane->getParameter(paramName, ""));
290  }
291  break;
292  default:
293  break;
294  }
295  }
296  server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_OK, "", outputStorage);
297  server.writeResponseWithLength(outputStorage, tempMsg);
298  return true;
299 }
300 
301 
302 bool
304  tcpip::Storage& outputStorage) {
305  std::string warning = ""; // additional description for response
306  // variable
307  int variable = inputStorage.readUnsignedByte();
308  if (variable != VAR_MAXSPEED && variable != VAR_LENGTH && variable != LANE_ALLOWED && variable != LANE_DISALLOWED
309  && variable != VAR_PARAMETER) {
310  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable specified", outputStorage);
311  }
312  // id
313  std::string id = inputStorage.readString();
314  MSLane* l = MSLane::dictionary(id);
315  if (l == 0) {
316  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
317  }
318  // process
319  switch (variable) {
320  case VAR_MAXSPEED: {
321  double value = 0;
322  if (!server.readTypeCheckingDouble(inputStorage, value)) {
323  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
324  }
325  l->setMaxSpeed(value);
326  }
327  break;
328  case VAR_LENGTH: {
329  double value = 0;
330  if (!server.readTypeCheckingDouble(inputStorage, value)) {
331  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
332  }
333  l->setLength(value);
334  }
335  break;
336  case LANE_ALLOWED: {
337  std::vector<std::string> classes;
338  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
339  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
340  }
341  l->setPermissions(parseVehicleClasses(classes));
343  }
344  break;
345  case LANE_DISALLOWED: {
346  std::vector<std::string> classes;
347  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
348  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
349  }
350  l->setPermissions(~parseVehicleClasses(classes)); // negation yields allowed
352  }
353  break;
354  case VAR_PARAMETER: {
355  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
356  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
357  }
358  //readt itemNo
359  inputStorage.readInt();
360  std::string name;
361  if (!server.readTypeCheckingString(inputStorage, name)) {
362  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
363  }
364  std::string value;
365  if (!server.readTypeCheckingString(inputStorage, value)) {
366  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
367  }
368  l->addParameter(name, value);
369  }
370  break;
371  default:
372  break;
373  }
374  server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_OK, warning, outputStorage);
375  return true;
376 }
377 
378 
379 bool
380 TraCIServerAPI_Lane::getShape(const std::string& id, PositionVector& shape) {
381  const MSLane* const l = MSLane::dictionary(id);
382  if (l == 0) {
383  return false;
384  }
385  shape = l->getShape();
386  return true;
387 }
388 
389 
390 void
392  switch (myDomain) {
394  const MSLane::VehCont& vehs = l->getVehiclesSecure();
395  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
396  if (myShape.distance((*j)->getPosition()) <= myRange) {
397  myIDs.insert((*j)->getID());
398  }
399  }
400  l->releaseVehicles();
401  }
402  break;
403  case CMD_GET_EDGE_VARIABLE: {
404  if (myShape.size() != 1 || l->getShape().distance(myShape[0]) <= myRange) {
405  myIDs.insert(l->getEdge().getID());
406  }
407  }
408  break;
409  case CMD_GET_LANE_VARIABLE: {
410  if (myShape.size() != 1 || l->getShape().distance(myShape[0]) <= myRange) {
411  myIDs.insert(l->getID());
412  }
413  }
414  break;
415  default:
416  break;
417 
418  }
419 }
420 
421 
422 #endif
423 
424 
425 /****************************************************************************/
426 
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa3: Get Lane Variable)
#define LAST_STEP_MEAN_SPEED
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:461
long long int SUMOTime
Definition: SUMOTime.h:43
#define VAR_CO2EMISSION
SUMOReal distance(const Position &p, bool perpendicular=false) const
#define VAR_LENGTH
SUMOReal getWaitingSeconds() const
Returns the overall waiting time on this lane.
Definition: MSLane.cpp:1543
static bool processSet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a set value command (Command 0xc3: Change Lane State)
static void insertIDs(std::vector< std::string > &into)
Adds the ids of all stored lanes into the given vector.
Definition: MSLane.cpp:916
#define CMD_GET_VEHICLE_VARIABLE
#define TYPE_COMPOUND
#define VAR_CURRENT_TRAVELTIME
#define LANE_LINKS
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:303
#define LANE_EDGE_ID
#define LANE_LINK_NUMBER
SUMOReal getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:376
#define TYPE_UBYTE
int SVCPermissions
#define RTYPE_OK
void setLength(SUMOReal val)
Sets a new length for the lane (used by TraCI only)
Definition: MSLane.cpp:1072
#define VAR_WAITING_TIME
std::vector< std::string > getVehicleClassNamesList(SVCPermissions permissions)
Returns the ids of the given classes, divided using a &#39; &#39;.
#define TYPE_POLYGON
SUMOReal length
The physical vehicle length.
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:88
SUMOReal getFuelConsumption() const
Returns the sum of last step fuel consumption.
Definition: MSLane.cpp:1632
SUMOReal getWidth() const
Returns the lane&#39;s width.
Definition: MSLane.h:392
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:162
SUMOReal getNettoOccupancy() const
Returns the netto (excluding minGaps) occupancy of this lane during the last step (including minGaps)...
Definition: MSLane.cpp:1522
bool readTypeCheckingString(tcpip::Storage &inputStorage, std::string &into)
Reads the value type and a string, verifying the type.
#define TYPE_STRINGLIST
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
void setPermissions(SVCPermissions permissions)
Definition: MSLane.h:572
static StringBijection< LinkState > LinkStates
const SVCPermissions SVCAll
virtual void writeUnsignedByte(int)
const std::string & getParameter(const std::string &key, const std::string &defaultValue) const
Returns the value for a given key.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:255
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
#define VAR_SHAPE
#define VAR_NOISEEMISSION
#define VAR_FUELCONSUMPTION
virtual void writeInt(int)
SUMOReal getHCEmissions() const
Returns the sum of last step HC emissions.
Definition: MSLane.cpp:1620
#define TYPE_STRING
virtual int readUnsignedByte()
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:296
#define LAST_STEP_LENGTH
void setMaxSpeed(SUMOReal val)
Sets a new maximum speed for the lane (used by TraCI and MSCalibrator)
Definition: MSLane.cpp:1065
#define VAR_NOXEMISSION
static StringBijection< LinkDirection > LinkDirections
const std::string & getID() const
Returns the id.
Definition: Named.h:65
#define RESPONSE_GET_LANE_VARIABLE
#define LANE_ALLOWED
void rebuildAllowedLanes()
Definition: MSEdge.cpp:175
virtual int readInt()
void add(const MSLane *const l) const
Adds the given object to the container.
A list of positions.
SUMOReal getMeanSpeed() const
Returns the mean speed on this lane.
Definition: MSLane.cpp:1556
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
virtual void writeStringList(const std::vector< std::string > &s)
#define VAR_PMXEMISSION
#define CMD_GET_LANE_VARIABLE
T MIN2(T a, T b)
Definition: StdDefs.h:73
SUMOReal getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:368
virtual std::string readString()
#define CMD_GET_EDGE_VARIABLE
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:384
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
virtual void writeStorage(tcpip::Storage &store)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
#define LAST_STEP_VEHICLE_NUMBER
unsigned int getVehicleNumber() const
Returns the number of vehicles on this lane.
Definition: MSLane.h:285
void addParameter(const std::string &key, const std::string &value)
Adds a parameter.
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:884
SUMOReal getNOxEmissions() const
Returns the sum of last step NOx emissions.
Definition: MSLane.cpp:1608
#define VAR_COEMISSION
SUMOReal getPMxEmissions() const
Returns the sum of last step PMx emissions.
Definition: MSLane.cpp:1596
virtual void writeString(const std::string &s)
#define LAST_STEP_VEHICLE_ID_LIST
#define LANE_DISALLOWED
static SUMOReal getDefaultDecel(const SUMOVehicleClass vc=SVC_IGNORING)
Returns the default deceleration for the given vehicle class This needs to be a function because the ...
SUMOReal impatience
The vehicle&#39;s impatience (willingness to obstruct others)
#define TYPE_DOUBLE
SUMOReal getCO2Emissions() const
Returns the sum of last step CO2 emissions.
Definition: MSLane.cpp:1572
#define CMD_SET_LANE_VARIABLE
SUMOReal getCOEmissions() const
Returns the sum of last step CO emissions.
Definition: MSLane.cpp:1584
const SUMOReal SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:57
virtual void writeDouble(double)
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:323
#define SUMOReal
Definition: config.h:214
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
#define LAST_STEP_OCCUPANCY
#define VAR_MAXSPEED
const MSLinkCont & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.cpp:1059
#define VAR_PARAMETER
#define ID_COUNT
SUMOReal getHarmonoise_NoiseEmissions() const
Returns the sum of last step noise emissions.
Definition: MSLane.cpp:1644
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
#define TYPE_INTEGER
#define ID_LIST
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
#define LAST_STEP_VEHICLE_HALTING_NUMBER
#define VAR_HCEMISSION
#define VAR_WIDTH
static bool getShape(const std::string &id, PositionVector &shape)
Returns the named lane&#39;s shape.