SUMO - Simulation of Urban MObility
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.dlr.de/
13 // Copyright (C) 2009-2017 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/MSNet.h>
39 #include "TraCIConstants.h"
40 #include "TraCIDefs.h"
42 
43 
44 // ===========================================================================
45 // method definitions
46 // ===========================================================================
47 bool
49  tcpip::Storage& outputStorage) {
50  // variable & id
51  int variable = inputStorage.readUnsignedByte();
52  std::string id = inputStorage.readString();
53  // check variable
54  if (variable != ID_LIST && variable != LAST_STEP_VEHICLE_NUMBER && variable != LAST_STEP_MEAN_SPEED
55  && variable != LAST_STEP_VEHICLE_ID_LIST && variable != LAST_STEP_OCCUPANCY
56  && variable != LAST_STEP_LENGTH && variable != LAST_STEP_TIME_SINCE_DETECTION
57  && variable != LAST_STEP_VEHICLE_DATA && variable != ID_COUNT
58  && variable != VAR_POSITION && variable != VAR_LANE_ID) {
59  return server.writeErrorStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, "Get Induction Loop Variable: unsupported variable " + toHex(variable, 2) + " specified", outputStorage);
60  }
61  // begin response building
62  tcpip::Storage tempMsg;
63  // response-code, variableID, objectID
65  tempMsg.writeUnsignedByte(variable);
66  tempMsg.writeString(id);
67  // process request
68  try {
69  switch (variable) {
70  case ID_LIST:
73  break;
74  case ID_COUNT:
77  break;
78  case VAR_POSITION:
81  break;
82  case VAR_LANE_ID:
85  break;
89  break;
93  break;
97  break;
101  break;
102  case LAST_STEP_LENGTH:
105  break;
109  break;
110  case LAST_STEP_VEHICLE_DATA: {
111  std::vector<TraCIVehicleData> vd = TraCI_InductionLoop::getVehicleData(id);
113  tcpip::Storage tempContent;
114  int cnt = 0;
115  tempContent.writeUnsignedByte(TYPE_INTEGER);
116  tempContent.writeInt((int) vd.size());
117  ++cnt;
118  for (int i = 0; i < (int)vd.size(); ++i) {
119  TraCIVehicleData& svd = vd[i];
120  tempContent.writeUnsignedByte(TYPE_STRING);
121  tempContent.writeString(svd.id);
122  ++cnt;
123  tempContent.writeUnsignedByte(TYPE_DOUBLE);
124  tempContent.writeDouble(svd.length);
125  ++cnt;
126  tempContent.writeUnsignedByte(TYPE_DOUBLE);
127  tempContent.writeDouble(svd.entryTime);
128  ++cnt;
129  tempContent.writeUnsignedByte(TYPE_DOUBLE);
130  tempContent.writeDouble(svd.leaveTime);
131  ++cnt;
132  tempContent.writeUnsignedByte(TYPE_STRING);
133  tempContent.writeString(svd.typeID);
134  ++cnt;
135  }
136 
137  tempMsg.writeInt((int) cnt);
138  tempMsg.writeStorage(tempContent);
139  break;
140  }
141  default:
142  break;
143  }
144  } catch (TraCIException& e) {
145  return server.writeErrorStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, e.what(), outputStorage);
146  }
147  server.writeStatusCmd(CMD_GET_INDUCTIONLOOP_VARIABLE, RTYPE_OK, "", outputStorage);
148  server.writeResponseWithLength(outputStorage, tempMsg);
149  return true;
150 }
151 
152 
153 bool
156  if (il == 0) {
157  return false;
158  }
159  p = il->getLane()->getShape().positionAtOffset(il->getPosition());
160  return true;
161 }
162 
163 
164 NamedRTree*
166  NamedRTree* t = new NamedRTree();
167  const std::map<std::string, MSDetectorFileOutput*>& dets = MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).getMyMap();
168  for (std::map<std::string, MSDetectorFileOutput*>::const_iterator i = dets.begin(); i != dets.end(); ++i) {
169  MSInductLoop* il = static_cast<MSInductLoop*>((*i).second);
171  const float cmin[2] = {(float) p.x(), (float) p.y()};
172  const float cmax[2] = {(float) p.x(), (float) p.y()};
173  t->Insert(cmin, cmax, il);
174  }
175  return t;
176 }
177 
178 #endif
179 
180 
181 /****************************************************************************/
182 
static double getLastStepMeanSpeed(const std::string &detID)
#define LAST_STEP_MEAN_SPEED
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:90
const MSLane * getLane() const
Returns the lane the reminder works on.
alternative tag for e1 detector
double length
Length of the vehicle.
Definition: TraCIDefs.h:169
#define TYPE_COMPOUND
#define VAR_POSITION
#define CMD_GET_INDUCTIONLOOP_VARIABLE
double leaveTime
Leave-time of the vehicle in [s].
Definition: TraCIDefs.h:173
#define LAST_STEP_VEHICLE_DATA
double y() const
Returns the y-position.
Definition: Position.h:68
#define RTYPE_OK
T get(const std::string &id) const
Retrieves an item.
A RT-tree for efficient storing of SUMO&#39;s Named objects.
Definition: NamedRTree.h:72
double x() const
Returns the x-position.
Definition: Position.h:63
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
#define TYPE_STRINGLIST
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:426
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.
static double getLastStepMeanLength(const std::string &detID)
virtual void writeInt(int)
#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.
static std::vector< std::string > getIDList()
static std::vector< std::string > getLastStepVehicleIDs(const std::string &detID)
#define LAST_STEP_TIME_SINCE_DETECTION
std::string id
The id of the vehicle.
Definition: TraCIDefs.h:167
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
const IDMap & getMyMap() const
double getPosition() const
Returns the position of the detector on the lane.
Definition: MSInductLoop.h:101
virtual void writeStringList(const std::vector< std::string > &s)
virtual std::string readString()
static int getLastStepVehicleNumber(const std::string &detID)
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:373
#define RESPONSE_GET_INDUCTIONLOOP_VARIABLE
virtual void writeStorage(tcpip::Storage &store)
static std::string getLaneID(const std::string &detID)
void writeResponseWithLength(tcpip::Storage &outputStorage, tcpip::Storage &tempMsg)
#define LAST_STEP_VEHICLE_NUMBER
virtual void writeString(const std::string &s)
#define LAST_STEP_VEHICLE_ID_LIST
#define TYPE_DOUBLE
std::string toHex(const T i, std::streamsize numDigits=0)
Definition: ToString.h:66
std::string typeID
Type of the vehicle in.
Definition: TraCIDefs.h:175
static NamedRTree * getTree()
Returns a tree filled with inductive loop instances.
virtual void writeDouble(double)
void writeStatusCmd(int commandId, int status, const std::string &description, tcpip::Storage &outputStorage)
Writes a status command to the given storage.
static double getLastStepOccupancy(const std::string &detID)
#define LAST_STEP_OCCUPANCY
static double getTimeSinceDetection(const std::string &detID)
static double getPosition(const std::string &detID)
#define ID_COUNT
#define VAR_LANE_ID
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
double entryTime
Entry-time of the vehicle in [s].
Definition: TraCIDefs.h:171
#define TYPE_INTEGER
#define ID_LIST
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
static std::vector< TraCIVehicleData > getVehicleData(const std::string &detID)
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:71