SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TraCIServerAPI_InductionLoop.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // APIs for getting/setting induction loop values via TraCI
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifndef NO_TRACI
34 
35 #include "TraCIConstants.h"
36 #include <microsim/MSNet.h>
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // used namespaces
48 // ===========================================================================
49 using namespace traci;
50 
51 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
55 bool
57  tcpip::Storage& outputStorage) {
58  // variable & id
59  int variable = inputStorage.readUnsignedByte();
60  std::string id = inputStorage.readString();
61  // check variable
62  if (variable != ID_LIST && variable != LAST_STEP_VEHICLE_NUMBER && variable != LAST_STEP_MEAN_SPEED
63  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY
64  && variable != LAST_STEP_LENGTH && variable != LAST_STEP_TIME_SINCE_DETECTION
65  && variable != LAST_STEP_VEHICLE_DATA && variable != ID_COUNT
66  && variable != VAR_POSITION && variable != VAR_LANE_ID) {
67  return server.writeErrorStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, "Get Induction Loop Variable: unsupported variable specified", outputStorage);
68  }
69  // begin response building
70  tcpip::Storage tempMsg;
71  // response-code, variableID, objectID
73  tempMsg.writeUnsignedByte(variable);
74  tempMsg.writeString(id);
75  // process request
76  if (variable == ID_LIST) {
77  std::vector<std::string> ids;
80  tempMsg.writeStringList(ids);
81  } else if (variable == ID_COUNT) {
82  std::vector<std::string> ids;
85  tempMsg.writeInt((int) ids.size());
86  } else {
88  if (il == 0) {
89  return server.writeErrorStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, "Induction loop '" + id + "' is not known", outputStorage);
90  }
91  switch (variable) {
92  case ID_LIST:
93  break;
96  tempMsg.writeInt((int)(il->getCurrentPassedNumber()));
97  break;
100  tempMsg.writeDouble(il->getCurrentSpeed());
101  break;
104  std::vector<std::string> ids = il->getCurrentVehicleIDs();
105  tempMsg.writeStringList(ids);
106  }
107  break;
108  case LAST_STEP_OCCUPANCY:
110  tempMsg.writeDouble(il->getCurrentOccupancy());
111  break;
112  case LAST_STEP_LENGTH:
114  tempMsg.writeDouble(il->getCurrentLength());
115  break;
119  break;
120  case LAST_STEP_VEHICLE_DATA: {
121  std::vector<MSInductLoop::VehicleData> vd = il->collectVehiclesOnDet(MSNet::getInstance()->getCurrentTimeStep() - DELTA_T);
123  tcpip::Storage tempContent;
124  unsigned int cnt = 0;
125  tempContent.writeUnsignedByte(TYPE_INTEGER);
126  tempContent.writeInt((int) vd.size());
127  ++cnt;
128  for (unsigned int i = 0; i < vd.size(); ++i) {
129  MSInductLoop::VehicleData& svd = vd[i];
130  tempContent.writeUnsignedByte(TYPE_STRING);
131  tempContent.writeString(svd.idM);
132  ++cnt;
133  tempContent.writeUnsignedByte(TYPE_DOUBLE);
134  tempContent.writeDouble(svd.lengthM);
135  ++cnt;
136  tempContent.writeUnsignedByte(TYPE_DOUBLE);
137  tempContent.writeDouble(svd.entryTimeM);
138  ++cnt;
139  tempContent.writeUnsignedByte(TYPE_DOUBLE);
140  tempContent.writeDouble(svd.leaveTimeM);
141  ++cnt;
142  tempContent.writeUnsignedByte(TYPE_STRING);
143  tempContent.writeString(svd.typeIDM);
144  ++cnt;
145  }
146 
147  tempMsg.writeInt((int) cnt);
148  tempMsg.writeStorage(tempContent);
149  break;
150  }
151  case VAR_POSITION:
153  tempMsg.writeDouble(il->getPosition());
154  break;
155  case VAR_LANE_ID:
157  tempMsg.writeString(il->getLane()->getID());
158  break;
159  default:
160  break;
161  }
162  }
163  server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_OK, "", outputStorage);
164  server.writeResponseWithLength(outputStorage, tempMsg);
165  return true;
166 }
167 
168 
169 bool
172  if (il == 0) {
173  return false;
174  }
175  p = il->getLane()->getShape().positionAtOffset(il->getPosition());
176  return true;
177 }
178 
179 
180 TraCIRTree*
182  TraCIRTree* t = new TraCIRTree();
183  const std::map<std::string, MSDetectorFileOutput*>& dets = MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).getMyMap();
184  for (std::map<std::string, MSDetectorFileOutput*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
185  MSInductLoop* il = static_cast<MSInductLoop*>((*i).second);
187  t->addObject(il, p);
188  }
189  return t;
190 }
191 
192 #endif
193 
194 
195 /****************************************************************************/
196 
#define LAST_STEP_MEAN_SPEED
static TraCIRTree * getTree()
Returns a tree filled with inductive loop instances.
static bool processGet(traci::TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa0: Get Induction Loop Variable)
Position positionAtOffset(SUMOReal pos) const
Returns the position at the given length.
#define TYPE_COMPOUND
#define VAR_POSITION
#define CMD_GET_INDUCTIONLOOP_VARIABLE
#define LAST_STEP_VEHICLE_DATA
#define RTYPE_OK
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
SUMOReal getPosition() const
Returns the position of the detector on the lane.
Definition: MSInductLoop.h:99
virtual std::vector< VehicleData > collectVehiclesOnDet(SUMOTime t) const
Returns vehicle data for vehicles that have been on the detector starting at the given time...
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:150
#define TYPE_STRINGLIST
SUMOReal getCurrentOccupancy() const
Returns the current occupancy.
virtual void writeUnsignedByte(int)
const MSLane * getLane() const
Returns the lane the reminder works on.
virtual void writeInt(int)
SUMOReal lengthM
Length of the vehicle.
Definition: MSInductLoop.h:273
#define TYPE_STRING
virtual int readUnsignedByte()
#define LAST_STEP_LENGTH
static bool getPosition(const std::string &id, Position &p)
Returns the named inductive loop&#39;s position.
T get(const std::string &id) const
Retrieves an item.
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:76
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
std::vector< std::string > getCurrentVehicleIDs() const
Returns the ids of vehicles that have passed the detector.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
#define LAST_STEP_TIME_SINCE_DETECTION
SUMOReal leaveTimeM
Leave-time of the vehicle in [s].
Definition: MSInductLoop.h:277
SUMOReal entryTimeM
Entry-time of the vehicle in [s].
Definition: MSInductLoop.h:275
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
void insertIDs(std::vector< std::string > &into) const
virtual void writeStringList(const std::vector< std::string > &s)
void addObject(Named *o, Boundary &b)
Adds an additional object (detector/shape/trigger) for visualisation.
Definition: TraCIRTree.h:115
virtual std::string readString()
std::string idM
The id of the vehicle.
Definition: MSInductLoop.h:271
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:289
#define RESPONSE_GET_INDUCTIONLOOP_VARIABLE
virtual void writeStorage(tcpip::Storage &store)
SUMOReal getCurrentSpeed() const
Returns the speed of the vehicle on the detector.
unsigned int getCurrentPassedNumber() const
Returns the number of vehicles that have passed the detector.
const IDMap & getMyMap() const
#define LAST_STEP_VEHICLE_NUMBER
A RT-tree for efficient storing of SUMO&#39;s GL-objects.
Definition: TraCIRTree.h:59
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
virtual void writeString(const std::string &s)
#define LAST_STEP_VEHICLE_ID_LIST
Struct to store the data of the counted vehicle internally.
Definition: MSInductLoop.h:256
#define TYPE_DOUBLE
virtual void writeDouble(double)
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:318
SUMOReal getTimestepsSinceLastDetection() const
Returns the time since the last vehicle left the detector.
#define LAST_STEP_OCCUPANCY
#define DELTA_T
Definition: SUMOTime.h:50
#define ID_COUNT
SUMOReal getCurrentLength() const
Returns the length of the vehicle on the detector.
#define VAR_LANE_ID
#define TYPE_INTEGER
#define ID_LIST
std::string typeIDM
Type of the vehicle in.
Definition: MSInductLoop.h:281
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:70