Eclipse SUMO - Simulation of Urban MObility
MSVehicleTransfer.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
17 // A mover of vehicles that got stucked due to grid locks
18 /****************************************************************************/
19 
20 
21 // ===========================================================================
22 // included modules
23 // ===========================================================================
24 #include <config.h>
25 
26 #include <iostream>
29 #include "MSNet.h"
30 #include "MSLane.h"
31 #include "MSEdge.h"
32 #include "MSVehicle.h"
33 #include "MSParkingArea.h"
35 #include "MSVehicleControl.h"
36 #include "MSInsertionControl.h"
37 #include "MSVehicleTransfer.h"
38 
39 
40 // ===========================================================================
41 // static member definitions
42 // ===========================================================================
45 
46 
47 // ===========================================================================
48 // member method definitions
49 // ===========================================================================
50 bool
52  return myVeh->getNumericalID() < v2.myVeh->getNumericalID();
53 }
54 
55 
56 void
58  if (veh->isParking()) {
62  } else {
65  if (veh->succEdge(1) == nullptr) {
66  WRITE_WARNING("Vehicle '" + veh->getID() + "' teleports beyond arrival edge '" + veh->getEdge()->getID() + "', time " + time2string(t) + ".");
69  return;
70  }
72  veh->enterLaneAtMove(veh->succEdge(1)->getLanes()[0], true);
73  }
74  myVehicles.push_back(VehicleInformation(t, veh, -1, veh->isParking()));
75 }
76 
77 
78 void
80  auto& vehInfos = myVehicles.getContainer();
81  for (auto i = vehInfos.begin(); i != vehInfos.end(); ++i) {
82  if (i->myVeh == veh) {
83  if (i->myParking) {
84  veh->getLane()->removeParking(veh);
85  }
86  vehInfos.erase(i);
87  break;
88  }
89  }
90  myVehicles.unlock();
91 }
92 
93 
94 void
96  // go through vehicles
97  auto& vehInfos = myVehicles.getContainer();
98  std::sort(vehInfos.begin(), vehInfos.end());
99  for (auto i = vehInfos.begin(); i != vehInfos.end();) {
100  // vehicle information cannot be const because we need to assign the proceed time
101  VehicleInformation& desc = *i;
102 
103  if (desc.myParking) {
104  // handle parking vehicles
105  if (time != desc.myTransferTime) {
106  // avoid calling processNextStop twice in the transfer step
107  desc.myVeh->processNextStop(1);
108  }
109  if (desc.myVeh->keepStopping(true)) {
110  i++;
111  continue;
112  }
113  // parking finished, head back into traffic
114  }
115  const SUMOVehicleClass vclass = desc.myVeh->getVehicleType().getVehicleClass();
116  const MSEdge* e = desc.myVeh->getEdge();
117  const MSEdge* nextEdge = desc.myVeh->succEdge(1);
118 
119 
120  if (desc.myParking) {
122  const double departPos = pa != nullptr ? pa->getInsertionPosition(*desc.myVeh) : desc.myVeh->getPositionOnLane();
123  // handle parking vehicles
124  if (desc.myVeh->getLane()->isInsertionSuccess(desc.myVeh, 0, departPos, desc.myVeh->getLateralPositionOnLane(),
127  desc.myVeh->getLane()->removeParking(desc.myVeh);
128  i = vehInfos.erase(i);
129  } else {
130  // blocked from entering the road
132  // signal wish to re-enter the road
134  if (pa) {
135  // update freePosition so other vehicles can help with insertion
137  }
138  }
139  i++;
140  }
141  } else {
142  const double departPos = 0;
143  // get the lane on which this vehicle should continue
144  // first select all the lanes which allow continuation onto nextEdge
145  // then pick the one which is least occupied
146  MSLane* l = (nextEdge != nullptr ? e->getFreeLane(e->allowedLanes(*nextEdge, vclass), vclass, departPos) :
147  e->getFreeLane(nullptr, vclass, departPos));
148  // handle teleporting vehicles, lane may be 0 because permissions were modified by a closing rerouter or TraCI
149  if (l != nullptr && l->freeInsertion(*(desc.myVeh), MIN2(l->getSpeedLimit(), desc.myVeh->getMaxSpeed()), 0, MSMoveReminder::NOTIFICATION_TELEPORT)) {
150  WRITE_WARNING("Vehicle '" + desc.myVeh->getID() + "' ends teleporting on edge '" + e->getID() + "', time " + time2string(time) + ".");
152  i = vehInfos.erase(i);
153  } else {
154  // could not insert. maybe we should proceed in virtual space
155  if (desc.myProceedTime < 0) {
156  // initialize proceed time (delayed to avoid lane-order dependency in executeMove)
158  } else if (desc.myProceedTime < time) {
159  if (desc.myVeh->succEdge(1) == nullptr) {
160  WRITE_WARNING("Vehicle '" + desc.myVeh->getID() + "' teleports beyond arrival edge '" + e->getID() + "', time " + time2string(time) + ".");
163  i = vehInfos.erase(i);
164  continue;
165  }
166  // let the vehicle move to the next edge
168  // active move reminders (i.e. rerouters)
169  desc.myVeh->enterLaneAtMove(desc.myVeh->succEdge(1)->getLanes()[0], true);
170  // use current travel time to determine when to move the vehicle forward
172  }
173  ++i;
174  }
175  }
176  }
177  myVehicles.unlock();
178 }
179 
180 
183  if (myInstance == nullptr) {
185  }
186  return myInstance;
187 }
188 
189 
191 
192 
194  myInstance = nullptr;
195 }
196 
197 
198 void
200  for (const VehicleInformation& vehInfo : myVehicles.getContainer()) {
202  out.writeAttr(SUMO_ATTR_ID, vehInfo.myVeh->getID());
203  out.writeAttr(SUMO_ATTR_DEPART, vehInfo.myProceedTime);
204  if (vehInfo.myParking) {
205  out.writeAttr(SUMO_ATTR_PARKING, vehInfo.myVeh->getLane()->getID());
206  }
207  out.closeTag();
208  }
209  myVehicles.unlock();
210 }
211 
212 
213 void
215  MSVehicle* veh = dynamic_cast<MSVehicle*>(vc.getVehicle(attrs.getString(SUMO_ATTR_ID)));
216  if (veh == nullptr) {
217  // deleted
218  return;
219  }
220  SUMOTime proceedTime = (SUMOTime)attrs.getLong(SUMO_ATTR_DEPART);
221  MSLane* parkingLane = attrs.hasAttribute(SUMO_ATTR_PARKING) ? MSLane::dictionary(attrs.getString(SUMO_ATTR_PARKING)) : nullptr;
222  myVehicles.push_back(VehicleInformation(-1, veh, proceedTime - offset, parkingLane != nullptr));
223  if (parkingLane != nullptr) {
224  parkingLane->addParking(veh);
225  veh->setTentativeLaneAndPosition(parkingLane, veh->getPositionOnLane());
226  veh->processNextStop(veh->getSpeed());
227  }
229 }
230 
231 
232 /****************************************************************************/
A lane area vehicles can halt at.
Definition: MSParkingArea.h:59
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:256
bool enterLaneAtMove(MSLane *enteredLane, bool onTeleporting=false)
Update when the vehicle enters a new lane in the move step.
Definition: MSVehicle.cpp:4395
VehicleInformation(SUMOTime t, MSVehicle *veh, SUMOTime proceedTime, bool parking)
Constructor.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:80
long long int SUMOTime
Definition: SUMOTime.h:35
static MSVehicleTransfer * myInstance
The static singleton-instance.
void notifyEgressBlocked()
update state so that vehicles wishing to enter cooperate with exiting vehicles
void remove(MSVehicle *veh)
Remove a vehicle from this transfer object.
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to, const std::string &info="")
Informs all added listeners about a vehicle&#39;s state change.
Definition: MSNet.cpp:882
Holds the information needed to move the vehicle over the network.
virtual void removeParking(MSVehicle *veh)
remove parking vehicle. This must be syncrhonized when running with GUI
Definition: MSLane.cpp:3012
MSVehicleTransfer()
Constructor.
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
bool operator<(const VehicleInformation &v2) const
sort by vehicle ID for repeatable parallel simulation
MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:561
bool isInsertionSuccess(MSVehicle *vehicle, double speed, double pos, double posLat, bool recheckNextLanes, MSMoveReminder::Notification notification)
Tries to insert the given vehicle with the given state (speed and pos)
Definition: MSLane.cpp:638
double getInsertionPosition(const SUMOVehicle &forVehicle) const
Returns the insertion position of a parked vehicle.
double getPositionOnLane() const
Get the vehicle&#39;s position along the lane.
Definition: MSVehicle.h:397
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
virtual ~MSVehicleTransfer()
Destructor.
std::string time2string(SUMOTime t)
Definition: SUMOTime.cpp:65
const std::vector< MSLane * > * allowedLanes(const MSEdge &destination, SUMOVehicleClass vclass=SVC_IGNORING) const
Get the allowed lanes to reach the destination-edge.
Definition: MSEdge.cpp:398
const std::vector< MSLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: MSEdge.h:165
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:168
FXSynchQue< VehicleInformation, std::vector< VehicleInformation > > myVehicles
The information about stored vehicles to move virtually.
const std::string & getID() const
Returns the id.
Definition: Named.h:77
#define TIME2STEPS(x)
Definition: SUMOTime.h:59
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
bool myParking
whether the vehicle is or was parking
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:239
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:4609
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
Right blinker lights are switched on.
Definition: MSVehicle.h:1180
double getCurrentTravelTime(const double minSpeed=NUMERICAL_EPS) const
Computes and returns the current travel time for this edge.
Definition: MSEdge.cpp:787
double getMaxSpeed() const
Returns the maximum speed.
bool signalSet(int which) const
Returns whether the given signal is on.
Definition: MSVehicle.h:1258
A road/street connecting two junctions.
Definition: MSEdge.h:76
void leaveLane(const MSMoveReminder::Notification reason, const MSLane *approachedLane=0)
Update of members if vehicle leaves a new lane in the lane change step or at arrival.
Definition: MSVehicle.cpp:4559
Left blinker lights are switched on.
Definition: MSVehicle.h:1182
virtual long long int getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
The vehicles starts to park.
Definition: MSNet.h:550
void saveState(OutputDevice &out)
Saves the current state into the given stream.
void checkInsertions(SUMOTime time)
Checks "movement" of stored vehicles.
Encapsulated SAX-Attributes.
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:337
double getSpeedLimit() const
Returns the lane&#39;s maximum allowed speed.
Definition: MSLane.h:533
T MIN2(T a, T b)
Definition: StdDefs.h:74
void add(const SUMOTime t, MSVehicle *veh)
Adds a vehicle to this transfer object.
The vehicle started to teleport.
Definition: MSNet.h:542
MSParkingArea * getCurrentParkingArea()
get the current parking area stop or nullptr
Definition: MSVehicle.cpp:1741
void onRemovalFromNet(const MSMoveReminder::Notification reason)
Called when the vehicle is removed from the network.
Definition: MSVehicle.cpp:1055
SUMOTime myProceedTime
The time at which the vehicle should be moved virtually one edge further.
void loadState(const SUMOSAXAttributes &attrs, const SUMOTime offset, MSVehicleControl &vc)
Loads one transfer vehicle state from the given descriptionn.
The vehicle ends to park.
Definition: MSNet.h:552
double getLateralPositionOnLane() const
Get the vehicle&#39;s lateral position on the lane.
Definition: MSVehicle.h:434
void alreadyDeparted(SUMOVehicle *veh)
stops trying to emit the given vehicle (because it already departed)
static MSVehicleTransfer * getInstance()
Returns the instance of this object.
bool freeInsertion(MSVehicle &veh, double speed, double posLat, MSMoveReminder::Notification notification=MSMoveReminder::NOTIFICATION_DEPARTED)
Tries to insert the given vehicle on any place.
Definition: MSLane.cpp:386
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1855
The vehicle starts or ends parking.
bool keepStopping(bool afterProcessing=false) const
Returns whether the vehicle is stopped and must continue to do so.
Definition: MSVehicle.cpp:1767
const MSVehicleType & getVehicleType() const
Returns the vehicle&#39;s type definition.
double processNextStop(double currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
Definition: MSVehicle.cpp:1819
void setTentativeLaneAndPosition(MSLane *lane, double pos, double posLat=0)
set tentative lane and position during insertion to ensure that all cfmodels work (some of them requi...
Definition: MSVehicle.cpp:5240
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:390
NumericalID getNumericalID() const
return the numerical ID which is only for internal usage
const MSEdge * succEdge(int nSuccs) const
Returns the nSuccs&#39;th successor of edge the vehicle is currently at.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:64
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
void switchOnSignal(int signal)
Switches the given signal on.
Definition: MSVehicle.h:1233
MSVehicle * myVeh
The vehicle itself.
SUMOTime myTransferTime
the time at which this vehicle was removed from the network
void addParking(MSVehicle *veh)
add parking vehicle. This should only used during state loading
Definition: MSLane.cpp:3006
The vehicle was teleported out of the net.
The class responsible for building and deletion of vehicles.
bool isParking() const
Returns whether the vehicle is parking.
Definition: MSVehicle.cpp:1793
double getSpeed() const
Returns the vehicle&#39;s current speed.
Definition: MSVehicle.h:477
const std::string & getID() const
Returns the name of the vehicle.
Representation of a lane in the micro simulation.
Definition: MSLane.h:83
void scheduleVehicleRemoval(SUMOVehicle *veh, bool checkDuplicate=false)
Removes a vehicle after it has ended.
MSLane * getFreeLane(const std::vector< MSLane *> *allowed, const SUMOVehicleClass vclass, double departPos) const
Finds the emptiest lane allowing the vehicle class.
Definition: MSEdge.cpp:444
The vehicle ended being teleported.
Definition: MSNet.h:544
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
static const double TeleportMinSpeed
The minimum speed while teleporting.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type&#39;s vehicle class.
The vehicle is being teleported.
void endLaneChangeManeuver(const MSMoveReminder::Notification reason=MSMoveReminder::NOTIFICATION_LANE_CHANGE)