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-2020 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
20 // A mover of vehicles that got stucked due to grid locks
21 /****************************************************************************/
22 #include <config.h>
23 
24 #include <iostream>
27 #include "MSNet.h"
28 #include "MSLane.h"
29 #include "MSEdge.h"
30 #include "MSVehicle.h"
31 #include "MSParkingArea.h"
33 #include "MSVehicleControl.h"
34 #include "MSInsertionControl.h"
35 #include "MSVehicleTransfer.h"
36 
37 
38 // ===========================================================================
39 // static member definitions
40 // ===========================================================================
43 
44 
45 // ===========================================================================
46 // member method definitions
47 // ===========================================================================
48 bool
50  return myVeh->getNumericalID() < v2.myVeh->getNumericalID();
51 }
52 
53 
54 void
56  if (veh->isParking()) {
60  } else {
63  if (veh->succEdge(1) == nullptr) {
64  WRITE_WARNINGF("Vehicle '%' teleports beyond arrival edge '%', time %.", veh->getID(), veh->getEdge()->getID(), time2string(t));
67  return;
68  }
70  veh->enterLaneAtMove(veh->succEdge(1)->getLanes()[0], true);
71  }
72  myVehicles.push_back(VehicleInformation(t, veh, -1, veh->isParking()));
73 }
74 
75 
76 void
78  auto& vehInfos = myVehicles.getContainer();
79  for (auto i = vehInfos.begin(); i != vehInfos.end(); ++i) {
80  if (i->myVeh == veh) {
81  if (i->myParking) {
82  veh->getMutableLane()->removeParking(veh);
83  }
84  vehInfos.erase(i);
85  break;
86  }
87  }
88  myVehicles.unlock();
89 }
90 
91 
92 void
94  // go through vehicles
95  auto& vehInfos = myVehicles.getContainer();
96  std::sort(vehInfos.begin(), vehInfos.end());
97  for (auto i = vehInfos.begin(); i != vehInfos.end();) {
98  // vehicle information cannot be const because we need to assign the proceed time
99  VehicleInformation& desc = *i;
100 
101  if (desc.myParking) {
102  // handle parking vehicles
103  if (time != desc.myTransferTime) {
104  // avoid calling processNextStop twice in the transfer step
105  desc.myVeh->processNextStop(1);
106  desc.myVeh->updateParkingState();
107  }
108  if (desc.myVeh->keepStopping(true)) {
109  i++;
110  continue;
111  }
112  // parking finished, head back into traffic
113  }
114  const SUMOVehicleClass vclass = desc.myVeh->getVehicleType().getVehicleClass();
115  const MSEdge* e = desc.myVeh->getEdge();
116  const MSEdge* nextEdge = desc.myVeh->succEdge(1);
117 
118 
119  if (desc.myParking) {
121  const double departPos = pa != nullptr ? pa->getInsertionPosition(*desc.myVeh) : desc.myVeh->getPositionOnLane();
122  // handle parking vehicles
123  desc.myVeh->setIdling(true);
124  if (desc.myVeh->getMutableLane()->isInsertionSuccess(desc.myVeh, 0, departPos, desc.myVeh->getLateralPositionOnLane(),
127  desc.myVeh->getMutableLane()->removeParking(desc.myVeh);
128  // at this point we are in the lane, blocking traffic & if required we configure the exit manoeuvre
131  }
132  desc.myVeh->setIdling(false);
133  i = vehInfos.erase(i);
134  } else {
135  // blocked from entering the road - engine assumed to be idling.
136  desc.myVeh->workOnIdleReminders();
138  // signal wish to re-enter the road
140  if (pa) {
141  // update freePosition so other vehicles can help with insertion
143  }
144  }
145  i++;
146  }
147  } else {
148  const double departPos = 0;
149  // get the lane on which this vehicle should continue
150  // first select all the lanes which allow continuation onto nextEdge
151  // then pick the one which is least occupied
152  MSLane* l = (nextEdge != nullptr ? e->getFreeLane(e->allowedLanes(*nextEdge, vclass), vclass, departPos) :
153  e->getFreeLane(nullptr, vclass, departPos));
154  // handle teleporting vehicles, lane may be 0 because permissions were modified by a closing rerouter or TraCI
155  if (l != nullptr && l->freeInsertion(*(desc.myVeh), MIN2(l->getSpeedLimit(), desc.myVeh->getMaxSpeed()), 0, MSMoveReminder::NOTIFICATION_TELEPORT)) {
156  WRITE_WARNINGF("Vehicle '%' ends teleporting on edge '%', time %.", desc.myVeh->getID(), e->getID(), time2string(time));
158  i = vehInfos.erase(i);
159  } else {
160  // could not insert. maybe we should proceed in virtual space
161  if (desc.myProceedTime < 0) {
162  // initialize proceed time (delayed to avoid lane-order dependency in executeMove)
164  } else if (desc.myProceedTime < time) {
165  if (desc.myVeh->succEdge(1) == nullptr) {
166  WRITE_WARNINGF("Vehicle '%' teleports beyond arrival edge '%', time %.", desc.myVeh->getID(), e->getID(), time2string(time));
169  i = vehInfos.erase(i);
170  continue;
171  }
172  // let the vehicle move to the next edge
174  // active move reminders (i.e. rerouters)
175  desc.myVeh->enterLaneAtMove(desc.myVeh->succEdge(1)->getLanes()[0], true);
176  // use current travel time to determine when to move the vehicle forward
178  }
179  ++i;
180  }
181  }
182  }
183  myVehicles.unlock();
184 }
185 
186 
189  if (myInstance == nullptr) {
191  }
192  return myInstance;
193 }
194 
195 
197 
198 
200  myInstance = nullptr;
201 }
202 
203 
204 void
206  for (const VehicleInformation& vehInfo : myVehicles.getContainer()) {
208  out.writeAttr(SUMO_ATTR_ID, vehInfo.myVeh->getID());
209  out.writeAttr(SUMO_ATTR_DEPART, vehInfo.myProceedTime);
210  if (vehInfo.myParking) {
211  out.writeAttr(SUMO_ATTR_PARKING, vehInfo.myVeh->getLane()->getID());
212  }
213  out.closeTag();
214  }
215  myVehicles.unlock();
216 }
217 
218 
219 void
221  myVehicles.clear();
222 }
223 
224 
225 void
227  MSVehicle* veh = dynamic_cast<MSVehicle*>(vc.getVehicle(attrs.getString(SUMO_ATTR_ID)));
228  if (veh == nullptr) {
229  // deleted
230  return;
231  }
232  SUMOTime proceedTime = (SUMOTime)attrs.getLong(SUMO_ATTR_DEPART);
233  MSLane* parkingLane = attrs.hasAttribute(SUMO_ATTR_PARKING) ? MSLane::dictionary(attrs.getString(SUMO_ATTR_PARKING)) : nullptr;
234  myVehicles.push_back(VehicleInformation(-1, veh, proceedTime - offset, parkingLane != nullptr));
235  if (parkingLane != nullptr) {
236  parkingLane->addParking(veh);
237  veh->setTentativeLaneAndPosition(parkingLane, veh->getPositionOnLane());
238  veh->processNextStop(veh->getSpeed());
239  }
241 }
242 
243 
244 /****************************************************************************/
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:277
std::string time2string(SUMOTime t)
convert SUMOTime to string
Definition: SUMOTime.cpp:68
#define TIME2STEPS(x)
Definition: SUMOTime.h:55
long long int SUMOTime
Definition: SUMOTime.h:31
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SUMO_TAG_VEHICLETRANSFER
@ SUMO_ATTR_PARKING
@ SUMO_ATTR_DEPART
@ SUMO_ATTR_ID
T MIN2(T a, T b)
Definition: StdDefs.h:73
void endLaneChangeManeuver(const MSMoveReminder::Notification reason=MSMoveReminder::NOTIFICATION_LANE_CHANGE)
double getMaxSpeed() const
Returns the maximum speed.
const MSEdge * succEdge(int nSuccs) const
Returns the nSuccs'th successor of edge the vehicle is currently at.
bool isParking() const
Returns whether the vehicle is parking.
const MSEdge * getEdge() const
Returns the edge the vehicle is currently at.
const MSVehicleType & getVehicleType() const
Returns the vehicle's type definition.
NumericalID getNumericalID() const
return the numerical ID which is only for internal usage
A road/street connecting two junctions.
Definition: MSEdge.h:77
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:366
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:166
double getCurrentTravelTime(const double minSpeed=NUMERICAL_EPS) const
Computes and returns the current travel time for this edge.
Definition: MSEdge.cpp:798
MSLane * getFreeLane(const std::vector< MSLane * > *allowed, const SUMOVehicleClass vclass, double departPos) const
Finds the emptiest lane allowing the vehicle class.
Definition: MSEdge.cpp:412
static bool gModelParkingManoeuver
whether parking simulation includes manoeuver time and any associated lane blocking
Definition: MSGlobals.h:127
static bool gLefthand
Whether lefthand-drive is being simulated.
Definition: MSGlobals.h:136
void alreadyDeparted(SUMOVehicle *veh)
stops trying to emit the given vehicle (because it already departed)
Representation of a lane in the micro simulation.
Definition: MSLane.h:82
virtual void removeParking(MSVehicle *veh)
remove parking vehicle. This must be syncrhonized when running with GUI
Definition: MSLane.cpp:3065
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:671
double getSpeedLimit() const
Returns the lane's maximum allowed speed.
Definition: MSLane.h:531
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1908
void addParking(MSVehicle *veh)
add parking vehicle. This should only used during state loading
Definition: MSLane.cpp:3059
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:397
@ NOTIFICATION_TELEPORT_ARRIVED
The vehicle was teleported out of the net.
@ NOTIFICATION_PARKING
The vehicle starts or ends parking.
@ NOTIFICATION_TELEPORT
The vehicle is being teleported.
@ VEHICLE_STATE_ENDING_TELEPORT
The vehicle ended being teleported.
Definition: MSNet.h:595
@ VEHICLE_STATE_STARTING_PARKING
The vehicles starts to park.
Definition: MSNet.h:601
@ VEHICLE_STATE_MANEUVERING
Vehicle maneuvering either entering or exiting a parking space.
Definition: MSNet.h:613
@ VEHICLE_STATE_STARTING_TELEPORT
The vehicle started to teleport.
Definition: MSNet.h:593
@ VEHICLE_STATE_ENDING_PARKING
The vehicle ends to park.
Definition: MSNet.h:603
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:171
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:371
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:424
void informVehicleStateListener(const SUMOVehicle *const vehicle, VehicleState to, const std::string &info="")
Informs all added listeners about a vehicle's state change.
Definition: MSNet.cpp:1071
A lane area vehicles can halt at.
Definition: MSParkingArea.h:57
void notifyEgressBlocked()
update state so that vehicles wishing to enter cooperate with exiting vehicles
double getInsertionPosition(const SUMOVehicle &forVehicle) const
Returns the insertion position of a parked vehicle.
The class responsible for building and deletion of vehicles.
SUMOVehicle * getVehicle(const std::string &id) const
Returns the vehicle with the given id.
void scheduleVehicleRemoval(SUMOVehicle *veh, bool checkDuplicate=false)
Removes a vehicle after it has ended.
Representation of a vehicle in the micro simulation.
Definition: MSVehicle.h:77
bool enterLaneAtMove(MSLane *enteredLane, bool onTeleporting=false)
Update when the vehicle enters a new lane in the move step.
Definition: MSVehicle.cpp:4452
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:5463
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:4699
bool signalSet(int which) const
Returns whether the given signal is on.
Definition: MSVehicle.h:1159
MSLane * getMutableLane() const
Returns the lane the vehicle is on Non const version indicates that something volatile is going on.
Definition: MSVehicle.h:558
MSParkingArea * getCurrentParkingArea()
get the current parking area stop or nullptr
Definition: MSVehicle.cpp:1427
void setIdling(bool amIdling)
access function for Idling flag used to record whether vehicle is waiting to enter lane (after parkin...
Definition: MSVehicle.h:588
void onRemovalFromNet(const MSMoveReminder::Notification reason)
Called when the vehicle is removed from the network.
Definition: MSVehicle.cpp:967
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:4634
bool setExitManoeuvre()
accessor function to myManoeuvre equivalent
Definition: MSVehicle.cpp:6417
@ VEH_SIGNAL_BLINKER_RIGHT
Right blinker lights are switched on.
Definition: MSVehicle.h:1081
@ VEH_SIGNAL_BLINKER_LEFT
Left blinker lights are switched on.
Definition: MSVehicle.h:1083
double getLateralPositionOnLane() const
Get the vehicle's lateral position on the lane.
Definition: MSVehicle.h:411
double getSpeed() const
Returns the vehicle's current speed.
Definition: MSVehicle.h:458
bool keepStopping(bool afterProcessing=false) const
Returns whether the vehicle is stopped and must continue to do so.
Definition: MSVehicle.cpp:1447
void workOnIdleReminders()
cycle through vehicle devices invoking notifyIdle
Definition: MSVehicle.cpp:1072
double processNextStop(double currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
Definition: MSVehicle.cpp:1480
double getPositionOnLane() const
Get the vehicle's position along the lane.
Definition: MSVehicle.h:374
void switchOnSignal(int signal)
Switches the given signal on.
Definition: MSVehicle.h:1134
void updateParkingState()
update state while parking
Definition: MSVehicle.cpp:3960
static MSVehicleTransfer * getInstance()
Returns the instance of this object.
void remove(MSVehicle *veh)
Remove a vehicle from this transfer object.
virtual ~MSVehicleTransfer()
Destructor.
void add(const SUMOTime t, MSVehicle *veh)
Adds a vehicle to this transfer object.
void saveState(OutputDevice &out)
Saves the current state into the given stream.
void checkInsertions(SUMOTime time)
Checks "movement" of stored vehicles.
void loadState(const SUMOSAXAttributes &attrs, const SUMOTime offset, MSVehicleControl &vc)
Loads one transfer vehicle state from the given descriptionn.
FXSynchQue< VehicleInformation, std::vector< VehicleInformation > > myVehicles
The information about stored vehicles to move virtually.
MSVehicleTransfer()
Constructor.
static MSVehicleTransfer * myInstance
The static singleton-instance.
void clearState()
Remove all vehicles before quick-loading state.
static const double TeleportMinSpeed
The minimum speed while teleporting.
SUMOVehicleClass getVehicleClass() const
Get this vehicle type's vehicle class.
const std::string & getID() const
Returns the id.
Definition: Named.h:73
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:239
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Encapsulated SAX-Attributes.
virtual long long int getLong(int id) const =0
Returns the long-value of the named (by its enum-value) attribute.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
Holds the information needed to move the vehicle over the network.
bool myParking
whether the vehicle is or was parking
SUMOTime myTransferTime
the time at which this vehicle was removed from the network
MSVehicle * myVeh
The vehicle itself.
SUMOTime myProceedTime
The time at which the vehicle should be moved virtually one edge further.
bool operator<(const VehicleInformation &v2) const
sort by vehicle ID for repeatable parallel simulation