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.org/sumo
3 // Copyright (C) 2011-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // An instantaneous induction loop
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include "MSInstantInductLoop.h"
27 #include <cassert>
28 #include <numeric>
29 #include <utility>
31 #include <utils/common/ToString.h>
33 #include <microsim/MSLane.h>
34 #include <microsim/MSVehicle.h>
35 #include <microsim/MSNet.h>
40 
41 
42 // ===========================================================================
43 // method definitions
44 // ===========================================================================
46  OutputDevice& od, MSLane* const lane, double positionInMeters,
47  const std::string& vTypes) :
48  MSMoveReminder(id, lane),
49  MSDetectorFileOutput(id, vTypes),
50  myOutputDevice(od),
51  myPosition(positionInMeters), myLastExitTime(-1) {
52  assert(myPosition >= 0 && myPosition <= myLane->getLength());
54 }
55 
56 
58 }
59 
60 
61 bool
63  double newPos, double newSpeed) {
64  if (!vehicleApplies(veh)) {
65  return false;
66  }
67  if (newPos < myPosition) {
68  // detector not reached yet
69  return true;
70  }
71 
72  const double oldSpeed = veh.getPreviousSpeed();
73  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
74 
75  if (newPos >= myPosition && oldPos < myPosition/* && static_cast<MSVehicle&>(veh).getLane() == myLane*/) {
76  const double timeBeforeEnter = MSCFModel::passingTime(oldPos, myPosition, newPos, oldSpeed, newSpeed);
77  const double entryTime = SIMTIME - TS + timeBeforeEnter;
78  enterSpeed = MSCFModel::speedAfterTime(timeBeforeEnter, oldSpeed, newPos - oldPos);
79  if (myLastExitTime >= 0) {
80  write("enter", entryTime, veh, enterSpeed, "gap", entryTime - myLastExitTime);
81  } else {
82  write("enter", entryTime, veh, enterSpeed);
83  }
84  myEntryTimes[&veh] = entryTime;
85  }
86  const double newBackPos = newPos - veh.getVehicleType().getLength();
87  const double oldBackPos = oldPos - veh.getVehicleType().getLength();
88  if (newBackPos > myPosition) {
89  std::map<SUMOTrafficObject*, double>::iterator i = myEntryTimes.find(&veh);
90  if (i != myEntryTimes.end()) {
91  // vehicle passed the detector
92  const double timeBeforeLeave = MSCFModel::passingTime(oldBackPos, myPosition, newBackPos, oldSpeed, newSpeed);
93  const double leaveTime = SIMTIME - TS + timeBeforeLeave;
94  write("leave", leaveTime, veh, newSpeed, "occupancy", leaveTime - (*i).second);
95  myEntryTimes.erase(i);
96  myLastExitTime = leaveTime;
97  }
98  return false;
99  }
100  // vehicle stays on the detector
101  write("stay", SIMTIME, veh, newSpeed);
102  return true;
103 }
104 
105 
106 void
107 MSInstantInductLoop::write(const char* state, double t, SUMOTrafficObject& veh, double speed, const char* add, double addValue) {
108  myOutputDevice.openTag("instantOut").writeAttr(
109  "id", getID()).writeAttr("time", toString(t)).writeAttr("state", state).writeAttr(
110  "vehID", veh.getID()).writeAttr("speed", toString(speed)).writeAttr(
111  "length", toString(veh.getVehicleType().getLength())).writeAttr(
112  "type", veh.getVehicleType().getID());
113  if (add != nullptr) {
114  myOutputDevice.writeAttr(add, toString(addValue));
115  }
117 }
118 
119 
120 bool
121 MSInstantInductLoop::notifyLeave(SUMOTrafficObject& veh, double /* lastPos */, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
123  // vehicle might have jumped over detector at the end of the lane. we need
124  // one more notifyMove to register it
125  return true;
126  }
127  std::map<SUMOTrafficObject*, double>::iterator i = myEntryTimes.find(&veh);
128  if (i != myEntryTimes.end()) {
129  write("leave", SIMTIME, veh, veh.getSpeed());
130  myEntryTimes.erase(i);
131  }
132  return false;
133 }
134 
135 
136 void
138  dev.writeXMLHeader("instantE1", "instant_e1_file.xsd");
139 }
140 
141 
142 /****************************************************************************/
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:48
ToString.h
MSVehicleType::getID
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:94
MSNet.h
MSDetectorFileOutput
Base of value-generating classes (detectors)
Definition: MSDetectorFileOutput.h:64
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
MSInstantInductLoop::myOutputDevice
OutputDevice & myOutputDevice
The output device to use.
Definition: MSInstantInductLoop.h:158
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
MSInstantInductLoop.h
MSInstantInductLoop::writeXMLDetectorProlog
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
Definition: MSInstantInductLoop.cpp:137
SUMOTrafficObject::getVehicleType
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle's type.
SUMOTrafficObject::getID
virtual const std::string & getID() const =0
Get the vehicle's ID.
MsgHandler.h
WrappingCommand.h
MSInstantInductLoop::myPosition
const double myPosition
Detector's position on lane [m].
Definition: MSInstantInductLoop.h:161
MSInstantInductLoop::myEntryTimes
std::map< SUMOTrafficObject *, double > myEntryTimes
The last exit time.
Definition: MSInstantInductLoop.h:167
MSVehicle.h
MSInstantInductLoop::MSInstantInductLoop
MSInstantInductLoop(const std::string &id, OutputDevice &od, MSLane *const lane, double positionInMeters, const std::string &vTypes)
Constructor.
Definition: MSInstantInductLoop.cpp:45
MSMoveReminder
Something on a lane to be noticed about vehicle movement.
Definition: MSMoveReminder.h:64
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:254
MSInstantInductLoop::~MSInstantInductLoop
~MSInstantInductLoop()
Destructor.
Definition: MSInstantInductLoop.cpp:57
MSInstantInductLoop::notifyMove
bool notifyMove(SUMOTrafficObject &veh, double oldPos, double newPos, double newSpeed)
Checks whether the vehicle shall be counted and/or shall still touch this MSMoveReminder.
Definition: MSInstantInductLoop.cpp:62
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
SIMTIME
#define SIMTIME
Definition: SUMOTime.h:64
TS
#define TS
Definition: SUMOTime.h:44
OutputDevice.h
UtilExceptions.h
MSCFModel::speedAfterTime
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:674
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:240
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
StringUtils.h
SUMOTrafficObject::getPreviousSpeed
virtual double getPreviousSpeed() const =0
Returns the vehicle's previous speed.
MSInstantInductLoop::notifyLeave
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.
Definition: MSInstantInductLoop.cpp:121
MSVehicleType::getLength
double getLength() const
Get vehicle's length [m].
Definition: MSVehicleType.h:110
MSInstantInductLoop::write
void write(const char *state, double t, SUMOTrafficObject &veh, double speed, const char *add=0, double addValue=-1)
Writes an event line.
Definition: MSInstantInductLoop.cpp:107
MSDetectorFileOutput::vehicleApplies
bool vehicleApplies(const SUMOTrafficObject &veh) const
Checks whether the detector measures vehicles of the given type.
Definition: MSDetectorFileOutput.h:142
config.h
MSCFModel::passingTime
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:597
MSInstantInductLoop::myLastExitTime
double myLastExitTime
The last exit time.
Definition: MSInstantInductLoop.h:164
MSEventControl.h
MSLane.h
OutputDevice::writeXMLHeader
bool writeXMLHeader(const std::string &rootElement, const std::string &schemaFile, std::map< SumoXMLAttr, std::string > attrs=std::map< SumoXMLAttr, std::string >())
Writes an XML header with optional configuration.
Definition: OutputDevice.cpp:228
MSGlobals::gSemiImplicitEulerUpdate
static bool gSemiImplicitEulerUpdate
Definition: MSGlobals.h:56
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:77
MSMoveReminder::Notification
Notification
Definition of a vehicle state.
Definition: MSMoveReminder.h:89
MSMoveReminder::NOTIFICATION_JUNCTION
The vehicle arrived at a junction.
Definition: MSMoveReminder.h:93
SUMOTrafficObject::getSpeed
virtual double getSpeed() const =0
Returns the vehicle's current speed.