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.org/sumo
3 // Copyright (C) 2012-2020 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 <microsim/MSNet.h>
28 #include <libsumo/TraCIDefs.h>
29 #include <libsumo/TraCIConstants.h>
30 #include "InductionLoop.h"
31 
32 
33 namespace libsumo {
34 // ===========================================================================
35 // static member initializations
36 // ===========================================================================
37 SubscriptionResults InductionLoop::mySubscriptionResults;
38 ContextSubscriptionResults InductionLoop::myContextSubscriptionResults;
39 NamedRTree* InductionLoop::myTree(nullptr);
40 
41 
42 // ===========================================================================
43 // member definitions
44 // ===========================================================================
45 std::vector<std::string>
46 InductionLoop::getIDList() {
47  std::vector<std::string> ids;
49  return ids;
50 }
51 
52 
53 int
54 InductionLoop::getIDCount() {
55  std::vector<std::string> ids;
57 }
58 
59 
60 double
61 InductionLoop::getPosition(const std::string& detID) {
62  return getDetector(detID)->getPosition();
63 }
64 
65 
66 std::string
67 InductionLoop::getLaneID(const std::string& detID) {
68  return getDetector(detID)->getLane()->getID();
69 }
70 
71 
72 int
73 InductionLoop::getLastStepVehicleNumber(const std::string& detID) {
74  return (int)getDetector(detID)->getEnteredNumber((int)DELTA_T);
75 }
76 
77 
78 double
79 InductionLoop::getLastStepMeanSpeed(const std::string& detID) {
80  return getDetector(detID)->getSpeed((int)DELTA_T);
81 }
82 
83 
84 std::vector<std::string>
85 InductionLoop::getLastStepVehicleIDs(const std::string& detID) {
86  return getDetector(detID)->getVehicleIDs((int)DELTA_T);
87 }
88 
89 
90 double
91 InductionLoop::getLastStepOccupancy(const std::string& detID) {
92  return getDetector(detID)->getOccupancy();
93 }
94 
95 
96 double
97 InductionLoop::getLastStepMeanLength(const std::string& detID) {
98  return getDetector(detID)->getVehicleLength((int)DELTA_T);
99 }
100 
101 
102 double
103 InductionLoop::getTimeSinceDetection(const std::string& detID) {
104  return getDetector(detID)->getTimeSinceLastDetection();
105 }
106 
107 
108 std::vector<libsumo::TraCIVehicleData>
109 InductionLoop::getVehicleData(const std::string& detID) {
110  const std::vector<MSInductLoop::VehicleData> vd = getDetector(detID)->collectVehiclesOnDet(SIMSTEP - DELTA_T, true, true);
111  std::vector<libsumo::TraCIVehicleData> tvd;
112  for (const MSInductLoop::VehicleData& vdi : vd) {
113  tvd.push_back(libsumo::TraCIVehicleData());
114  tvd.back().id = vdi.idM;
115  tvd.back().length = vdi.lengthM;
116  tvd.back().entryTime = vdi.entryTimeM;
117  tvd.back().leaveTime = vdi.leaveTimeM;
118  tvd.back().typeID = vdi.typeIDM;
119  }
120  return tvd;
121 }
122 
123 
125 InductionLoop::getDetector(const std::string& id) {
127  if (il == nullptr) {
128  throw TraCIException("Induction loop '" + id + "' is not known");
129  }
130  return il;
131 }
132 
133 
134 std::string
135 InductionLoop::getParameter(const std::string& detID, const std::string& param) {
136  return getDetector(detID)->getParameter(param, "");
137 }
138 
139 
141 
142 
143 void
144 InductionLoop::setParameter(const std::string& detID, const std::string& name, const std::string& value) {
145  getDetector(detID)->setParameter(name, value);
146 }
147 
148 
150 
151 
152 NamedRTree*
153 InductionLoop::getTree() {
154  if (myTree == nullptr) {
155  myTree = new NamedRTree();
156  for (const auto& i : MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP)) {
157  MSInductLoop* il = static_cast<MSInductLoop*>(i.second);
159  const float cmin[2] = {(float) p.x(), (float) p.y()};
160  const float cmax[2] = {(float) p.x(), (float) p.y()};
161  myTree->Insert(cmin, cmax, il);
162  }
163  }
164  return myTree;
165 }
166 
167 void
168 InductionLoop::cleanup() {
169  delete myTree;
170  myTree = nullptr;
171 }
172 
173 void
174 InductionLoop::storeShape(const std::string& id, PositionVector& shape) {
175  MSInductLoop* const il = getDetector(id);
176  shape.push_back(il->getLane()->getShape().positionAtOffset(il->getPosition()));
177 }
178 
179 
180 std::shared_ptr<VariableWrapper>
181 InductionLoop::makeWrapper() {
182  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
183 }
184 
185 
186 bool
187 InductionLoop::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
188  switch (variable) {
189  case TRACI_ID_LIST:
190  return wrapper->wrapStringList(objID, variable, getIDList());
191  case ID_COUNT:
192  return wrapper->wrapInt(objID, variable, getIDCount());
193  case VAR_POSITION:
194  return wrapper->wrapDouble(objID, variable, getPosition(objID));
195  case VAR_LANE_ID:
196  return wrapper->wrapString(objID, variable, getLaneID(objID));
198  return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
200  return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
202  return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
203  case LAST_STEP_OCCUPANCY:
204  return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
205  case LAST_STEP_LENGTH:
206  return wrapper->wrapDouble(objID, variable, getLastStepMeanLength(objID));
208  return wrapper->wrapDouble(objID, variable, getTimeSinceDetection(objID));
209  default:
210  return false;
211  }
212 }
213 
214 
215 }
216 
217 
218 /****************************************************************************/
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
#define SIMSTEP
Definition: SUMOTime.h:59
@ SUMO_TAG_INDUCTION_LOOP
alternative tag for e1 detector
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:57
#define LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(CLASS)
Definition: TraCIDefs.h:104
C++ TraCI client API implementation.
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:62
double getPosition() const
Returns the position of the detector on the lane.
Definition: MSInductLoop.h:93
const PositionVector & getShape() const
Returns this lane's shape.
Definition: MSLane.h:476
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:171
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:434
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:60
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:36
double x() const
Returns the x-position.
Definition: Position.h:54
double y() const
Returns the y-position.
Definition: Position.h:59
A list of positions.
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
TRACI_CONST int LAST_STEP_VEHICLE_ID_LIST
TRACI_CONST int LAST_STEP_VEHICLE_NUMBER
TRACI_CONST int TRACI_ID_LIST
TRACI_CONST int LAST_STEP_LENGTH
TRACI_CONST int VAR_POSITION
TRACI_CONST int LAST_STEP_MEAN_SPEED
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:250
TRACI_CONST int ID_COUNT
TRACI_CONST int VAR_LANE_ID
TRACI_CONST int LAST_STEP_OCCUPANCY
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:251
TRACI_CONST int LAST_STEP_TIME_SINCE_DETECTION
Struct to store the data of the counted vehicle internally.
Definition: MSInductLoop.h:249
mirrors MSInductLoop::VehicleData
Definition: TraCIDefs.h:328