SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MSDevice_Tripinfo.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A device which collects info on the vehicle trip
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2009-2015 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
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 <microsim/MSGlobals.h>
34 #include <microsim/MSNet.h>
35 #include <microsim/MSLane.h>
36 #include <microsim/MSEdge.h>
37 #include <microsim/MSVehicle.h>
40 #include "MSDevice_Tripinfo.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 #define NOT_ARRIVED TIME2STEPS(-1)
47 
48 
49 // ===========================================================================
50 // static members
51 // ===========================================================================
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 // ---------------------------------------------------------------------------
58 // static initialisation methods
59 // ---------------------------------------------------------------------------
60 void
61 MSDevice_Tripinfo::buildVehicleDevices(SUMOVehicle& v, std::vector<MSDevice*>& into) {
62  if (OptionsCont::getOptions().isSet("tripinfo-output")) {
63  MSDevice_Tripinfo* device = new MSDevice_Tripinfo(v, "tripinfo_" + v.getID());
64  into.push_back(device);
65  myPendingOutput.insert(device);
66  }
67 }
68 
69 
70 // ---------------------------------------------------------------------------
71 // MSDevice_Tripinfo-methods
72 // ---------------------------------------------------------------------------
73 MSDevice_Tripinfo::MSDevice_Tripinfo(SUMOVehicle& holder, const std::string& id) :
74  MSDevice(holder, id),
75  myDepartLane(""),
76  myDepartPos(-1),
77  myDepartSpeed(-1),
78  myWaitingSteps(0),
79  myArrivalTime(NOT_ARRIVED),
80  myArrivalLane(""),
81  myArrivalPos(-1),
82  myArrivalSpeed(-1),
83  myTimeLoss(0) {
84 }
85 
86 
88  // ensure clean up for vaporized vehicles which do not generate output
89  myPendingOutput.erase(this);
90 }
91 
92 
93 bool
95  SUMOReal /*newPos*/, SUMOReal newSpeed) {
96  if (newSpeed <= SUMO_const_haltingSpeed) {
98  }
99  // @note we are including the speed factor here, thus myTimeLoss can never be
100  // negative. The value is that of a driver who compares his travel time when
101  // the road is clear (which includes speed factor) with the actual travel time.
102  // @todo It might be usefull to recognize a departing vehicle and not
103  // count the time spent accelerating towards time loss since it is unavoidable
104  // (current interfaces do not give access to maximum acceleration)
105  const SUMOReal vmax = MIN2(veh.getMaxSpeed(), veh.getEdge()->getVehicleMaxSpeed(&veh));
106  if (vmax > 0) {
107  myTimeLoss += TIME2STEPS(TS * (vmax - newSpeed) / vmax);
108  }
109  return true;
110 }
111 
112 
113 bool
116  if (!MSGlobals::gUseMesoSim) {
117  myDepartLane = static_cast<MSVehicle&>(veh).getLane()->getID();
118  }
120  myDepartSpeed = veh.getSpeed();
121  }
122  return true;
123 }
124 
125 
126 bool
129  if (reason >= MSMoveReminder::NOTIFICATION_ARRIVED) {
131  if (!MSGlobals::gUseMesoSim) {
132  myArrivalLane = static_cast<MSVehicle&>(veh).getLane()->getID();
133  }
134  // @note vehicle may have moved past its arrivalPos during the last step
135  // due to non-zero arrivalspeed but we consider it as arrived at the desired position
136  // However, vaporization may happen anywhere (via TraCI)
139  } else {
141  }
142  myArrivalSpeed = veh.getSpeed();
143  }
144  return true;
145 }
146 
147 
148 void
150  myPendingOutput.erase(this);
151  SUMOTime finalTime;
152  SUMOReal finalPos;
153  SUMOReal finalPosOnInternal = 0;
154  if (myArrivalTime == NOT_ARRIVED) {
155  finalTime = MSNet::getInstance()->getCurrentTimeStep();
156  finalPos = myHolder.getPositionOnLane();
157  if (!MSGlobals::gUseMesoSim) {
158  const MSLane* lane = static_cast<MSVehicle&>(myHolder).getLane();
159  if (lane->getEdge().isInternal()) {
160  finalPosOnInternal = finalPos;
161  finalPos = myHolder.getEdge()->getLength();
162  }
163  }
164  } else {
165  finalTime = myArrivalTime;
166  finalPos = myArrivalPos;
167  }
168  const bool includeInternalLengths = MSGlobals::gUsingInternalLanes && MSNet::getInstance()->hasInternalLinks();
169  const SUMOReal routeLength = myHolder.getRoute().getDistanceBetween(myDepartPos, finalPos,
170  myHolder.getRoute().begin(), myHolder.getCurrentRouteEdge(), includeInternalLengths) + finalPosOnInternal;
171 
172  // write
173  OutputDevice& os = OutputDevice::getDeviceByOption("tripinfo-output");
174  os.openTag("tripinfo").writeAttr("id", myHolder.getID());
175  os.writeAttr("depart", time2string(myHolder.getDeparture()));
176  os.writeAttr("departLane", myDepartLane);
177  os.writeAttr("departPos", myDepartPos);
178  os.writeAttr("departSpeed", myDepartSpeed);
180  os.writeAttr("arrival", time2string(myArrivalTime));
181  os.writeAttr("arrivalLane", myArrivalLane);
182  os.writeAttr("arrivalPos", myArrivalPos);
183  os.writeAttr("arrivalSpeed", myArrivalSpeed);
184  os.writeAttr("duration", time2string(finalTime - myHolder.getDeparture()));
185  os.writeAttr("routeLength", routeLength);
186  os.writeAttr("waitSteps", myWaitingSteps);
187  os.writeAttr("timeLoss", time2string(myTimeLoss));
188  os.writeAttr("rerouteNo", myHolder.getNumberReroutes());
189  const std::vector<MSDevice*>& devices = myHolder.getDevices();
190  std::ostringstream str;
191  for (std::vector<MSDevice*>::const_iterator i = devices.begin(); i != devices.end(); ++i) {
192  if (i != devices.begin()) {
193  str << ' ';
194  }
195  str << (*i)->getID();
196  }
197  os.writeAttr("devices", str.str());
198  os.writeAttr("vType", myHolder.getVehicleType().getID());
199  os.writeAttr("vaporized", (myHolder.getEdge() == *(myHolder.getRoute().end() - 1) ? "" : "0"));
200  // cannot close tag because emission device output might follow
201 }
202 
203 
204 void
206  while (myPendingOutput.size() > 0) {
207  const MSDevice_Tripinfo* d = *myPendingOutput.begin();
208  if (d->myHolder.hasDeparted()) {
209  d->generateOutput();
210  // @todo also generate emission output if holder has a device
211  OutputDevice::getDeviceByOption("tripinfo-output").closeTag();
212  } else {
213  myPendingOutput.erase(d);
214  }
215  }
216 }
217 
218 
219 
220 /****************************************************************************/
221 
SUMOTime myArrivalTime
The vehicle&#39;s arrival time.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:257
MSEdge & getEdge() const
Returns the lane&#39;s edge.
Definition: MSLane.h:455
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
virtual bool hasDeparted() const =0
Returns whether this vehicle has departed.
virtual const MSRoute & getRoute() const =0
Returns the current route.
A device which collects info on the vehicle trip (mainly on departure and arrival) ...
SUMOVehicle & myHolder
The vehicle that stores the device.
Definition: MSDevice.h:153
virtual SUMOReal getPositionOnLane() const =0
Get the vehicle&#39;s position along the lane.
bool hasInternalLinks() const
return whether the network contains internal links
Definition: MSNet.h:585
virtual const MSEdge * getEdge() const =0
Returns the edge the vehicle is currently at.
Notification
Definition of a vehicle state.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:61
void generateOutput() const
Called on writing tripinfo output.
static void buildVehicleDevices(SUMOVehicle &v, std::vector< MSDevice * > &into)
Build devices for the given vehicle, if needed.
SUMOTime myTimeLoss
The time loss when compared to the desired and allowed speed.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
SUMOReal myArrivalPos
The position on the lane the vehicle arrived at.
~MSDevice_Tripinfo()
Destructor.
The vehicle got vaporized.
virtual SUMOReal getMaxSpeed() const =0
Returns the vehicle&#39;s maximum speed.
#define TIME2STEPS(x)
Definition: SUMOTime.h:66
#define TS
Definition: SUMOTime.h:52
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:235
bool notifyMove(SUMOVehicle &veh, SUMOReal oldPos, SUMOReal newPos, SUMOReal newSpeed)
Checks for waiting steps when the vehicle moves.
const MSLane * getLane() const
Returns the lane the reminder works on.
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
SUMOReal myDepartSpeed
The speed on departure.
SUMOReal myArrivalSpeed
The speed when arriving.
#define NOT_ARRIVED
static DeviceSet myPendingOutput
const std::string & getID() const
Returns the id.
Definition: Named.h:60
bool notifyEnter(SUMOVehicle &veh, MSMoveReminder::Notification reason)
Saves departure info on insertion.
virtual const std::vector< MSDevice * > & getDevices() const =0
Returns this vehicle&#39;s devices.
SUMOReal getLength() const
return the length of the edge
Definition: MSEdge.h:535
static void generateOutputForUnfinished()
generate output for vehicles which are still in the network
Representation of a vehicle.
Definition: SUMOVehicle.h:65
SUMOReal getDistanceBetween(SUMOReal fromPos, SUMOReal toPos, const MSEdge *fromEdge, const MSEdge *toEdge, bool includeInternal=true) const
Compute the distance between 2 given edges on this route, including the length of internal lanes...
Definition: MSRoute.cpp:255
unsigned int myWaitingSteps
The overall number of waiting steps.
The vehicle arrived at its destination (is deleted)
SUMOTime depart
The vehicle&#39;s departure time.
SUMOReal myDepartPos
The position on the lane the vehicle departed at.
T MIN2(T a, T b)
Definition: StdDefs.h:68
static bool gUsingInternalLanes
Information whether the simulation regards internal lanes.
Definition: MSGlobals.h:71
bool isInternal() const
return whether this edge is an internal edge
Definition: MSEdge.h:240
std::string myDepartLane
The lane the vehicle departed at.
Abstract in-vehicle device.
Definition: MSDevice.h:69
The vehicle has departed (was inserted into the network)
virtual SUMOReal getSpeed() const =0
Returns the vehicle&#39;s current speed.
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:81
std::string myArrivalLane
The lane the vehicle arrived at.
virtual SUMOTime getDeparture() const =0
Returns this vehicle&#39;s real departure time.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
virtual const SUMOVehicleParameter & getParameter() const =0
Returns the vehicle&#39;s parameter (including departure definition)
MSDevice_Tripinfo()
dummy constructor
const SUMOReal SUMO_const_haltingSpeed
the speed threshold at which vehicles are considered as halting
Definition: StdDefs.h:54
const std::string & getID() const
Returns the name of the vehicle type.
int SUMOTime
Definition: SUMOTime.h:43
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool notifyLeave(SUMOVehicle &veh, SUMOReal lastPos, MSMoveReminder::Notification reason)
Saves arrival info.
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:218
static const bool gUseMesoSim
Definition: MSGlobals.h:102
SUMOReal getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the maximum speed the vehicle may use on this edge.
Definition: MSEdge.cpp:641
std::set< const MSDevice_Tripinfo *, Named::NamedLikeComparatorIdLess< MSDevice_Tripinfo > > DeviceSet
devices which may still need to produce output
virtual const ConstMSEdgeVector::const_iterator & getCurrentRouteEdge() const =0
Returns an iterator pointing to the current edge in this vehicles route.
virtual SUMOReal getArrivalPos() const =0
Returns this vehicle&#39;s desired arrivalPos for its current route (may change on reroute) ...
Representation of a lane in the micro simulation.
Definition: MSLane.h:77
virtual unsigned int getNumberReroutes() const =0
Returns the number of new routes this vehicle got.
virtual const std::string & getID() const =0
Get the vehicle&#39;s ID.
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:75
virtual const MSVehicleType & getVehicleType() const =0
Returns the vehicle&#39;s type.