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-2016 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 #ifdef CHECK_MEMORY_LEAKS
49 #include <foreign/nvwa/debug_new.h>
50 #endif // CHECK_MEMORY_LEAKS
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
57  OutputDevice& od, MSLane* const lane, SUMOReal positionInMeters,
58  const std::string& vTypes) :
59  MSMoveReminder(id, lane),
60  MSDetectorFileOutput(id, vTypes),
61  myOutputDevice(od),
62  myPosition(positionInMeters), myLastExitTime(-1) {
63  assert(myPosition >= 0 && myPosition <= myLane->getLength());
65 }
66 
67 
69 }
70 
71 
72 bool
74  SUMOReal newPos, SUMOReal newSpeed) {
75  if (!vehicleApplies(veh)) {
76  return false;
77  }
78  if (newPos < myPosition) {
79  // detector not reached yet
80  return true;
81  }
82 
83  const SUMOReal oldSpeed = veh.getPreviousSpeed();
84  SUMOReal enterSpeed = MSGlobals::gSemiImplicitEulerUpdate ? newSpeed : oldSpeed; // NOTE: For the euler update, the vehicle is assumed to travel at constant speed for the whole time step
85 
86  if (newPos >= myPosition && oldPos < myPosition/* && static_cast<MSVehicle&>(veh).getLane() == myLane*/) {
87  const SUMOReal timeBeforeEnter = MSCFModel::passingTime(oldPos, myPosition, newPos, oldSpeed, newSpeed);
88  const SUMOReal entryTime = SIMTIME - TS + timeBeforeEnter;
89  enterSpeed = MSCFModel::speedAfterTime(timeBeforeEnter, oldSpeed, newPos - oldPos);
90  if (myLastExitTime >= 0) {
91  write("enter", entryTime, veh, enterSpeed, "gap", entryTime - myLastExitTime);
92  } else {
93  write("enter", entryTime, veh, enterSpeed);
94  }
95  myEntryTimes[&veh] = entryTime;
96  }
97  const SUMOReal newBackPos = newPos - veh.getVehicleType().getLength();
98  const SUMOReal oldBackPos = oldPos - veh.getVehicleType().getLength();
99  if (newBackPos > myPosition) {
100  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
101  if (i != myEntryTimes.end()) {
102  // vehicle passed the detector
103  const SUMOReal timeBeforeLeave = MSCFModel::passingTime(oldBackPos, myPosition, newBackPos, oldSpeed, newSpeed);
104  const SUMOReal leaveTime = SIMTIME - TS + timeBeforeLeave;
105  write("leave", leaveTime, veh, newSpeed, "occupancy", leaveTime - (*i).second);
106  myEntryTimes.erase(i);
107  myLastExitTime = leaveTime;
108  }
109  return false;
110  }
111  // vehicle stays on the detector
112  write("stay", SIMTIME, veh, newSpeed);
113  return true;
114 }
115 
116 
117 void
118 MSInstantInductLoop::write(const char* state, SUMOReal t, SUMOVehicle& veh, SUMOReal speed, const char* add, SUMOReal addValue) {
119  myOutputDevice.openTag("instantOut").writeAttr(
120  "id", getID()).writeAttr("time", toString(t)).writeAttr("state", state).writeAttr(
121  "vehID", veh.getID()).writeAttr("speed", toString(speed)).writeAttr(
122  "length", toString(veh.getVehicleType().getLength())).writeAttr(
123  "type", veh.getVehicleType().getID());
124  if (add != 0) {
125  myOutputDevice.writeAttr(add, toString(addValue));
126  }
128 }
129 
130 
131 bool
134  // vehicle might have jumped over detector at the end of the lane. we need
135  // one more notifyMove to register it
136  return true;
137  }
138  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
139  if (i != myEntryTimes.end()) {
140  write("leave", SIMTIME, veh, veh.getSpeed());
141  myEntryTimes.erase(i);
142  }
143  return false;
144 }
145 
146 
147 void
149  dev.writeXMLHeader("instantE1");
150 }
151 
152 
153 /****************************************************************************/
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
bool vehicleApplies(const SUMOVehicle &veh) const
Checks whether the detector measures vehicles of the given type.
MSInstantInductLoop(const std::string &id, OutputDevice &od, MSLane *const lane, SUMOReal positionInMeters, const std::string &vTypes)
Constructor.
The vehicle arrived at a junction.
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
Notification
Definition of a vehicle state.
SUMOReal getLength() const
Get vehicle&#39;s length [m].
virtual SUMOReal getPreviousSpeed() const =0
Returns the vehicle&#39;s previous speed.
bool notifyLeave(SUMOVehicle &veh, SUMOReal lastPos, MSMoveReminder::Notification reason)
Dismisses the vehicle if it is on the detector due to a lane change.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
void write(const char *state, SUMOReal t, SUMOVehicle &veh, SUMOReal speed, const char *add=0, SUMOReal addValue=-1)
Writes an event line.
#define TS
Definition: SUMOTime.h:52
SUMOReal myLastExitTime
The last exit time.
const SUMOReal myPosition
Detector&#39;s position on lane [m].
#define SIMTIME
Definition: SUMOTime.h:70
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
static SUMOReal speedAfterTime(const SUMOReal t, const SUMOReal oldSpeed, const SUMOReal dist)
Calculates the speed after a time t [0,TS] given the initial speed and the distance traveled in an i...
Definition: MSCFModel.cpp:439
std::map< SUMOVehicle *, SUMOReal > myEntryTimes
The last exit time.
~MSInstantInductLoop()
Destructor.
Representation of a vehicle.
Definition: SUMOVehicle.h:66
static SUMOReal passingTime(const SUMOReal lastPos, const SUMOReal passedPos, const SUMOReal currentPos, const SUMOReal lastSpeed, const SUMOReal currentSpeed)
Calculates the time at which the position passedPosition has been passed In case of a ballistic updat...
Definition: MSCFModel.cpp:367
Something on a lane to be noticed about vehicle movement.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
OutputDevice & myOutputDevice
The output device to use.
virtual SUMOReal getSpeed() const =0
Returns the vehicle&#39;s current speed.
const std::string & getID() const
Returns the name of the vehicle type.
bool notifyMove(SUMOVehicle &veh, SUMOReal oldPos, SUMOReal newPos, SUMOReal newSpeed)
Checks whether the vehicle shall be counted and/or shall still touch this MSMoveReminder.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
static bool gSemiImplicitEulerUpdate
Definition: MSGlobals.h:63
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
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.