SUMO - Simulation of Urban MObility
MEVehicle.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // A vehicle from the mesoscopic point of view
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
10 // Copyright (C) 2001-2017 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 <iostream>
32 #include <cassert>
33 #include <utils/common/StdDefs.h>
39 #include <microsim/MSGlobals.h>
40 #include <microsim/MSEdge.h>
41 #include <microsim/MSLane.h>
42 #include <microsim/MSNet.h>
43 #include <microsim/MSVehicleType.h>
44 #include <microsim/MSLink.h>
45 #include "MELoop.h"
46 #include "MEVehicle.h"
47 #include "MESegment.h"
48 
49 #ifdef CHECK_MEMORY_LEAKS
50 #include <foreign/nvwa/debug_new.h>
51 #endif // CHECK_MEMORY_LEAKS
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
58  const MSVehicleType* type, const SUMOReal speedFactor) :
59  MSBaseVehicle(pars, route, type, speedFactor),
60  mySegment(0),
61  myQueIndex(0),
62  myEventTime(SUMOTime_MIN),
63  myLastEntryTime(SUMOTime_MIN),
64  myBlockTime(SUMOTime_MAX) {
65  if ((*myCurrEdge)->getPurpose() != MSEdge::EDGEFUNCTION_DISTRICT) {
66  if ((*myCurrEdge)->allowedLanes(type->getVehicleClass()) == 0) {
67  throw ProcessError("Vehicle '" + pars->id + "' is not allowed to depart on any lane of its first edge.");
68  }
69  if (pars->departSpeedProcedure == DEPART_SPEED_GIVEN && pars->departSpeed > type->getMaxSpeed()) {
70  throw ProcessError("Departure speed for vehicle '" + pars->id +
71  "' is too high for the vehicle type '" + type->getID() + "'.");
72  }
73  }
74 }
75 
76 
78 MEVehicle::getBackPositionOnLane(const MSLane* /* lane */) const {
80 }
81 
82 
85 // the following interpolation causes problems with arrivals and calibrators
86 // const SUMOReal fracOnSegment = MIN2(SUMOReal(1), STEPS2TIME(MSNet::getInstance()->getCurrentTimeStep() - myLastEntryTime) / STEPS2TIME(myEventTime - myLastEntryTime));
87  return (SUMOReal(mySegment->getIndex()) /* + fracOnSegment */) * mySegment->getLength();
88 }
89 
90 
93  const MSLane* const lane = getEdge()->getLanes()[0];
95 }
96 
97 
100  const MSLane* const lane = getEdge()->getLanes()[0];
102 }
103 
104 
105 Position
106 MEVehicle::getPosition(const SUMOReal offset) const {
107  const MSLane* const lane = getEdge()->getLanes()[0];
108  return lane->geometryPositionAtOffset(getPositionOnLane() + offset);
109 }
110 
111 
112 SUMOReal
114  if (getWaitingTime() > 0) {
115  return 0;
116  } else {
117  return getAverageSpeed();
118  }
119 }
120 
121 
122 SUMOReal
125 }
126 
127 
128 SUMOReal
131  const SUMOReal v = getSpeed();
132  return MIN2(link->getViaLaneOrLane()->getVehicleMaxSpeed(this),
133  (SUMOReal)sqrt(2 * link->getLength() * getVehicleType().getCarFollowModel().getMaxAccel() + v * v));
134 }
135 
136 
137 SUMOReal
138 MEVehicle::getConservativeSpeed(SUMOTime& earliestArrival) const {
139  earliestArrival = MAX2(myEventTime, earliestArrival - DELTA_T); // event times have subsecond resolution
140  return mySegment->getLength() / STEPS2TIME(earliestArrival - myLastEntryTime);
141 }
142 
143 
144 bool
146  // vehicle has just entered a new edge. Position is 0
147  if (myCurrEdge == myRoute->end() - 1) { // may happen during teleport
148  return true;
149  }
150  ++myCurrEdge;
151  if ((*myCurrEdge)->isVaporizing()) {
152  return true;
153  }
154  return hasArrived();
155 }
156 
157 
158 bool
160  // mySegment may be 0 due to teleporting or arrival
161  return myCurrEdge == myRoute->end() - 1 && (
162  (mySegment == 0)
165 }
166 
167 bool
169  return getSegment() != 0;
170 }
171 
172 
173 bool
175  return false; // parking attribute of a stop is not yet evaluated /implemented
176 }
177 
178 
179 bool
180 MEVehicle::replaceRoute(const MSRoute* newRoute, bool onInit, int offset, bool addStops) {
181  UNUSED_PARAMETER(addStops); // @todo recheck!
182  const ConstMSEdgeVector& edges = newRoute->getEdges();
183  // assert the vehicle may continue (must not be "teleported" or whatever to another position)
184  if (!onInit && !newRoute->contains(*myCurrEdge)) {
185  return false;
186  }
187  MSLink* oldLink = 0;
188  MSLink* newLink = 0;
189  if (mySegment != 0) {
190  oldLink = mySegment->getLink(this);
191  }
192  // rebuild in-vehicle route information
193  if (onInit) {
194  myCurrEdge = newRoute->begin();
195  } else {
196  myCurrEdge = find(edges.begin() + offset, edges.end(), *myCurrEdge);
197  }
198  // check whether the old route may be deleted (is not used by anyone else)
199  newRoute->addReference();
200  myRoute->release();
201  // assign new route
202  myRoute = newRoute;
203  if (mySegment != 0) {
204  newLink = mySegment->getLink(this);
205  }
206  // update approaching vehicle information
207  if (oldLink != 0 && oldLink != newLink) {
208  oldLink->removeApproaching(this);
209  MELoop::setApproaching(this, newLink);
210  }
211  // update arrival definition
213  // save information that the vehicle was rerouted
217  return true;
218 }
219 
220 
221 bool
222 MEVehicle::addStop(const SUMOVehicleParameter::Stop& stopPar, std::string& /*errorMsg*/, SUMOTime /* untilOffset */) {
223  const MSEdge* const edge = MSEdge::dictionary(stopPar.lane.substr(0, stopPar.lane.rfind('_')));
224  assert(edge != 0);
225  MESegment* stopSeg = MSGlobals::gMesoNet->getSegmentForEdge(*edge, stopPar.endPos);
226  myStops[stopSeg] += stopPar.duration;
227  return true;
228 }
229 
230 
231 bool
233  return myStops.find(mySegment) != myStops.end();
234 }
235 
236 
237 bool
239  return false;
240 }
241 
242 
243 SUMOTime
244 MEVehicle::getStoptime(const MESegment* const seg) const {
245  if (myStops.find(seg) != myStops.end()) {
246  return myStops.find(seg)->second;
247  }
248  return 0;
249 }
250 
251 
252 const ConstMSEdgeVector
254  ConstMSEdgeVector result;
255  for (std::map<const MESegment* const, SUMOTime>::const_iterator iter = myStops.begin(); iter != myStops.end(); ++iter) {
256  result.push_back(&iter->first->getEdge());
257  }
258  return result;
259 }
260 
261 
262 bool
264  return mySegment == 0 || mySegment->isOpen(this);
265 }
266 
267 
268 SUMOReal
270  if (mySegment == 0) {
271  return 0;
272  } else {
273  return STEPS2TIME(mySegment->getLinkPenalty(this));
274  }
275 }
276 
277 
278 void
280  for (MoveReminderCont::iterator i = myMoveReminders.begin(); i != myMoveReminders.end(); ++i) {
281  if (i->first == rem) {
283  (mySegment->getIndex() + 1) * mySegment->getLength(),
284  getLastEntryTime(), currentTime, exitTime, false);
285 #ifdef _DEBUG
286  if (myTraceMoveReminders) {
287  traceMoveReminder("notifyMove", i->first, i->second, true);
288  }
289 #endif
290  return;
291  }
292  }
293 }
294 
295 
296 void
297 MEVehicle::updateDetectors(SUMOTime currentTime, const bool isLeave, const MSMoveReminder::Notification reason) {
298  // segments of the same edge have the same reminder so no cleaning up must take place
299  const bool cleanUp = isLeave && (reason != MSMoveReminder::NOTIFICATION_SEGMENT);
300  for (MoveReminderCont::iterator rem = myMoveReminders.begin(); rem != myMoveReminders.end();) {
301  if (currentTime != getLastEntryTime()) {
302  rem->first->updateDetector(*this, mySegment->getIndex() * mySegment->getLength(),
303  (mySegment->getIndex() + 1) * mySegment->getLength(),
304  getLastEntryTime(), currentTime, getEventTime(), cleanUp);
305 #ifdef _DEBUG
306  if (myTraceMoveReminders) {
307  traceMoveReminder("notifyMove", rem->first, rem->second, true);
308  }
309 #endif
310  }
311  if (!isLeave || rem->first->notifyLeave(*this, mySegment->getLength(), reason)) {
312 #ifdef _DEBUG
313  if (isLeave && myTraceMoveReminders) {
314  traceMoveReminder("notifyLeave", rem->first, rem->second, true);
315  }
316 #endif
317  ++rem;
318  } else {
319 #ifdef _DEBUG
320  if (myTraceMoveReminders) {
321  traceMoveReminder("remove", rem->first, rem->second, false);
322  }
323 #endif
324  rem = myMoveReminders.erase(rem);
325  }
326  }
327 }
328 
329 
330 void
333  assert(mySegment == 0 || *myCurrEdge == &mySegment->getEdge());
334  std::vector<SUMOTime> internals;
335  internals.push_back(myDeparture);
336  internals.push_back((SUMOTime)distance(myRoute->begin(), myCurrEdge));
337  internals.push_back(mySegment == 0 ? (SUMOTime) - 1 : (SUMOTime)mySegment->getIndex());
338  internals.push_back((SUMOTime)getQueIndex());
339  internals.push_back(myEventTime);
340  internals.push_back(myLastEntryTime);
341  internals.push_back(myBlockTime);
342  out.writeAttr(SUMO_ATTR_STATE, toString(internals));
343  out.closeTag();
344 }
345 
346 
347 void
348 MEVehicle::loadState(const SUMOSAXAttributes& attrs, const SUMOTime offset) {
349  if (attrs.hasAttribute(SUMO_ATTR_POSITION)) {
350  throw ProcessError("Error: Invalid vehicles in state (may be a micro state)!");
351  }
352  int routeOffset;
353  int segIndex;
354  int queIndex;
355  std::istringstream bis(attrs.getString(SUMO_ATTR_STATE));
356  bis >> myDeparture;
357  bis >> routeOffset;
358  bis >> segIndex;
359  bis >> queIndex;
360  bis >> myEventTime;
361  bis >> myLastEntryTime;
362  bis >> myBlockTime;
363  if (hasDeparted()) {
364  myDeparture -= offset;
365  myEventTime -= offset;
366  myLastEntryTime -= offset;
367  myCurrEdge += routeOffset;
368  if (segIndex >= 0) {
370  while (seg->getIndex() != (int)segIndex) {
371  seg = seg->getNextSegment();
372  assert(seg != 0);
373  }
374  setSegment(seg, queIndex);
375  } else {
376  // on teleport
377  setSegment(0, 0);
378  assert(myEventTime != SUMOTime_MIN);
380  }
381  }
382  if (myBlockTime != SUMOTime_MAX) {
383  myBlockTime -= offset;
384  }
385 }
386 
387 
388 /****************************************************************************/
389 
void updateDetectorForWriting(MSMoveReminder *rem, SUMOTime currentTime, SUMOTime exitTime)
Updates a single vehicle detector if present.
Definition: MEVehicle.cpp:279
void loadState(const SUMOSAXAttributes &attrs, const SUMOTime offset)
Loads the state of this vehicle from the given description.
Definition: MEVehicle.cpp:348
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
bool isParking() const
Returns whether the vehicle is parking.
Definition: MEVehicle.cpp:174
virtual void setSegment(MESegment *s, int idx=0)
Sets the current segment the vehicle is at together with its que.
Definition: MEVehicle.h:216
SUMOReal getMaxSpeed() const
Get vehicle&#39;s maximum speed [m/s].
long long int SUMOTime
Definition: SUMOTime.h:43
SUMOVehicleClass getVehicleClass() const
Get this vehicle type&#39;s vehicle class.
bool addStop(const SUMOVehicleParameter::Stop &stopPar, std::string &errorMsg, SUMOTime untilOffset=0)
Adds a stop.
Definition: MEVehicle.cpp:222
bool hasArrived() const
Returns whether this vehicle has already arived (reached the arrivalPosition on its final edge) ...
Definition: MEVehicle.cpp:159
SUMOReal rotationAtOffset(SUMOReal pos) const
Returns the rotation at the given length.
MoveReminderCont myMoveReminders
Currently relevant move reminders.
SUMOReal myArrivalPos
The position on the destination lane where the vehicle stops.
SUMOTime duration
The stopping duration.
bool replaceRoute(const MSRoute *route, bool onInit=false, int offset=0, bool addStops=true)
Replaces the current route by the given one.
Definition: MEVehicle.cpp:180
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:192
SUMOReal getSlope() const
Returns the slope of the road at vehicle&#39;s position.
Definition: MEVehicle.cpp:99
SUMOTime getStoptime(const MESegment *const seg) const
Returns how long to stop at the given segment.
Definition: MEVehicle.cpp:244
void updateDetector(SUMOVehicle &veh, SUMOReal entryPos, SUMOReal leavePos, SUMOTime entryTime, SUMOTime currentTime, SUMOTime leaveTime, bool cleanUp)
SUMOReal departSpeed
(optional) The initial speed of the vehicle
bool isStoppedTriggered() const
Returns whether the vehicle is on a triggered stop.
Definition: MEVehicle.cpp:238
SUMOTime getWaitingTime() const
Returns the duration for which the vehicle was blocked.
Definition: MEVehicle.h:272
SUMOReal getSpeed() const
Returns the vehicle&#39;s estimated speed assuming no delays.
Definition: MEVehicle.cpp:113
Notification
Definition of a vehicle state.
SUMOReal getLength() const
Get vehicle&#39;s length [m].
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:159
T MAX2(T a, T b)
Definition: StdDefs.h:70
SUMOTime getEventTime() const
Returns the (planned) time at which the vehicle leaves his current cell.
Definition: MEVehicle.h:207
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
const MSRoute * myRoute
This Vehicle&#39;s route.
bool hasDeparted() const
Returns whether this vehicle has already departed.
static bool dictionary(const std::string &id, MSEdge *edge)
Inserts edge into the static dictionary Returns true if the key id isn&#39;t already in the dictionary...
Definition: MSEdge.cpp:693
The vehicle changes the segment (meso only)
std::vector< const MSEdge * > ConstMSEdgeVector
Definition: MSEdge.h:78
The base class for microscopic and mesoscopic vehicles.
Definition: MSBaseVehicle.h:56
SUMOTime getLastEntryTime() const
Returns the time the vehicle entered the current segment.
Definition: MEVehicle.h:249
SUMOReal getAverageSpeed() const
Returns the vehicle&#39;s estimated average speed on the segment assuming no further delays.
Definition: MEVehicle.cpp:123
const Position geometryPositionAtOffset(SUMOReal offset, SUMOReal lateralOffset=0) const
Definition: MSLane.h:444
const MSEdge & getEdge() const
Returns the edge this segment belongs to.
Definition: MESegment.h:271
SUMOReal getConservativeSpeed(SUMOTime &earliestArrival) const
Returns the vehicle&#39;s estimated speed taking into account delays.
Definition: MEVehicle.cpp:138
#define UNUSED_PARAMETER(x)
Definition: StdDefs.h:38
The speed is given.
The car-following model and parameter.
Definition: MSVehicleType.h:74
virtual void saveState(OutputDevice &out)
Saves the (common) state of a vehicle.
const MSCFModel & getCarFollowModel() const
Returns the vehicle type&#39;s car following model definition (const version)
SUMOReal slopeDegreeAtOffset(SUMOReal pos) const
Returns the slope at the given length.
void calculateArrivalParams()
(Re-)Calculates the arrival position and lane from the vehicle parameters
The state of a link.
MSLink * getLink(const MEVehicle *veh, bool tlsPenalty=false) const
Returns the link the given car will use when passing the next junction.
Definition: MESegment.cpp:399
#define SUMOTime_MIN
Definition: SUMOTime.h:45
A road/street connecting two junctions.
Definition: MSEdge.h:80
SUMOReal getBackPositionOnLane(const MSLane *lane) const
Get the vehicle&#39;s position relative to the given lane.
Definition: MEVehicle.cpp:78
bool isStopped() const
Returns whether the vehicle is at a stop.
Definition: MEVehicle.cpp:232
DepartSpeedDefinition departSpeedProcedure
Information how the vehicle&#39;s initial speed shall be chosen.
MESegment * getNextSegment() const
Returns the following segment on the same edge (0 if it is the last).
Definition: MESegment.h:157
The edge is a district edge.
Definition: MSEdge.h:99
The vehicle got a new route.
Definition: MSNet.h:580
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
bool isOnRoad() const
Returns the information whether the vehicle is on a road (is simulated)
Definition: MEVehicle.cpp:168
Encapsulated SAX-Attributes.
SUMOTime getLinkPenalty(const MEVehicle *veh) const
Returns the penalty time for passing a link (if using gMesoTLSPenalty > 0 or gMesoMinorPenalty > 0) ...
Definition: MESegment.cpp:703
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
SUMOReal endPos
The stopping position end.
SUMOReal getLength() const
Returns the length of the segment in meters.
Definition: MESegment.h:165
Position getPosition(const SUMOReal offset=0) const
Return current position (x/y, cartesian)
Definition: MEVehicle.cpp:106
bool contains(const MSEdge *const edge) const
Definition: MSRoute.h:110
#define STEPS2TIME(x)
Definition: SUMOTime.h:65
const ConstMSEdgeVector & getEdges() const
Definition: MSRoute.h:128
T MIN2(T a, T b)
Definition: StdDefs.h:64
#define POSITION_EPS
Definition: config.h:182
SUMOTime myLastEntryTime
The time the vehicle entered its current segment.
Definition: MEVehicle.h:347
Something on a lane to be noticed about vehicle movement.
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
void addReference() const
increments the reference counter for the route
Definition: MSRoute.cpp:103
void saveState(OutputDevice &out)
Saves the states of a vehicle.
Definition: MEVehicle.cpp:331
bool isOpen(const MEVehicle *veh) const
Returns whether the vehicle may use the next link.
Definition: MESegment.cpp:430
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
MESegment * mySegment
The segment the vehicle is at.
Definition: MEVehicle.h:338
static void setApproaching(MEVehicle *veh, MSLink *link)
registers vehicle with the given link
Definition: MELoop.cpp:215
std::string lane
The lane to stop at.
bool moveRoutePointer()
Update when the vehicle enters a new edge in the move step.
Definition: MEVehicle.cpp:145
SUMOReal getMaxAccel() const
Get the vehicle type&#39;s maximum acceleration [m/s^2].
Definition: MSCFModel.h:193
void addStops(const bool ignoreStopErrors)
Adds stops to the built vehicle.
Structure representing possible vehicle parameter.
MSRouteIterator end() const
Returns the end of the list of edges to pass.
Definition: MSRoute.cpp:84
#define SUMOTime_MAX
Definition: SUMOTime.h:44
SUMOReal interpolateLanePosToGeometryPos(SUMOReal lanePos) const
Definition: MSLane.h:438
A single mesoscopic segment (cell)
Definition: MESegment.h:57
Definition of vehicle stop (position and duration)
void updateDetectors(SUMOTime currentTime, const bool isLeave, const MSMoveReminder::Notification reason=MSMoveReminder::NOTIFICATION_JUNCTION)
Updates all vehicle detectors.
Definition: MEVehicle.cpp:297
int getQueIndex() const
Returns the index of the que the vehicle is in.
Definition: MEVehicle.h:233
const MSVehicleType & getVehicleType() const
Returns the vehicle&#39;s type definition.
MESegment * getSegmentForEdge(const MSEdge &e, SUMOReal pos=0)
Get the segment for a given edge at a given position.
Definition: MELoop.cpp:293
SUMOReal getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MEVehicle.cpp:84
const std::string & getID() const
Returns the name of the vehicle type.
std::map< const MESegment *const, SUMOTime > myStops
where to stop
Definition: MEVehicle.h:353
static MELoop * gMesoNet
mesoscopic simulation infrastructure
Definition: MSGlobals.h:110
int myNumberReroutes
The number of reroutings.
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to)
Informs all added listeners about a vehicle&#39;s state change.
Definition: MSNet.cpp:791
int getIndex() const
Returns the running index of the segment in the edge (0 is the most upstream).
Definition: MESegment.h:149
const PositionVector & getShape() const
Returns this lane&#39;s shape.
Definition: MSLane.h:427
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:71
bool closeTag()
Closes the most recently opened tag.
#define SUMOReal
Definition: config.h:208
SUMOTime myEventTime
The (planned) time of leaving the segment (cell)
Definition: MEVehicle.h:344
const ConstMSEdgeVector getStopEdges() const
Returns the list of still pending stop edges.
Definition: MEVehicle.cpp:253
MSRouteIterator myCurrEdge
Iterator to current route-edge.
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
bool mayProceed() const
Returns whether the vehicle is allowed to pass the next junction.
Definition: MEVehicle.cpp:263
void release() const
deletes the route if there are no further references to it
Definition: MSRoute.cpp:109
SUMOReal getVehicleMaxSpeed(const SUMOVehicle *const veh) const
Returns the lane&#39;s maximum speed, given a vehicle&#39;s speed limit adaptation.
Definition: MSLane.h:458
SUMOTime myBlockTime
The time at which the vehicle was blocked on its current segment.
Definition: MEVehicle.h:350
Representation of a lane in the micro simulation.
Definition: MSLane.h:79
void addLeaderCar(MEVehicle *veh, MSLink *link)
Adds the given car to the leading vehicles.
Definition: MELoop.cpp:208
MESegment * getSegment() const
Returns the current segment the vehicle is on.
Definition: MEVehicle.h:225
SUMOReal estimateLeaveSpeed(const MSLink *link) const
Returns the vehicle&#39;s estimated speed after driving accross the link.
Definition: MEVehicle.cpp:129
SUMOReal getAngle() const
Returns the vehicle&#39;s direction in degrees.
Definition: MEVehicle.cpp:92
SUMOTime myDeparture
The real departure time.
MSRouteIterator begin() const
Returns the begin of the list of edges to pass.
Definition: MSRoute.cpp:78
SUMOReal getCurrentLinkPenaltySeconds() const
Returns the delay that is accrued due to option –meso-tls-penalty or –meso-minor-penalty.
Definition: MEVehicle.cpp:269
std::string id
The vehicle&#39;s id.
MEVehicle(SUMOVehicleParameter *pars, const MSRoute *route, const MSVehicleType *type, const SUMOReal speedFactor)
Constructor.
Definition: MEVehicle.cpp:57