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-2022 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  // vehicle is visible while show-route is active. Make it's state more obvious
161  desc.myVeh->computeAngle();
162  desc.myVeh->setLateralPositionOnLane(-desc.myVeh->getLane()->getWidth() / 2);
164  // could not insert. maybe we should proceed in virtual space
165  if (desc.myProceedTime < 0) {
166  // initialize proceed time (delayed to avoid lane-order dependency in executeMove)
168  } else if (desc.myProceedTime < time) {
169  if (desc.myVeh->succEdge(1) == nullptr) {
170  WRITE_WARNINGF("Vehicle '%' teleports beyond arrival edge '%', time=%.", desc.myVeh->getID(), e->getID(), time2string(time));
173  i = vehInfos.erase(i);
174  continue;
175  }
176  // let the vehicle move to the next edge
178  // active move reminders (i.e. rerouters)
179  desc.myVeh->enterLaneAtMove(desc.myVeh->succEdge(1)->getLanes()[0], true);
180  // use current travel time to determine when to move the vehicle forward
182  }
183  ++i;
184  }
185  }
186  }
187  myVehicles.unlock();
188 }
189 
190 
193  if (myInstance == nullptr) {
195  }
196  return myInstance;
197 }
198 
199 
201 
202 
204  myInstance = nullptr;
205 }
206 
207 
208 void
210  for (const VehicleInformation& vehInfo : myVehicles.getContainer()) {
212  out.writeAttr(SUMO_ATTR_ID, vehInfo.myVeh->getID());
213  out.writeAttr(SUMO_ATTR_DEPART, vehInfo.myProceedTime);
214  if (vehInfo.myParking) {
215  out.writeAttr(SUMO_ATTR_PARKING, vehInfo.myVeh->getLane()->getID());
216  }
217  out.closeTag();
218  }
219  myVehicles.unlock();
220 }
221 
222 
223 void
225  myVehicles.clear();
226 }
227 
228 
229 void
231  MSVehicle* veh = dynamic_cast<MSVehicle*>(vc.getVehicle(attrs.getString(SUMO_ATTR_ID)));
232  if (veh == nullptr) {
233  // deleted
234  return;
235  }
236  SUMOTime proceedTime = (SUMOTime)attrs.getLong(SUMO_ATTR_DEPART);
237  MSLane* parkingLane = attrs.hasAttribute(SUMO_ATTR_PARKING) ? MSLane::dictionary(attrs.getString(SUMO_ATTR_PARKING)) : nullptr;
238  myVehicles.push_back(VehicleInformation(-1, veh, proceedTime - offset, parkingLane != nullptr));
239  if (parkingLane != nullptr) {
240  parkingLane->addParking(veh);
241  veh->setTentativeLaneAndPosition(parkingLane, veh->getPositionOnLane());
242  veh->processNextStop(veh->getSpeed());
243  }
245 }
246 
247 
248 /****************************************************************************/
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:281
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:32
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:74
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:408
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:869
MSLane * getFreeLane(const std::vector< MSLane * > *allowed, const SUMOVehicleClass vclass, double departPos) const
Finds the emptiest lane allowing the vehicle class.
Definition: MSEdge.cpp:454
static bool gModelParkingManoeuver
whether parking simulation includes manoeuver time and any associated lane blocking
Definition: MSGlobals.h:145
static bool gLefthand
Whether lefthand-drive is being simulated.
Definition: MSGlobals.h:157
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(MSBaseVehicle *veh)
remove parking vehicle. This must be syncrhonized when running with GUI
Definition: MSLane.cpp:3160
void addParking(MSBaseVehicle *veh)
add parking vehicle. This should only used during state loading
Definition: MSLane.cpp:3154
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:694
double getSpeedLimit() const
Returns the lane's maximum allowed speed.
Definition: MSLane.h:533
static bool dictionary(const std::string &id, MSLane *lane)
Static (sic!) container methods {.
Definition: MSLane.cpp:1991
double getWidth() const
Returns the lane's width.
Definition: MSLane.h:556
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:404
@ 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.
@ 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:174
MSVehicleControl & getVehicleControl()
Returns the vehicle control.
Definition: MSNet.h:376
MSInsertionControl & getInsertionControl()
Returns the insertion control.
Definition: MSNet.h:429
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:1144
A lane area vehicles can halt at.
Definition: MSParkingArea.h:58
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:75
double computeAngle() const
compute the current vehicle angle
Definition: MSVehicle.cpp:1330
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:5878
MSAbstractLaneChangeModel & getLaneChangeModel()
Definition: MSVehicle.cpp:5051
bool signalSet(int which) const
Returns whether the given signal is on.
Definition: MSVehicle.h:1146
MSLane * getMutableLane() const
Returns the lane the vehicle is on Non const version indicates that something volatile is going on.
Definition: MSVehicle.h:560
void enterLaneAtMove(MSLane *enteredLane, bool onTeleporting=false)
Update when the vehicle enters a new lane in the move step.
Definition: MSVehicle.cpp:4772
MSParkingArea * getCurrentParkingArea()
get the current parking area stop or nullptr
Definition: MSVehicle.cpp:1462
void setIdling(bool amIdling)
access function for Idling flag used to record whether vehicle is waiting to enter lane (after parkin...
Definition: MSVehicle.h:582
void onRemovalFromNet(const MSMoveReminder::Notification reason)
Called when the vehicle is removed from the network.
Definition: MSVehicle.cpp:978
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:4965
bool setExitManoeuvre()
accessor function to myManoeuvre equivalent
Definition: MSVehicle.cpp:6771
@ VEH_SIGNAL_BLINKER_RIGHT
Right blinker lights are switched on.
Definition: MSVehicle.h:1068
@ VEH_SIGNAL_BLINKER_LEFT
Left blinker lights are switched on.
Definition: MSVehicle.h:1070
void invalidateCachedPosition()
Definition: MSVehicle.h:419
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:462
bool keepStopping(bool afterProcessing=false) const
Returns whether the vehicle is stopped and must continue to do so.
Definition: MSVehicle.cpp:1482
void workOnIdleReminders()
cycle through vehicle devices invoking notifyIdle
Definition: MSVehicle.cpp:1089
double processNextStop(double currentVelocity)
Processes stops, returns the velocity needed to reach the stop.
Definition: MSVehicle.cpp:1515
double getPositionOnLane() const
Get the vehicle's position along the lane.
Definition: MSVehicle.h:372
const MSLane * getLane() const
Returns the lane the vehicle is on.
Definition: MSVehicle.h:552
void setLateralPositionOnLane(double posLat)
Definition: MSVehicle.h:415
void switchOnSignal(int signal)
Switches the given signal on.
Definition: MSVehicle.h:1121
void updateParkingState()
update state while parking
Definition: MSVehicle.cpp:4246
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: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:248
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