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-2015 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  MSMoveReminder(id, lane),
60  myOutputDevice(od),
61  myPosition(positionInMeters), myLastExitTime(-1) {
62  assert(myPosition >= 0 && myPosition <= myLane->getLength());
64 }
65 
66 
68 }
69 
70 
71 bool
73  SUMOReal newPos, SUMOReal newSpeed) {
74  if (newPos < myPosition) {
75  // detector not reached yet
76  return true;
77  }
78  if (newPos >= myPosition && oldPos < myPosition/* && static_cast<MSVehicle&>(veh).getLane() == myLane*/) {
79  SUMOReal entryTime = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep());
80  if (newSpeed != 0) {
81  if (myPosition > oldPos) {
82  entryTime += (myPosition - oldPos) / newSpeed;
83  }
84  }
85  if (myLastExitTime >= 0) {
86  write("enter", entryTime, veh, newSpeed, "gap", entryTime - myLastExitTime);
87  } else {
88  write("enter", entryTime, veh, newSpeed);
89  }
90  myEntryTimes[&veh] = entryTime;
91  }
92  if (newPos - veh.getVehicleType().getLength() > myPosition) {
93  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
94  if (i != myEntryTimes.end()) {
95  // vehicle passed the detector
96  SUMOReal leaveTime = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep());
97  leaveTime += (myPosition - oldPos + veh.getVehicleType().getLength()) / newSpeed;
98  write("leave", leaveTime, veh, newSpeed, "occupancy", leaveTime - (*i).second);
99  myEntryTimes.erase(i);
100  myLastExitTime = leaveTime;
101  }
102  return false;
103  }
104  // vehicle stays on the detector
105  write("stay", STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() + DELTA_T), veh, newSpeed);
106  return true;
107 }
108 
109 
110 void
111 MSInstantInductLoop::write(const char* state, SUMOReal t, SUMOVehicle& veh, SUMOReal speed, const char* add, SUMOReal addValue) {
112  myOutputDevice.openTag("instantOut").writeAttr(
113  "id", getID()).writeAttr("time", toString(t)).writeAttr("state", state).writeAttr(
114  "vehID", veh.getID()).writeAttr("speed", toString(speed)).writeAttr(
115  "length", toString(veh.getVehicleType().getLength())).writeAttr(
116  "type", veh.getVehicleType().getID());
117  if (add != 0) {
118  myOutputDevice.writeAttr(add, toString(addValue));
119  }
121 }
122 
123 bool
125  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
126  if (i != myEntryTimes.end()) {
127  write("leave", i->second, veh, veh.getSpeed());
128  myEntryTimes.erase(i);
129  return false;
130  }
132  // vehicle might have jumped over detector at the end of the lane. we need
133  // one more notifyMove to register it
134  return true;
135  } else {
136  return false;
137  }
138 }
139 
140 
141 void
143  dev.writeXMLHeader("instantE1");
144 }
145 
146 
147 
148 /****************************************************************************/
149 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
MSInstantInductLoop(const std::string &id, OutputDevice &od, MSLane *const lane, SUMOReal positionInMeters)
Constructor.
The vehicle arrived at a junction.
Notification
Definition of a vehicle state.
SUMOReal getLength() const
Get vehicle's length [m].
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
bool notifyLeave(SUMOVehicle &veh, SUMOReal lastPos, MSMoveReminder::Notification reason)
Dismisses the vehicle if it is on the detector due to a lane change.
void write(const char *state, SUMOReal t, SUMOVehicle &veh, SUMOReal speed, const char *add=0, SUMOReal addValue=-1)
Writes an event line.
SUMOReal myLastExitTime
The last exit time.
const SUMOReal myPosition
Detector's position on lane [m].
bool writeXMLHeader(const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
const std::string & getID() const
Returns the id.
Definition: Named.h:60
std::map< SUMOVehicle *, SUMOReal > myEntryTimes
The last exit time.
~MSInstantInductLoop()
Destructor.
Representation of a vehicle.
Definition: SUMOVehicle.h:65
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
Something on a lane to be noticed about vehicle movement.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
OutputDevice & myOutputDevice
The output device to use.
virtual SUMOReal getSpeed() const =0
Returns the vehicle'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
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:218
#define DELTA_T
Definition: SUMOTime.h:50
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
virtual const std::string & getID() const =0
Get the vehicle'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's type.