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-2013 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  return true;
90  }
91  if (newPos - veh.getVehicleType().getLength() > myPosition) {
92  // vehicle passed the detector
93  SUMOReal leaveTime = STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep());
94  leaveTime += (myPosition - oldPos + veh.getVehicleType().getLength()) / newSpeed;
95  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
96  if (i != myEntryTimes.end()) {
97  write("leave", leaveTime, veh, newSpeed, "occupancy", leaveTime - (*i).second);
98  myEntryTimes.erase(i);
99  } else {
100  write("leave", leaveTime, veh, newSpeed);
101  }
102  myLastExitTime = leaveTime;
103  return false;
104  }
105  // vehicle stays on the detector
106  write("stay", STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep()), veh, newSpeed);
107  return true;
108 }
109 
110 
111 void
112 MSInstantInductLoop::write(const char* state, SUMOReal t, SUMOVehicle& veh, SUMOReal speed, const char* add, SUMOReal addValue) {
113  myOutputDevice.openTag("instantOut").writeAttr(
114  "id", getID()).writeAttr("time", toString(t)).writeAttr("state", state).writeAttr(
115  "vehID", veh.getID()).writeAttr("speed", toString(speed)).writeAttr(
116  "length", toString(veh.getVehicleType().getLength())).writeAttr(
117  "type", veh.getVehicleType().getID());
118  if (add != 0) {
119  myOutputDevice.writeAttr(add, toString(addValue));
120  }
122 }
123 
124 bool
126  std::map<SUMOVehicle*, SUMOReal>::iterator i = myEntryTimes.find(&veh);
127  if (i != myEntryTimes.end()) {
128  myEntryTimes.erase(i);
129  }
131  write("leave", STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep()), veh, veh.getSpeed());
132  return false;
133  }
134  return true;
135 }
136 
137 
138 void
140  dev.writeXMLHeader("instantE1");
141 }
142 
143 
144 
145 /****************************************************************************/
146