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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
19 // An instantaneous induction loop
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include "MSInstantInductLoop.h"
33 #include <cassert>
34 #include <numeric>
35 #include <utility>
37 #include <utils/common/ToString.h>
39 #include <microsim/MSLane.h>
40 #include <microsim/MSVehicle.h>
41 #include <microsim/MSNet.h>
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
52  OutputDevice& od, MSLane* const lane, double positionInMeters,
53  const std::string& vTypes) :
54  MSMoveReminder(id, lane),
55  MSDetectorFileOutput(id, vTypes),
56  myOutputDevice(od),
57  myPosition(positionInMeters), myLastExitTime(-1) {
58  assert(myPosition >= 0 && myPosition <= myLane->getLength());
60 }
61 
62 
64 }
65 
66 
67 bool
69  double newPos, double newSpeed) {
70  if (!vehicleApplies(veh)) {
71  return false;
72  }
73  if (newPos < myPosition) {
74  // detector not reached yet
75  return true;
76  }
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<SUMOVehicle*, 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, SUMOVehicle& veh, double speed, const char* add, double addValue) {
114  myOutputDevice.openTag("instantOut").writeAttr(
115  "id", getID()).writeAttr("time", toString(t)).writeAttr("state", state).writeAttr(
116  "vehID", veh.getID()).writeAttr("speed", toString(speed)).writeAttr(
117  "length", toString(veh.getVehicleType().getLength())).writeAttr(
118  "type", veh.getVehicleType().getID());
119  if (add != 0) {
120  myOutputDevice.writeAttr(add, toString(addValue));
121  }
123 }
124 
125 
126 bool
127 MSInstantInductLoop::notifyLeave(SUMOVehicle& veh, double /* lastPos */, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
129  // vehicle might have jumped over detector at the end of the lane. we need
130  // one more notifyMove to register it
131  return true;
132  }
133  std::map<SUMOVehicle*, double>::iterator i = myEntryTimes.find(&veh);
134  if (i != myEntryTimes.end()) {
135  write("leave", SIMTIME, veh, veh.getSpeed());
136  myEntryTimes.erase(i);
137  }
138  return false;
139 }
140 
141 
142 void
144  dev.writeXMLHeader("instantE1", "instant_e1_file.xsd");
145 }
146 
147 
148 /****************************************************************************/
static double speedAfterTime(const double t, const double oldSpeed, const double dist)
Calculates the speed after a time t [0,TS] given the initial speed and the distance traveled in an i...
Definition: MSCFModel.cpp:624
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:260
bool vehicleApplies(const SUMOVehicle &veh) const
Checks whether the detector measures vehicles of the given type.
The vehicle arrived at a junction.
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
Notification
Definition of a vehicle state.
bool notifyMove(SUMOVehicle &veh, double oldPos, double newPos, double newSpeed)
Checks whether the vehicle shall be counted and/or shall still touch this MSMoveReminder.
const std::string & getID() const
Returns the id.
Definition: Named.h:74
#define TS
Definition: SUMOTime.h:51
bool notifyLeave(SUMOVehicle &veh, double lastPos, MSMoveReminder::Notification reason, const MSLane *enteredLane=0)
Dismisses the vehicle if it is on the detector due to a lane change.
#define SIMTIME
Definition: SUMOTime.h:71
double myLastExitTime
The last exit time.
MSInstantInductLoop(const std::string &id, OutputDevice &od, MSLane *const lane, double positionInMeters, const std::string &vTypes)
Constructor.
std::map< SUMOVehicle *, double > myEntryTimes
The last exit time.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
~MSInstantInductLoop()
Destructor.
Representation of a vehicle.
Definition: SUMOVehicle.h:66
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.
Something on a lane to be noticed about vehicle movement.
const double myPosition
Detector&#39;s position on lane [m].
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:552
OutputDevice & myOutputDevice
The output device to use.
void write(const char *state, double t, SUMOVehicle &veh, double speed, const char *add=0, double addValue=-1)
Writes an event line.
const std::string & getID() const
Returns the name of the vehicle type.
double getLength() const
Get vehicle&#39;s length [m].
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:70
bool closeTag()
Closes the most recently opened tag.
static bool gSemiImplicitEulerUpdate
Definition: MSGlobals.h:62
virtual double getSpeed() const =0
Returns the vehicle&#39;s current speed.
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
virtual double getPreviousSpeed() const =0
Returns the vehicle&#39;s previous speed.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Base of value-generating classes (detectors)
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.