Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
MSInstantInductLoop.cpp
Go to the documentation of this file.
1/****************************************************************************/
2// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3// Copyright (C) 2011-2026 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/****************************************************************************/
20// An instantaneous induction loop
21/****************************************************************************/
22#include <config.h>
23
24#include "MSInstantInductLoop.h"
25#include <algorithm>
26#include <cassert>
27#include <numeric>
28#include <utility>
29#ifdef HAVE_FOX
31#endif
35#include <microsim/MSLane.h>
36#include <microsim/MSVehicle.h>
37#include <microsim/MSNet.h>
42
43
44// ===========================================================================
45// method definitions
46// ===========================================================================
48 OutputDevice& od, MSLane* const lane, double positionInMeters,
49 const std::string name, const std::string& vTypes,
50 const std::string& nextEdges,
51 int detectPersons) :
52 MSMoveReminder(id, lane),
53 MSDetectorFileOutput(id, vTypes, nextEdges, detectPersons),
54 myName(name),
55 myOutputDevice(od),
56 myPosition(positionInMeters), myLastExitTime(-1) {
57 assert(myPosition >= 0 && myPosition <= myLane->getLength());
59}
60
61
64
65bool
67 // vehicles must be kept if the "inductionloop" wants to detect passeengers
68 if (!vehicleApplies(veh)) {
69 return false;
70 }
71#ifdef HAVE_FOX
72 ScopedLocker<> lock(myNotificationMutex, MSGlobals::gNumSimThreads > 1);
73#endif
74 if (reason != NOTIFICATION_JUNCTION) { // the junction case is handled in notifyMove
76 return false;
77 }
78 if (veh.getPositionOnLane() >= myPosition) {
79 double entryTime = SIMTIME;
80 if (myLastExitTime >= 0) {
81 write("enter", entryTime, veh, veh.getSpeed(), "gap", entryTime - myLastExitTime);
82 } else {
83 write("enter", entryTime, veh, veh.getSpeed());
84 }
85 myEntryTimes[&veh] = entryTime;
86 }
87 }
88 return true;
89}
90
91
92bool
94 double newPos, double newSpeed) {
95 if (!vehicleApplies(veh)) {
96 return false;
97 }
98 if (newPos < myPosition) {
99 // detector not reached yet
100 return true;
101 }
102#ifdef HAVE_FOX
103 ScopedLocker<> lock(myNotificationMutex, MSGlobals::gNumSimThreads > 1);
104#endif
105
106 const double oldSpeed = veh.getPreviousSpeed();
107 double enterSpeed = MSGlobals::gSemiImplicitEulerUpdate ? newSpeed : oldSpeed; // NOTE: For the euler update, the vehicle is assumed to travel at constant speed for the whole time step
108
109 if (newPos >= myPosition && oldPos < myPosition/* && static_cast<MSVehicle&>(veh).getLane() == myLane*/) {
110 const double timeBeforeEnter = MSCFModel::passingTime(oldPos, myPosition, newPos, oldSpeed, newSpeed);
111 const double entryTime = SIMTIME - TS + timeBeforeEnter;
112 enterSpeed = MSCFModel::speedAfterTime(timeBeforeEnter, oldSpeed, newPos - oldPos);
113 if (myLastExitTime >= 0) {
114 write("enter", entryTime, veh, enterSpeed, "gap", entryTime - myLastExitTime);
115 } else {
116 write("enter", entryTime, veh, enterSpeed);
117 }
118 myEntryTimes[&veh] = entryTime;
119 }
120 const double newBackPos = newPos - veh.getVehicleType().getLength();
121 const double oldBackPos = oldPos - veh.getVehicleType().getLength();
122 if (newBackPos > myPosition) {
123 std::map<SUMOTrafficObject*, double>::iterator i = myEntryTimes.find(&veh);
124 if (i != myEntryTimes.end()) {
125 // vehicle passed the detector
126 const double timeBeforeLeave = MSCFModel::passingTime(oldBackPos, myPosition, newBackPos, oldSpeed, newSpeed);
127 const double leaveTime = SIMTIME - TS + timeBeforeLeave;
128 write("leave", leaveTime, veh, newSpeed, "occupancy", leaveTime - (*i).second);
129 myEntryTimes.erase(i);
130 myLastExitTime = leaveTime;
131 }
132 return false;
133 }
134 // vehicle stays on the detector
135 write("stay", SIMTIME, veh, newSpeed);
136 return true;
137}
138
139
140void
141MSInstantInductLoop::write(const char* state, double t, SUMOTrafficObject& veh, double speed, const char* add, double addValue) {
142 if (!myOutputDevice.isNull()) {
143 // with threads, detectors sharing a file must not write while vehicles move
144 myEvents.push_back({t, veh.getNumericalID(), state, veh.getID(), speed, veh.getVehicleType().getLength(), veh.getVehicleType().getID(), add, addValue});
145 }
146}
147
148
149void
151 // vehicles of different lanes may report in any order
152 std::stable_sort(myEvents.begin(), myEvents.end(), [](const Event & a, const Event & b) {
153 return a.time < b.time || (a.time == b.time && a.numericalID < b.numericalID);
154 });
155 for (const Event& e : myEvents) {
156 myOutputDevice.openTag("instantOut").writeAttr(
157 "id", getID()).writeAttr("time", toString(e.time)).writeAttr("state", e.state).writeAttr(
158 "vehID", e.vehID).writeAttr("speed", toString(e.speed)).writeAttr(
159 "length", toString(e.length)).writeAttr(
160 "type", e.typeID);
161 if (e.add != nullptr) {
162 myOutputDevice.writeAttr(e.add, toString(e.addValue));
163 }
165 }
166 myEvents.clear();
167}
168
169
170bool
171MSInstantInductLoop::notifyLeave(SUMOTrafficObject& veh, double /* lastPos */, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
173 // vehicle might have jumped over detector at the end of the lane. we need
174 // one more notifyMove to register it
175 return true;
176 }
177#ifdef HAVE_FOX
178 ScopedLocker<> lock(myNotificationMutex, MSGlobals::gNumSimThreads > 1);
179#endif
180 std::map<SUMOTrafficObject*, double>::iterator i = myEntryTimes.find(&veh);
181 if (i != myEntryTimes.end()) {
182 write("leave", SIMTIME, veh, veh.getSpeed());
183 myEntryTimes.erase(i);
184 }
185 return false;
186}
187
188
189void
191 dev.writeXMLHeader("instantE1", "instant_e1_file.xsd");
192}
193
194
195/****************************************************************************/
long long int SUMOTime
Definition GUI.h:36
#define TS
Definition SUMOTime.h:45
#define SIMTIME
Definition SUMOTime.h:70
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:57
static double speedAfterTime(const double t, const double oldSpeed, const double dist)
Calculates the speed after a time t \in [0,TS] given the initial speed and the distance traveled in a...
static double passingTime(const double lastPos, const double passedPos, const double currentPos, const double lastSpeed, const double currentSpeed)
Calculates the time at which the position passedPosition has been passed In case of a ballistic updat...
Base of value-generating classes (detectors)
bool vehicleApplies(const SUMOTrafficObject &veh) const
Checks whether the detector measures vehicles of the given type.
static bool gSemiImplicitEulerUpdate
Definition MSGlobals.h:53
static int gNumSimThreads
how many threads to use for simulation
Definition MSGlobals.h:149
std::map< SUMOTrafficObject *, double > myEntryTimes
The last exit time.
std::vector< Event > myEvents
the events of this step
void detectorUpdate(const SUMOTime step)
writes the events of this step (detectors are updated serially in a fixed order)
MSInstantInductLoop(const std::string &id, OutputDevice &od, MSLane *const lane, double positionInMeters, const std::string name, const std::string &vTypes, const std::string &nextEdges, int detectPersons)
Constructor.
bool notifyEnter(SUMOTrafficObject &veh, Notification reason, const MSLane *enteredLane=0)
Checks whether the reminder is activated by a vehicle entering the lane.
OutputDevice & myOutputDevice
The output device to use.
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
void write(const char *state, double t, SUMOTrafficObject &veh, double speed, const char *add=0, double addValue=-1)
Writes an event line.
const double myPosition
Detector's position on lane [m].
bool notifyLeave(SUMOTrafficObject &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Dismisses the vehicle if it is on the detector due to a lane change.
double myLastExitTime
The last exit time.
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks whether the vehicle shall be counted and/or shall still touch this MSMoveReminder.
Representation of a lane in the micro simulation.
Definition MSLane.h:84
Something on a lane to be noticed about vehicle movement.
Notification
Definition of a vehicle state.
@ NOTIFICATION_JUNCTION
The vehicle arrived at a junction.
MSLane * myLane
Lane on which the reminder works.
const std::string & getID() const
Returns the name of the vehicle type.
double getLength() const
Get vehicle's length [m].
const std::string & getID() const
Returns the id.
Definition Named.h:73
Static storage of an output device and its base (abstract) implementation.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const ATTR_TYPE &attr, const T &val, const bool isNull=false, const bool escape=false)
writes a named attribute
virtual bool isNull()
returns the information whether the device will discard all output
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >(), bool includeConfig=true)
Writes an XML header with optional configuration.
Representation of a vehicle, person, or container.
virtual NumericalID getNumericalID() const =0
return the numerical ID which is only for internal usage
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
virtual double getPreviousSpeed() const =0
Returns the object's previous speed.
virtual double getSpeed() const =0
Returns the object's current speed.
virtual double getBackPositionOnLane(const MSLane *lane) const =0
Get the object's back position along the given lane.
virtual double getPositionOnLane() const =0
Get the object's position along the lane.
A scoped lock which only triggers on condition.
an event of this step, written in detectorUpdate