SUMO - Simulation of Urban MObility
MSInstantInductLoop.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // An instantaneous induction loop
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2011-2017 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include "MSInstantInductLoop.h"
34 #include <cassert>
35 #include <numeric>
36 #include <utility>
38 #include <utils/common/ToString.h>
40 #include <microsim/MSLane.h>
41 #include <microsim/MSVehicle.h>
42 #include <microsim/MSNet.h>
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
53  OutputDevice& od, MSLane* const lane, double positionInMeters,
54  const std::string& vTypes) :
55  MSMoveReminder(id, lane),
56  MSDetectorFileOutput(id, vTypes),
57  myOutputDevice(od),
58  myPosition(positionInMeters), myLastExitTime(-1) {
59  assert(myPosition >= 0 && myPosition <= myLane->getLength());
61 }
62 
63 
65 }
66 
67 
68 bool
70  double newPos, double newSpeed) {
71  if (!vehicleApplies(veh)) {
72  return false;
73  }
74  if (newPos < myPosition) {
75  // detector not reached yet
76  return true;
77  }
78 
79  const double oldSpeed = veh.getPreviousSpeed();
80  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
81 
82  if (newPos >= myPosition && oldPos < myPosition/* && static_cast<MSVehicle&>(veh).getLane() == myLane*/) {
83  const double timeBeforeEnter = MSCFModel::passingTime(oldPos, myPosition, newPos, oldSpeed, newSpeed);
84  const double entryTime = SIMTIME - TS + timeBeforeEnter;
85  enterSpeed = MSCFModel::speedAfterTime(timeBeforeEnter, oldSpeed, newPos - oldPos);
86  if (myLastExitTime >= 0) {
87  write("enter", entryTime, veh, enterSpeed, "gap", entryTime - myLastExitTime);
88  } else {
89  write("enter", entryTime, veh, enterSpeed);
90  }
91  myEntryTimes[&veh] = entryTime;
92  }
93  const double newBackPos = newPos - veh.getVehicleType().getLength();
94  const double oldBackPos = oldPos - veh.getVehicleType().getLength();
95  if (newBackPos > myPosition) {
96  std::map<SUMOVehicle*, double>::iterator i = myEntryTimes.find(&veh);
97  if (i != myEntryTimes.end()) {
98  // vehicle passed the detector
99  const double timeBeforeLeave = MSCFModel::passingTime(oldBackPos, myPosition, newBackPos, oldSpeed, newSpeed);
100  const double leaveTime = SIMTIME - TS + timeBeforeLeave;
101  write("leave", leaveTime, veh, newSpeed, "occupancy", leaveTime - (*i).second);
102  myEntryTimes.erase(i);
103  myLastExitTime = leaveTime;
104  }
105  return false;
106  }
107  // vehicle stays on the detector
108  write("stay", SIMTIME, veh, newSpeed);
109  return true;
110 }
111 
112 
113 void
114 MSInstantInductLoop::write(const char* state, double t, SUMOVehicle& veh, double speed, const char* add, double addValue) {
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 != 0) {
121  myOutputDevice.writeAttr(add, toString(addValue));
122  }
124 }
125 
126 
127 bool
128 MSInstantInductLoop::notifyLeave(SUMOVehicle& veh, double /* lastPos */, MSMoveReminder::Notification reason, const MSLane* /* enteredLane */) {
130  // vehicle might have jumped over detector at the end of the lane. we need
131  // one more notifyMove to register it
132  return true;
133  }
134  std::map<SUMOVehicle*, double>::iterator i = myEntryTimes.find(&veh);
135  if (i != myEntryTimes.end()) {
136  write("leave", SIMTIME, veh, veh.getSpeed());
137  myEntryTimes.erase(i);
138  }
139  return false;
140 }
141 
142 
143 void
145  dev.writeXMLHeader("instantE1", "instant_e1_file.xsd");
146 }
147 
148 
149 /****************************************************************************/
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:444
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
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:66
#define TS
Definition: SUMOTime.h:52
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:70
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:56
~MSInstantInductLoop()
Destructor.
Representation of a vehicle.
Definition: SUMOVehicle.h:67
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:372
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:71
bool closeTag()
Closes the most recently opened tag.
static bool gSemiImplicitEulerUpdate
Definition: MSGlobals.h:63
virtual double getSpeed() const =0
Returns the vehicle&#39;s current speed.
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
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.