SUMO - Simulation of Urban MObility
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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // C++ TraCI client API implementation
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
29 #include <microsim/MSNet.h>
30 #include <libsumo/TraCIDefs.h>
32 #include "InductionLoop.h"
33 
34 
35 namespace libsumo {
36 // ===========================================================================
37 // static member initializations
38 // ===========================================================================
41 
42 
43 // ===========================================================================
44 // member definitions
45 // ===========================================================================
46 std::vector<std::string>
48  std::vector<std::string> ids;
50  return ids;
51 }
52 
53 
54 int
56  std::vector<std::string> ids;
58 }
59 
60 
61 double
62 InductionLoop::getPosition(const std::string& detID) {
63  return getDetector(detID)->getPosition();
64 }
65 
66 
67 std::string
68 InductionLoop::getLaneID(const std::string& detID) {
69  return getDetector(detID)->getLane()->getID();
70 }
71 
72 
73 int
74 InductionLoop::getLastStepVehicleNumber(const std::string& detID) {
75  return getDetector(detID)->getCurrentPassedNumber();
76 }
77 
78 
79 double
80 InductionLoop::getLastStepMeanSpeed(const std::string& detID) {
81  return getDetector(detID)->getCurrentSpeed();
82 }
83 
84 
85 std::vector<std::string>
86 InductionLoop::getLastStepVehicleIDs(const std::string& detID) {
87  return getDetector(detID)->getCurrentVehicleIDs();
88 }
89 
90 
91 double
92 InductionLoop::getLastStepOccupancy(const std::string& detID) {
93  return getDetector(detID)->getCurrentOccupancy();
94 }
95 
96 
97 double
98 InductionLoop::getLastStepMeanLength(const std::string& detID) {
99  return getDetector(detID)->getCurrentLength();
100 }
101 
102 
103 double
104 InductionLoop::getTimeSinceDetection(const std::string& detID) {
105  return getDetector(detID)->getTimeSinceLastDetection();
106 }
107 
108 
109 std::vector<libsumo::TraCIVehicleData>
110 InductionLoop::getVehicleData(const std::string& detID) {
111  std::vector<MSInductLoop::VehicleData> vd = getDetector(detID)->collectVehiclesOnDet(MSNet::getInstance()->getCurrentTimeStep() - DELTA_T, true);
112  std::vector<libsumo::TraCIVehicleData> tvd;
113  for (std::vector<MSInductLoop::VehicleData>::const_iterator vdi = vd.begin(); vdi != vd.end(); ++vdi) {
114  tvd.push_back(libsumo::TraCIVehicleData());
115  tvd.back().id = vdi->idM;
116  tvd.back().length = vdi->lengthM;
117  tvd.back().entryTime = vdi->entryTimeM;
118  tvd.back().leaveTime = vdi->leaveTimeM;
119  tvd.back().typeID = vdi->typeIDM;
120  }
121  return tvd;
122 
123 }
124 
125 
127 InductionLoop::getDetector(const std::string& id) {
129  if (il == nullptr) {
130  throw TraCIException("Induction loop '" + id + "' is not known");
131  }
132  return il;
133 }
134 
135 
137 
138 
139 NamedRTree*
141  NamedRTree* t = new NamedRTree();
142  for (const auto& i : MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP)) {
143  MSInductLoop* il = static_cast<MSInductLoop*>(i.second);
145  const float cmin[2] = {(float) p.x(), (float) p.y()};
146  const float cmax[2] = {(float) p.x(), (float) p.y()};
147  t->Insert(cmin, cmax, il);
148  }
149  return t;
150 }
151 
152 
153 void
154 InductionLoop::storeShape(const std::string& id, PositionVector& shape) {
155  MSInductLoop* const il = getDetector(id);
156  shape.push_back(il->getLane()->getShape().positionAtOffset(il->getPosition()));
157 }
158 
159 
160 std::shared_ptr<VariableWrapper>
162  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
163 }
164 
165 
166 bool
167 InductionLoop::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
168  switch (variable) {
169  case TRACI_ID_LIST:
170  return wrapper->wrapStringList(objID, variable, getIDList());
171  case ID_COUNT:
172  return wrapper->wrapInt(objID, variable, getIDCount());
173  case VAR_POSITION:
174  return wrapper->wrapDouble(objID, variable, getPosition(objID));
175  case VAR_LANE_ID:
176  return wrapper->wrapString(objID, variable, getLaneID(objID));
178  return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
180  return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
182  return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
183  case LAST_STEP_OCCUPANCY:
184  return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
185  case LAST_STEP_LENGTH:
186  return wrapper->wrapDouble(objID, variable, getLastStepMeanLength(objID));
188  return wrapper->wrapDouble(objID, variable, getTimeSinceDetection(objID));
189  default:
190  return false;
191  }
192 }
193 
194 
195 }
196 
197 /****************************************************************************/
#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:83
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:199
const MSLane * getLane() const
Returns the lane the reminder works on.
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
alternative tag for e1 detector
#define VAR_POSITION
mirrors MSInductLoop::VehicleData
Definition: TraCIDefs.h:272
static std::string getLaneID(const std::string &detID)
int size() const
Returns the number of stored items within the container.
double y() const
Returns the y-position.
Definition: Position.h:62
std::vector< std::string > getCurrentVehicleIDs() const
Returns the ids of vehicles that have passed the detector.
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:65
double x() const
Returns the x-position.
Definition: Position.h:57
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:165
SUMOTime DELTA_T
Definition: SUMOTime.cpp:35
double getCurrentLength() const
Returns the length of the vehicle on the detector.
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:456
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:200
const std::string & getID() const
Returns the id.
Definition: Named.h:78
virtual bool wrapString(const std::string &objID, const int variable, const std::string &value)=0
static std::shared_ptr< VariableWrapper > makeWrapper()
void insertIDs(std::vector< std::string > &into) const
static void storeShape(const std::string &id, PositionVector &shape)
Saves the shape of the requested object in the given container.
static std::vector< std::string > getIDList()
#define LAST_STEP_LENGTH
static double getLastStepOccupancy(const std::string &detID)
#define LAST_STEP_TIME_SINCE_DETECTION
#define TRACI_ID_LIST
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
A list of positions.
double getCurrentSpeed() const
Returns the speed of the vehicle on the detector.
static double getTimeSinceDetection(const std::string &detID)
double getPosition() const
Returns the position of the detector on the lane.
Definition: MSInductLoop.h:94
static std::vector< std::string > getLastStepVehicleIDs(const std::string &detID)
static MSInductLoop * getDetector(const std::string &detID)
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:52
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:379
Definition: Edge.cpp:30
double getTimeSinceLastDetection() const
Returns the time since the last vehicle left the detector.
virtual bool wrapDouble(const std::string &objID, const int variable, const double value)=0
#define LAST_STEP_VEHICLE_NUMBER
int getCurrentPassedNumber() const
Returns the number of vehicles that have passed the detector.
static int getLastStepVehicleNumber(const std::string &detID)
static std::vector< libsumo::TraCIVehicleData > getVehicleData(const std::string &detID)
#define LAST_STEP_VEHICLE_ID_LIST
static SubscriptionResults mySubscriptionResults
Definition: InductionLoop.h:85
static double getLastStepMeanSpeed(const std::string &detID)
static LIBSUMO_SUBSCRIPTION_API NamedRTree * getTree()
Returns a tree filled with inductive loop instances.
virtual std::vector< VehicleData > collectVehiclesOnDet(SUMOTime t, bool leaveTime=false) const
Returns vehicle data for vehicles that have been on the detector starting at the given time...
#define LAST_STEP_OCCUPANCY
static double getPosition(const std::string &detID)
#define ID_COUNT
#define VAR_LANE_ID
virtual bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
double getCurrentOccupancy() const
Returns the current occupancy.
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
static ContextSubscriptionResults myContextSubscriptionResults
Definition: InductionLoop.h:86
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:64
static double getLastStepMeanLength(const std::string &detID)