Eclipse SUMO - Simulation of Urban MObility
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-2024 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 <cassert>
26 #include <numeric>
27 #include <utility>
28 #ifdef HAVE_FOX
30 #endif
32 #include <utils/common/ToString.h>
34 #include <microsim/MSLane.h>
35 #include <microsim/MSVehicle.h>
36 #include <microsim/MSNet.h>
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
47  OutputDevice& od, MSLane* const lane, double positionInMeters,
48  const std::string name, const std::string& vTypes,
49  const std::string& nextEdges) :
50  MSMoveReminder(id, lane),
51  MSDetectorFileOutput(id, vTypes, nextEdges),
52  myName(name),
53  myOutputDevice(od),
54  myPosition(positionInMeters), myLastExitTime(-1) {
55  assert(myPosition >= 0 && myPosition <= myLane->getLength());
57 }
58 
59 
61 }
62 
63 
64 bool
66  double newPos, double newSpeed) {
67  if (!vehicleApplies(veh)) {
68  return false;
69  }
70  if (newPos < myPosition) {
71  // detector not reached yet
72  return true;
73  }
74 #ifdef HAVE_FOX
75  ScopedLocker<> lock(myNotificationMutex, MSGlobals::gNumSimThreads > 1);
76 #endif
77 
78  const double oldSpeed = veh.getPreviousSpeed();
79  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
80 
81  if (newPos >= myPosition && oldPos < myPosition/* && static_cast<MSVehicle&>(veh).getLane() == myLane*/) {
82  const double timeBeforeEnter = MSCFModel::passingTime(oldPos, myPosition, newPos, oldSpeed, newSpeed);
83  const double entryTime = SIMTIME - TS + timeBeforeEnter;
84  enterSpeed = MSCFModel::speedAfterTime(timeBeforeEnter, oldSpeed, newPos - oldPos);
85  if (myLastExitTime >= 0) {
86  write("enter", entryTime, veh, enterSpeed, "gap", entryTime - myLastExitTime);
87  } else {
88  write("enter", entryTime, veh, enterSpeed);
89  }
90  myEntryTimes[&veh] = entryTime;
91  }
92  const double newBackPos = newPos - veh.getVehicleType().getLength();
93  const double oldBackPos = oldPos - veh.getVehicleType().getLength();
94  if (newBackPos > myPosition) {
95  std::map<SUMOTrafficObject*, double>::iterator i = myEntryTimes.find(&veh);
96  if (i != myEntryTimes.end()) {
97  // vehicle passed the detector
98  const double timeBeforeLeave = MSCFModel::passingTime(oldBackPos, myPosition, newBackPos, oldSpeed, newSpeed);
99  const double leaveTime = SIMTIME - TS + timeBeforeLeave;
100  write("leave", leaveTime, veh, newSpeed, "occupancy", leaveTime - (*i).second);
101  myEntryTimes.erase(i);
102  myLastExitTime = leaveTime;
103  }
104  return false;
105  }
106  // vehicle stays on the detector
107  write("stay", SIMTIME, veh, newSpeed);
108  return true;
109 }
110 
111 
112 void
113 MSInstantInductLoop::write(const char* state, double t, SUMOTrafficObject& veh, double speed, const char* add, double addValue) {
114  if (!myOutputDevice.isNull()) {
115  myOutputDevice.openTag("instantOut").writeAttr(
116  "id", getID()).writeAttr("time", toString(t)).writeAttr("state", state).writeAttr(
117  "vehID", veh.getID()).writeAttr("speed", toString(speed)).writeAttr(
118  "length", toString(veh.getVehicleType().getLength())).writeAttr(
119  "type", veh.getVehicleType().getID());
120  if (add != nullptr) {
121  myOutputDevice.writeAttr(add, toString(addValue));
122  }
124  }
125 }
126 
127 
128 bool
129 MSInstantInductLoop::notifyLeave(SUMOTrafficObject& veh, double /* lastPos */, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
131  // vehicle might have jumped over detector at the end of the lane. we need
132  // one more notifyMove to register it
133  return true;
134  }
135  std::map<SUMOTrafficObject*, double>::iterator i = myEntryTimes.find(&veh);
136  if (i != myEntryTimes.end()) {
137  write("leave", SIMTIME, veh, veh.getSpeed());
138  myEntryTimes.erase(i);
139  }
140  return false;
141 }
142 
143 
144 void
146  dev.writeXMLHeader("instantE1", "instant_e1_file.xsd");
147 }
148 
149 
150 /****************************************************************************/
#define TS
Definition: SUMOTime.h:42
#define SIMTIME
Definition: SUMOTime.h:62
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
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...
Definition: MSCFModel.cpp:736
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...
Definition: MSCFModel.cpp:658
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:143
std::map< SUMOTrafficObject *, double > myEntryTimes
The last exit time.
OutputDevice & myOutputDevice
The output device to use.
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
~MSInstantInductLoop()
Destructor.
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].
MSInstantInductLoop(const std::string &id, OutputDevice &od, MSLane *const lane, double positionInMeters, const std::string name, const std::string &vTypes, const std::string &nextEdges)
Constructor.
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.
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:91
double getLength() const
Get vehicle's length [m].
const std::string & getID() const
Returns the id.
Definition: Named.h:74
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
virtual bool isNull()
returns the information whether the device will discard all output
Definition: OutputDevice.h:155
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
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 double getPreviousSpeed() const =0
Returns the object's previous speed.
virtual double getSpeed() const =0
Returns the object's current speed.
virtual const MSVehicleType & getVehicleType() const =0
Returns the object's "vehicle" type.
A scoped lock which only triggers on condition.
Definition: ScopedLocker.h:40