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 /****************************************************************************/
9 // An instantaneous induction loop
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
12 // Copyright (C) 2011-2014 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