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-2023 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
124double
125InductionLoop::getIntervalOccupancy(const std::string& detID) {
126 return getDetector(detID)->getIntervalOccupancy();
127}
128
129
130double
131InductionLoop::getIntervalMeanSpeed(const std::string& detID) {
132 return getDetector(detID)->getIntervalMeanSpeed();
133}
134
135
136int
137InductionLoop::getIntervalVehicleNumber(const std::string& detID) {
138 return getDetector(detID)->getIntervalVehicleNumber();
139}
140
141
142std::vector<std::string>
143InductionLoop::getIntervalVehicleIDs(const std::string& detID) {
144 return getDetector(detID)->getIntervalVehicleIDs();
145}
146
147
148double
149InductionLoop::getLastIntervalOccupancy(const std::string& detID) {
150 return getDetector(detID)->getIntervalOccupancy(true);
151}
152
153
154double
155InductionLoop::getLastIntervalMeanSpeed(const std::string& detID) {
156 return getDetector(detID)->getIntervalMeanSpeed(true);
157}
158
159
160int
161InductionLoop::getLastIntervalVehicleNumber(const std::string& detID) {
162 return getDetector(detID)->getIntervalVehicleNumber(true);
163}
164
165
166std::vector<std::string>
167InductionLoop::getLastIntervalVehicleIDs(const std::string& detID) {
168 return getDetector(detID)->getIntervalVehicleIDs(true);
169}
170
171
172void
173InductionLoop::overrideTimeSinceDetection(const std::string& detID, double time) {
174 getDetector(detID)->overrideTimeSinceDetection(time);
175}
176
177
179InductionLoop::getDetector(const std::string& id) {
181 if (il == nullptr) {
182 throw TraCIException("Induction loop '" + id + "' is not known");
183 }
184 return il;
185}
186
187
188std::string
189InductionLoop::getParameter(const std::string& detID, const std::string& param) {
190 return getDetector(detID)->getParameter(param, "");
191}
192
193
195
196
197void
198InductionLoop::setParameter(const std::string& detID, const std::string& name, const std::string& value) {
199 getDetector(detID)->setParameter(name, value);
200}
201
202
204
205
207InductionLoop::getTree() {
208 if (myTree == nullptr) {
209 myTree = new NamedRTree();
210 for (const auto& i : MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP)) {
211 MSInductLoop* il = static_cast<MSInductLoop*>(i.second);
213 const float cmin[2] = {(float) p.x(), (float) p.y()};
214 const float cmax[2] = {(float) p.x(), (float) p.y()};
215 myTree->Insert(cmin, cmax, il);
216 }
217 }
218 return myTree;
219}
220
221void
222InductionLoop::cleanup() {
223 delete myTree;
224 myTree = nullptr;
225}
226
227void
228InductionLoop::storeShape(const std::string& id, PositionVector& shape) {
229 MSInductLoop* const il = getDetector(id);
230 shape.push_back(il->getLane()->getShape().positionAtOffset(il->getPosition()));
231}
232
233
234std::shared_ptr<VariableWrapper>
235InductionLoop::makeWrapper() {
236 return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
237}
238
239
240bool
241InductionLoop::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
242 switch (variable) {
243 case TRACI_ID_LIST:
244 return wrapper->wrapStringList(objID, variable, getIDList());
245 case ID_COUNT:
246 return wrapper->wrapInt(objID, variable, getIDCount());
247 case VAR_POSITION:
248 return wrapper->wrapDouble(objID, variable, getPosition(objID));
249 case VAR_LANE_ID:
250 return wrapper->wrapString(objID, variable, getLaneID(objID));
252 return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
254 return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
256 return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
258 return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
259 case LAST_STEP_LENGTH:
260 return wrapper->wrapDouble(objID, variable, getLastStepMeanLength(objID));
262 return wrapper->wrapDouble(objID, variable, getTimeSinceDetection(objID));
264 return wrapper->wrapDouble(objID, variable, getIntervalOccupancy(objID));
266 return wrapper->wrapDouble(objID, variable, getIntervalMeanSpeed(objID));
268 return wrapper->wrapInt(objID, variable, getIntervalVehicleNumber(objID));
269 case VAR_INTERVAL_IDS:
270 return wrapper->wrapStringList(objID, variable, getIntervalVehicleIDs(objID));
272 return wrapper->wrapDouble(objID, variable, getLastIntervalOccupancy(objID));
274 return wrapper->wrapDouble(objID, variable, getLastIntervalMeanSpeed(objID));
276 return wrapper->wrapInt(objID, variable, getLastIntervalVehicleNumber(objID));
278 return wrapper->wrapStringList(objID, variable, getLastIntervalVehicleIDs(objID));
280 paramData->readUnsignedByte();
281 return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
283 paramData->readUnsignedByte();
284 return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
285 default:
286 return false;
287 }
288}
289
290
291}
292
293
294/****************************************************************************/
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
virtual const PositionVector & getShape(bool) const
Definition: MSLane.h:293
const MSLane * getLane() const
Returns the lane the reminder works on.
MSDetectorControl & getDetectorControl()
Returns the detector control.
Definition: MSNet.h:441
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 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 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:285
mirrors MSInductLoop::VehicleData
Definition: TraCIDefs.h:416