SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSInstantInductLoop.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // An instantaneous induction loop
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
10 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include "MSInstantInductLoop.h"
32 #include <cassert>
33 #include <numeric>
34 #include <utility>
36 #include <utils/common/ToString.h>
38 #include <microsim/MSLane.h>
39 #include <microsim/MSVehicle.h>
40 #include <microsim/MSNet.h>
45 
46 #ifdef CHECK_MEMORY_LEAKS
47 #include <foreign/nvwa/debug_new.h>
48 #endif // CHECK_MEMORY_LEAKS
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
55  OutputDevice& od, MSLane* const lane, SUMOReal positionInMeters) :
56  MSMoveReminder(id, lane),
58  myOutputDevice(od),
59  myPosition(positionInMeters), myLastExitTime(-1) {
60  assert(myPosition >= 0 && myPosition <= myLane->getLength());
62 }
63 
64 
66 }
67 
68 
69 bool
71  SUMOReal newPos, SUMOReal newSpeed) {
72  if (newPos < myPosition) {
73  // detector not reached yet
74  return true;
75  }
76  if (newPos >= myPosition && oldPos < myPosition/* && static_cast<MSVehicle&>(veh).getLane() == myLane*/) {
77  SUMOReal entryTime = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep());
78  if (newSpeed != 0) {
79  if (myPosition > oldPos) {
80  entryTime += (myPosition - oldPos) / newSpeed;
81  }
82  }
83  if (myLastExitTime >= 0) {
84  write("enter", entryTime, veh, newSpeed, "gap", entryTime - myLastExitTime);
85  } else {
86  write("enter", entryTime, veh, newSpeed);
87  }
88  myEntryTimes[&veh] = entryTime;
89  }
90  if (newPos - veh.getVehicleType().getLength() > myPosition) {
91  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
92  if (i != myEntryTimes.end()) {
93  // vehicle passed the detector
94  SUMOReal leaveTime = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep());
95  leaveTime += (myPosition - oldPos + veh.getVehicleType().getLength()) / newSpeed;
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", STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() + DELTA_T), veh, newSpeed);
104  return true;
105 }
106 
107 
108 void
109 MSInstantInductLoop::write(const char* state, SUMOReal t, SUMOVehicle& veh, SUMOReal speed, const char* add, SUMOReal 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 != 0) {
116  myOutputDevice.writeAttr(add, toString(addValue));
117  }
119 }
120 
121 bool
123  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
124  if (i != myEntryTimes.end()) {
125  write("leave", i->second, veh, veh.getSpeed());
126  myEntryTimes.erase(i);
127  return false;
128  }
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  } else {
134  return false;
135  }
136 }
137 
138 
139 void
141  dev.writeXMLHeader("instantE1");
142 }
143 
144 
145 
146 /****************************************************************************/
147 
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
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:154
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:63
#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:52
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:70
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:215
#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.