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 /****************************************************************************/
16 // An instantaneous induction loop
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include "MSInstantInductLoop.h"
26 #include <cassert>
27 #include <numeric>
28 #include <utility>
30 #include <utils/common/ToString.h>
32 #include <microsim/MSLane.h>
33 #include <microsim/MSVehicle.h>
34 #include <microsim/MSNet.h>
39 
40 
41 // ===========================================================================
42 // method definitions
43 // ===========================================================================
45  OutputDevice& od, MSLane* const lane, double positionInMeters,
46  const std::string& vTypes) :
47  MSMoveReminder(id, lane),
48  MSDetectorFileOutput(id, vTypes),
49  myOutputDevice(od),
50  myPosition(positionInMeters), myLastExitTime(-1) {
51  assert(myPosition >= 0 && myPosition <= myLane->getLength());
53 }
54 
55 
57 }
58 
59 
60 bool
62  double newPos, double newSpeed) {
63  if (!vehicleApplies(veh)) {
64  return false;
65  }
66  if (newPos < myPosition) {
67  // detector not reached yet
68  return true;
69  }
70 #ifdef HAVE_FOX
71  FXConditionalLock lock(myNotificationMutex, MSGlobals::gNumSimThreads > 1);
72 #endif
73 
74  const double oldSpeed = veh.getPreviousSpeed();
75  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
76 
77  if (newPos >= myPosition && oldPos < myPosition/* && static_cast<MSVehicle&>(veh).getLane() == myLane*/) {
78  const double timeBeforeEnter = MSCFModel::passingTime(oldPos, myPosition, newPos, oldSpeed, newSpeed);
79  const double entryTime = SIMTIME - TS + timeBeforeEnter;
80  enterSpeed = MSCFModel::speedAfterTime(timeBeforeEnter, oldSpeed, newPos - oldPos);
81  if (myLastExitTime >= 0) {
82  write("enter", entryTime, veh, enterSpeed, "gap", entryTime - myLastExitTime);
83  } else {
84  write("enter", entryTime, veh, enterSpeed);
85  }
86  myEntryTimes[&veh] = entryTime;
87  }
88  const double newBackPos = newPos - veh.getVehicleType().getLength();
89  const double oldBackPos = oldPos - veh.getVehicleType().getLength();
90  if (newBackPos > myPosition) {
91  std::map<SUMOTrafficObject*, double>::iterator i = myEntryTimes.find(&veh);
92  if (i != myEntryTimes.end()) {
93  // vehicle passed the detector
94  const double timeBeforeLeave = MSCFModel::passingTime(oldBackPos, myPosition, newBackPos, oldSpeed, newSpeed);
95  const double leaveTime = SIMTIME - TS + timeBeforeLeave;
96  write("leave", leaveTime, veh, newSpeed, "occupancy", leaveTime - (*i).second);
97  myEntryTimes.erase(i);
98  myLastExitTime = leaveTime;
99  }
100  return false;
101  }
102  // vehicle stays on the detector
103  write("stay", SIMTIME, veh, newSpeed);
104  return true;
105 }
106 
107 
108 void
109 MSInstantInductLoop::write(const char* state, double t, SUMOTrafficObject& veh, double speed, const char* add, double addValue) {
110  myOutputDevice.openTag("instantOut").writeAttr(
111  "id", getID()).writeAttr("time", toString(t)).writeAttr("state", state).writeAttr(
112  "vehID", veh.getID()).writeAttr("speed", toString(speed)).writeAttr(
113  "length", toString(veh.getVehicleType().getLength())).writeAttr(
114  "type", veh.getVehicleType().getID());
115  if (add != nullptr) {
116  myOutputDevice.writeAttr(add, toString(addValue));
117  }
119 }
120 
121 
122 bool
123 MSInstantInductLoop::notifyLeave(SUMOTrafficObject& veh, double /* lastPos */, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
125  // vehicle might have jumped over detector at the end of the lane. we need
126  // one more notifyMove to register it
127  return true;
128  }
129  std::map<SUMOTrafficObject*, double>::iterator i = myEntryTimes.find(&veh);
130  if (i != myEntryTimes.end()) {
131  write("leave", SIMTIME, veh, veh.getSpeed());
132  myEntryTimes.erase(i);
133  }
134  return false;
135 }
136 
137 
138 void
140  dev.writeXMLHeader("instantE1", "instant_e1_file.xsd");
141 }
142 
143 
144 /****************************************************************************/
SUMOTrafficObject
Representation of a vehicle or person.
Definition: SUMOTrafficObject.h:47
ToString.h
MSVehicleType::getID
const std::string & getID() const
Returns the name of the vehicle type.
Definition: MSVehicleType.h:93
MSNet.h
MSDetectorFileOutput
Base of value-generating classes (detectors)
Definition: MSDetectorFileOutput.h:63
MSLane
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
MSInstantInductLoop::myOutputDevice
OutputDevice & myOutputDevice
The output device to use.
Definition: MSInstantInductLoop.h:157
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
MSInstantInductLoop.h
MSInstantInductLoop::writeXMLDetectorProlog
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
Definition: MSInstantInductLoop.cpp:139
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:160
MSGlobals::gNumSimThreads
static int gNumSimThreads
how many threads to use for simulation
Definition: MSGlobals.h:123
MSInstantInductLoop::myEntryTimes
std::map< SUMOTrafficObject *, double > myEntryTimes
The last exit time.
Definition: MSInstantInductLoop.h:166
MSVehicle.h
MSInstantInductLoop::MSInstantInductLoop
MSInstantInductLoop(const std::string &id, OutputDevice &od, MSLane *const lane, double positionInMeters, const std::string &vTypes)
Constructor.
Definition: MSInstantInductLoop.cpp:44
MSMoveReminder
Something on a lane to be noticed about vehicle movement.
Definition: MSMoveReminder.h:66
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
MSInstantInductLoop::~MSInstantInductLoop
~MSInstantInductLoop()
Destructor.
Definition: MSInstantInductLoop.cpp:56
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:61
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:255
SIMTIME
#define SIMTIME
Definition: SUMOTime.h:63
TS
#define TS
Definition: SUMOTime.h:43
FXConditionalLock
A scoped lock which only triggers on condition.
Definition: FXConditionalLock.h:36
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:673
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:239
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
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:123
MSVehicleType::getLength
double getLength() const
Get vehicle's length [m].
Definition: MSVehicleType.h:109
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:109
MSDetectorFileOutput::vehicleApplies
bool vehicleApplies(const SUMOTrafficObject &veh) const
Checks whether the detector measures vehicles of the given type.
Definition: MSDetectorFileOutput.h:141
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:596
MSInstantInductLoop::myLastExitTime
double myLastExitTime
The last exit time.
Definition: MSInstantInductLoop.h:163
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:227
MSGlobals::gSemiImplicitEulerUpdate
static bool gSemiImplicitEulerUpdate
Definition: MSGlobals.h:55
Named::getID
const std::string & getID() const
Returns the id.
Definition: Named.h:76
MSMoveReminder::Notification
Notification
Definition of a vehicle state.
Definition: MSMoveReminder.h:91
MSMoveReminder::NOTIFICATION_JUNCTION
The vehicle arrived at a junction.
Definition: MSMoveReminder.h:95
SUMOTrafficObject::getSpeed
virtual double getSpeed() const =0
Returns the vehicle's current speed.