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.dev/sumo
3 // Copyright (C) 2001-2024 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  const bool jumping = veh->isJumping();
57  const SUMOTime proceed = jumping ? t + veh->getPastStops().back().jump : -1;
58  if (veh->isParking()) {
62  } else {
65  if (veh->succEdge(1) == nullptr) {
66  WRITE_WARNINGF(TL("Vehicle '%' teleports beyond arrival edge '%', time=%."), veh->getID(), veh->getEdge()->getID(), time2string(t));
69  return;
70  }
72  veh->enterLaneAtMove(veh->succEdge(1)->getLanes()[0], true);
73  }
74  myVehicles.push_back(VehicleInformation(t, veh, proceed, veh->isParking(), jumping));
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->getMutableLane()->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  const MSLane* lane = desc.myVeh->getLane();
108  // lane must be locked because pedestrians may be added in during stop processing while existing passengers are being drawn simultaneously
109  if (lane != nullptr) {
110  lane->getVehiclesSecure();
111  }
112  desc.myVeh->processNextStop(1);
113  desc.myVeh->updateParkingState();
114  if (lane != nullptr) {
115  lane->releaseVehicles();
116  }
117  }
118  if (desc.myVeh->keepStopping(true)) {
119  i++;
120  continue;
121  }
122  // parking finished, head back into traffic
123  }
124  const SUMOVehicleClass vclass = desc.myVeh->getVehicleType().getVehicleClass();
125  const MSEdge* e = desc.myVeh->getEdge();
126  const MSEdge* nextEdge = desc.myVeh->succEdge(1);
127 
128 
129  if (desc.myParking) {
131  const double departPos = pa != nullptr ? pa->getInsertionPosition(*desc.myVeh) : desc.myVeh->getPositionOnLane();
132  // handle parking vehicles
133  desc.myVeh->setIdling(true);
134  if (desc.myVeh->getMutableLane()->isInsertionSuccess(desc.myVeh, 0, departPos, desc.myVeh->getLateralPositionOnLane(),
137  desc.myVeh->getMutableLane()->removeParking(desc.myVeh);
138  // at this point we are in the lane, blocking traffic & if required we configure the exit manoeuvre
141  }
142  desc.myVeh->setIdling(false);
143  i = vehInfos.erase(i);
144  } else {
145  // blocked from entering the road - engine assumed to be idling.
146  desc.myVeh->workOnIdleReminders();
148  // signal wish to re-enter the road
150  if (pa) {
151  // update freePosition so other vehicles can help with insertion
153  }
154  }
155  i++;
156  }
157  } else if (desc.myJumping && desc.myProceedTime > time) {
158  ++i;
159  } else {
160  const double departPos = 0;
161  // get the lane on which this vehicle should continue
162  // first select all the lanes which allow continuation onto nextEdge
163  // then pick the one which is least occupied
164  MSLane* l = (nextEdge != nullptr ? e->getFreeLane(e->allowedLanes(*nextEdge, vclass), vclass, departPos) :
165  e->getFreeLane(nullptr, vclass, departPos));
166  // handle teleporting vehicles, lane may be 0 because permissions were modified by a closing rerouter or TraCI
167  const bool busyBidi = l != nullptr && l->getBidiLane() != nullptr && l->getBidiLane()->getVehicleNumberWithPartials() > 0;
168  if (l != nullptr && !busyBidi && l->freeInsertion(*(desc.myVeh), MIN2(l->getSpeedLimit(), desc.myVeh->getMaxSpeed()), 0, MSMoveReminder::NOTIFICATION_TELEPORT)) {
169  if (!desc.myJumping) {
170  WRITE_WARNINGF(TL("Vehicle '%' ends teleporting on edge '%', time=%."), desc.myVeh->getID(), e->getID(), time2string(time));
171  }
173  i = vehInfos.erase(i);
174  } else if (desc.myJumping) {
175  // try again later
176  ++i;
177  } else {
178  // vehicle is visible while show-route is active. Make its state more obvious
179  desc.myVeh->computeAngle();
180  desc.myVeh->setLateralPositionOnLane(-desc.myVeh->getLane()->getWidth() / 2);
182  // could not insert. maybe we should proceed in virtual space
183  if (desc.myProceedTime < 0) {
184  // initialize proceed time (delayed to avoid lane-order dependency in executeMove)
186  } else if (desc.myProceedTime < time) {
187  if (desc.myVeh->succEdge(1) == nullptr) {
188  WRITE_WARNINGF(TL("Vehicle '%' teleports beyond arrival edge '%', time=%."), desc.myVeh->getID(), e->getID(), time2string(time));
191  i = vehInfos.erase(i);
192  continue;
193  }
194  // let the vehicle move to the next edge
196  // active move reminders (i.e. rerouters)
197  const std::vector<MSLane*>* allowedLanes = nextEdge->allowedLanes(vclass);
198  MSLane* laneToEnter = (allowedLanes != nullptr) ? allowedLanes->at(0) : nextEdge->getLanes()[0];
199  desc.myVeh->enterLaneAtMove(laneToEnter, true);
200  // use current travel time to determine when to move the vehicle forward
202  }
203  ++i;
204  }
205  }
206  }
207  myVehicles.unlock();
208 }
209 
210 
213  if (myInstance == nullptr) {
215  }
216  return myInstance;
217 }
218 
219 
221 
222 
224  myInstance = nullptr;
225 }
226 
227 
228 void
230  for (const VehicleInformation& vehInfo : myVehicles.getContainer()) {
232  out.writeAttr(SUMO_ATTR_ID, vehInfo.myVeh->getID());
233  out.writeAttr(SUMO_ATTR_DEPART, vehInfo.myProceedTime);
234  if (vehInfo.myParking) {
235  out.writeAttr(SUMO_ATTR_PARKING, vehInfo.myVeh->getLane()->getID());
236  }
237  if (vehInfo.myJumping) {
238  out.writeAttr(SUMO_ATTR_JUMP, true);
239  }
240  out.closeTag();
241  }
242  myVehicles.unlock();
243 }
244 
245 
246 void
248  myVehicles.clear();
249 }
250 
251 
252 void
254  MSVehicle* veh = dynamic_cast<MSVehicle*>(vc.getVehicle(attrs.getString(SUMO_ATTR_ID)));
255  if (veh == nullptr) {
256  // deleted
257  return;
258  }
259  SUMOTime proceedTime = (SUMOTime)attrs.getLong(SUMO_ATTR_DEPART);
260  MSLane* parkingLane = attrs.hasAttribute(SUMO_ATTR_PARKING) ? MSLane::dictionary(attrs.getString(SUMO_ATTR_PARKING)) : nullptr;
261  bool ok = true;
262  const bool jumping = attrs.getOpt<bool>(SUMO_ATTR_JUMP, veh->getID().c_str(), ok, false);
263  myVehicles.push_back(VehicleInformation(-1, veh, proceedTime - offset, parkingLane != nullptr, jumping));
264  if (parkingLane != nullptr) {
265  parkingLane->addParking(veh);
266  veh->setTentativeLaneAndPosition(parkingLane, veh->getPositionOnLane());
267  veh->processNextStop(veh->getSpeed());
268  }
270 }
271 
272 
273 /****************************************************************************/
long long int SUMOTime
Definition: GUI.h:35
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:296
#define TL(string)
Definition: MsgHandler.h:315
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition: SUMOTime.cpp:69
#define TIME2STEPS(x)
Definition: SUMOTime.h:57
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_JUMP
@ SUMO_ATTR_ID
T MIN2(T a, T b)
Definition: StdDefs.h:76
void endLaneChangeManeuver(const MSMoveReminder::Notification reason=MSMoveReminder::NOTIFICATION_LANE_CHANGE)
double getMaxSpeed() const
Returns the maximum speed (the minimum of desired and technical maximum speed)
const MSEdge * succEdge(int nSuccs) const
Returns the nSuccs'th successor of edge the vehicle is currently at.
bool isJumping() const
Returns whether the vehicle is perform a jump.
bool isParking() const
Returns whether the vehicle is parking.
MSParkingArea * getCurrentParkingArea()
get the current parking area stop or nullptr
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
const std::vector< SUMOVehicleParameter::Stop > & getPastStops() const
A road/street connecting two junctions.
Definition: MSEdge.h:77
const std::vector< MSLane * > * allowedLanes(const MSEdge &destination, SUMOVehicleClass vclass=SVC_IGNORING, bool ignoreTransientPermissions=false) const
Get the allowed lanes to reach the destination-edge.
Definition: MSEdge.cpp:474
const std::vector< MSLane * > & getLanes() const
Returns this edge's lanes.
Definition: MSEdge.h:168
double getCurrentTravelTime(const double minSpeed=NUMERICAL_EPS) const
Computes and returns the current travel time for this edge.
Definition: MSEdge.cpp:967
MSLane * getFreeLane(const std::vector< MSLane * > *allowed, const SUMOVehicleClass vclass, double departPos) const
Finds the emptiest lane allowing the vehicle class.
Definition: MSEdge.cpp:521
static bool gModelParkingManoeuver
whether parking simulation includes manoeuver time and any associated lane blocking
Definition: MSGlobals.h:157
static bool gLefthand
Whether lefthand-drive is being simulated.
Definition: MSGlobals.h:169
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:84
virtual void removeParking(MSBaseVehicle *veh)
remove parking vehicle. This must be syncrhonized when running with GUI
Definition: MSLane.cpp:3551
void addParking(MSBaseVehicle *veh)
add parking vehicle. This should only used during state loading
Definition: MSLane.cpp:3545
int getVehicleNumberWithPartials() const
Returns the number of vehicles on this lane (including partial occupators)
Definition: MSLane.h:456
virtual const VehCont & getVehiclesSecure() const
Returns the vehicles container; locks it for microsimulation.
Definition: MSLane.h:475
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:812
double getSpeedLimit() const
Returns the lane's maximum allowed speed.
Definition: MSLane.h:584
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:2368
virtual void releaseVehicles() const
Allows to use the container for microsimulation again.
Definition: MSLane.h:505
MSLane * getBidiLane() const
retrieve bidirectional lane or nullptr
Definition: MSLane.cpp:4507
double getWidth() const
Returns the lane's width.
Definition: MSLane.h:627
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:490
@ 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.
@ NOTIFICATION_TELEPORT_CONTINUATION
The vehicle continues being teleported past an edge.
@ ENDING_PARKING
The vehicle ends to park.
@ STARTING_PARKING
The vehicles starts to park.
@ STARTING_TELEPORT
The vehicle started to teleport.
@ ENDING_TELEPORT
The vehicle ended being teleported.
@ MANEUVERING
Vehicle maneuvering either entering or exiting a parking space.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:182
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:378
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:431
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:1258
A lane area vehicles can halt at.
Definition: MSParkingArea.h:60
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
double computeAngle() const
compute the current vehicle angle
Definition: MSVehicle.cpp:1466
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:6619
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:5720
bool signalSet(int which) const
Returns whether the given signal is on.
Definition: MSVehicle.h:1190
MSLane * getMutableLane() const
Returns the lane the vehicle is on Non const version indicates that something volatile is going on.
Definition: MSVehicle.h:592
void enterLaneAtMove(MSLane *enteredLane, bool onTeleporting=false)
Update when the vehicle enters a new lane in the move step.
Definition: MSVehicle.cpp:5360
void setIdling(bool amIdling)
access function for Idling flag used to record whether vehicle is waiting to enter lane (after parkin...
Definition: MSVehicle.h:616
void onRemovalFromNet(const MSMoveReminder::Notification reason)
Called when the vehicle is removed from the network.
Definition: MSVehicle.cpp:1043
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:5626
bool setExitManoeuvre()
accessor function to myManoeuvre equivalent
Definition: MSVehicle.cpp:7629
@ VEH_SIGNAL_BLINKER_RIGHT
Right blinker lights are switched on.
Definition: MSVehicle.h:1112
@ VEH_SIGNAL_BLINKER_LEFT
Left blinker lights are switched on.
Definition: MSVehicle.h:1114
void invalidateCachedPosition()
Definition: MSVehicle.h:424
double getLateralPositionOnLane() const
Get the vehicle's lateral position on the lane.
Definition: MSVehicle.h:416
double getSpeed() const
Returns the vehicle's current speed.
Definition: MSVehicle.h:493
bool keepStopping(bool afterProcessing=false) const
Returns whether the vehicle is stopped and must continue to do so.
Definition: MSVehicle.cpp:1578
void workOnIdleReminders()
cycle through vehicle devices invoking notifyIdle
Definition: MSVehicle.cpp:1177
double processNextStop(double currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
Definition: MSVehicle.cpp:1613
double getPositionOnLane() const
Get the vehicle's position along the lane.
Definition: MSVehicle.h:377
const MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:584
void setLateralPositionOnLane(double posLat)
Definition: MSVehicle.h:420
void switchOnSignal(int signal)
Switches the given signal on.
Definition: MSVehicle.h:1165
void updateParkingState()
update state while parking
Definition: MSVehicle.cpp:4795
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.
MFXSynchQue< VehicleInformation, std::vector< VehicleInformation > > myVehicles
The information about stored vehicles to move virtually.
void loadState(const SUMOSAXAttributes &attrs, const SUMOTime offset, MSVehicleControl &vc)
Loads one transfer vehicle state from the given descriptionn.
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:74
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
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:254
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Encapsulated SAX-Attributes.
virtual std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) attribute.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), bool report=true) const
Tries to read given attribute assuming it is an int.
virtual long long int getLong(int id) const
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.
Holds the information needed to move the vehicle over the network.
bool myParking
whether the vehicle is or was parking
bool myJumping
whether the vehicle is or was jumping
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