Eclipse SUMO - Simulation of Urban MObility
libsumo/InductionLoop.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2012-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // C++ TraCI client API implementation
22 /****************************************************************************/
23 #include <config.h>
24 
27 #include <mesosim/MEInductLoop.h>
28 #include <microsim/MSNet.h>
29 #include <microsim/MSEdge.h>
30 #include <libsumo/Helper.h>
31 #include <libsumo/TraCIDefs.h>
32 #include <libsumo/TraCIConstants.h>
33 #include "InductionLoop.h"
34 
35 
36 namespace libsumo {
37 // ===========================================================================
38 // static member initializations
39 // ===========================================================================
40 SubscriptionResults InductionLoop::mySubscriptionResults;
41 ContextSubscriptionResults InductionLoop::myContextSubscriptionResults;
42 NamedRTree* InductionLoop::myTree(nullptr);
43 
44 
45 // ===========================================================================
46 // member definitions
47 // ===========================================================================
48 std::vector<std::string>
49 InductionLoop::getIDList() {
50  std::vector<std::string> ids;
52  return ids;
53 }
54 
55 
56 int
57 InductionLoop::getIDCount() {
58  std::vector<std::string> ids;
60 }
61 
62 
63 double
64 InductionLoop::getPosition(const std::string& loopID) {
65  return getDetector(loopID)->getPosition();
66 }
67 
68 
69 std::string
70 InductionLoop::getLaneID(const std::string& loopID) {
71  return getDetector(loopID)->getLane()->getID();
72 }
73 
74 
75 int
76 InductionLoop::getLastStepVehicleNumber(const std::string& loopID) {
77  return (int)getDetector(loopID)->getEnteredNumber((int)DELTA_T);
78 }
79 
80 
81 double
82 InductionLoop::getLastStepMeanSpeed(const std::string& loopID) {
83  return getDetector(loopID)->getSpeed((int)DELTA_T);
84 }
85 
86 
87 std::vector<std::string>
88 InductionLoop::getLastStepVehicleIDs(const std::string& loopID) {
89  return getDetector(loopID)->getVehicleIDs((int)DELTA_T);
90 }
91 
92 
93 double
94 InductionLoop::getLastStepOccupancy(const std::string& loopID) {
95  return getDetector(loopID)->getOccupancy();
96 }
97 
98 
99 double
100 InductionLoop::getLastStepMeanLength(const std::string& loopID) {
101  return getDetector(loopID)->getVehicleLength((int)DELTA_T);
102 }
103 
104 
105 double
106 InductionLoop::getTimeSinceDetection(const std::string& loopID) {
107  return getDetector(loopID)->getTimeSinceLastDetection();
108 }
109 
110 std::vector<libsumo::TraCIVehicleData>
111 InductionLoop::getVehicleData(const std::string& loopID) {
112  const std::vector<MSInductLoop::VehicleData> vd = getDetector(loopID)->collectVehiclesOnDet(SIMSTEP - DELTA_T, true, true);
113  std::vector<libsumo::TraCIVehicleData> tvd;
114  for (const MSInductLoop::VehicleData& vdi : vd) {
115  tvd.push_back(libsumo::TraCIVehicleData());
116  tvd.back().id = vdi.idM;
117  tvd.back().length = vdi.lengthM;
118  tvd.back().entryTime = vdi.entryTimeM;
119  tvd.back().leaveTime = vdi.leaveTimeM;
120  tvd.back().typeID = vdi.typeIDM;
121  }
122  return tvd;
123 }
124 
125 
126 double
127 InductionLoop::getIntervalOccupancy(const std::string& loopID) {
129  const MEInductLoop* det = getMEDetector(loopID);
130  const auto& meanData = det->getMeanData();
131  return meanData.getOccupancy(SIMSTEP - meanData.getResetTime(), det->getEdge().getNumLanes());
132  } else {
133  return getDetector(loopID)->getIntervalOccupancy();
134  }
135 }
136 
137 
138 double
139 InductionLoop::getIntervalMeanSpeed(const std::string& loopID) {
141  const MEInductLoop* det = getMEDetector(loopID);
142  const auto& meanData = det->getMeanData();
143  if (meanData.getSamples() != 0) {
144  return meanData.getTravelledDistance() / meanData.getSamples();
145  } else {
146  const double defaultTravelTime = det->getEdge().getLength() / det->getEdge().getSpeedLimit();
147  return meanData.getLaneLength() / defaultTravelTime;
148  }
149  } else {
150  return getDetector(loopID)->getIntervalMeanSpeed();
151  }
152 }
153 
154 
155 int
156 InductionLoop::getIntervalVehicleNumber(const std::string& loopID) {
158  const auto& meanData = getMEDetector(loopID)->getMeanData();
159  return meanData.nVehDeparted + meanData.nVehEntered;
160  } else {
161  return getDetector(loopID)->getIntervalVehicleNumber();
162  }
163 }
164 
165 
166 std::vector<std::string>
167 InductionLoop::getIntervalVehicleIDs(const std::string& loopID) {
169  WRITE_ERROR("getIntervalVehicleIDs not applicable for meso");
170  return std::vector<std::string>();
171  }
172  return getDetector(loopID)->getIntervalVehicleIDs();
173 }
174 
175 
176 double
177 InductionLoop::getLastIntervalOccupancy(const std::string& loopID) {
179  WRITE_ERROR("getLastIntervalOccupancy not applicable for meso");
180  return INVALID_DOUBLE_VALUE;
181  }
182  return getDetector(loopID)->getIntervalOccupancy(true);
183 }
184 
185 
186 double
187 InductionLoop::getLastIntervalMeanSpeed(const std::string& loopID) {
189  WRITE_ERROR("getLastIntervalMeanSpeed not applicable for meso");
190  return INVALID_DOUBLE_VALUE;
191  }
192  return getDetector(loopID)->getIntervalMeanSpeed(true);
193 }
194 
195 
196 int
197 InductionLoop::getLastIntervalVehicleNumber(const std::string& loopID) {
199  WRITE_ERROR("getLastIntervalVehicleNumber not applicable for meso");
200  return INVALID_INT_VALUE;
201  }
202  return getDetector(loopID)->getIntervalVehicleNumber(true);
203 }
204 
205 
206 std::vector<std::string>
207 InductionLoop::getLastIntervalVehicleIDs(const std::string& loopID) {
209  WRITE_ERROR("getLastIntervalVehicleIDs not applicable for meso");
210  return std::vector<std::string>();
211  }
212  return getDetector(loopID)->getIntervalVehicleIDs(true);
213 }
214 
215 
216 void
217 InductionLoop::overrideTimeSinceDetection(const std::string& loopID, double time) {
218  getDetector(loopID)->overrideTimeSinceDetection(time);
219 }
220 
221 
223 InductionLoop::getDetector(const std::string& id) {
225  if (il == nullptr) {
226  throw TraCIException("Induction loop '" + id + "' is not known");
227  }
228  return il;
229 }
230 
231 
233 InductionLoop::getMEDetector(const std::string& id) {
235  if (il == nullptr) {
236  throw TraCIException("Induction loop '" + id + "' is not known");
237  }
238  return il;
239 }
240 
241 
242 
243 std::string
244 InductionLoop::getParameter(const std::string& loopID, const std::string& param) {
245  return getDetector(loopID)->getParameter(param, "");
246 }
247 
248 
250 
251 
252 void
253 InductionLoop::setParameter(const std::string& loopID, const std::string& name, const std::string& value) {
254  getDetector(loopID)->setParameter(name, value);
255 }
256 
257 
259 
260 
261 NamedRTree*
262 InductionLoop::getTree() {
263  if (myTree == nullptr) {
264  myTree = new NamedRTree();
265  for (const auto& i : MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP)) {
266  MSInductLoop* il = static_cast<MSInductLoop*>(i.second);
268  const float cmin[2] = {(float) p.x(), (float) p.y()};
269  const float cmax[2] = {(float) p.x(), (float) p.y()};
270  myTree->Insert(cmin, cmax, il);
271  }
272  }
273  return myTree;
274 }
275 
276 void
277 InductionLoop::cleanup() {
278  delete myTree;
279  myTree = nullptr;
280 }
281 
282 void
283 InductionLoop::storeShape(const std::string& id, PositionVector& shape) {
284  MSInductLoop* const il = getDetector(id);
285  shape.push_back(il->getLane()->getShape().positionAtOffset(il->getPosition()));
286 }
287 
288 
289 std::shared_ptr<VariableWrapper>
290 InductionLoop::makeWrapper() {
291  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
292 }
293 
294 
295 bool
296 InductionLoop::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
297  switch (variable) {
298  case TRACI_ID_LIST:
299  return wrapper->wrapStringList(objID, variable, getIDList());
300  case ID_COUNT:
301  return wrapper->wrapInt(objID, variable, getIDCount());
302  case VAR_POSITION:
303  return wrapper->wrapDouble(objID, variable, getPosition(objID));
304  case VAR_LANE_ID:
305  return wrapper->wrapString(objID, variable, getLaneID(objID));
307  return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
309  return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
311  return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
312  case LAST_STEP_OCCUPANCY:
313  return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
314  case LAST_STEP_LENGTH:
315  return wrapper->wrapDouble(objID, variable, getLastStepMeanLength(objID));
317  return wrapper->wrapDouble(objID, variable, getTimeSinceDetection(objID));
319  return wrapper->wrapDouble(objID, variable, getIntervalOccupancy(objID));
320  case VAR_INTERVAL_SPEED:
321  return wrapper->wrapDouble(objID, variable, getIntervalMeanSpeed(objID));
322  case VAR_INTERVAL_NUMBER:
323  return wrapper->wrapInt(objID, variable, getIntervalVehicleNumber(objID));
324  case VAR_INTERVAL_IDS:
325  return wrapper->wrapStringList(objID, variable, getIntervalVehicleIDs(objID));
327  return wrapper->wrapDouble(objID, variable, getLastIntervalOccupancy(objID));
329  return wrapper->wrapDouble(objID, variable, getLastIntervalMeanSpeed(objID));
331  return wrapper->wrapInt(objID, variable, getLastIntervalVehicleNumber(objID));
333  return wrapper->wrapStringList(objID, variable, getLastIntervalVehicleIDs(objID));
335  paramData->readUnsignedByte();
336  return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
338  paramData->readUnsignedByte();
339  return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
340  default:
341  return false;
342  }
343 }
344 
345 
346 }
347 
348 
349 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:304
SUMOTime DELTA_T
Definition: SUMOTime.cpp:38
#define SIMSTEP
Definition: SUMOTime.h:61
@ SUMO_TAG_INDUCTION_LOOP
alternative tag for e1 detector
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOM)
Definition: TraCIDefs.h:76
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:123
C++ TraCI client API implementation.
Definition: InductionLoop.h:33
An induction loop for mesoscopic simulation.
Definition: MEInductLoop.h:45
const MSEdge & getEdge() const
const MSMeanData_Net::MSLaneMeanDataValues & getMeanData() const
Definition: MEInductLoop.h:89
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
double getSpeedLimit() const
Returns the speed limit of the edge @caution The speed limit of the first lane is retured; should pro...
Definition: MSEdge.cpp:1094
double getLength() const
return the length of the edge
Definition: MSEdge.h:662
int getNumLanes() const
Definition: MSEdge.h:172
static bool gUseMesoSim
Definition: MSGlobals.h:103
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:63
double getPosition() const
Returns the position of the detector on the lane.
Definition: MSInductLoop.h:100
virtual const PositionVector & getShape(bool) const
Definition: MSLane.h:294
double getTravelledDistance() const
Returns the total travelled distance.
Definition: MSMeanData.h:156
double getOccupancy(SUMOTime period, int numLanes) const
const MSLane * getLane() const
Returns the lane the reminder works on.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:182
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:441
T get(const std::string &id) const
Retrieves an item.
void insertIDs(std::vector< std::string > &into) const
int size() const
Returns the number of stored items within the container.
A RT-tree for efficient storing of SUMO's Named objects.
Definition: NamedRTree.h:61
virtual const std::string getParameter(const std::string &key, const std::string defaultValue="") const
Returns the value for a given key.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
double x() const
Returns the x-position.
Definition: Position.h:55
double y() const
Returns the y-position.
Definition: Position.h:60
A list of positions.
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
virtual std::string readString()
Definition: storage.cpp:180
virtual int readUnsignedByte()
Definition: storage.cpp:155
TRACI_CONST double INVALID_DOUBLE_VALUE
TRACI_CONST int LAST_STEP_VEHICLE_ID_LIST
TRACI_CONST int VAR_INTERVAL_NUMBER
TRACI_CONST int LAST_STEP_VEHICLE_NUMBER
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int VAR_LAST_INTERVAL_IDS
TRACI_CONST int VAR_LAST_INTERVAL_NUMBER
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:338
TRACI_CONST int LAST_STEP_LENGTH
TRACI_CONST int VAR_POSITION
TRACI_CONST int LAST_STEP_MEAN_SPEED
TRACI_CONST int VAR_LAST_INTERVAL_OCCUPANCY
TRACI_CONST int VAR_INTERVAL_IDS
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:337
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_PARAMETER
TRACI_CONST int VAR_LANE_ID
TRACI_CONST int LAST_STEP_OCCUPANCY
TRACI_CONST int VAR_PARAMETER_WITH_KEY
TRACI_CONST int INVALID_INT_VALUE
TRACI_CONST int VAR_INTERVAL_SPEED
TRACI_CONST int VAR_LAST_INTERVAL_SPEED
TRACI_CONST int VAR_INTERVAL_OCCUPANCY
TRACI_CONST int LAST_STEP_TIME_SINCE_DETECTION
Struct to store the data of the counted vehicle internally.
Definition: MSInductLoop.h:286
mirrors MSInductLoop::VehicleData
Definition: TraCIDefs.h:416