forked from eclipse-sumo/sumo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInductionLoop.cpp
349 lines (282 loc) · 11.9 KB
/
InductionLoop.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/****************************************************************************/
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
// Copyright (C) 2012-2024 German Aerospace Center (DLR) and others.
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0/
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License 2.0 are satisfied: GNU General Public License, version 2
// or later which is available at
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
/****************************************************************************/
/// @file InductionLoop.cpp
/// @author Daniel Krajzewicz
/// @author Mario Krumnow
/// @author Jakob Erdmann
/// @author Michael Behrisch
/// @date 30.05.2012
///
// C++ TraCI client API implementation
/****************************************************************************/
#include <config.h>
#include <microsim/output/MSDetectorControl.h>
#include <microsim/output/MSInductLoop.h>
#include <mesosim/MEInductLoop.h>
#include <microsim/MSNet.h>
#include <microsim/MSEdge.h>
#include <libsumo/Helper.h>
#include <libsumo/TraCIDefs.h>
#include <libsumo/TraCIConstants.h>
#include "InductionLoop.h"
namespace libsumo {
// ===========================================================================
// static member initializations
// ===========================================================================
SubscriptionResults InductionLoop::mySubscriptionResults;
ContextSubscriptionResults InductionLoop::myContextSubscriptionResults;
NamedRTree* InductionLoop::myTree(nullptr);
// ===========================================================================
// member definitions
// ===========================================================================
std::vector<std::string>
InductionLoop::getIDList() {
std::vector<std::string> ids;
MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).insertIDs(ids);
return ids;
}
int
InductionLoop::getIDCount() {
std::vector<std::string> ids;
return (int)MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).size();
}
double
InductionLoop::getPosition(const std::string& loopID) {
return getDetector(loopID)->getPosition();
}
std::string
InductionLoop::getLaneID(const std::string& loopID) {
return getDetector(loopID)->getLane()->getID();
}
int
InductionLoop::getLastStepVehicleNumber(const std::string& loopID) {
return (int)getDetector(loopID)->getEnteredNumber((int)DELTA_T);
}
double
InductionLoop::getLastStepMeanSpeed(const std::string& loopID) {
return getDetector(loopID)->getSpeed((int)DELTA_T);
}
std::vector<std::string>
InductionLoop::getLastStepVehicleIDs(const std::string& loopID) {
return getDetector(loopID)->getVehicleIDs((int)DELTA_T);
}
double
InductionLoop::getLastStepOccupancy(const std::string& loopID) {
return getDetector(loopID)->getOccupancy();
}
double
InductionLoop::getLastStepMeanLength(const std::string& loopID) {
return getDetector(loopID)->getVehicleLength((int)DELTA_T);
}
double
InductionLoop::getTimeSinceDetection(const std::string& loopID) {
return getDetector(loopID)->getTimeSinceLastDetection();
}
std::vector<libsumo::TraCIVehicleData>
InductionLoop::getVehicleData(const std::string& loopID) {
const std::vector<MSInductLoop::VehicleData> vd = getDetector(loopID)->collectVehiclesOnDet(SIMSTEP - DELTA_T, true, true);
std::vector<libsumo::TraCIVehicleData> tvd;
for (const MSInductLoop::VehicleData& vdi : vd) {
tvd.push_back(libsumo::TraCIVehicleData());
tvd.back().id = vdi.idM;
tvd.back().length = vdi.lengthM;
tvd.back().entryTime = vdi.entryTimeM;
tvd.back().leaveTime = vdi.leaveTimeM;
tvd.back().typeID = vdi.typeIDM;
}
return tvd;
}
double
InductionLoop::getIntervalOccupancy(const std::string& loopID) {
if (MSGlobals::gUseMesoSim) {
const MEInductLoop* det = getMEDetector(loopID);
const auto& meanData = det->getMeanData();
return meanData.getOccupancy(SIMSTEP - meanData.getResetTime(), det->getEdge().getNumLanes());
} else {
return getDetector(loopID)->getIntervalOccupancy();
}
}
double
InductionLoop::getIntervalMeanSpeed(const std::string& loopID) {
if (MSGlobals::gUseMesoSim) {
const MEInductLoop* det = getMEDetector(loopID);
const auto& meanData = det->getMeanData();
if (meanData.getSamples() != 0) {
return meanData.getTravelledDistance() / meanData.getSamples();
} else {
const double defaultTravelTime = det->getEdge().getLength() / det->getEdge().getSpeedLimit();
return meanData.getLaneLength() / defaultTravelTime;
}
} else {
return getDetector(loopID)->getIntervalMeanSpeed();
}
}
int
InductionLoop::getIntervalVehicleNumber(const std::string& loopID) {
if (MSGlobals::gUseMesoSim) {
const auto& meanData = getMEDetector(loopID)->getMeanData();
return meanData.nVehDeparted + meanData.nVehEntered;
} else {
return getDetector(loopID)->getIntervalVehicleNumber();
}
}
std::vector<std::string>
InductionLoop::getIntervalVehicleIDs(const std::string& loopID) {
if (MSGlobals::gUseMesoSim) {
WRITE_ERROR("getIntervalVehicleIDs not applicable for meso");
return std::vector<std::string>();
}
return getDetector(loopID)->getIntervalVehicleIDs();
}
double
InductionLoop::getLastIntervalOccupancy(const std::string& loopID) {
if (MSGlobals::gUseMesoSim) {
WRITE_ERROR("getLastIntervalOccupancy not applicable for meso");
return INVALID_DOUBLE_VALUE;
}
return getDetector(loopID)->getIntervalOccupancy(true);
}
double
InductionLoop::getLastIntervalMeanSpeed(const std::string& loopID) {
if (MSGlobals::gUseMesoSim) {
WRITE_ERROR("getLastIntervalMeanSpeed not applicable for meso");
return INVALID_DOUBLE_VALUE;
}
return getDetector(loopID)->getIntervalMeanSpeed(true);
}
int
InductionLoop::getLastIntervalVehicleNumber(const std::string& loopID) {
if (MSGlobals::gUseMesoSim) {
WRITE_ERROR("getLastIntervalVehicleNumber not applicable for meso");
return INVALID_DOUBLE_VALUE;
}
return getDetector(loopID)->getIntervalVehicleNumber(true);
}
std::vector<std::string>
InductionLoop::getLastIntervalVehicleIDs(const std::string& loopID) {
if (MSGlobals::gUseMesoSim) {
WRITE_ERROR("getLastIntervalVehicleIDs not applicable for meso");
return std::vector<std::string>();
}
return getDetector(loopID)->getIntervalVehicleIDs(true);
}
void
InductionLoop::overrideTimeSinceDetection(const std::string& loopID, double time) {
getDetector(loopID)->overrideTimeSinceDetection(time);
}
MSInductLoop*
InductionLoop::getDetector(const std::string& id) {
MSInductLoop* il = dynamic_cast<MSInductLoop*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).get(id));
if (il == nullptr) {
throw TraCIException("Induction loop '" + id + "' is not known");
}
return il;
}
MEInductLoop*
InductionLoop::getMEDetector(const std::string& id) {
MEInductLoop* il = dynamic_cast<MEInductLoop*>(MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP).get(id));
if (il == nullptr) {
throw TraCIException("Induction loop '" + id + "' is not known");
}
return il;
}
std::string
InductionLoop::getParameter(const std::string& loopID, const std::string& param) {
return getDetector(loopID)->getParameter(param, "");
}
LIBSUMO_GET_PARAMETER_WITH_KEY_IMPLEMENTATION(InductionLoop)
void
InductionLoop::setParameter(const std::string& loopID, const std::string& name, const std::string& value) {
getDetector(loopID)->setParameter(name, value);
}
LIBSUMO_SUBSCRIPTION_IMPLEMENTATION(InductionLoop, INDUCTIONLOOP)
NamedRTree*
InductionLoop::getTree() {
if (myTree == nullptr) {
myTree = new NamedRTree();
for (const auto& i : MSNet::getInstance()->getDetectorControl().getTypedDetectors(SUMO_TAG_INDUCTION_LOOP)) {
MSInductLoop* il = static_cast<MSInductLoop*>(i.second);
Position p = il->getLane()->getShape().positionAtOffset(il->getPosition());
const float cmin[2] = {(float) p.x(), (float) p.y()};
const float cmax[2] = {(float) p.x(), (float) p.y()};
myTree->Insert(cmin, cmax, il);
}
}
return myTree;
}
void
InductionLoop::cleanup() {
delete myTree;
myTree = nullptr;
}
void
InductionLoop::storeShape(const std::string& id, PositionVector& shape) {
MSInductLoop* const il = getDetector(id);
shape.push_back(il->getLane()->getShape().positionAtOffset(il->getPosition()));
}
std::shared_ptr<VariableWrapper>
InductionLoop::makeWrapper() {
return std::make_shared<Helper::SubscriptionWrapper>(handleVariable, mySubscriptionResults, myContextSubscriptionResults);
}
bool
InductionLoop::handleVariable(const std::string& objID, const int variable, VariableWrapper* wrapper, tcpip::Storage* paramData) {
switch (variable) {
case TRACI_ID_LIST:
return wrapper->wrapStringList(objID, variable, getIDList());
case ID_COUNT:
return wrapper->wrapInt(objID, variable, getIDCount());
case VAR_POSITION:
return wrapper->wrapDouble(objID, variable, getPosition(objID));
case VAR_LANE_ID:
return wrapper->wrapString(objID, variable, getLaneID(objID));
case LAST_STEP_VEHICLE_NUMBER:
return wrapper->wrapInt(objID, variable, getLastStepVehicleNumber(objID));
case LAST_STEP_MEAN_SPEED:
return wrapper->wrapDouble(objID, variable, getLastStepMeanSpeed(objID));
case LAST_STEP_VEHICLE_ID_LIST:
return wrapper->wrapStringList(objID, variable, getLastStepVehicleIDs(objID));
case LAST_STEP_OCCUPANCY:
return wrapper->wrapDouble(objID, variable, getLastStepOccupancy(objID));
case LAST_STEP_LENGTH:
return wrapper->wrapDouble(objID, variable, getLastStepMeanLength(objID));
case LAST_STEP_TIME_SINCE_DETECTION:
return wrapper->wrapDouble(objID, variable, getTimeSinceDetection(objID));
case VAR_INTERVAL_OCCUPANCY:
return wrapper->wrapDouble(objID, variable, getIntervalOccupancy(objID));
case VAR_INTERVAL_SPEED:
return wrapper->wrapDouble(objID, variable, getIntervalMeanSpeed(objID));
case VAR_INTERVAL_NUMBER:
return wrapper->wrapInt(objID, variable, getIntervalVehicleNumber(objID));
case VAR_INTERVAL_IDS:
return wrapper->wrapStringList(objID, variable, getIntervalVehicleIDs(objID));
case VAR_LAST_INTERVAL_OCCUPANCY:
return wrapper->wrapDouble(objID, variable, getLastIntervalOccupancy(objID));
case VAR_LAST_INTERVAL_SPEED:
return wrapper->wrapDouble(objID, variable, getLastIntervalMeanSpeed(objID));
case VAR_LAST_INTERVAL_NUMBER:
return wrapper->wrapInt(objID, variable, getLastIntervalVehicleNumber(objID));
case VAR_LAST_INTERVAL_IDS:
return wrapper->wrapStringList(objID, variable, getLastIntervalVehicleIDs(objID));
case libsumo::VAR_PARAMETER:
paramData->readUnsignedByte();
return wrapper->wrapString(objID, variable, getParameter(objID, paramData->readString()));
case libsumo::VAR_PARAMETER_WITH_KEY:
paramData->readUnsignedByte();
return wrapper->wrapStringPair(objID, variable, getParameterWithKey(objID, paramData->readString()));
default:
return false;
}
}
}
/****************************************************************************/