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-2016 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 <microsim/MSVehicle.h>
42 #include "TraCIConstants.h"
43 #include "TraCIServer.h"
44 #include "TraCIServerAPI_Lane.h"
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
54 bool
56  tcpip::Storage& outputStorage) {
57  // variable
58  int variable = inputStorage.readUnsignedByte();
59  std::string id = inputStorage.readString();
60  // check variable
61  if (variable != ID_LIST && variable != LANE_LINK_NUMBER && variable != LANE_EDGE_ID && variable != VAR_LENGTH
62  && variable != VAR_MAXSPEED && variable != LANE_LINKS && variable != VAR_SHAPE
63  && variable != VAR_CO2EMISSION && variable != VAR_COEMISSION && variable != VAR_HCEMISSION && variable != VAR_PMXEMISSION
64  && variable != VAR_NOXEMISSION && variable != VAR_FUELCONSUMPTION && variable != VAR_NOISEEMISSION && variable != VAR_WAITING_TIME
65  && variable != LAST_STEP_MEAN_SPEED && variable != LAST_STEP_VEHICLE_NUMBER
66  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY && variable != LAST_STEP_VEHICLE_HALTING_NUMBER
67  && variable != LAST_STEP_LENGTH && variable != VAR_CURRENT_TRAVELTIME
68  && variable != LANE_ALLOWED && variable != LANE_DISALLOWED && variable != VAR_WIDTH && variable != ID_COUNT
69  && variable != VAR_PARAMETER
70  ) {
71  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
72  }
73  // begin response building
74  tcpip::Storage tempMsg;
75  // response-code, variableID, objectID
77  tempMsg.writeUnsignedByte(variable);
78  tempMsg.writeString(id);
79  if (variable == ID_LIST) {
80  std::vector<std::string> ids;
81  MSLane::insertIDs(ids);
83  tempMsg.writeStringList(ids);
84  } else if (variable == ID_COUNT) {
85  std::vector<std::string> ids;
86  MSLane::insertIDs(ids);
88  tempMsg.writeInt((int) ids.size());
89  } else {
90  MSLane* lane = MSLane::dictionary(id);
91  if (lane == 0) {
92  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
93  }
94  switch (variable) {
95  case LANE_LINK_NUMBER:
97  tempMsg.writeUnsignedByte((int) lane->getLinkCont().size());
98  break;
99  case LANE_EDGE_ID:
101  tempMsg.writeString(lane->getEdge().getID());
102  break;
103  case VAR_LENGTH:
105  tempMsg.writeDouble(lane->getLength());
106  break;
107  case VAR_MAXSPEED:
109  tempMsg.writeDouble(lane->getSpeedLimit());
110  break;
111  case LANE_LINKS: {
113  tcpip::Storage tempContent;
114  unsigned int cnt = 0;
115  tempContent.writeUnsignedByte(TYPE_INTEGER);
116  const MSLinkCont& links = lane->getLinkCont();
117  tempContent.writeInt((int) links.size());
118  ++cnt;
119  const SUMOTime currTime = MSNet::getInstance()->getCurrentTimeStep();
120  for (MSLinkCont::const_iterator i = links.begin(); i != links.end(); ++i) {
121  MSLink* link = (*i);
122  // approached non-internal lane (if any)
123  tempContent.writeUnsignedByte(TYPE_STRING);
124  tempContent.writeString(link->getLane() != 0 ? link->getLane()->getID() : "");
125  ++cnt;
126  // approached "via", internal lane (if any)
127  tempContent.writeUnsignedByte(TYPE_STRING);
128 #ifdef HAVE_INTERNAL_LANES
129  tempContent.writeString(link->getViaLane() != 0 ? link->getViaLane()->getID() : "");
130 #else
131  tempContent.writeString("");
132 #endif
133  ++cnt;
134  // priority
135  tempContent.writeUnsignedByte(TYPE_UBYTE);
136  tempContent.writeUnsignedByte(link->havePriority() ? 1 : 0);
137  ++cnt;
138  // opened
139  tempContent.writeUnsignedByte(TYPE_UBYTE);
140  const SUMOReal speed = MIN2(lane->getSpeedLimit(), link->getLane()->getSpeedLimit());
141  tempContent.writeUnsignedByte(link->opened(currTime, speed, speed, SUMOVTypeParameter::getDefault().length,
143  ++cnt;
144  // approaching foe
145  tempContent.writeUnsignedByte(TYPE_UBYTE);
146  tempContent.writeUnsignedByte(link->hasApproachingFoe(currTime, currTime, 0, SUMOVTypeParameter::getDefaultDecel()) ? 1 : 0);
147  ++cnt;
148  // state (not implemented, yet)
149  tempContent.writeUnsignedByte(TYPE_STRING);
150  tempContent.writeString(SUMOXMLDefinitions::LinkStates.getString(link->getState()));
151  ++cnt;
152  // direction
153  tempContent.writeUnsignedByte(TYPE_STRING);
154  tempContent.writeString(SUMOXMLDefinitions::LinkDirections.getString(link->getDirection()));
155  ++cnt;
156  // length
157  tempContent.writeUnsignedByte(TYPE_DOUBLE);
158  tempContent.writeDouble(link->getLength());
159  ++cnt;
160  }
161  tempMsg.writeInt((int) cnt);
162  tempMsg.writeStorage(tempContent);
163  }
164  break;
165  case LANE_ALLOWED: {
167  SVCPermissions permissions = lane->getPermissions();
168  if (permissions == SVCAll) { // special case: write nothing
169  permissions = 0;
170  }
171  tempMsg.writeStringList(getVehicleClassNamesList(permissions));
172  }
173  case LANE_DISALLOWED: {
175  tempMsg.writeStringList(getVehicleClassNamesList(~(lane->getPermissions()))); // negation yields disallowed
176  }
177  break;
178  case VAR_SHAPE:
180  tempMsg.writeUnsignedByte((int)MIN2(static_cast<size_t>(255), lane->getShape().size()));
181  for (unsigned int iPoint = 0; iPoint < MIN2(static_cast<size_t>(255), lane->getShape().size()); ++iPoint) {
182  tempMsg.writeDouble(lane->getShape()[iPoint].x());
183  tempMsg.writeDouble(lane->getShape()[iPoint].y());
184  }
185  break;
186  case VAR_CO2EMISSION:
188  tempMsg.writeDouble(lane->getCO2Emissions());
189  break;
190  case VAR_COEMISSION:
192  tempMsg.writeDouble(lane->getCOEmissions());
193  break;
194  case VAR_HCEMISSION:
196  tempMsg.writeDouble(lane->getHCEmissions());
197  break;
198  case VAR_PMXEMISSION:
200  tempMsg.writeDouble(lane->getPMxEmissions());
201  break;
202  case VAR_NOXEMISSION:
204  tempMsg.writeDouble(lane->getNOxEmissions());
205  break;
206  case VAR_FUELCONSUMPTION:
208  tempMsg.writeDouble(lane->getFuelConsumption());
209  break;
210  case VAR_NOISEEMISSION:
212  tempMsg.writeDouble(lane->getHarmonoise_NoiseEmissions());
213  break;
216  tempMsg.writeInt((int) lane->getVehicleNumber());
217  break;
220  tempMsg.writeDouble(lane->getMeanSpeed());
221  break;
223  std::vector<std::string> vehIDs;
224  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
225  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
226  vehIDs.push_back((*j)->getID());
227  }
228  lane->releaseVehicles();
230  tempMsg.writeStringList(vehIDs);
231  }
232  break;
233  case LAST_STEP_OCCUPANCY:
235  tempMsg.writeDouble(lane->getNettoOccupancy());
236  break;
238  int halting = 0;
239  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
240  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
241  if ((*j)->getSpeed() < SUMO_const_haltingSpeed) {
242  ++halting;
243  }
244  }
245  lane->releaseVehicles();
247  tempMsg.writeInt(halting);
248  }
249  break;
250  case LAST_STEP_LENGTH: {
251  SUMOReal lengthSum = 0;
252  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
253  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
254  lengthSum += (*j)->getVehicleType().getLength();
255  }
257  if (vehs.size() == 0) {
258  tempMsg.writeDouble(0);
259  } else {
260  tempMsg.writeDouble(lengthSum / (SUMOReal) vehs.size());
261  }
262  lane->releaseVehicles();
263  }
264  break;
265  case VAR_WAITING_TIME: {
267  tempMsg.writeDouble(lane->getWaitingSeconds());
268  }
269  break;
270  case VAR_CURRENT_TRAVELTIME: {
271  SUMOReal meanSpeed = lane->getMeanSpeed();
273  if (meanSpeed != 0) {
274  tempMsg.writeDouble(lane->getLength() / meanSpeed);
275  } else {
276  tempMsg.writeDouble(1000000.);
277  }
278  }
279  break;
280  case VAR_WIDTH:
282  tempMsg.writeDouble(lane->getWidth());
283  break;
284  case VAR_PARAMETER: {
285  std::string paramName = "";
286  if (!server.readTypeCheckingString(inputStorage, paramName)) {
287  return server.writeErrorStatusCmd(RESPONSE_GET_LANE_VARIABLE, "Retrieval of a parameter requires its name.", outputStorage);
288  }
290  tempMsg.writeString(lane->getParameter(paramName, ""));
291  }
292  break;
293  default:
294  break;
295  }
296  }
297  server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_OK, "", outputStorage);
298  server.writeResponseWithLength(outputStorage, tempMsg);
299  return true;
300 }
301 
302 
303 bool
305  tcpip::Storage& outputStorage) {
306  std::string warning = ""; // additional description for response
307  // variable
308  int variable = inputStorage.readUnsignedByte();
309  if (variable != VAR_MAXSPEED && variable != VAR_LENGTH && variable != LANE_ALLOWED && variable != LANE_DISALLOWED
310  && variable != VAR_PARAMETER) {
311  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
312  }
313  // id
314  std::string id = inputStorage.readString();
315  MSLane* l = MSLane::dictionary(id);
316  if (l == 0) {
317  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
318  }
319  // process
320  switch (variable) {
321  case VAR_MAXSPEED: {
322  double value = 0;
323  if (!server.readTypeCheckingDouble(inputStorage, value)) {
324  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
325  }
326  l->setMaxSpeed(value);
327  }
328  break;
329  case VAR_LENGTH: {
330  double value = 0;
331  if (!server.readTypeCheckingDouble(inputStorage, value)) {
332  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
333  }
334  l->setLength(value);
335  }
336  break;
337  case LANE_ALLOWED: {
338  std::vector<std::string> classes;
339  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
340  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
341  }
342  l->setPermissions(parseVehicleClasses(classes));
344  }
345  break;
346  case LANE_DISALLOWED: {
347  std::vector<std::string> classes;
348  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
349  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
350  }
351  l->setPermissions(~parseVehicleClasses(classes)); // negation yields allowed
353  }
354  break;
355  case VAR_PARAMETER: {
356  if (inputStorage.readUnsignedByte() != TYPE_COMPOUND) {
357  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "A compound object is needed for setting a parameter.", outputStorage);
358  }
359  //readt itemNo
360  inputStorage.readInt();
361  std::string name;
362  if (!server.readTypeCheckingString(inputStorage, name)) {
363  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The name of the parameter must be given as a string.", outputStorage);
364  }
365  std::string value;
366  if (!server.readTypeCheckingString(inputStorage, value)) {
367  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The value of the parameter must be given as a string.", outputStorage);
368  }
369  l->addParameter(name, value);
370  }
371  break;
372  default:
373  break;
374  }
375  server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_OK, warning, outputStorage);
376  return true;
377 }
378 
379 
380 bool
381 TraCIServerAPI_Lane::getShape(const std::string& id, PositionVector& shape) {
382  const MSLane* const l = MSLane::dictionary(id);
383  if (l == 0) {
384  return false;
385  }
386  shape = l->getShape();
387  return true;
388 }
389 
390 
391 void
393  switch (myDomain) {
395  const MSLane::VehCont& vehs = l->getVehiclesSecure();
396  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
397  if (myShape.distance((*j)->getPosition()) <= myRange) {
398  myIDs.insert((*j)->getID());
399  }
400  }
401  l->releaseVehicles();
402  }
403  break;
404  case CMD_GET_EDGE_VARIABLE: {
405  if (myShape.size() != 1 || l->getShape().distance(myShape[0]) <= myRange) {
406  myIDs.insert(l->getEdge().getID());
407  }
408  }
409  break;
410  case CMD_GET_LANE_VARIABLE: {
411  if (myShape.size() != 1 || l->getShape().distance(myShape[0]) <= myRange) {
412  myIDs.insert(l->getID());
413  }
414  }
415  break;
416  default:
417  break;
418 
419  }
420 }
421 
422 
423 #endif
424 
425 
426 /****************************************************************************/
427 
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:467
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:1431
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:804
#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:302
#define LANE_EDGE_ID
#define LANE_LINK_NUMBER
SUMOReal getLength() const
Returns the lane&#39;s length.
Definition: MSLane.h:375
#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:960
#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:1520
SUMOReal getWidth() const
Returns the lane&#39;s width.
Definition: MSLane.h:391
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:160
SUMOReal getNettoOccupancy() const
Returns the netto (excluding minGaps) occupancy of this lane during the last step (including minGaps)...
Definition: MSLane.cpp:1410
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:571
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:1508
#define TYPE_STRING
virtual int readUnsignedByte()
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:295
#define LAST_STEP_LENGTH
void setMaxSpeed(SUMOReal val)
Sets a new maximum speed for the lane (used by TraCI and MSCalibrator)
Definition: MSLane.cpp:953
#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:172
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:1444
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:69
SUMOReal getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:367
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:383
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:73
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:284
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:772
SUMOReal getNOxEmissions() const
Returns the sum of last step NOx emissions.
Definition: MSLane.cpp:1496
#define VAR_COEMISSION
SUMOReal getPMxEmissions() const
Returns the sum of last step PMx emissions.
Definition: MSLane.cpp:1484
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
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:64
SUMOReal getCO2Emissions() const
Returns the sum of last step CO2 emissions.
Definition: MSLane.cpp:1460
#define CMD_SET_LANE_VARIABLE
SUMOReal getCOEmissions() const
Returns the sum of last step CO emissions.
Definition: MSLane.cpp:1472
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:322
#define SUMOReal
Definition: config.h:213
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:947
#define VAR_PARAMETER
#define ID_COUNT
SUMOReal getHarmonoise_NoiseEmissions() const
Returns the sum of last step noise emissions.
Definition: MSLane.cpp:1544
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.