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  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 = SIMTIME;
80  if (newSpeed != 0) {
81  if (myPosition < newPos) {
82  entryTime -= (newPos - myPosition) / 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  const SUMOReal leaveTime = SIMTIME - (newPos - veh.getVehicleType().getLength() - myPosition) / newSpeed;
97  write("leave", leaveTime, veh, newSpeed, "occupancy", leaveTime - (*i).second);
98  myEntryTimes.erase(i);
99  myLastExitTime = leaveTime;
100  }
101  return false;
102  }
103  // vehicle stays on the detector
104  write("stay", SIMTIME, veh, newSpeed);
105  return true;
106 }
107 
108 
109 void
110 MSInstantInductLoop::write(const char* state, SUMOReal t, SUMOVehicle& veh, SUMOReal speed, const char* add, SUMOReal addValue) {
111  myOutputDevice.openTag("instantOut").writeAttr(
112  "id", getID()).writeAttr("time", toString(t)).writeAttr("state", state).writeAttr(
113  "vehID", veh.getID()).writeAttr("speed", toString(speed)).writeAttr(
114  "length", toString(veh.getVehicleType().getLength())).writeAttr(
115  "type", veh.getVehicleType().getID());
116  if (add != 0) {
117  myOutputDevice.writeAttr(add, toString(addValue));
118  }
120 }
121 
122 
123 bool
126  // vehicle might have jumped over detector at the end of the lane. we need
127  // one more notifyMove to register it
128  return true;
129  }
130  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
131  if (i != myEntryTimes.end()) {
132  write("leave", SIMTIME, veh, veh.getSpeed());
133  myEntryTimes.erase(i);
134  }
135  return false;
136 }
137 
138 
139 void
141  dev.writeXMLHeader("instantE1");
142 }
143 
144 
145 /****************************************************************************/
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&#39;s length [m].
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&#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.
const std::string & getID() const
Returns the id.
Definition: Named.h:66
std::map< SUMOVehicle *, SUMOReal > myEntryTimes
The last exit time.
~MSInstantInductLoop()
Destructor.
Representation of a vehicle.
Definition: SUMOVehicle.h:66
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
void writeXMLDetectorProlog(OutputDevice &dev) const
Open the XML-output.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:213
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.