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.sourceforge.net/
10 // Copyright (C) 2001-2012 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(lane), MSDetectorFileOutput(id), myOutputDevice(od),
57  myPosition(positionInMeters), myLastExitTime(-1) {
58  assert(myPosition >= 0 && myPosition <= myLane->getLength());
60 }
61 
62 
64 }
65 
66 
67 bool
69  SUMOReal newPos, SUMOReal newSpeed) {
70  if (newPos < myPosition) {
71  // detector not reached yet
72  return true;
73  }
74  if (newPos >= myPosition && oldPos < myPosition/* && static_cast<MSVehicle&>(veh).getLane() == myLane*/) {
75  SUMOReal entryTime = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep());
76  if (newSpeed != 0) {
77  if (myPosition > oldPos) {
78  entryTime += (myPosition - oldPos) / newSpeed;
79  }
80  }
81  if (myLastExitTime >= 0) {
82  write("enter", entryTime, veh, newSpeed, "gap", entryTime - myLastExitTime);
83  } else {
84  write("enter", entryTime, veh, newSpeed);
85  }
86  myEntryTimes[&veh] = entryTime;
87  return true;
88  }
89  if (newPos - veh.getVehicleType().getLength() > myPosition) {
90  // vehicle passed the detector
91  SUMOReal leaveTime = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep());
92  leaveTime += (myPosition - oldPos + veh.getVehicleType().getLength()) / newSpeed;
93  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
94  if (i != myEntryTimes.end()) {
95  write("leave", leaveTime, veh, newSpeed, "occupancy", leaveTime - (*i).second);
96  myEntryTimes.erase(i);
97  } else {
98  write("leave", leaveTime, veh, newSpeed);
99  }
100  myLastExitTime = leaveTime;
101  return false;
102  }
103  // vehicle stays on the detector
104  write("stay", STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep()), 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  }
119  myOutputDevice << ">";
121 }
122 
123 bool
125  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
126  if (i != myEntryTimes.end()) {
127  myEntryTimes.erase(i);
128  }
130  write("leave", STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep()), veh, veh.getSpeed());
131  return false;
132  }
133  return true;
134 }
135 
136 
137 bool
139  if (veh.getPositionOnLane() - veh.getVehicleType().getLength() > myPosition) {
140  // vehicle-front is beyond detector. Ignore
141  return false;
142  }
143  // vehicle is in front of detector
144  return true;
145 }
146 
147 
148 void
150  dev.writeXMLHeader("instantE1");
151 }
152 
153 
154 
155 /****************************************************************************/
156