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-2022 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/Helper.h>
29#include <libsumo/TraCIDefs.h>
31#include "InductionLoop.h"
32
33
34namespace libsumo {
35// ===========================================================================
36// static member initializations
37// ===========================================================================
38SubscriptionResults InductionLoop::mySubscriptionResults;
39ContextSubscriptionResults InductionLoop::myContextSubscriptionResults;
40NamedRTree* InductionLoop::myTree(nullptr);
41
42
43// ===========================================================================
44// member definitions
45// ===========================================================================
46std::vector<std::string>
47InductionLoop::getIDList() {
48 std::vector<std::string> ids;
50 return ids;
51}
52
53
54int
55InductionLoop::getIDCount() {
56 std::vector<std::string> ids;
58}
59
60
61double
62InductionLoop::getPosition(const std::string& detID) {
63 return getDetector(detID)->getPosition();
64}
65
66
67std::string
68InductionLoop::getLaneID(const std::string& detID) {
69 return getDetector(detID)->getLane()->getID();
70}
71
72
73int
74InductionLoop::getLastStepVehicleNumber(const std::string& detID) {
75 return (int)getDetector(detID)->getEnteredNumber((int)DELTA_T);
76}
77
78
79double
80InductionLoop::getLastStepMeanSpeed(const std::string& detID) {
81 return getDetector(detID)->getSpeed((int)DELTA_T);
82}
83
84
85std::vector<std::string>
86InductionLoop::getLastStepVehicleIDs(const std::string& detID) {
87 return getDetector(detID)->getVehicleIDs((int)DELTA_T);
88}
89
90
91double
92InductionLoop::getLastStepOccupancy(const std::string& detID) {
93 return getDetector(detID)->getOccupancy();
94}
95
96
97double
98InductionLoop::getLastStepMeanLength(const std::string& detID) {
99 return getDetector(detID)->getVehicleLength((int)DELTA_T);
100}
101
102
103double
104InductionLoop::getTimeSinceDetection(const std::string& detID) {
105 return getDetector(detID)->getTimeSinceLastDetection();
106}
107
108std::vector<libsumo::TraCIVehicleData>
109InductionLoop::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
124void
125InductionLoop::overrideTimeSinceDetection(const std::string& detID, double time) {
126 getDetector(detID)->overrideTimeSinceDetection(time);
127}
128
129
131InductionLoop::getDetector(const std::string& id) {
133 if (il == nullptr) {
134 throw TraCIException("Induction loop '" + id + "' is not known");
135 }
136 return il;
137}
138
139
140std::string
141InductionLoop::getParameter(const std::string& detID, const std::string& param) {
142 return getDetector(detID)->getParameter(param, "");
143}
144
145
147
148
149void
150InductionLoop::setParameter(const std::string& detID, const std::string& name, const std::string& value) {
151 getDetector(detID)->setParameter(name, value);
152}
153
154
156
157
159InductionLoop::getTree() {
160 if (myTree == nullptr) {
161 myTree = new NamedRTree();
162 for (const auto& i : MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP)) {
163 MSInductLoop* il = static_cast<MSInductLoop*>(i.second);
165 const float cmin[2] = {(float) p.x(), (float) p.y()};
166 const float cmax[2] = {(float) p.x(), (float) p.y()};
167 myTree->Insert(cmin, cmax, il);
168 }
169 }
170 return myTree;
171}
172
173void
174InductionLoop::cleanup() {
175 delete myTree;
176 myTree = nullptr;
177}
178
179void
180InductionLoop::storeShape(const std::string& id, PositionVector& shape) {
181 MSInductLoop* const il = getDetector(id);
182 shape.push_back(il->getLane()->getShape().positionAtOffset(il->getPosition()));
183}
184
185
186std::shared_ptr<VariableWrapper>
187InductionLoop::makeWrapper() {
188 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
189}
190
191
192bool
193InductionLoop::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
194 switch (variable) {
195 case TRACI_ID_LIST:
196 return wrapper->wrapStringList(objID, variable, getIDList());
197 case ID_COUNT:
198 return wrapper->wrapInt(objID, variable, getIDCount());
199 case VAR_POSITION:
200 return wrapper->wrapDouble(objID, variable, getPosition(objID));
201 case VAR_LANE_ID:
202 return wrapper->wrapString(objID, variable, getLaneID(objID));
204 return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
206 return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
208 return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
210 return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
211 case LAST_STEP_LENGTH:
212 return wrapper->wrapDouble(objID, variable, getLastStepMeanLength(objID));
214 return wrapper->wrapDouble(objID, variable, getTimeSinceDetection(objID));
216 paramData->readUnsignedByte();
217 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
219 paramData->readUnsignedByte();
220 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
221 default:
222 return false;
223 }
224}
225
226
227}
228
229
230/****************************************************************************/
SUMOTime DELTA_T
Definition: SUMOTime.cpp:37
#define SIMSTEP
Definition: SUMOTime.h:60
@ 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.
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:99
const PositionVector & getShape() const
Returns this lane's shape.
Definition: MSLane.h:506
const MSLane * getLane() const
Returns the lane the reminder works on.
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:442
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:183
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 int LAST_STEP_VEHICLE_ID_LIST
TRACI_CONST int LAST_STEP_VEHICLE_NUMBER
TRACI_CONST int TRACI_ID_LIST
std::map< std::string, libsumo::SubscriptionResults > ContextSubscriptionResults
Definition: TraCIDefs.h:301
TRACI_CONST int LAST_STEP_LENGTH
TRACI_CONST int VAR_POSITION
TRACI_CONST int LAST_STEP_MEAN_SPEED
std::map< std::string, libsumo::TraCIResults > SubscriptionResults
{object->{variable->value}}
Definition: TraCIDefs.h:300
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 LAST_STEP_TIME_SINCE_DETECTION
Struct to store the data of the counted vehicle internally.
Definition: MSInductLoop.h:281
mirrors MSInductLoop::VehicleData
Definition: TraCIDefs.h:379