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 /****************************************************************************/
10 // APIs for getting/setting induction loop values via TraCI
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2009-2014 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 "TraCIConstants.h"
37 #include <microsim/MSNet.h>
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 bool
52  tcpip::Storage& outputStorage) {
53  // variable & id
54  int variable = inputStorage.readUnsignedByte();
55  std::string id = inputStorage.readString();
56  // check variable
57  if (variable != ID_LIST && variable != LAST_STEP_VEHICLE_NUMBER && variable != LAST_STEP_MEAN_SPEED
58  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY
59  && variable != LAST_STEP_LENGTH && variable != LAST_STEP_TIME_SINCE_DETECTION
60  && variable != LAST_STEP_VEHICLE_DATA && variable != ID_COUNT
61  && variable != VAR_POSITION && variable != VAR_LANE_ID) {
62  return server.writeErrorStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, "Get Induction Loop Variable: unsupported variable specified", outputStorage);
63  }
64  // begin response building
65  tcpip::Storage tempMsg;
66  // response-code, variableID, objectID
68  tempMsg.writeUnsignedByte(variable);
69  tempMsg.writeString(id);
70  // process request
71  if (variable == ID_LIST) {
72  std::vector<std::string> ids;
75  tempMsg.writeStringList(ids);
76  } else if (variable == ID_COUNT) {
77  std::vector<std::string> ids;
80  tempMsg.writeInt((int) ids.size());
81  } else {
83  if (il == 0) {
84  return server.writeErrorStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, "Induction loop '" + id + "' is not known", outputStorage);
85  }
86  switch (variable) {
87  case ID_LIST:
88  break;
91  tempMsg.writeInt((int)(il->getCurrentPassedNumber()));
92  break;
95  tempMsg.writeDouble(il->getCurrentSpeed());
96  break;
99  std::vector<std::string> ids = il->getCurrentVehicleIDs();
100  tempMsg.writeStringList(ids);
101  }
102  break;
103  case LAST_STEP_OCCUPANCY:
105  tempMsg.writeDouble(il->getCurrentOccupancy());
106  break;
107  case LAST_STEP_LENGTH:
109  tempMsg.writeDouble(il->getCurrentLength());
110  break;
114  break;
115  case LAST_STEP_VEHICLE_DATA: {
116  std::vector<MSInductLoop::VehicleData> vd = il->collectVehiclesOnDet(MSNet::getInstance()->getCurrentTimeStep() - DELTA_T);
118  tcpip::Storage tempContent;
119  unsigned int cnt = 0;
120  tempContent.writeUnsignedByte(TYPE_INTEGER);
121  tempContent.writeInt((int) vd.size());
122  ++cnt;
123  for (unsigned int i = 0; i < vd.size(); ++i) {
124  MSInductLoop::VehicleData& svd = vd[i];
125  tempContent.writeUnsignedByte(TYPE_STRING);
126  tempContent.writeString(svd.idM);
127  ++cnt;
128  tempContent.writeUnsignedByte(TYPE_DOUBLE);
129  tempContent.writeDouble(svd.lengthM);
130  ++cnt;
131  tempContent.writeUnsignedByte(TYPE_DOUBLE);
132  tempContent.writeDouble(svd.entryTimeM);
133  ++cnt;
134  tempContent.writeUnsignedByte(TYPE_DOUBLE);
135  tempContent.writeDouble(svd.leaveTimeM);
136  ++cnt;
137  tempContent.writeUnsignedByte(TYPE_STRING);
138  tempContent.writeString(svd.typeIDM);
139  ++cnt;
140  }
141 
142  tempMsg.writeInt((int) cnt);
143  tempMsg.writeStorage(tempContent);
144  break;
145  }
146  case VAR_POSITION:
148  tempMsg.writeDouble(il->getPosition());
149  break;
150  case VAR_LANE_ID:
152  tempMsg.writeString(il->getLane()->getID());
153  break;
154  default:
155  break;
156  }
157  }
158  server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_OK, "", outputStorage);
159  server.writeResponseWithLength(outputStorage, tempMsg);
160  return true;
161 }
162 
163 
164 bool
167  if (il == 0) {
168  return false;
169  }
170  p = il->getLane()->getShape().positionAtOffset(il->getPosition());
171  return true;
172 }
173 
174 
175 NamedRTree*
177  NamedRTree* t = new NamedRTree();
178  const std::map<std::string, MSDetectorFileOutput*>& dets = MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).getMyMap();
179  for (std::map<std::string, MSDetectorFileOutput*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
180  MSInductLoop* il = static_cast<MSInductLoop*>((*i).second);
182  const float cmin[2] = {(float) p.x(), (float) p.y()};
183  const float cmax[2] = {(float) p.x(), (float) p.y()};
184  t->Insert(cmin, cmax, il);
185  }
186  return t;
187 }
188 
189 #endif
190 
191 
192 /****************************************************************************/
193 
#define LAST_STEP_MEAN_SPEED
void Insert(const float a_min[2], const float a_max[2], Named *a_data)
Insert entry.
Definition: NamedRTree.h:91
#define TYPE_COMPOUND
#define VAR_POSITION
#define CMD_GET_INDUCTIONLOOP_VARIABLE
#define LAST_STEP_VEHICLE_DATA
#define RTYPE_OK
A RT-tree for efficient storing of SUMO's Named objects.
Definition: NamedRTree.h:72
SUMOReal getPosition() const
Returns the position of the detector on the lane.
Definition: MSInductLoop.h:100
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:159
#define TYPE_STRINGLIST
SUMOReal getCurrentOccupancy() const
Returns the current occupancy.
virtual void writeUnsignedByte(int)
bool writeErrorStatusCmd(int commandId, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage with status = RTYPE_ERR.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
const MSLane * getLane() const
Returns the lane the reminder works on.
virtual void writeInt(int)
SUMOReal lengthM
Length of the vehicle.
Definition: MSInductLoop.h:260
#define TYPE_STRING
virtual int readUnsignedByte()
#define LAST_STEP_LENGTH
static bool getPosition(const std::string &id, Position &p)
Returns the named inductive loop's position.
T get(const std::string &id) const
Retrieves an item.
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:264
SUMOReal entryTimeM
Entry-time of the vehicle in [s].
Definition: MSInductLoop.h:262
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)
Position positionAtOffset(SUMOReal pos, SUMOReal lateralOffset=0) const
Returns the position at the given length.
virtual std::string readString()
std::string idM
The id of the vehicle.
Definition: MSInductLoop.h:258
static bool processGet(TraCIServer &server, tcpip::Storage &inputStorage, tcpip::Storage &outputStorage)
Processes a get value command (Command 0xa0: Get Induction Loop Variable)
TraCI server used to control sumo by a remote TraCI client.
Definition: TraCIServer.h:74
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:310
#define RESPONSE_GET_INDUCTIONLOOP_VARIABLE
virtual void writeStorage(tcpip::Storage &store)
SUMOReal getCurrentSpeed() const
Returns the speed of the vehicle on the detector.
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
unsigned int getCurrentPassedNumber() const
Returns the number of vehicles that have passed the detector.
const IDMap & getMyMap() const
#define LAST_STEP_VEHICLE_NUMBER
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:243
#define TYPE_DOUBLE
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
static NamedRTree * getTree()
Returns a tree filled with inductive loop instances.
virtual void writeDouble(double)
const PositionVector & getShape() const
Returns this lane's shape.
Definition: MSLane.h:323
SUMOReal getTimestepsSinceLastDetection() const
Returns the time since the last vehicle left the detector.
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 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:268
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:71