SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_Lane.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // APIs for getting/setting lane values via TraCI
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #ifndef NO_TRACI
35 
36 #include <microsim/MSEdge.h>
37 #include <microsim/MSEdgeControl.h>
38 #include <microsim/MSLane.h>
39 #include <microsim/MSNet.h>
40 #include "TraCIConstants.h"
41 #include "TraCIServer.h"
42 #include "TraCIServerAPI_Lane.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
52 bool
54  tcpip::Storage& outputStorage) {
55  // variable
56  int variable = inputStorage.readUnsignedByte();
57  std::string id = inputStorage.readString();
58  // check variable
59  if (variable != ID_LIST && variable != LANE_LINK_NUMBER && variable != LANE_EDGE_ID && variable != VAR_LENGTH
60  && variable != VAR_MAXSPEED && variable != LANE_LINKS && variable != VAR_SHAPE
61  && variable != VAR_CO2EMISSION && variable != VAR_COEMISSION && variable != VAR_HCEMISSION && variable != VAR_PMXEMISSION
62  && variable != VAR_NOXEMISSION && variable != VAR_FUELCONSUMPTION && variable != VAR_NOISEEMISSION && variable != VAR_WAITING_TIME
63  && variable != LAST_STEP_MEAN_SPEED && variable != LAST_STEP_VEHICLE_NUMBER
64  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY && variable != LAST_STEP_VEHICLE_HALTING_NUMBER
65  && variable != LAST_STEP_LENGTH && variable != VAR_CURRENT_TRAVELTIME
66  && variable != LANE_ALLOWED && variable != LANE_DISALLOWED && variable != VAR_WIDTH && variable != ID_COUNT
67  ) {
68  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Get Lane Variable: unsupported variable specified", outputStorage);
69  }
70  // begin response building
71  tcpip::Storage tempMsg;
72  // response-code, variableID, objectID
74  tempMsg.writeUnsignedByte(variable);
75  tempMsg.writeString(id);
76  if (variable == ID_LIST) {
77  std::vector<std::string> ids;
78  MSLane::insertIDs(ids);
80  tempMsg.writeStringList(ids);
81  } else if (variable == ID_COUNT) {
82  std::vector<std::string> ids;
83  MSLane::insertIDs(ids);
85  tempMsg.writeInt((int) ids.size());
86  } else {
87  MSLane* lane = MSLane::dictionary(id);
88  if (lane == 0) {
89  return server.writeErrorStatusCmd(CMD_GET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
90  }
91  switch (variable) {
92  case LANE_LINK_NUMBER:
94  tempMsg.writeUnsignedByte((int) lane->getLinkCont().size());
95  break;
96  case LANE_EDGE_ID:
98  tempMsg.writeString(lane->getEdge().getID());
99  break;
100  case VAR_LENGTH:
102  tempMsg.writeDouble(lane->getLength());
103  break;
104  case VAR_MAXSPEED:
106  tempMsg.writeDouble(lane->getSpeedLimit());
107  break;
108  case LANE_LINKS: {
110  tcpip::Storage tempContent;
111  unsigned int cnt = 0;
112  tempContent.writeUnsignedByte(TYPE_INTEGER);
113  const MSLinkCont& links = lane->getLinkCont();
114  tempContent.writeInt((int) links.size());
115  ++cnt;
116  const SUMOTime currTime = MSNet::getInstance()->getCurrentTimeStep();
117  for (MSLinkCont::const_iterator i = links.begin(); i != links.end(); ++i) {
118  MSLink* link = (*i);
119  // approached non-internal lane (if any)
120  tempContent.writeUnsignedByte(TYPE_STRING);
121  tempContent.writeString(link->getLane() != 0 ? link->getLane()->getID() : "");
122  ++cnt;
123  // approached "via", internal lane (if any)
124  tempContent.writeUnsignedByte(TYPE_STRING);
125 #ifdef HAVE_INTERNAL_LANES
126  tempContent.writeString(link->getViaLane() != 0 ? link->getViaLane()->getID() : "");
127 #else
128  tempContent.writeString("");
129 #endif
130  ++cnt;
131  // priority
132  tempContent.writeUnsignedByte(TYPE_UBYTE);
133  tempContent.writeUnsignedByte(link->havePriority() ? 1 : 0);
134  ++cnt;
135  // opened
136  tempContent.writeUnsignedByte(TYPE_UBYTE);
137  const SUMOReal speed = MIN2(lane->getSpeedLimit(), link->getLane()->getSpeedLimit());
138  tempContent.writeUnsignedByte(link->opened(currTime, speed, speed, DEFAULT_VEH_LENGTH, 0.0, DEFAULT_VEH_DECEL, 0) ? 1 : 0);
139  ++cnt;
140  // approaching foe
141  tempContent.writeUnsignedByte(TYPE_UBYTE);
142  tempContent.writeUnsignedByte(link->hasApproachingFoe(currTime, currTime, 0) ? 1 : 0);
143  ++cnt;
144  // state (not implemented, yet)
145  tempContent.writeUnsignedByte(TYPE_STRING);
146  tempContent.writeString(SUMOXMLDefinitions::LinkStates.getString(link->getState()));
147  ++cnt;
148  // direction
149  tempContent.writeUnsignedByte(TYPE_STRING);
150  tempContent.writeString(SUMOXMLDefinitions::LinkDirections.getString(link->getDirection()));
151  ++cnt;
152  // length
153  tempContent.writeUnsignedByte(TYPE_DOUBLE);
154  tempContent.writeDouble(link->getLength());
155  ++cnt;
156  }
157  tempMsg.writeInt((int) cnt);
158  tempMsg.writeStorage(tempContent);
159  }
160  break;
161  case LANE_ALLOWED: {
163  SVCPermissions permissions = lane->getPermissions();
164  if (permissions == SVCFreeForAll) { // special case: write nothing
165  permissions = 0;
166  }
167  tempMsg.writeStringList(getAllowedVehicleClassNamesList(permissions));
168  }
169  case LANE_DISALLOWED: {
171  tempMsg.writeStringList(getAllowedVehicleClassNamesList(~(lane->getPermissions()))); // negation yields disallowed
172  }
173  break;
174  case VAR_SHAPE:
176  tempMsg.writeUnsignedByte((int)MIN2(static_cast<size_t>(255), lane->getShape().size()));
177  for (unsigned int iPoint = 0; iPoint < MIN2(static_cast<size_t>(255), lane->getShape().size()); ++iPoint) {
178  tempMsg.writeDouble(lane->getShape()[iPoint].x());
179  tempMsg.writeDouble(lane->getShape()[iPoint].y());
180  }
181  break;
182  case VAR_CO2EMISSION:
184  tempMsg.writeDouble(lane->getHBEFA_CO2Emissions());
185  break;
186  case VAR_COEMISSION:
188  tempMsg.writeDouble(lane->getHBEFA_COEmissions());
189  break;
190  case VAR_HCEMISSION:
192  tempMsg.writeDouble(lane->getHBEFA_HCEmissions());
193  break;
194  case VAR_PMXEMISSION:
196  tempMsg.writeDouble(lane->getHBEFA_PMxEmissions());
197  break;
198  case VAR_NOXEMISSION:
200  tempMsg.writeDouble(lane->getHBEFA_NOxEmissions());
201  break;
202  case VAR_FUELCONSUMPTION:
204  tempMsg.writeDouble(lane->getHBEFA_FuelConsumption());
205  break;
206  case VAR_NOISEEMISSION:
208  tempMsg.writeDouble(lane->getHarmonoise_NoiseEmissions());
209  break;
212  tempMsg.writeInt((int) lane->getVehicleNumber());
213  break;
216  tempMsg.writeDouble(lane->getMeanSpeed());
217  break;
219  std::vector<std::string> vehIDs;
220  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
221  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
222  vehIDs.push_back((*j)->getID());
223  }
224  lane->releaseVehicles();
226  tempMsg.writeStringList(vehIDs);
227  }
228  break;
229  case LAST_STEP_OCCUPANCY:
231  tempMsg.writeDouble(lane->getNettoOccupancy());
232  break;
234  int halting = 0;
235  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
236  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
237  if ((*j)->getSpeed() < SUMO_const_haltingSpeed) {
238  ++halting;
239  }
240  }
241  lane->releaseVehicles();
243  tempMsg.writeInt(halting);
244  }
245  break;
246  case LAST_STEP_LENGTH: {
247  SUMOReal lengthSum = 0;
248  const MSLane::VehCont& vehs = lane->getVehiclesSecure();
249  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
250  lengthSum += (*j)->getVehicleType().getLength();
251  }
253  if (vehs.size() == 0) {
254  tempMsg.writeDouble(0);
255  } else {
256  tempMsg.writeDouble(lengthSum / (SUMOReal) vehs.size());
257  }
258  lane->releaseVehicles();
259  }
260  break;
261  case VAR_WAITING_TIME: {
263  tempMsg.writeDouble(lane->getWaitingSeconds());
264  }
265  break;
266  case VAR_CURRENT_TRAVELTIME: {
267  SUMOReal meanSpeed = lane->getMeanSpeed();
269  if (meanSpeed != 0) {
270  tempMsg.writeDouble(lane->getLength() / meanSpeed);
271  } else {
272  tempMsg.writeDouble(1000000.);
273  }
274  }
275  break;
276  case VAR_WIDTH:
278  tempMsg.writeDouble(lane->getWidth());
279  break;
280  default:
281  break;
282  }
283  }
284  server.writeStatusCmd(CMD_GET_LANE_VARIABLE, RTYPE_OK, "", outputStorage);
285  server.writeResponseWithLength(outputStorage, tempMsg);
286  return true;
287 }
288 
289 
290 bool
292  tcpip::Storage& outputStorage) {
293  std::string warning = ""; // additional description for response
294  // variable
295  int variable = inputStorage.readUnsignedByte();
296  if (variable != VAR_MAXSPEED && variable != VAR_LENGTH && variable != LANE_ALLOWED && variable != LANE_DISALLOWED) {
297  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Change Lane State: unsupported variable specified", outputStorage);
298  }
299  // id
300  std::string id = inputStorage.readString();
301  MSLane* l = MSLane::dictionary(id);
302  if (l == 0) {
303  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Lane '" + id + "' is not known", outputStorage);
304  }
305  // process
306  switch (variable) {
307  case VAR_MAXSPEED: {
308  double value = 0;
309  if (!server.readTypeCheckingDouble(inputStorage, value)) {
310  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The speed must be given as a double.", outputStorage);
311  }
312  l->setMaxSpeed(value);
313  }
314  break;
315  case VAR_LENGTH: {
316  double value = 0;
317  if (!server.readTypeCheckingDouble(inputStorage, value)) {
318  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "The length must be given as a double.", outputStorage);
319  }
320  l->setLength(value);
321  }
322  break;
323  case LANE_ALLOWED: {
324  std::vector<std::string> classes;
325  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
326  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Allowed classes must be given as a list of strings.", outputStorage);
327  }
328  l->setPermissions(parseVehicleClasses(classes));
330  }
331  break;
332  case LANE_DISALLOWED: {
333  std::vector<std::string> classes;
334  if (!server.readTypeCheckingStringList(inputStorage, classes)) {
335  return server.writeErrorStatusCmd(CMD_SET_LANE_VARIABLE, "Not allowed classes must be given as a list of strings.", outputStorage);
336  }
337  l->setPermissions(~parseVehicleClasses(classes)); // negation yields allowed
339  }
340  break;
341  default:
342  break;
343  }
344  server.writeStatusCmd(CMD_SET_LANE_VARIABLE, RTYPE_OK, warning, outputStorage);
345  return true;
346 }
347 
348 
349 bool
350 TraCIServerAPI_Lane::getShape(const std::string& id, PositionVector& shape) {
351  const MSLane* const l = MSLane::dictionary(id);
352  if (l == 0) {
353  return false;
354  }
355  shape.push_back(l->getShape());
356  return true;
357 }
358 
359 
360 void
362  switch (myDomain) {
364  const MSLane::VehCont& vehs = l->getVehiclesSecure();
365  for (MSLane::VehCont::const_iterator j = vehs.begin(); j != vehs.end(); ++j) {
366  if (myShape.distance((*j)->getPosition()) <= myRange) {
367  myIDs.insert((*j)->getID());
368  }
369  }
370  l->releaseVehicles();
371  }
372  break;
373  case CMD_GET_EDGE_VARIABLE: {
374  if (myShape.size() != 1 || l->getShape().distance(myShape[0]) <= myRange) {
375  myIDs.insert(l->getEdge().getID());
376  }
377  }
378  break;
379  case CMD_GET_LANE_VARIABLE: {
380  if (myShape.size() != 1 || l->getShape().distance(myShape[0]) <= myRange) {
381  myIDs.insert(l->getID());
382  }
383  }
384  break;
385  default:
386  break;
387 
388  }
389 }
390 
391 
392 #endif
393 
394 
395 /****************************************************************************/
396 
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:447
#define VAR_CO2EMISSION
#define VAR_LENGTH
SUMOReal getWaitingSeconds() const
Returns the overall waiting time on this lane.
Definition: MSLane.cpp:1291
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:844
#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:365
#define TYPE_UBYTE
#define RTYPE_OK
void setLength(SUMOReal val)
Definition: MSLane.h:439
#define VAR_WAITING_TIME
#define TYPE_POLYGON
std::vector< MSVehicle * > VehCont
Container for vehicles.
Definition: MSLane.h:88
SUMOReal getWidth() const
Returns the lane&#39;s width.
Definition: MSLane.h:381
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
SUMOReal getNettoOccupancy() const
Returns the netto (excluding minGaps) occupancy of this lane during the last step (including minGaps)...
Definition: MSLane.cpp:1270
#define TYPE_STRINGLIST
bool readTypeCheckingDouble(tcpip::Storage &inputStorage, double &into)
Reads the value type and a double, verifying the type.
const SUMOReal DEFAULT_VEH_LENGTH
void setPermissions(SVCPermissions permissions)
Definition: MSLane.h:549
std::set< std::string > & myIDs
The container.
static StringBijection< LinkState > LinkStates
virtual void writeUnsignedByte(int)
SUMOTime getCurrentTimeStep() const
Returns the current simulation step (in s)
Definition: MSNet.cpp:502
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)
#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)
Definition: MSLane.h:435
#define VAR_NOXEMISSION
static StringBijection< LinkDirection > LinkDirections
const std::string & getID() const
Returns the id.
Definition: Named.h:60
SUMOReal getHBEFA_HCEmissions() const
Returns the sum of last step HC emissions.
Definition: MSLane.cpp:1368
#define RESPONSE_GET_LANE_VARIABLE
#define LANE_ALLOWED
void rebuildAllowedLanes()
Definition: MSEdge.cpp:165
void add(const MSLane *const l) const
Adds the given object to the container.
return static_cast< size_t >(bytesReceived)
A list of positions.
SUMOReal getHBEFA_COEmissions() const
Returns the sum of last step CO emissions.
Definition: MSLane.cpp:1332
SUMOReal getMeanSpeed() const
Returns the mean speed on this lane.
Definition: MSLane.cpp:1304
bool readTypeCheckingStringList(tcpip::Storage &inputStorage, std::vector< std::string > &into)
Reads the value type and a string list, verifying the type.
SUMOReal distance(const Position &p) const
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:57
SUMOReal getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:357
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. ...
SUMOReal getHBEFA_PMxEmissions() const
Returns the sum of last step PMx emissions.
Definition: MSLane.cpp:1344
SVCPermissions getPermissions() const
Returns the vehicle class permissions for this lane.
Definition: MSLane.h:373
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
virtual void writeStorage(tcpip::Storage &store)
const SVCPermissions SVCFreeForAll
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 push_back(const PositionVector &p)
Appends all positions from the given vector.
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:812
#define VAR_COEMISSION
virtual void writeString(const std::string &s)
#define LAST_STEP_VEHICLE_ID_LIST
#define LANE_DISALLOWED
#define TYPE_DOUBLE
#define CMD_SET_LANE_VARIABLE
const SUMOReal SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:49
virtual void writeDouble(double)
SUMOReal getHBEFA_FuelConsumption() const
Returns the sum of last step fuel consumption.
Definition: MSLane.cpp:1380
SUMOReal getHBEFA_NOxEmissions() const
Returns the sum of last step NOx emissions.
Definition: MSLane.cpp:1356
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:323
std::vector< std::string > getAllowedVehicleClassNamesList(SVCPermissions permissions)
Returns the ids of the given classes, divided using a &#39; &#39;.
#define SUMOReal
Definition: config.h:215
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
SUMOReal getHBEFA_CO2Emissions() const
Returns the sum of last step CO2 emissions.
Definition: MSLane.cpp:1320
#define LAST_STEP_OCCUPANCY
#define VAR_MAXSPEED
const MSLinkCont & getLinkCont() const
returns the container with all links !!!
Definition: MSLane.cpp:981
#define ID_COUNT
SUMOReal getHarmonoise_NoiseEmissions() const
Returns the sum of last step noise emissions.
Definition: MSLane.cpp:1392
#define TYPE_INTEGER
#define ID_LIST
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
const SUMOReal DEFAULT_VEH_DECEL
#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.