Eclipse 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-2019 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 /****************************************************************************/
17 // C++ TraCI client API implementation
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
28 #include <microsim/MSNet.h>
29 #include <libsumo/TraCIDefs.h>
30 #include <libsumo/TraCIConstants.h>
31 #include "InductionLoop.h"
32 
33 
34 namespace libsumo {
35 // ===========================================================================
36 // static member initializations
37 // ===========================================================================
40 
41 
42 // ===========================================================================
43 // member definitions
44 // ===========================================================================
45 std::vector<std::string>
47  std::vector<std::string> ids;
49  return ids;
50 }
51 
52 
53 int
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)->getPassedNumber((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  std::vector<MSInductLoop::VehicleData> vd = getDetector(detID)->collectVehiclesOnDet(SIMSTEP - DELTA_T, true);
111  std::vector<libsumo::TraCIVehicleData> tvd;
112  for (std::vector<MSInductLoop::VehicleData>::const_iterator vdi = vd.begin(); vdi != vd.end(); ++vdi) {
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 
124 
126 InductionLoop::getDetector(const std::string& id) {
127  MSInductLoop* il = dynamic_cast<MSInductLoop*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).get(id));
128  if (il == nullptr) {
129  throw TraCIException("Induction loop '" + id + "' is not known");
130  }
131  return il;
132 }
133 
134 
136 
137 
138 NamedRTree*
140  NamedRTree* t = new NamedRTree();
141  for (const auto& i : MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP)) {
142  MSInductLoop* il = static_cast<MSInductLoop*>(i.second);
144  const float cmin[2] = {(float) p.x(), (float) p.y()};
145  const float cmax[2] = {(float) p.x(), (float) p.y()};
146  t->Insert(cmin, cmax, il);
147  }
148  return t;
149 }
150 
151 
152 void
153 InductionLoop::storeShape(const std::string& id, PositionVector& shape) {
154  MSInductLoop* const il = getDetector(id);
155  shape.push_back(il->getLane()->getShape().positionAtOffset(il->getPosition()));
156 }
157 
158 
159 std::shared_ptr<VariableWrapper>
161  return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
162 }
163 
164 
165 bool
166 InductionLoop::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper) {
167  switch (variable) {
168  case TRACI_ID_LIST:
169  return wrapper->wrapStringList(objID, variable, getIDList());
170  case ID_COUNT:
171  return wrapper->wrapInt(objID, variable, getIDCount());
172  case VAR_POSITION:
173  return wrapper->wrapDouble(objID, variable, getPosition(objID));
174  case VAR_LANE_ID:
175  return wrapper->wrapString(objID, variable, getLaneID(objID));
177  return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
179  return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
181  return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
182  case LAST_STEP_OCCUPANCY:
183  return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
184  case LAST_STEP_LENGTH:
185  return wrapper->wrapDouble(objID, variable, getLastStepMeanLength(objID));
187  return wrapper->wrapDouble(objID, variable, getTimeSinceDetection(objID));
188  default:
189  return false;
190  }
191 }
192 
193 
194 }
195 
196 /****************************************************************************/
libsumo::VAR_LANE_ID
TRACI_CONST int VAR_LANE_ID
Definition: TraCIConstants.h:678
MSNet::getDetectorControl
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:399
MSInductLoop::getPosition
double getPosition() const
Returns the position of the detector on the lane.
Definition: MSInductLoop.h:93
MSNet.h
libsumo::InductionLoop::getDetector
static MSInductLoop * getDetector(const std::string &detID)
Definition: InductionLoop.cpp:126
MSDetectorControl.h
libsumo::VAR_POSITION
TRACI_CONST int VAR_POSITION
Definition: TraCIConstants.h:618
libsumo::VariableWrapper::wrapString
virtual bool wrapString(const std::string &objID, const int variable, const std::string &value)=0
MSInductLoop::getVehicleLength
double getVehicleLength(const int offset) const
Returns the length of the vehicle on the detector.
Definition: MSInductLoop.cpp:157
NamedObjectCont::insertIDs
void insertIDs(std::vector< std::string > &into) const
Definition: NamedObjectCont.h:123
DELTA_T
SUMOTime DELTA_T
Definition: SUMOTime.cpp:36
libsumo::VariableWrapper
Definition: Subscription.h:132
libsumo::InductionLoop::getVehicleData
static std::vector< libsumo::TraCIVehicleData > getVehicleData(const std::string &detID)
Definition: InductionLoop.cpp:109
libsumo::InductionLoop::getLastStepVehicleIDs
static std::vector< std::string > getLastStepVehicleIDs(const std::string &detID)
Definition: InductionLoop.cpp:85
libsumo::InductionLoop::getLaneID
static std::string getLaneID(const std::string &detID)
Definition: InductionLoop.cpp:67
libsumo::ContextSubscriptionResults
std::map< std::string, SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:204
MSInductLoop
An unextended detector measuring at a fixed position on a fixed lane.
Definition: MSInductLoop.h:63
libsumo::InductionLoop::getIDCount
static int getIDCount()
Definition: InductionLoop.cpp:54
libsumo::LAST_STEP_VEHICLE_ID_LIST
TRACI_CONST int LAST_STEP_VEHICLE_ID_LIST
Definition: TraCIConstants.h:519
libsumo::TraCIVehicleData
mirrors MSInductLoop::VehicleData
Definition: TraCIDefs.h:286
MSInductLoop::getTimeSinceLastDetection
double getTimeSinceLastDetection() const
Returns the time since the last vehicle left the detector.
Definition: MSInductLoop.cpp:195
PositionVector
A list of positions.
Definition: PositionVector.h:45
libsumo::InductionLoop::getLastStepOccupancy
static double getLastStepOccupancy(const std::string &detID)
Definition: InductionLoop.cpp:91
libsumo::InductionLoop::getPosition
static double getPosition(const std::string &detID)
Definition: InductionLoop.cpp:61
libsumo
Definition: Edge.cpp:29
libsumo::InductionLoop::myContextSubscriptionResults
static ContextSubscriptionResults myContextSubscriptionResults
Definition: InductionLoop.h:85
libsumo::VariableWrapper::wrapDouble
virtual bool wrapDouble(const std::string &objID, const int variable, const double value)=0
NamedObjectCont::size
int size() const
Returns the number of stored items within the container.
Definition: NamedObjectCont.h:116
TraCIConstants.h
NamedRTree::Insert
void Insert(const float a_min[2], const float a_max[2], Named *const &a_data)
Insert entry.
Definition: NamedRTree.h:81
MSDetectorControl::getTypedDetectors
const NamedObjectCont< MSDetectorFileOutput * > & getTypedDetectors(SumoXMLTag type) const
Returns the list of detectors of the given type.
Definition: MSDetectorControl.cpp:103
libsumo::VariableWrapper::wrapStringList
virtual bool wrapStringList(const std::string &objID, const int variable, const std::vector< std::string > &value)=0
PositionVector::positionAtOffset
Position positionAtOffset(double pos, double lateralOffset=0) const
Returns the position at the given length.
Definition: PositionVector.cpp:248
MSInductLoop::getVehicleIDs
std::vector< std::string > getVehicleIDs(const int offset) const
Returns the ids of vehicles that have passed the detector.
Definition: MSInductLoop.cpp:184
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
Position::x
double x() const
Returns the x-position.
Definition: Position.h:56
InductionLoop.h
libsumo::InductionLoop::getLastStepMeanLength
static double getLastStepMeanLength(const std::string &detID)
Definition: InductionLoop.cpp:97
libsumo::TRACI_ID_LIST
TRACI_CONST int TRACI_ID_LIST
Definition: TraCIConstants.h:498
SIMSTEP
#define SIMSTEP
Definition: SUMOTime.h:62
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION
#define LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(CLASS, DOMAIN)
Definition: TraCIDefs.h:50
libsumo::InductionLoop::handleVariable
static bool handleVariable(const std::string &objID, const int variable, VariableWrapper *wrapper)
Definition: InductionLoop.cpp:166
libsumo::ID_COUNT
TRACI_CONST int ID_COUNT
Definition: TraCIConstants.h:501
MSInductLoop::collectVehiclesOnDet
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.
Definition: MSInductLoop.cpp:281
libsumo::InductionLoop::getTree
static LIBSUMO_SUBSCRIPTION_API NamedRTree * getTree()
Returns a tree filled with inductive loop instances.
Definition: InductionLoop.cpp:139
libsumo::TraCIException
Definition: TraCIDefs.h:89
MSMoveReminder::getLane
const MSLane * getLane() const
Returns the lane the reminder works on.
Definition: MSMoveReminder.h:85
MSInductLoop::getPassedNumber
double getPassedNumber(const int offset) const
Returns the number of vehicles that have passed the detector.
Definition: MSInductLoop.cpp:178
MSLane::getShape
const PositionVector & getShape() const
Returns this lane's shape.
Definition: MSLane.h:477
MSNet::getInstance
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:167
Position::y
double y() const
Returns the y-position.
Definition: Position.h:61
libsumo::LAST_STEP_MEAN_SPEED
TRACI_CONST int LAST_STEP_MEAN_SPEED
Definition: TraCIConstants.h:516
libsumo::VariableWrapper::wrapInt
virtual bool wrapInt(const std::string &objID, const int variable, const int value)=0
libsumo::InductionLoop::mySubscriptionResults
static SubscriptionResults mySubscriptionResults
Definition: InductionLoop.h:84
MSInductLoop::getSpeed
double getSpeed(const int offset) const
Returns the speed of the vehicle on the detector.
Definition: MSInductLoop.cpp:150
NamedRTree
A RT-tree for efficient storing of SUMO's Named objects.
Definition: NamedRTree.h:63
libsumo::InductionLoop::getIDList
static std::vector< std::string > getIDList()
Definition: InductionLoop.cpp:46
config.h
libsumo::LAST_STEP_VEHICLE_NUMBER
TRACI_CONST int LAST_STEP_VEHICLE_NUMBER
Definition: TraCIConstants.h:513
libsumo::InductionLoop::getLastStepVehicleNumber
static int getLastStepVehicleNumber(const std::string &detID)
Definition: InductionLoop.cpp:73
MSInductLoop::getOccupancy
double getOccupancy() const
Returns the current occupancy.
Definition: MSInductLoop.cpp:164
SUMO_TAG_INDUCTION_LOOP
alternative tag for e1 detector
Definition: SUMOXMLDefinitions.h:65
MSInductLoop.h
libsumo::LAST_STEP_OCCUPANCY
TRACI_CONST int LAST_STEP_OCCUPANCY
Definition: TraCIConstants.h:522
libsumo::LAST_STEP_TIME_SINCE_DETECTION
TRACI_CONST int LAST_STEP_TIME_SINCE_DETECTION
Definition: TraCIConstants.h:531
libsumo::InductionLoop::makeWrapper
static std::shared_ptr< VariableWrapper > makeWrapper()
Definition: InductionLoop.cpp:160
libsumo::InductionLoop::storeShape
static void storeShape(const std::string &id, PositionVector &shape)
Saves the shape of the requested object in the given container.
Definition: InductionLoop.cpp:153
libsumo::InductionLoop::getLastStepMeanSpeed
static double getLastStepMeanSpeed(const std::string &detID)
Definition: InductionLoop.cpp:79
TraCIDefs.h
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
libsumo::LAST_STEP_LENGTH
TRACI_CONST int LAST_STEP_LENGTH
Definition: TraCIConstants.h:528
libsumo::SubscriptionResults
std::map< std::string, TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:203
libsumo::InductionLoop
Definition: InductionLoop.h:49
libsumo::InductionLoop::getTimeSinceDetection
static double getTimeSinceDetection(const std::string &detID)
Definition: InductionLoop.cpp:103